Skip to content

Commit

Permalink
Delegator unbond events with Id handled. Delegated Amount deduction m…
Browse files Browse the repository at this point in the history
…oved to shareburn event and removed from unstake event
  • Loading branch information
reddyismav committed Mar 22, 2021
1 parent ab11033 commit 4eed023
Show file tree
Hide file tree
Showing 3 changed files with 173 additions and 3 deletions.
133 changes: 133 additions & 0 deletions root/abis/StakingInfo.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": [
Expand Down Expand Up @@ -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": [
Expand Down Expand Up @@ -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": [
Expand Down Expand Up @@ -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": [
Expand Down
39 changes: 36 additions & 3 deletions root/src/mappings/staking-info.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@ import {
ClaimRewards,
DelegatorClaimedRewards,
DelegatorUnstaked,
DelegatorUnstakeWithId,
DynastyValueChange,
Jailed,
OwnershipTransferred,
ProposerBonusChange,
Restaked,
ShareBurned,
ShareBurnedWithId,
ShareMinted,
SignerChange,
StakeUpdate,
Expand Down Expand Up @@ -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()
Expand All @@ -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()
}
Expand Down
4 changes: 4 additions & 0 deletions root/subgraph.template.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 4eed023

Please sign in to comment.