delegated vesting refactor

This commit is contained in:
gozzy 2023-03-11 21:08:48 +00:00
parent de6762b45c
commit eb35892b1c

View File

@ -1,5 +1,3 @@
// SPDX-License-Identifier: Unlicense
pragma solidity 0.8.0;
import "./interfaces/IERC20.sol";
@ -7,152 +5,141 @@ import "./interfaces/IGovernance.sol";
contract DelegatedInstance {
address public spender;
address public sender;
uint256 public balance;
address public _spender;
address public _sender;
uint256 public _balance;
IGovernance governance;
IERC20 token;
address public _governanceAddress;
address public _tokenAddress;
constructor(
address stakeholderAddress,
address governanceAddress,
address tokenAddress,
uint256 stakeAmount
) {
governance = IGovernance(governanceAddress);
token = IERC20(tokenAddress);
constructor(
address stakeholderAddress,
address governanceAddress,
address tokenAddress,
uint256 stakeAmount
) {
_governanceAddress = governanceAddress;
_tokenAddress = tokenAddress;
spender = stakeholderAddress;
balance = stakeAmount;
sender = msg.sender;
}
_spender = stakeholderAddress;
_balance = stakeAmount;
_sender = msg.sender;
}
function delegate(address to) public {
require(msg.sender == spender, "Incorrect spender");
modifier isSpender() {
require(msg.sender == _spender, "INVALID SPENDER");
_;
}
governance.delegate(to);
}
function delegate(address to) public isSpender {
IGovernance(_governanceAddress).delegate(to);
}
function lockAndDelegate(
address to,
uint256 amount, uint256 deadline,
uint8 v, bytes32 r, bytes32 s
) external {
require(msg.sender == sender);
function lockAndDelegate(
address to,
uint256 amount,
uint256 deadline,
uint8 v,
bytes32 r,
bytes32 s
) external isSpender {
IERC20(_tokenAddress).transferFrom(msg.sender, address(this), amount);
IGovernance(_governanceAddress).lock(address(this), amount, deadline, v, r, s);
IGovernance(_governanceAddress).delegate(to);
}
token.transferFrom(msg.sender, address(this), amount);
governance.lock(
address(this), amount, deadline, v, r, s
);
governance.delegate(to);
}
function unlockAndRedeem() public isSpender {
uint256 stake = _balance;
function unlockAndRedeem() public {
require(msg.sender == sender, "Incorrect sender");
delete balance;
uint256 stake = balance;
delete balance;
governance.unlock(stake);
token.transfer(spender, stake);
}
IGovernance(_governanceAddress).unlock(stake);
IERC20(_tokenAddress).transfer(_spender, stake);
}
}
contract DelegatedVesting {
uint256 public vestingPeriod;
address public vestingGovernance;
IERC20 public vestingToken;
uint256 public _period;
address public _governanceAddress;
address public _tokenAddress;
mapping(address => uint256) balances;
mapping(address => address) delegations;
mapping(address => uint256) commitments;
mapping(address => uint256) _balances;
mapping(address => address) _instance;
mapping(address => uint256) _commitments;
constructor(
uint256 vestingTimestamp,
uint256 vestingDuration,
address governanceAddress,
address tokenAddress
) {
vestingPeriod = vestingTimestamp;
vestingToken = IERC20(tokenAddress);
vestingGovernance = governanceAddress;
_period = vestingDuration;
_tokenAddress = tokenAddress;
_operatorAddress = msg.sender;
_governanceAddress = governanceAddress;
}
function isActiveCommitment(address stakeholderAddress) public view returns (bool) {
uint256 commitment = commitments[stakeholderAddress];
uint256 stake = balances[stakeholderAddress];
return stake > 0 && commitment > now;
function isActiveCommitment(address stakeholder) public view returns (bool) {
return _balances[stakeholder] > 0 && _commitments[stakeholder] > block.timestamp;
}
function isDelegatedCommitment(address stakeholderAddress) public view returns (bool) {
uint256 delegated = delegations[stakeholderAddress];
bool state = isActiveCommitment(stakeholderAddress);
return state && delegated != address(0x0);
function isDelegatedCommitment(address stakeholder) public view returns (bool) {
return isActiveCommitment(stakeholder) && _instance[stakeholder] != address(0x0);
}
function isFulfilledCommitment(address stakeholderAddress) public view returns (bool) {
uint256 commitment = commitments[stakeholderAddress];
uint256 stake = balances[stakeholderAddress];
return stake > 0 && commitment < now;
function isFulfilledCommitment(address stakeholder) public view returns (bool) {
return _balances[stakeholder] > 0 && _commitments[stakeholder] < block.timestamp;
}
function makeCommitment(
address recipientAddress,
uint256 amount
) public {
require(vestingToken.transferFrom(msg.sender, address(this), amount));
function makeCommitment(address stakeholder, uint256 amount) public {
require(IERC20(_tokenAddress).transferFrom(msg.sender, address(this), amount));
commitments[recipientAddress] = vestingPeriod;
if(isActiveCommitment(recipientAddress)) {
balances[recipientAddress] = balances[recipientAddress] + amount;
} else {
balances[recipientAddress] = amount;
}
_commitments[stakeholder] = block.timestmap + _period;
_balances[stakeholder] += amount;
}
function delegateCommitment(
address to,
address delegateAddress,
uint256 deadline,
uint8 v, bytes32 r, bytes32 s
uint8 v,
bytes32 r,
bytes32 s
) public {
require(isActiveCommitment(msg.sender), "Not an active commitment");
require(isActiveCommitment(msg.sender), "INVALID COMMITMENT");
if(isDelegatedCommitment(msg.sender)) {
DelegatedInstance(delegations[msg.sender]).delegate(to);
DelegatedInstance(_instance[msg.sender]).delegate(delegateAddress);
} else {
DelegatedInstance e = new DelegatedInstance(
msg.sender,
vestingGovernance,
address(vestingToken),
balances[msg.sender],
_governanceAddress,
_tokenAddress,
_balances[msg.sender],
deadline,
v, r, s
);
vestingToken.approve(address(e), balances[msg.sender]);
e.lockAndDelegate(to, balances[msg.sender], deadline, v, r, s);
delegations[msg.sender] = address(e);
_instance[msg.sender] = address(e);
IERC20(_tokenAddress).approve(address(e), _balances[msg.sender]);
e.lockAndDelegate(delegateAddress, _balances[msg.sender], deadline, v, r, s);
}
}
function fulfilCommitment() public {
require(isFulfilledCommitment(msg.sender), "Commitment is not possible to fulfil");
function fulfillCommitment() public {
require(isFulfilledCommitment(msg.sender), "INVALID FULFILLMENT");
uint256 stake = balances[msg.sender];
uint256 delegated = delegations[msg.sender];
delete balances[msg.sender];
uint256 stake = _balances[msg.sender];
address delegated = _instance[msg.sender];
delete _balances[msg.sender];
delete _instance[msg.sender];
if(delegated != address(0x0)){
delete delegations[msg.sender];
DelegatedInstance(delegated).unlockAndRedeem();
} else {
vestingToken.transfer(msg.sender, stake);
IERC20(_tokenAddress).transfer(msg.sender, stake);
}
}