From 4eed0236fa3b6257028ea13fe70806ab48066dba Mon Sep 17 00:00:00 2001 From: Vamsi Date: Mon, 22 Mar 2021 16:15:03 +0530 Subject: [PATCH 1/2] Delegator unbond events with Id handled. Delegated Amount deduction moved to shareburn event and removed from unstake event --- root/abis/StakingInfo.json | 133 ++++++++++++++++++++++++++++++ root/src/mappings/staking-info.ts | 39 ++++++++- root/subgraph.template.yaml | 4 + 3 files changed, 173 insertions(+), 3 deletions(-) diff --git a/root/abis/StakingInfo.json b/root/abis/StakingInfo.json index c732d8a..1ba99fa 100644 --- a/root/abis/StakingInfo.json +++ b/root/abis/StakingInfo.json @@ -155,6 +155,37 @@ "name": "DelegatorUnstaked", "type": "event" }, + { + "anonymous": false, + "inputs": [ + { + "indexed": true, + "internalType": "uint256", + "name": "validatorId", + "type": "uint256" + }, + { + "indexed": true, + "internalType": "address", + "name": "user", + "type": "address" + }, + { + "indexed": false, + "internalType": "uint256", + "name": "amount", + "type": "uint256" + }, + { + "indexed": false, + "internalType": "uint256", + "name": "nonce", + "type": "uint256" + } + ], + "name": "DelegatorUnstakeWithId", + "type": "event" + }, { "anonymous": false, "inputs": [ @@ -312,6 +343,43 @@ "name": "ShareBurned", "type": "event" }, + { + "anonymous": false, + "inputs": [ + { + "indexed": true, + "internalType": "uint256", + "name": "validatorId", + "type": "uint256" + }, + { + "indexed": true, + "internalType": "address", + "name": "user", + "type": "address" + }, + { + "indexed": true, + "internalType": "uint256", + "name": "amount", + "type": "uint256" + }, + { + "indexed": false, + "internalType": "uint256", + "name": "tokens", + "type": "uint256" + }, + { + "indexed": false, + "internalType": "uint256", + "name": "nonce", + "type": "uint256" + } + ], + "name": "ShareBurnedWithId", + "type": "event" + }, { "anonymous": false, "inputs": [ @@ -1351,6 +1419,41 @@ "stateMutability": "nonpayable", "type": "function" }, + { + "constant": false, + "inputs": [ + { + "internalType": "uint256", + "name": "validatorId", + "type": "uint256" + }, + { + "internalType": "address", + "name": "user", + "type": "address" + }, + { + "internalType": "uint256", + "name": "amount", + "type": "uint256" + }, + { + "internalType": "uint256", + "name": "tokens", + "type": "uint256" + }, + { + "internalType": "uint256", + "name": "nonce", + "type": "uint256" + } + ], + "name": "logShareBurnedWithId", + "outputs": [], + "payable": false, + "stateMutability": "nonpayable", + "type": "function" + } { "constant": false, "inputs": [ @@ -1426,6 +1529,36 @@ "stateMutability": "nonpayable", "type": "function" }, + { + "constant": false, + "inputs": [ + { + "internalType": "uint256", + "name": "validatorId", + "type": "uint256" + }, + { + "internalType": "address", + "name": "user", + "type": "address" + }, + { + "internalType": "uint256", + "name": "amount", + "type": "uint256" + }, + { + "internalType": "uint256", + "name": "nonce", + "type": "uint256" + } + ], + "name": "logDelegatorUnstakedWithId", + "outputs": [], + "payable": false, + "stateMutability": "nonpayable", + "type": "function" + }, { "constant": false, "inputs": [ diff --git a/root/src/mappings/staking-info.ts b/root/src/mappings/staking-info.ts index 53a0d6d..3b5973d 100644 --- a/root/src/mappings/staking-info.ts +++ b/root/src/mappings/staking-info.ts @@ -11,12 +11,14 @@ import { ClaimRewards, DelegatorClaimedRewards, DelegatorUnstaked, + DelegatorUnstakeWithId, DynastyValueChange, Jailed, OwnershipTransferred, ProposerBonusChange, Restaked, ShareBurned, + ShareBurnedWithId, ShareMinted, SignerChange, StakeUpdate, @@ -277,6 +279,30 @@ export function handleShareBurned(event: ShareBurned): void { delegator.unclaimedAmount = delegator.unclaimedAmount.plus(event.params.amount) // this works until tokens are not transaferable delegator.tokens = delegator.tokens.minus(event.params.tokens) + delegator.delegatedAmount = delegator.delegatedAmount.minus(event.params.amount) + + // save entity + delegator.save() + + // -- Also updating delegated stake for validator + let validator = loadValidator(event.params.validatorId) + + validator.delegatedStake = validator.delegatedStake.minus(event.params.amount) + + validator.save() + // -- Saving updation +} + +export function handleShareBurnedWithId(event: ShareBurnedWithId): void { + let delegator = loadDelegator(event.params.validatorId, event.params.user) + + // update claimedAmount and tokens + // it is possible to have: claimed amount < amount (when slashing happens) + // that's why having claimedAmount would be better + delegator.unclaimedAmount = delegator.unclaimedAmount.plus(event.params.amount) + // this works until tokens are not transaferable + delegator.tokens = delegator.tokens.minus(event.params.tokens) + delegator.delegatedAmount = delegator.delegatedAmount.minus(event.params.amount) // save entity delegator.save() @@ -298,9 +324,16 @@ export function handleDelegatorUnstaked(event: DelegatorUnstaked): void { // update claimed amount delegator.claimedAmount = delegator.claimedAmount.plus(event.params.amount) - // As delegator just unstaked, we can decrement their delegatedAmount i.e. - // `stake` as it's described in `staking-api` - delegator.delegatedAmount = delegator.delegatedAmount.minus(event.params.amount) + delegator.save() +} + +export function handleDelegatorUnstakeWithId(event: DelegatorUnstakeWithId): void { + let delegator = loadDelegator(event.params.validatorId, event.params.user) + + // update unclaimed amount by deducting total unclaimed amount + delegator.unclaimedAmount = delegator.unclaimedAmount.minus(event.params.amount) + // update claimed amount + delegator.claimedAmount = delegator.claimedAmount.plus(event.params.amount) delegator.save() } diff --git a/root/subgraph.template.yaml b/root/subgraph.template.yaml index 74b8cc5..e64bbd7 100644 --- a/root/subgraph.template.yaml +++ b/root/subgraph.template.yaml @@ -170,6 +170,10 @@ dataSources: # - event: RewardUpdate(uint256,uint256) - event: ShareBurned(indexed uint256,indexed address,indexed uint256,uint256) handler: handleShareBurned + - event: ShareBurnedWithId(indexed uint256,indexed address,indexed uint256,uint256,uint256) + handler: handleShareBurnedWithId + - event: DelegatorUnstakeWithId(indexed uint256,indexed address,uint256,uint256) + handler: handleDelegatorUnstakeWithId - event: ShareMinted(indexed uint256,indexed address,indexed uint256,uint256) handler: handleShareMinted - event: SignerChange(indexed uint256,uint256,indexed address,indexed address,bytes) From 9ef525991035f9f0a9a8c01f4c6ec49a008ba033 Mon Sep 17 00:00:00 2001 From: Vamsi Date: Mon, 22 Mar 2021 16:44:22 +0530 Subject: [PATCH 2/2] Missed comma --- root/abis/StakingInfo.json | 2 +- root/subgraph.template.yaml | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/root/abis/StakingInfo.json b/root/abis/StakingInfo.json index 1ba99fa..1cc939a 100644 --- a/root/abis/StakingInfo.json +++ b/root/abis/StakingInfo.json @@ -1453,7 +1453,7 @@ "payable": false, "stateMutability": "nonpayable", "type": "function" - } + }, { "constant": false, "inputs": [ diff --git a/root/subgraph.template.yaml b/root/subgraph.template.yaml index e64bbd7..ae7726c 100644 --- a/root/subgraph.template.yaml +++ b/root/subgraph.template.yaml @@ -157,6 +157,8 @@ dataSources: # handler: handleDelegatorRestaked - event: DelegatorUnstaked(indexed uint256,indexed address,uint256) handler: handleDelegatorUnstaked + - event: DelegatorUnstakeWithId(indexed uint256,indexed address,uint256,uint256) + handler: handleDelegatorUnstakeWithId - event: DynastyValueChange(uint256,uint256) handler: handleDynastyValueChange - event: Jailed(indexed uint256,indexed uint256,indexed address) @@ -172,8 +174,6 @@ dataSources: handler: handleShareBurned - event: ShareBurnedWithId(indexed uint256,indexed address,indexed uint256,uint256,uint256) handler: handleShareBurnedWithId - - event: DelegatorUnstakeWithId(indexed uint256,indexed address,uint256,uint256) - handler: handleDelegatorUnstakeWithId - event: ShareMinted(indexed uint256,indexed address,indexed uint256,uint256) handler: handleShareMinted - event: SignerChange(indexed uint256,uint256,indexed address,indexed address,bytes)