Skip to content

Commit

Permalink
👷 integrate Auction into KSXVault
Browse files Browse the repository at this point in the history
  • Loading branch information
cmontecoding committed Oct 24, 2024
1 parent 55873e5 commit 3f355ff
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 23 deletions.
4 changes: 3 additions & 1 deletion script/Deploy.s.sol
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,16 @@ contract Setup is Script {

function deploySystem(
address token,
address usdc,
address stakingRewards,
address auctionFactory,
uint8 decimalOffset,
uint256 timeOffset
)
public
returns (KSXVault ksxVault)
{
ksxVault = new KSXVault(token, stakingRewards, decimalOffset, timeOffset);
ksxVault = new KSXVault(token, usdc, stakingRewards, auctionFactory, decimalOffset, timeOffset);

// deploy ERC1967 proxy and set implementation to ksxVault
Proxy proxy = new Proxy(address(ksxVault), "");
Expand Down
43 changes: 33 additions & 10 deletions src/KSXVault.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import {ERC20, IERC20} from "@openzeppelin/contracts/token/ERC20/ERC20.sol";
import {ERC4626} from
"@openzeppelin/contracts/token/ERC20/extensions/ERC4626.sol";
import {IStakingRewardsV2} from "@token/interfaces/IStakingRewardsV2.sol";
import {AuctionFactory} from "./AuctionFactory.sol";
import {Auction} from "./Auction.sol";

/// @title KSXVault Contract
/// @notice KSX ERC4626 Vault
Expand All @@ -29,9 +31,16 @@ contract KSXVault is ERC4626 {
IStakingRewardsV2 internal immutable STAKING_REWARDS;

/// @notice KWENTA TOKEN
/// @dev The underlying asset of this vault
/// @dev The underlying asset of this vault
ERC20 private immutable KWENTA;

/// @notice USDC TOKEN
/// @dev The asset used for auctions
IERC20 private immutable USDC;

/// @notice Auction Factory
AuctionFactory private immutable auctionFactory;

/*//////////////////////////////////////////////////////////////
ERRORS
//////////////////////////////////////////////////////////////*/
Expand All @@ -58,13 +67,17 @@ contract KSXVault is ERC4626 {

/// @notice Constructs the KSXVault contract
/// @param _token Kwenta token address
/// @param _usdc USDC token address
/// @param _stakingRewards Kwenta v2 staking rewards contract
/// @param _auctionFactory the address of the auction factory
/// @param _decimalOffset offset in the decimal representation between the
/// underlying asset's decimals and the vault decimals
/// @param _daysToOffsetBy the number of days to offset the week by
constructor(
address _token,
address _usdc,
address _stakingRewards,
address _auctionFactory,
uint8 _decimalOffset,
uint256 _daysToOffsetBy
)
Expand All @@ -74,6 +87,8 @@ contract KSXVault is ERC4626 {
decimalOffset = _decimalOffset;
STAKING_REWARDS = IStakingRewardsV2(_stakingRewards);
KWENTA = ERC20(_token);
USDC = IERC20(_usdc);
auctionFactory = AuctionFactory(_auctionFactory);

if (_daysToOffsetBy > MAX_OFFSET_DAYS) {
revert OffsetTooBig();
Expand All @@ -92,21 +107,29 @@ contract KSXVault is ERC4626 {
START AUCTION FUNCTIONS
//////////////////////////////////////////////////////////////*/

/// @notice Starts the weekly auction with the USDC balance of the vault
function createAuction(address auctionOwner, address usdc, address kwenta, uint256 _startingBid, uint256 _bidBuffer) public {
/// @notice Starts the auction with the USDC balance of the vault
function createAuction(uint256 _startingBid, uint256 _bidBuffer) public {
if (!auctionReady()) {
revert AuctionNotReady();
}

lastAuctionStartTime = block.timestamp;

// auctionFactory.createAuction({
// _owner: auctionOwner,
// _usdc: usdc,
// _kwenta: kwenta,
// _startingBid: _startingBid,
// _bidBuffer: _bidBuffer
// });
auctionFactory.createAuction({
_owner: address(this),
_usdc: address(USDC),
_kwenta: address(KWENTA),
_startingBid: _startingBid,
_bidBuffer: _bidBuffer
});

address[] memory auctions = auctionFactory.getAllAuctions();
Auction auction = Auction(auctions[auctions.length - 1]);

uint256 auctionAmount = USDC.balanceOf(address(this));
USDC.transferFrom(address(this), address(auction), auctionAmount);
auction.start(auctionAmount);

}

/// @notice Checks if the auction is ready to start
Expand Down
26 changes: 17 additions & 9 deletions test/KSXVault.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,25 @@ import {Test} from "forge-std/Test.sol";
import {Bootstrap, KSXVault} from "test/utils/Bootstrap.sol";
import {MockERC20} from "test/mocks/MockERC20.sol";
import {MockStakingRewards} from "test/mocks/MockStakingRewards.sol";
import {Auction} from "src/Auction.sol";
import {AuctionFactory} from "src/AuctionFactory.sol";

contract KSXVaultTest is Bootstrap {

MockERC20 depositToken;
MockStakingRewards stakingRewards;
MockERC20 mockUSDC;
Auction auction;
AuctionFactory auctionFactory;

function setUp() public {

depositToken = new MockERC20("Deposit Token", "DT");
mockUSDC = new MockERC20("USDC", "USDC");
stakingRewards = new MockStakingRewards(address(depositToken));
initializeLocal(address(depositToken), address(stakingRewards), DECIMAL_OFFSET, 0);
auction = new Auction(address(this), address(0), address(0), 100, 100);
auctionFactory = new AuctionFactory(address(auction));
initializeLocal(address(depositToken), address(mockUSDC), address(stakingRewards), address(auctionFactory), DECIMAL_OFFSET, 0);

depositToken.mint(alice, 10 ether);
depositToken.mint(bob, 10 ether);
Expand Down Expand Up @@ -117,7 +125,7 @@ contract KSXVaultTest is Bootstrap {
vm.warp(block.timestamp + 1);
assertEq(ksxVault.auctionReady(), true);

ksxVault.createAuction(address(this), address(0), address(0), 100, 100);
ksxVault.createAuction(100, 100);

vm.warp(block.timestamp + 1 weeks - 1);
assertEq(ksxVault.auctionReady(), false);
Expand All @@ -127,8 +135,8 @@ contract KSXVaultTest is Bootstrap {

function test_auctionReady_offset() public {
vm.warp(block.timestamp + 2 weeks);
initializeLocal(address(depositToken), address(stakingRewards), DECIMAL_OFFSET, 1);
ksxVault.createAuction(address(this), address(0), address(0), 100, 100);
initializeLocal(address(depositToken), address(mockUSDC), address(stakingRewards), address(auctionFactory), DECIMAL_OFFSET, 1);
ksxVault.createAuction(100, 100);
assertEq(ksxVault.auctionReady(), false);
assertEq(block.timestamp, 2 weeks + 1);
vm.warp(block.timestamp + 1 days - 2);
Expand All @@ -139,16 +147,16 @@ contract KSXVaultTest is Bootstrap {

function test_auctionReady_offset_next_week() public {
vm.warp(block.timestamp + 2 weeks);
initializeLocal(address(depositToken), address(stakingRewards), DECIMAL_OFFSET, 1);
ksxVault.createAuction(address(this), address(0), address(0), 100, 100);
initializeLocal(address(depositToken), address(mockUSDC), address(stakingRewards), address(auctionFactory), DECIMAL_OFFSET, 1);
ksxVault.createAuction(100, 100);
assertEq(ksxVault.auctionReady(), false);
assertEq(block.timestamp, 2 weeks + 1);
vm.warp(block.timestamp + 1 days - 2);
assertEq(ksxVault.auctionReady(), false);
vm.warp(block.timestamp + 1);
assertEq(ksxVault.auctionReady(), true);

ksxVault.createAuction(address(this), address(0), address(0), 100, 100);
ksxVault.createAuction(100, 100);

vm.warp(block.timestamp + 1 weeks - 1);
assertEq(ksxVault.auctionReady(), false);
Expand All @@ -158,13 +166,13 @@ contract KSXVaultTest is Bootstrap {

function test_createAuction() public {
vm.warp(block.timestamp + 1 weeks);
ksxVault.createAuction(address(this), address(0), address(0), 100, 100);
ksxVault.createAuction(100, 100);
assertEq(ksxVault.lastAuctionStartTime(), block.timestamp);
}

function test_createAuction_AuctionNotReady() public {
vm.expectRevert(KSXVault.AuctionNotReady.selector);
ksxVault.createAuction(address(this), address(0), address(0), 100, 100);
ksxVault.createAuction(100, 100);
}

}
8 changes: 5 additions & 3 deletions test/utils/Bootstrap.sol
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ contract Bootstrap is Test, Constants {
address constant alice = address(0xAAAA);
address constant bob = address(0xBBBB);

function initializeLocal(address _token, address _stakingRewards, uint8 _decimalsOffset, uint256 _daysToOffsetBy) internal {
function initializeLocal(address _token, address _usdc, address _stakingRewards, address _auctionFactory, uint8 _decimalsOffset, uint256 _daysToOffsetBy) internal {
BootstrapLocal bootstrap = new BootstrapLocal();
(address ksxVaultAddress) = bootstrap.init(_token, _stakingRewards, _decimalsOffset, _daysToOffsetBy);
(address ksxVaultAddress) = bootstrap.init(_token, _usdc, _stakingRewards, _auctionFactory, _decimalsOffset, _daysToOffsetBy);

decimalsOffset = _decimalsOffset;
TOKEN = IERC20(_token);
Expand All @@ -47,14 +47,16 @@ contract BootstrapLocal is Setup {

function init(
address _token,
address _usdc,
address _stakingRewards,
address _auctionFactory,
uint8 _decimalsOffset,
uint256 _timeOffset
)
public
returns (address)
{
(KSXVault ksxvault) = Setup.deploySystem(_token, _stakingRewards, _decimalsOffset, _timeOffset);
(KSXVault ksxvault) = Setup.deploySystem(_token, _usdc, _stakingRewards, _auctionFactory, _decimalsOffset, _timeOffset);

return (address(ksxvault));
}
Expand Down

0 comments on commit 3f355ff

Please sign in to comment.