Skip to content

Commit

Permalink
Fixed restaking of the accumulated operator fee
Browse files Browse the repository at this point in the history
  • Loading branch information
u-hubar committed May 10, 2024
1 parent 172cacb commit a425046
Show file tree
Hide file tree
Showing 8 changed files with 202 additions and 139 deletions.
46 changes: 23 additions & 23 deletions abi/Staking.json
Original file line number Diff line number Diff line change
Expand Up @@ -200,29 +200,6 @@
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [
{
"internalType": "address",
"name": "sender",
"type": "address"
},
{
"internalType": "uint72",
"name": "identityId",
"type": "uint72"
},
{
"internalType": "uint96",
"name": "stakeAmount",
"type": "uint96"
}
],
"name": "addStake",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [
{
Expand Down Expand Up @@ -313,6 +290,29 @@
"stateMutability": "view",
"type": "function"
},
{
"inputs": [
{
"internalType": "address",
"name": "sender",
"type": "address"
},
{
"internalType": "uint72",
"name": "identityId",
"type": "uint72"
},
{
"internalType": "uint96",
"name": "stakeAmount",
"type": "uint96"
}
],
"name": "restakeAccumulatedOperatorFees",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [],
"name": "serviceAgreementStorageProxy",
Expand Down
46 changes: 23 additions & 23 deletions abi/StakingV2.json
Original file line number Diff line number Diff line change
Expand Up @@ -449,29 +449,6 @@
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [
{
"internalType": "address",
"name": "sender",
"type": "address"
},
{
"internalType": "uint72",
"name": "identityId",
"type": "uint72"
},
{
"internalType": "uint96",
"name": "stakeAmount",
"type": "uint96"
}
],
"name": "addStake",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [
{
Expand Down Expand Up @@ -588,6 +565,29 @@
"stateMutability": "view",
"type": "function"
},
{
"inputs": [
{
"internalType": "address",
"name": "sender",
"type": "address"
},
{
"internalType": "uint72",
"name": "identityId",
"type": "uint72"
},
{
"internalType": "uint96",
"name": "stakeAmount",
"type": "uint96"
}
],
"name": "restakeAccumulatedOperatorFees",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [],
"name": "serviceAgreementStorageProxy",
Expand Down
4 changes: 2 additions & 2 deletions contracts/v1/Profile.sol
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ contract Profile is Named, Versioned, ContractStatus, Initializable {
event AccumulatedOperatorFeeRestaked(uint72 indexed identityId, uint96 amount);

string private constant _NAME = "Profile";
string private constant _VERSION = "1.1.1";
string private constant _VERSION = "1.2.0";

HashingProxy public hashingProxy;
Identity public identityContract;
Expand Down Expand Up @@ -182,7 +182,7 @@ contract Profile is Named, Versioned, ContractStatus, Initializable {
revert ProfileErrors.NoOperatorFees(identityId);
}
ps.setAccumulatedOperatorFee(identityId, 0);
stakingContract.addStake(msg.sender, identityId, accumulatedOperatorFee);
stakingContract.restakeAccumulatedOperatorFees(msg.sender, identityId, accumulatedOperatorFee);

emit AccumulatedOperatorFeeRestaked(identityId, accumulatedOperatorFee);
}
Expand Down
104 changes: 66 additions & 38 deletions contracts/v1/Staking.sol
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ contract Staking is Named, Versioned, ContractStatus, Initializable {
event OperatorFeeUpdated(uint72 indexed identityId, bytes nodeId, uint8 operatorFee);

string private constant _NAME = "Staking";
string private constant _VERSION = "1.0.2";
string private constant _VERSION = "1.1.0";

ShardingTable public shardingTableContract;
IdentityStorage public identityStorage;
Expand Down Expand Up @@ -83,12 +83,74 @@ contract Staking is Named, Versioned, ContractStatus, Initializable {
return _VERSION;
}

function addStake(address sender, uint72 identityId, uint96 stakeAmount) external onlyContracts {
_addStake(sender, identityId, stakeAmount);
function restakeAccumulatedOperatorFees(
address sender,
uint72 identityId,
uint96 stakeAmount
) external onlyContracts {
StakingStorage ss = stakingStorage;
ProfileStorage ps = profileStorage;
ParametersStorage params = parametersStorage;

uint96 oldStake = ss.totalStakes(identityId);
uint96 newStake = oldStake + stakeAmount;

require(ps.profileExists(identityId), "Profile doesn't exist");
require(newStake <= params.maximumStake(), "Exceeded the maximum stake");

Shares sharesContract = Shares(ps.getSharesContractAddress(identityId));

uint256 sharesMinted;
if (sharesContract.totalSupply() == 0) {
sharesMinted = stakeAmount;
} else {
sharesMinted = ((stakeAmount * sharesContract.totalSupply()) / oldStake);
}

sharesContract.mint(sender, sharesMinted);

ss.setTotalStake(identityId, newStake);
ps.transferAccumulatedOperatorFee(address(ss), stakeAmount);

if (!shardingTableStorage.nodeExists(identityId) && newStake >= params.minimumStake()) {
shardingTableContract.pushBack(identityId);
}

emit StakeIncreased(identityId, ps.getNodeId(identityId), sender, oldStake, newStake);
}

function addStake(uint72 identityId, uint96 stakeAmount) external onlyAdmin(identityId) {
_addStake(msg.sender, identityId, stakeAmount);
StakingStorage ss = stakingStorage;
ProfileStorage ps = profileStorage;
ParametersStorage params = parametersStorage;
IERC20 tknc = tokenContract;

uint96 oldStake = ss.totalStakes(identityId);
uint96 newStake = oldStake + stakeAmount;

require(ps.profileExists(identityId), "Profile doesn't exist");
require(tknc.allowance(msg.sender, address(this)) >= stakeAmount, "Allowance < stakeAmount");
require(newStake <= params.maximumStake(), "Exceeded the maximum stake");

Shares sharesContract = Shares(ps.getSharesContractAddress(identityId));

uint256 sharesMinted;
if (sharesContract.totalSupply() == 0) {
sharesMinted = stakeAmount;
} else {
sharesMinted = ((stakeAmount * sharesContract.totalSupply()) / oldStake);
}

sharesContract.mint(msg.sender, sharesMinted);

ss.setTotalStake(identityId, newStake);
tknc.transferFrom(msg.sender, address(ss), stakeAmount);

if (!shardingTableStorage.nodeExists(identityId) && newStake >= params.minimumStake()) {
shardingTableContract.pushBack(identityId);
}

emit StakeIncreased(identityId, ps.getNodeId(identityId), msg.sender, oldStake, newStake);
}

function startStakeWithdrawal(uint72 identityId, uint96 sharesToBurn) external {
Expand Down Expand Up @@ -204,40 +266,6 @@ contract Staking is Named, Versioned, ContractStatus, Initializable {
emit OperatorFeeUpdated(identityId, profileStorage.getNodeId(identityId), operatorFee);
}

function _addStake(address sender, uint72 identityId, uint96 stakeAmount) internal virtual {
StakingStorage ss = stakingStorage;
ProfileStorage ps = profileStorage;
ParametersStorage params = parametersStorage;
IERC20 tknc = tokenContract;

uint96 oldStake = ss.totalStakes(identityId);
uint96 newStake = oldStake + stakeAmount;

require(ps.profileExists(identityId), "Profile doesn't exist");
require(tknc.allowance(sender, address(this)) >= stakeAmount, "Allowance < stakeAmount");
require(newStake <= params.maximumStake(), "Exceeded the maximum stake");

Shares sharesContract = Shares(ps.getSharesContractAddress(identityId));

uint256 sharesMinted;
if (sharesContract.totalSupply() == 0) {
sharesMinted = stakeAmount;
} else {
sharesMinted = ((stakeAmount * sharesContract.totalSupply()) / oldStake);
}

sharesContract.mint(sender, sharesMinted);

ss.setTotalStake(identityId, newStake);
tknc.transferFrom(sender, address(ss), stakeAmount);

if (!shardingTableStorage.nodeExists(identityId) && newStake >= params.minimumStake()) {
shardingTableContract.pushBack(identityId);
}

emit StakeIncreased(identityId, ps.getNodeId(identityId), sender, oldStake, newStake);
}

function _checkAdmin(uint72 identityId) internal view virtual {
require(
identityStorage.keyHasPurpose(identityId, keccak256(abi.encodePacked(msg.sender)), ADMIN_KEY),
Expand Down
Loading

0 comments on commit a425046

Please sign in to comment.