Skip to content

Commit

Permalink
Merge pull request #288 from OriginTrail/feature/cancel-stake-withdrawal
Browse files Browse the repository at this point in the history
[FEATURE] Added cancelStakeWithdrawal function
  • Loading branch information
u-hubar authored Aug 28, 2024
2 parents a676167 + b11bcd3 commit 5da8899
Show file tree
Hide file tree
Showing 15 changed files with 280 additions and 289 deletions.
62 changes: 62 additions & 0 deletions abi/StakingV2.json
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,55 @@
"name": "StakeIncreased",
"type": "event"
},
{
"anonymous": false,
"inputs": [
{
"indexed": true,
"internalType": "uint72",
"name": "identityId",
"type": "uint72"
},
{
"indexed": false,
"internalType": "bytes",
"name": "nodeId",
"type": "bytes"
},
{
"indexed": true,
"internalType": "address",
"name": "staker",
"type": "address"
},
{
"indexed": false,
"internalType": "uint96",
"name": "oldStake",
"type": "uint96"
},
{
"indexed": false,
"internalType": "uint96",
"name": "newStake",
"type": "uint96"
},
{
"indexed": false,
"internalType": "uint256",
"name": "sharesMintedAmount",
"type": "uint256"
},
{
"indexed": false,
"internalType": "uint256",
"name": "newTotalSupply",
"type": "uint256"
}
],
"name": "StakeWithdrawalCanceled",
"type": "event"
},
{
"anonymous": false,
"inputs": [
Expand Down Expand Up @@ -521,6 +570,19 @@
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [
{
"internalType": "uint72",
"name": "identityId",
"type": "uint72"
}
],
"name": "cancelStakeWithdrawal",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [
{
Expand Down
67 changes: 66 additions & 1 deletion contracts/v2/Staking.sol
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,15 @@ contract StakingV2 is Named, Versioned, ContractStatusV2, Initializable {
uint256 withdrawalPeriodEnd
);
event StakeWithdrawn(uint72 indexed identityId, bytes nodeId, address indexed staker, uint96 withdrawnStakeAmount);
event StakeWithdrawalCanceled(
uint72 indexed identityId,
bytes nodeId,
address indexed staker,
uint96 oldStake,
uint96 newStake,
uint256 sharesMintedAmount,
uint256 newTotalSupply
);
event InactiveStakeWithdrawn(
uint72 indexed identityId,
bytes nodeId,
Expand All @@ -79,7 +88,7 @@ contract StakingV2 is Named, Versioned, ContractStatusV2, Initializable {
event OperatorFeeChangeFinished(uint72 indexed identityId, bytes nodeId, uint8 operatorFee);

string private constant _NAME = "Staking";
string private constant _VERSION = "2.1.1";
string private constant _VERSION = "2.2.0";

ShardingTableV2 public shardingTableContract;
IdentityStorageV2 public identityStorage;
Expand Down Expand Up @@ -268,6 +277,7 @@ contract StakingV2 is Named, Versioned, ContractStatusV2, Initializable {
if (!ps.profileExists(identityId)) {
revert ProfileErrors.ProfileDoesntExist(identityId);
}

StakingStorage ss = stakingStorage;

uint96 stakeWithdrawalAmount;
Expand All @@ -287,6 +297,61 @@ contract StakingV2 is Named, Versioned, ContractStatusV2, Initializable {
emit StakeWithdrawn(identityId, ps.getNodeId(identityId), msg.sender, stakeWithdrawalAmount);
}

function cancelStakeWithdrawal(uint72 identityId) external {
ProfileStorage ps = profileStorage;

if (!ps.profileExists(identityId)) {
revert ProfileErrors.ProfileDoesntExist(identityId);
}

StakingStorage ss = stakingStorage;

uint96 stakeWithdrawalAmount;
uint256 withdrawalTimestamp;
(stakeWithdrawalAmount, withdrawalTimestamp) = ss.withdrawalRequests(identityId, msg.sender);

if (stakeWithdrawalAmount == 0) {
revert StakingErrors.WithdrawalWasntInitiated();
}

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

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

uint256 sharesMinted;
if (sharesContract.totalSupply() == 0) {
sharesMinted = stakeWithdrawalAmount;
} else {
sharesMinted = ((uint256(stakeWithdrawalAmount) * sharesContract.totalSupply()) / oldStake);
}
sharesContract.mint(msg.sender, sharesMinted);

ss.deleteWithdrawalRequest(identityId, msg.sender);
ss.setTotalStake(identityId, newStake);

ShardingTableStorageV2 sts = shardingTableStorage;
ParametersStorage params = parametersStorage;

if (!sts.nodeExists(identityId) && newStake >= params.minimumStake()) {
if (sts.nodesCount() >= params.shardingTableSizeLimit()) {
revert ShardingTableErrors.ShardingTableIsFull();
}
shardingTableContract.insertNode(identityId);
}
emit StakeWithdrawalCanceled(
identityId,
ps.getNodeId(identityId),
msg.sender,
oldStake,
newStake,
sharesMinted,
sharesContract.totalSupply()
);
emit StakeIncreased(identityId, ps.getNodeId(identityId), msg.sender, oldStake, newStake);
emit SharesMinted(identityId, address(sharesContract), msg.sender, sharesMinted, sharesContract.totalSupply());
}

function addReward(bytes32 agreementId, uint72 identityId, uint96 rewardAmount) external onlyContracts {
ServiceAgreementStorageProxy sasProxy = serviceAgreementStorageProxy;
NodeOperatorFeesStorage nofs = nodeOperatorFeesStorage;
Expand Down
12 changes: 6 additions & 6 deletions deployments/base_mainnet_contracts.json
Original file line number Diff line number Diff line change
Expand Up @@ -239,12 +239,12 @@
"deployed": true
},
"Staking": {
"evmAddress": "0x174FD3a9dbC2562F2360C9797d7dbF21f3bD6824",
"version": "2.1.1",
"gitBranch": "feature/base",
"gitCommitHash": "d3eab3d1a0fde48db522b16b49654558eed07dc3",
"deploymentBlock": 16310726,
"deploymentTimestamp": 1719410803582,
"evmAddress": "0x39713a4E7d05dAB796fCA6e84cBA5aFC905822d9",
"version": "2.2.0",
"gitBranch": "main",
"gitCommitHash": "b9e7dede2a75acf193272f84b1a5ce155210e47a",
"deploymentBlock": 19022524,
"deploymentTimestamp": 1724834405325,
"deployed": true
},
"Profile": {
Expand Down
12 changes: 6 additions & 6 deletions deployments/base_sepolia_dev_contracts.json
Original file line number Diff line number Diff line change
Expand Up @@ -239,12 +239,12 @@
"deployed": true
},
"Staking": {
"evmAddress": "0xd4847eF7c0BF7d223a33339DA4f4194DC1DC7f8c",
"version": "2.1.1",
"gitBranch": "feature/base",
"gitCommitHash": "49543b7f02d9543b4c744dc8b8153921cf178ad9",
"deploymentBlock": 11509099,
"deploymentTimestamp": 1718786489842,
"evmAddress": "0x1E9BaA5b1c0003385f1C27d8175D59974c10A0dA",
"version": "2.2.0",
"gitBranch": "main",
"gitCommitHash": "b9e7dede2a75acf193272f84b1a5ce155210e47a",
"deploymentBlock": 14543288,
"deploymentTimestamp": 1724854871142,
"deployed": true
},
"Profile": {
Expand Down
12 changes: 6 additions & 6 deletions deployments/base_sepolia_test_contracts.json
Original file line number Diff line number Diff line change
Expand Up @@ -239,12 +239,12 @@
"deployed": true
},
"Staking": {
"evmAddress": "0x6d206b6834fE82811C3c659e7a6A6deeB076fe0B",
"version": "2.1.1",
"gitBranch": "feature/base",
"gitCommitHash": "49543b7f02d9543b4c744dc8b8153921cf178ad9",
"deploymentBlock": 11510539,
"deploymentTimestamp": 1718789370362,
"evmAddress": "0x1dD6bE1EC9410aff2E95685d84cEA6aCf40bFBa9",
"version": "2.2.0",
"gitBranch": "main",
"gitCommitHash": "b9e7dede2a75acf193272f84b1a5ce155210e47a",
"deploymentBlock": 14543315,
"deploymentTimestamp": 1724854925334,
"deployed": true
},
"Profile": {
Expand Down
12 changes: 6 additions & 6 deletions deployments/gnosis_chiado_dev_contracts.json
Original file line number Diff line number Diff line change
Expand Up @@ -265,12 +265,12 @@
"deployed": true
},
"Staking": {
"evmAddress": "0x140CA29bca7e9449f50021E051c8EE3187b48e4D",
"version": "2.1.1",
"gitBranch": "deployment/4.3.0",
"gitCommitHash": "7bd7fcbc9d5e8bfa5eb47573d57b0958515aca00",
"deploymentBlock": 10291472,
"deploymentTimestamp": 1718285932780,
"evmAddress": "0x4991C23D82469e1b9b583515Ed400e5B0437d499",
"version": "2.2.0",
"gitBranch": "main",
"gitCommitHash": "b9e7dede2a75acf193272f84b1a5ce155210e47a",
"deploymentBlock": 11519675,
"deploymentTimestamp": 1724854757920,
"deployed": true
},
"ParanetsRegistry": {
Expand Down
12 changes: 6 additions & 6 deletions deployments/gnosis_chiado_test_contracts.json
Original file line number Diff line number Diff line change
Expand Up @@ -256,12 +256,12 @@
"deployed": true
},
"Staking": {
"evmAddress": "0x878686d27ab7611D6cCA884Ad715B543F35b467a",
"version": "2.1.1",
"gitBranch": "deployment/4.3.0",
"gitCommitHash": "7bd7fcbc9d5e8bfa5eb47573d57b0958515aca00",
"deploymentBlock": 10291479,
"deploymentTimestamp": 1718285965963,
"evmAddress": "0xe4ce91C83B018B6E1e5a117eD899F35E502400E0",
"version": "2.2.0",
"gitBranch": "main",
"gitCommitHash": "b9e7dede2a75acf193272f84b1a5ce155210e47a",
"deploymentBlock": 11519686,
"deploymentTimestamp": 1724854815721,
"deployed": true
},
"ContentAsset": {
Expand Down
12 changes: 6 additions & 6 deletions deployments/gnosis_mainnet_contracts.json
Original file line number Diff line number Diff line change
Expand Up @@ -201,12 +201,12 @@
"deployed": true
},
"Staking": {
"evmAddress": "0xAfFBc5E2833192aD3E28AE586aBA2c704aE76775",
"version": "2.1.1",
"gitBranch": "deployment/4.3.0",
"gitCommitHash": "7bd7fcbc9d5e8bfa5eb47573d57b0958515aca00",
"deploymentBlock": 34443978,
"deploymentTimestamp": 1718286039986,
"evmAddress": "0x8883aA8cd98fCf6D9203b731F843Fe9eed09E3bb",
"version": "2.2.0",
"gitBranch": "main",
"gitCommitHash": "b9e7dede2a75acf193272f84b1a5ce155210e47a",
"deploymentBlock": 35708723,
"deploymentTimestamp": 1724834659300,
"deployed": true
},
"Profile": {
Expand Down
Loading

0 comments on commit 5da8899

Please sign in to comment.