Skip to content

Commit

Permalink
Develop support retroactive bonus (#2599)
Browse files Browse the repository at this point in the history
* Support retroactive awarding benefits, and some extra events

* Also support staked for for operator

* Update the staked length functino name to be more consistent

---------

Co-authored-by: user <[email protected]>
  • Loading branch information
af-afk and user authored Apr 8, 2024
1 parent 313f434 commit 5fc34ae
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 7 deletions.
29 changes: 27 additions & 2 deletions contracts/ethereum/contracts/StakingV1.sol
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ contract StakingV1 is IStaking, IERC20, IEmergencyMode, IOperatorOwned {

event NewMerkleDistributor(address old, address _new);

event Day1BonusApplied(address user, uint stakedPosition);

/* ~~~~~~~~~~ HOUSEKEEPING ~~~~~~~~~~ */

/// @dev if false, emergency mode is active - can be called by either the
Expand Down Expand Up @@ -182,6 +184,8 @@ return a
// take the ERC20 from the spender.
flyToken_.safeTransferFrom(_spender, address(this), _flyAmount);

emit NewStake(_recipient, _flyAmount);

return _flyAmount;
}

Expand Down Expand Up @@ -210,7 +214,7 @@ return a
}
/* ~~~~~~~~~~ INFORMATIONAL ~~~~~~~~~~ */

function stakingPositionsLen(address _account) public view returns (uint) {
function stakedPositionsLen(address _account) public view returns (uint) {
return stakedStorage_[_account].length;
}

Expand All @@ -233,6 +237,16 @@ return a
return 0;
}

function stakedStorage(address _a, uint _p) public view returns (
bool receivedBonus,
uint256 flyVested,
uint256 depositTimestamp
) {
receivedBonus = stakedStorage_[_a][_p].receivedBonus;
flyVested = stakedStorage_[_a][_p].flyVested;
depositTimestamp = stakedStorage_[_a][_p].depositTimestamp;
}

/* ~~~~~~~~~~ NORMAL USER PUBLIC ~~~~~~~~~~ */

/// @inheritdoc IStaking
Expand All @@ -257,7 +271,10 @@ return a
uint256 flyStaked,
uint256 day1Points
) {
require(msg.sender == merkleDistributor_, "not merkle distributor");
require(
msg.sender == merkleDistributor_ || msg.sender == operator_,
"not merkle distributor"
);
flyStaked = _stake(msg.sender, _recipient, _flyAmount, true);
return (flyStaked, _calcDay1Points(_flyAmount));
}
Expand All @@ -283,6 +300,7 @@ return a
// take the full amount for this position, pop the staked amount, reduce
// the fly remaining, then move on.
flyRemaining -= s.flyVested;
emit UnstakeBeginning(msg.sender, s.flyVested, unstakedBy);
unstakingStorage_[msg.sender].push(UnstakingPrivate({
flyAmount: s.flyVested,
unstakedTimestamp: unstakedBy
Expand Down Expand Up @@ -348,6 +366,7 @@ return a
}
// now we can use ERC20 to send the token back, if they got more than 0 back.
if (flyReturned == 0) revert("no fly returned");
emit UnstakeFinalised(msg.sender, flyReturned);
flyToken_.safeTransfer(msg.sender, flyReturned);
}

Expand All @@ -360,6 +379,12 @@ return a
emit NewMerkleDistributor(_old, _new);
}

function applyDay1Bonus(address _user, uint _pos) public {
require(msg.sender == operator_, "operator only");
require(stakedStorage_[_user][_pos].flyVested > 0, "empty staking storage");
stakedStorage_[_user][_pos].receivedBonus = true;
}

/* ~~~~~~~~~~ EMERGENCY FUNCTIONS ~~~~~~~~~~ */

/// @inheritdoc IStaking
Expand Down
6 changes: 6 additions & 0 deletions contracts/ethereum/interfaces/IStaking.sol
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@ struct Staked {
* amounts out in an emergency mode stake.
*/
interface IStaking {
event NewStake(address staker, uint256 amount);

event UnstakeBeginning(address unstaker, uint256 amount, uint256 unlockTimestamp);

event UnstakeFinalised(address unstaker, uint256 amount);

/* ~~~~~~~~~~ SIMPLE GETTER ~~~~~~~~~~ */

/// @notice merkleDistributor that's in use for the stakeFor function.
Expand Down
10 changes: 5 additions & 5 deletions contracts/ethereum/test/staking-v1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1125,7 +1125,7 @@ describe("StakingV1", async () => {
expect(await stakingContract.callStatic.stake(amountStaked)).to.be.equal(amountStaked);
// then send on-chain to continue the life of the testing
await stakingContract.stake(amountStaked);
expect(await stakingContract.stakingPositionsLen(signerAddr)).to.be.equal(1);
expect(await stakingContract.stakedPositionsLen(signerAddr)).to.be.equal(1);
let { flyStaked } = await stakingContract.stakingDetails(signerAddr);
expect(flyStaked).to.be.equal(amountStaked);
const { flyRemaining } = await stakingContract.callStatic.beginUnstake(0);
Expand All @@ -1144,7 +1144,7 @@ describe("StakingV1", async () => {
.to.be.equal(amountStaked);

await stakingContract.stake(amountStaked);
expect(await stakingContract.stakingPositionsLen(signerAddr))
expect(await stakingContract.stakedPositionsLen(signerAddr))
.to.be.equal(1);

// now wait a seeminly random amount of time, get the points
Expand Down Expand Up @@ -1256,7 +1256,7 @@ describe("StakingV1", async () => {

// the amount of positions remaining should be equal to 2 since we
// closed the newest position.
expect(await stakingContract.stakingPositionsLen(signerAddr)).to.be.equal(2);
expect(await stakingContract.stakedPositionsLen(signerAddr)).to.be.equal(2);

({ flyStaked } = await stakingContract.stakingDetails(signerAddr));
expect(flyStaked).to.be.equal(s1.add(s2.sub(flyUnstake1)).add(s3));
Expand All @@ -1273,7 +1273,7 @@ describe("StakingV1", async () => {

({ flyStaked } = await stakingContract.stakingDetails(signerAddr));
expect(flyStaked).to.be.equal(s3);
expect(await stakingContract.stakingPositionsLen(signerAddr)).to.be.equal(1);
expect(await stakingContract.stakedPositionsLen(signerAddr)).to.be.equal(1);

/*
* unstake just 1 this time.
Expand All @@ -1287,7 +1287,7 @@ describe("StakingV1", async () => {

({ flyStaked } = await stakingContract.stakingDetails(signerAddr));
expect(flyStaked).to.be.equal(s3.sub(1));
expect(await stakingContract.stakingPositionsLen(signerAddr)).to.be.equal(1);
expect(await stakingContract.stakedPositionsLen(signerAddr)).to.be.equal(1);
});
});

Expand Down

0 comments on commit 5fc34ae

Please sign in to comment.