Skip to content

Commit

Permalink
Merge pull request #299 from OriginTrail/v8/paranet-ka-submission-fix
Browse files Browse the repository at this point in the history
Fixed check during KA submission to Paranet, added redelegation
  • Loading branch information
br41nl3t authored Oct 30, 2024
2 parents 6ad3e9b + 4c19721 commit 3725a24
Show file tree
Hide file tree
Showing 30 changed files with 484 additions and 6,332 deletions.
23 changes: 23 additions & 0 deletions abi/StakingV2.json
Original file line number Diff line number Diff line change
Expand Up @@ -681,6 +681,29 @@
"stateMutability": "view",
"type": "function"
},
{
"inputs": [
{
"internalType": "uint72",
"name": "from",
"type": "uint72"
},
{
"internalType": "uint72",
"name": "to",
"type": "uint72"
},
{
"internalType": "uint96",
"name": "sharesToBurn",
"type": "uint96"
}
],
"name": "redelegate",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [],
"name": "serviceAgreementStorageProxy",
Expand Down
76 changes: 75 additions & 1 deletion contracts/v2/Staking.sol
Original file line number Diff line number Diff line change
Expand Up @@ -88,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.2.0";
string private constant _VERSION = "2.3.0";

ShardingTableV2 public shardingTableContract;
IdentityStorageV2 public identityStorage;
Expand Down Expand Up @@ -212,6 +212,80 @@ contract StakingV2 is Named, Versioned, ContractStatusV2, Initializable {
emit SharesMinted(identityId, address(sharesContract), msg.sender, sharesMinted, sharesContract.totalSupply());
}

function redelegate(uint72 from, uint72 to, uint96 sharesToBurn) external {
if (sharesToBurn == 0) {
revert StakingErrors.ZeroSharesAmount();
}

ProfileStorage ps = profileStorage;
StakingStorage ss = stakingStorage;
ShardingTableStorageV2 sts = shardingTableStorage;

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

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

Shares fromSharesContract = Shares(ps.getSharesContractAddress(from));
Shares toSharesContract = Shares(ps.getSharesContractAddress(to));

if (sharesToBurn > fromSharesContract.balanceOf(msg.sender)) {
revert TokenErrors.TooLowBalance(address(fromSharesContract), fromSharesContract.balanceOf(msg.sender));
}

ParametersStorage params = parametersStorage;

uint96 fromCurrentStake = ss.totalStakes(from);
uint96 toCurrentStake = ss.totalStakes(to);

uint96 redelegationAmount = uint96(
(uint256(fromCurrentStake) * sharesToBurn) / fromSharesContract.totalSupply()
);

if (toCurrentStake + redelegationAmount > params.maximumStake()) {
revert StakingErrors.MaximumStakeExceeded(params.maximumStake());
}

fromSharesContract.burnFrom(msg.sender, sharesToBurn);

uint256 sharesToMint;
if (toSharesContract.totalSupply() == 0) {
sharesToMint = redelegationAmount;
} else {
sharesToMint = ((uint256(redelegationAmount) * toSharesContract.totalSupply()) / toCurrentStake);
}
toSharesContract.mint(msg.sender, sharesToMint);

ss.setTotalStake(from, fromCurrentStake - redelegationAmount);

if (sts.nodeExists(from) && (fromCurrentStake - redelegationAmount) < params.minimumStake()) {
shardingTableContract.removeNode(from);
}

ss.setTotalStake(to, toCurrentStake + redelegationAmount);

if (!sts.nodeExists(to) && (toCurrentStake + redelegationAmount >= params.minimumStake())) {
if (sts.nodesCount() >= params.shardingTableSizeLimit()) {
revert ShardingTableErrors.ShardingTableIsFull();
}
shardingTableContract.insertNode(to);
}

emit SharesBurned(
from,
address(fromSharesContract),
msg.sender,
sharesToBurn,
fromSharesContract.totalSupply()
);
emit StakeWithdrawn(from, ps.getNodeId(from), msg.sender, redelegationAmount);
emit SharesMinted(to, address(toSharesContract), msg.sender, sharesToMint, toSharesContract.totalSupply());
emit StakeIncreased(to, ps.getNodeId(to), msg.sender, toCurrentStake, toCurrentStake + redelegationAmount);
}

function startStakeWithdrawal(uint72 identityId, uint96 sharesToBurn) external {
if (sharesToBurn == 0) {
revert StakingErrors.ZeroSharesAmount();
Expand Down
8 changes: 6 additions & 2 deletions contracts/v2/paranets/Paranet.sol
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ contract Paranet is Named, Versioned, ContractStatusV2, Initializable {
);

string private constant _NAME = "Paranet";
string private constant _VERSION = "2.2.0";
string private constant _VERSION = "2.2.1";

ParanetsRegistry public paranetsRegistry;
ParanetServicesRegistry public paranetServicesRegistry;
Expand Down Expand Up @@ -914,7 +914,11 @@ contract Paranet is Named, Versioned, ContractStatusV2, Initializable {
}
}

if (paranetKnowledgeAssetsRegistry.isParanetKnowledgeAsset(paranetId)) {
if (
paranetKnowledgeAssetsRegistry.isParanetKnowledgeAsset(
keccak256(abi.encodePacked(knowledgeAssetStorageContract, knowledgeAssetTokenId))
)
) {
revert ParanetErrors.KnowledgeAssetIsAPartOfOtherParanet(
knowledgeAssetStorageContract,
knowledgeAssetTokenId,
Expand Down
2 changes: 1 addition & 1 deletion deploy/035_deploy_profile_v2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const func: DeployFunction = async function (hre: HardhatRuntimeEnvironment) {
};

export default func;
func.tags = ['Profile', 'v2'];
func.tags = ['ProfileV2', 'v2'];
func.dependencies = [
'Hub',
'Identity',
Expand Down
Loading

0 comments on commit 3725a24

Please sign in to comment.