Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/transfer stake #325

Draft
wants to merge 2 commits into
base: feat/voting-power
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions contracts/dao/DXDInfluence.sol
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,43 @@ contract DXDInfluence is OwnableUpgradeable, AccountSnapshot {
votingPower.callback();
}

/**
* @dev Mints influence tokens according to the amount staked and takes a snapshot. The influence value
* is not stored, only the linear and exponential terms of the formula are updated, which are then used
* to compute the influence on the fly in the balance getter.
* @param _account Account that has staked the tokens.
* @param _to Account that has staked the tokens.
* @param _amount Amount of tokens to have been staked.
* @param _timeCommitment Time that the user commits to lock the tokens.
*/
function transfer(
address _account,
address _to,
uint256 _amount,
uint256 _timeCommitment
) external onlyOwner {
CumulativeStake storage lastCumulativeStake = cumulativeStakesSnapshots[_account][_lastSnapshotId(_account)];
CumulativeStake storage lastToCumulativeStake = cumulativeStakesSnapshots[_to][_lastSnapshotId(_account)];
uint256 currentSnapshotId = _snapshot(_account);

(uint256 linearTerm, uint256 exponentialTerm) = getFormulaTerms(_amount, _timeCommitment);

// Update account's stake data
CumulativeStake storage cumulativeStake = cumulativeStakesSnapshots[_account][currentSnapshotId];
cumulativeStake.linearTerm = lastCumulativeStake.linearTerm - linearTerm;
cumulativeStake.exponentialTerm = lastCumulativeStake.exponentialTerm - exponentialTerm;
// Update recipients stake data
CumulativeStake storage toCumulativeStake = cumulativeStakesSnapshots[_to][currentSnapshotId];
toCumulativeStake.linearTerm = lastToCumulativeStake.linearTerm + linearTerm;
toCumulativeStake.exponentialTerm = lastToCumulativeStake.exponentialTerm + exponentialTerm;

// Update global stake data
_totalInfluenceSnapshots[currentSnapshotId] = _totalInfluenceSnapshots[currentSnapshotId - 1];

// Notify Voting Power contract.
votingPower.callback();
}

/**
* @dev Updates the time a given amount of DXD was staked for and takes a snapshot. The influence value
* is not stored, only the linear and exponential terms of the formula are updated, which are then used
Expand Down
35 changes: 35 additions & 0 deletions contracts/dao/DXDStake.sol
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,41 @@ contract DXDStake is OwnableUpgradeable, OptimizedERC20SnapshotUpgradeable {
stakeCommitment.commitmentEnd = uint40(block.timestamp) + _newTimeCommitment;
}

/**
* @dev Transfers a commitment. The recipient gets the voting power rights that come from DXDInfluence
* and can claim the locked DXD once the stakes completes.
* @param _commitmentId Id of the commitment. The Id is an incremental variable for each account.
* @param _amount How much of the stake to transfer.
* @param _to Address that the stake commitment will be transferred to.
*/
function transferCommitment(uint256 _commitmentId, uint176 _amount, address _to) external {
StakeCommitment storage stakeCommitment = stakeCommitments[msg.sender][_commitmentId];
require(stakeCommitment.commitmentEnd != 0, "DXDStake: commitment inactive");

// Set new commitment
StakeCommitment storage newStakeCommitment = stakeCommitments[_to].push();
newStakeCommitment.stake = _amount;
newStakeCommitment.timeCommitment = stakeCommitment.timeCommitment;
newStakeCommitment.commitmentEnd = stakeCommitment.commitmentEnd;

// Transfer influence.
dxdInfluence.transfer(msg.sender, _to, _amount, stakeCommitment.timeCommitment);

// Transfer staked DXD tokens.
_burn(msg.sender, _amount);
_mint(_to, _amount);
_snapshot();

stakeCommitment.stake -= _amount;
if (stakeCommitment.stake == 0) {
// Clean old commitment.
stakeCommitment.timeCommitment = 0;
stakeCommitment.commitmentEnd = 0;
userWithdrawals[msg.sender] += 1;
totalWithdrawals += 1;
}
}

/**
* @dev Withdraws the tokens to the user.
* @param _account Account that has staked.
Expand Down