Compare commits

...

2 Commits

Author SHA1 Message Date
gozzy
141211569a reimplement auction vesting 2023-04-09 18:26:54 +00:00
gozzy
860b9d4111 gov test env & remove old module 2023-04-09 17:10:49 +00:00
8 changed files with 209 additions and 40 deletions

4
.gitmodules vendored
View File

@ -2,10 +2,6 @@
path = lib/forge-std
url = https://github.com/foundry-rs/forge-std
branch = v1.5.1
[submodule "lib/prb-math"]
path = lib/prb-math
url = https://github.com/paulRBerg/prb-math
branch = v3
[submodule "lib/openzeppelin-contracts"]
path = lib/openzeppelin-contracts
url = https://github.com/OpenZeppelin/openzeppelin-contracts

@ -1 +0,0 @@
Subproject commit 9febcdd219f08f7fb70fdf0e156f1be4103c42dd

View File

@ -1,6 +1,7 @@
pragma solidity 0.8.13;
import { IRDA } from "@root/interfaces/IRDA.sol";
import { IRDA } from "@interfaces/IRDA.sol";
import { IDelegatedVesting } from "@interfaces/IDelegatedVesting.sol";
import { ERC20 } from "@openzeppelin/token/ERC20/ERC20.sol";
import { SafeERC20 } from "@openzeppelin/token/ERC20/utils/SafeERC20.sol";
@ -28,24 +29,7 @@ contract RDA is IRDA, ReentrancyGuard {
/* @dev Auction mapping for the window index */
mapping(bytes => uint256) public _windows;
struct Auction {
uint256 windowDuration; /* @dev Unix time window duration */
uint256 windowTimestamp; /* @dev Unix timestamp for window start */
uint256 startTimestamp; /* @dev Unix auction start timestamp */
uint256 endTimestamp; /* @dev Unix auction end timestamp */
uint256 duration; /* @dev Unix time auction duration */
uint256 proceeds; /* @dev Auction proceeds balance */
uint256 reserves; /* @dev Auction reserves balance */
uint256 price; /* @dev Auction origin price */
}
struct Window {
bytes bidId; /* @dev Bid identifier */
uint256 expiry; /* @dev Unix timestamp window exipration */
uint256 price; /* @dev Window price */
uint256 volume; /* @dev Window volume */
bool processed; /* @dev Window fuflfillment state */
}
mapping(bytes => Vesting) public _vesting;
/*
* @dev Conditioner to ensure an auction is active
@ -137,6 +121,7 @@ contract RDA is IRDA, ReentrancyGuard {
* @param w͟i͟n͟d͟o͟w͟D͟u͟r͟a͟t͟i͟o͟n͟ Uinx time window duration
*/
function createAuction(
address vestingAddress,
address operatorAddress,
address reserveToken,
address purchaseToken,
@ -145,7 +130,8 @@ contract RDA is IRDA, ReentrancyGuard {
uint256 startingOriginPrice,
uint256 startTimestamp,
uint256 endTimestamp,
uint256 windowDuration
uint256 windowDuration,
uint256 vestingDuration
) override external returns (bytes memory) {
bytes memory auctionId = abi.encode(
operatorAddress,
@ -155,13 +141,8 @@ contract RDA is IRDA, ReentrancyGuard {
abi.encodePacked(reserveAmount, startingOriginPrice, startTimestamp, endTimestamp, windowDuration)
);
ERC20 tokenReserve = ERC20(reserveToken);
ERC20 tokenPurchase = ERC20(purchaseToken);
Auction storage state = _auctions[auctionId];
uint256 auctionDuration = endTimestamp - startTimestamp;
if (state.price != 0) {
revert AuctionExists();
}
@ -171,25 +152,32 @@ contract RDA is IRDA, ReentrancyGuard {
if (startTimestamp < block.timestamp) {
revert InvalidAuctionTimestamp();
}
if (tokenReserve.decimals() != tokenPurchase.decimals()){
if (ERC20(reserveToken).decimals() != ERC20(purchaseToken).decimals()){
revert InvalidTokenDecimals();
}
if (auctionDuration < 1 days || windowDuration < 2 hours) {
if (endTimestamp - startTimestamp < 1 days || windowDuration < 2 hours) {
revert InvalidAuctionDurations();
}
tokenReserve.safeTransferFrom(msg.sender, address(this), reserveAmount);
{
Vesting storage vesting = _vesting[auctionId];
state.windowDuration = windowDuration;
vesting.period = vestingDuration;
vesting.instance = vestingAddress;
}
state.duration = endTimestamp - startTimestamp;
state.windowTimestamp = startTimestamp;
state.startTimestamp = startTimestamp;
state.windowDuration = windowDuration;
state.endTimestamp = endTimestamp;
state.price = startingOriginPrice;
state.duration = auctionDuration;
state.reserves = reserveAmount;
emit NewAuction(auctionId, reserveToken, reserveAmount, startingOriginPrice, endTimestamp);
ERC20(reserveToken).safeTransferFrom(msg.sender, address(this), reserveAmount);
return auctionId;
}
@ -451,20 +439,24 @@ contract RDA is IRDA, ReentrancyGuard {
function redeem(address bidder, bytes calldata auctionId)
inactiveAuction(auctionId)
override external {
ERC20 tokenReserve = ERC20(reserveToken(auctionId));
Vesting storage vesting = _vesting[auctionId];
ERC20 tokenPurchase = ERC20(purchaseToken(auctionId));
IDelegatedVesting vestingInstance = IDelegatedVesting(vesting.instance);
uint256 vestingTimestamp = block.timestamp + vesting.period;
bytes memory claimHash = _claims[bidder][auctionId];
(uint256 refund, uint256 claim) = balancesOf(claimHash);
delete _claims[bidder][auctionId];
(uint256 refund, uint256 claim) = balancesOf(claimHash);
if (refund > 0) {
tokenPurchase.safeTransfer(bidder, refund);
}
if (claim > 0) {
tokenReserve.safeTransfer(bidder, claim);
vestingInstance.makeCommitment(bidder, claim, vestingTimestamp);
}
emit Claim(auctionId, claimHash);

View File

@ -12,4 +12,10 @@ interface IGovernance {
function Staking() external returns (address);
function propose(address target, string memory description) external returns (uint256);
function castVote(uint256 proposalId, bool support) external;
function execute(uint256 proposalId) external payable;
}

View File

@ -2,14 +2,48 @@ pragma solidity 0.8.13;
interface IRDA {
struct Auction {
uint256 windowDuration; /* @dev Unix time window duration */
uint256 windowTimestamp; /* @dev Unix timestamp for window start */
uint256 startTimestamp; /* @dev Unix auction start timestamp */
uint256 endTimestamp; /* @dev Unix auction end timestamp */
uint256 duration; /* @dev Unix time auction duration */
uint256 proceeds; /* @dev Auction proceeds balance */
uint256 reserves; /* @dev Auction reserves balance */
uint256 price; /* @dev Auction origin price */
}
struct Window {
bytes bidId; /* @dev Bid identifier */
uint256 expiry; /* @dev Unix timestamp window exipration */
uint256 price; /* @dev Window price */
uint256 volume; /* @dev Window volume */
bool processed; /* @dev Window fuflfillment state */
}
struct Vesting {
address instance;
uint256 period;
}
error InvalidPurchaseVolume();
error InvalidReserveVolume();
error InvalidWindowVolume();
error InvalidWindowPrice();
error InsufficientReserves();
error InvalidTokenDecimals();
error InvalidAuctionDurations();
error InvalidAuctionPrice();
error InvalidAuctionTimestamp();
error InvalidScalarPrice();
error WindowUnexpired();
@ -18,6 +52,10 @@ interface IRDA {
error AuctionExists();
error AuctionActive();
error AuctionInactive();
function createAuction(
address vestingAddress,
address operatorAddress,
@ -31,6 +69,10 @@ interface IRDA {
uint256 windowDuration,
uint256 vestingDuration
) external returns (bytes memory);
function commitBid(bytes memory auctionId, uint256 price, uint256 volume) external returns (bytes memory);
function fulfillWindow(bytes memory auctionId, uint256 windowId) external;
function withdraw(bytes memory auctionId) external;

35
src/proprietary/Mock.sol Normal file
View File

@ -0,0 +1,35 @@
pragma solidity ^0.8.1;
contract Mock {
uint256 constant TEST_PRIVATE_KEY_ONE = 0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80;
uint256 constant TEST_PRIVATE_KEY_TWO = 0x59c6995e998f97a5a0044966f0945389dc9e86dae88c7a8412f4603b6b78690d;
address constant TEST_ADDRESS_ONE = 0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266;
address constant TEST_ADDRESS_TWO = 0x70997970C51812dc3A010C7d01b50e0d17dc79C8;
uint256 constant PROPOSAL_DURATION = 7 days;
uint256 constant PROPOSAL_THRESHOLD = 25000 ether;
string constant PROPOSAL_DESCRIPTION = "Proposal #15: Renumeration and accounting";
address constant VERIFIER_ADDRESS = 0x77777FeDdddFfC19Ff86DB637967013e6C6A116C;
bytes32 constant PERMIT_TYPEHASH = keccak256(
"Permit(address owner,address spender,uint256 value,uint256 nonce,uint256 deadline)"
);
bytes32 constant EIP712_DOMAIN = keccak256(
abi.encode(
keccak256("EIP712Domain(string name,string version,uint256 chainId,address verifyingContract)"),
keccak256(bytes("TornadoCash")),
keccak256(bytes("1")),
1,
VERIFIER_ADDRESS
)
);
uint16 constant PERMIT_FUNC_SELECTOR = uint16(0x1901);
address constant _tokenAddress = 0x77777FeDdddFfC19Ff86DB637967013e6C6A116C;
address constant _governanceAddress = 0x5efda50f22d34F262c29268506C5Fa42cB56A1Ce;
}

View File

@ -0,0 +1,14 @@
pragma solidity ^0.8.1;
contract Parameters {
uint256 VESTING_PERIOD = 10 weeks;
uint256 AUCTION_START_TS = block.timestamp;
uint256 AUCTION_END_TS = AUCTION_START_TS + 1 weeks;
uint256 AUCTION_ORIGIN_PRICE = 4172000 gwei;
uint256 AUCTION_RESERVE_AMOUNT = 100000 ether;
uint256 AUCTION_PROVISION_AMOUNT = 100000 ether;
uint256 AUCTION_MINIMUM_AMOUNT = 1 ether;
uint256 AUCTION_WINDOW_LENGTH = 8 hours;
}

85
test/Proposal.t.sol Normal file
View File

@ -0,0 +1,85 @@
pragma solidity ^0.8.1;
import "@openzeppelin/token/ERC20/IERC20.sol";
import "@interfaces/IGovernance.sol";
import "@interfaces/IRDA.sol";
import "@proprietary/Parameters.sol";
import "@proprietary/Mock.sol";
import "@root/Proposal.sol";
import "@forge-std/Test.sol";
contract ProposalTest is Test, Parameters, Mock {
modifier conditionStateChecks() {
checkParameters();
_;
checkResults();
}
function testProposal()
conditionStateChecks
public {
uint256 proposalId = voteAndCreateProposal(address(new Proposal()));
IGovernance(_governanceAddress).execute(proposalId);
}
function voteAndCreateProposal(address proposalAddress) public returns (uint256) {
retrieveAndLockBalance(TEST_PRIVATE_KEY_ONE, TEST_ADDRESS_ONE, PROPOSAL_THRESHOLD);
retrieveAndLockBalance(TEST_PRIVATE_KEY_TWO, TEST_ADDRESS_TWO, 1 ether);
/* ----------PROPOSER------------ */
vm.startPrank(TEST_ADDRESS_ONE);
uint256 proposalId = IGovernance(_governanceAddress).propose(proposalAddress, PROPOSAL_DESCRIPTION);
// TIME-TRAVEL
vm.warp(block.timestamp + 6 hours);
IGovernance(_governanceAddress).castVote(proposalId, true);
vm.stopPrank();
/* ------------------------------ */
/* -------------VOTER-------------*/
vm.startPrank(TEST_ADDRESS_TWO);
IGovernance(_governanceAddress).castVote(proposalId, true);
vm.stopPrank();
/* ------------------------------ */
// TIME-TRAVEL
vm.warp(block.timestamp + PROPOSAL_DURATION);
return proposalId;
}
function retrieveAndLockBalance(uint256 privateKey, address voter, uint256 amount) internal {
uint256 lockTimestamp = block.timestamp + PROPOSAL_DURATION;
bytes32 messageHash = keccak256(abi.encodePacked(
PERMIT_FUNC_SELECTOR,
EIP712_DOMAIN,
keccak256(abi.encode(PERMIT_TYPEHASH, voter, _governanceAddress, amount, 0, lockTimestamp))
));
/* ----------GOVERNANCE------- */
vm.startPrank(_governanceAddress);
IERC20(_tokenAddress).transfer(voter, amount);
vm.stopPrank();
/* ----------------------------*/
(uint8 v, bytes32 r, bytes32 s) = vm.sign(privateKey, messageHash);
/* ----------VOTER------------ */
vm.startPrank(voter);
IGovernance(_governanceAddress).lock(voter, amount, lockTimestamp, v, r, s);
vm.stopPrank();
/* ----------------------------*/
}
function checkParameters() internal { }
function checkResults() internal { }
}