Skip to content

Commit 549a8ee

Browse files
committed
feat: Balance update improved for staking rewards
1 parent d444dc9 commit 549a8ee

File tree

6 files changed

+54
-43
lines changed

6 files changed

+54
-43
lines changed

project.yaml

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,23 +22,31 @@ dataSources:
2222
file: "./dist/index.js"
2323
handlers:
2424
# Batch Calls
25-
- handler: batchCallHandler
25+
- handler: accountUpdateHandler
2626
kind: substrate/CallHandler
2727
filter:
2828
module: utility
2929
method: batchAll
3030
success: true
31-
- handler: batchCallHandler
31+
- handler: accountUpdateHandler
3232
kind: substrate/CallHandler
3333
filter:
3434
module: utility
3535
method: batch
36-
- handler: batchCallHandler
36+
- handler: accountUpdateHandler
3737
kind: substrate/CallHandler
3838
filter:
3939
module: utility
4040
method: forceBatch
4141

42+
# Staking Calls
43+
- handler: accountUpdateHandler
44+
kind: substrate/CallHandler
45+
filter:
46+
module: staking
47+
method: payoutStakers
48+
success: true
49+
4250
# Balance
4351
- handler: handleEvent
4452
kind: substrate/EventHandler

src/callHandlers/batch.ts renamed to src/callHandlers/account.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import { SubstrateExtrinsic } from "@subql/types"
2-
import { updateAccount } from "../helpers"
2+
import { updateAccounts } from "../helpers"
33

44
const balanceMethods = ["BalanceSet", "Deposit", "DustLost", "Endowed", "Reserved", "Slashed", "Unreserved", "Withdraw"]
55

6-
export async function batchCallHandler(extrinsic: SubstrateExtrinsic): Promise<void> {
6+
export async function accountUpdateHandler(extrinsic: SubstrateExtrinsic): Promise<void> {
77
if (!extrinsic.success) {
88
return
99
}
@@ -23,5 +23,5 @@ export async function batchCallHandler(extrinsic: SubstrateExtrinsic): Promise<v
2323
}
2424
}
2525

26-
await Promise.all(addressStack.map(async (address) => await updateAccount(address)))
26+
await updateAccounts(addressStack)
2727
}

src/callHandlers/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
export * from "./batch"
1+
export * from "./account"

src/eventHandlers/balances.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
import { SubstrateEvent } from "@subql/types"
22
import { Codec } from "@polkadot/types/types"
33
import { Balance } from "@polkadot/types/interfaces"
4-
import { updateAccount, getCommonEventData, roundPrice, CommonEventData, getSigner } from "../helpers"
4+
import { updateAccounts, getCommonEventData, roundPrice, CommonEventData, getSigner } from "../helpers"
55
import { TransferEntity } from "../types"
66

77
export const transferHandler = async (event: SubstrateEvent): Promise<void> => {
88
const commonEventData = getCommonEventData(event)
99
const signer = getSigner(event)
1010
const [from, to, amount] = event.event.data
1111
await genericTransferHandler(from, to, amount, commonEventData)
12-
if (from.toString() !== signer) await updateAccount(from.toString())
13-
await updateAccount(to.toString())
12+
if (from.toString() !== signer) await updateAccounts([from.toString()])
13+
await updateAccounts([to.toString()])
1414
}
1515

1616
export const genericTransferHandler = async (

src/helpers/account.ts

Lines changed: 33 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,41 @@
11
import type { AccountInfo } from "@polkadot/types/interfaces"
22

3-
import { AccountEntity } from "../types/models/AccountEntity"
43
import { roundPrice } from "../helpers"
4+
import { AccountEntity } from "../types"
55

6-
export const updateAccount = async (user: string) => {
6+
export const updateAccounts = async (addresses: string[]) => {
77
try {
8-
const res = await api.query.system.account<AccountInfo>(user)
9-
const balance = res.data
10-
if (balance) {
11-
const { feeFrozen, free, miscFrozen, reserved } = balance
12-
const date = new Date()
13-
let record = await AccountEntity.get(user)
14-
if (record === undefined) {
15-
record = new AccountEntity(user)
16-
record.createdAt = date
17-
}
18-
const balanceFrozenFee = feeFrozen.toBigInt()
19-
const balanceFrozenMisc = miscFrozen.toBigInt()
20-
const balanceReserved = reserved.toBigInt()
21-
const balanceFree = free.toBigInt()
22-
const frozen = balanceFrozenFee > balanceFrozenMisc ? balanceFrozenMisc : balanceFrozenMisc
23-
const total = balanceFree + balanceReserved
24-
const transferable = balanceFree - frozen
25-
record.capsAmount = transferable.toString()
26-
record.capsAmountFrozen = frozen.toString()
27-
record.capsAmountTotal = total.toString()
28-
record.capsAmountRounded = roundPrice(record.capsAmount)
29-
record.capsAmountFrozenRounded = roundPrice(record.capsAmountFrozen)
30-
record.capsAmountTotalRounded = roundPrice(record.capsAmountTotal)
31-
record.updatedAt = date
32-
await record.save()
33-
} else {
34-
logger.error("Error in update accout : Balance not found")
35-
}
8+
const res = await api.query.system.account.multi<AccountInfo>(addresses)
9+
await Promise.all(
10+
res.map(async ({ data: balance }, idx) => {
11+
if (balance) {
12+
const { free, miscFrozen, reserved } = balance
13+
const address = addresses[idx]
14+
const date = new Date()
15+
const balanceFrozenMisc = miscFrozen.toBigInt()
16+
const balanceReserved = reserved.toBigInt()
17+
const balanceFree = free.toBigInt()
18+
const capsAmountFrozen = balanceFrozenMisc.toString()
19+
const capsAmountTotal = (balanceFree + balanceReserved).toString()
20+
const capsAmount = (balanceFree - balanceFrozenMisc).toString()
21+
let record = await AccountEntity.get(address)
22+
if (record === undefined) {
23+
record = new AccountEntity(address)
24+
record.createdAt = date
25+
}
26+
record.capsAmount = capsAmount
27+
record.capsAmountFrozen = capsAmountFrozen
28+
record.capsAmountTotal = capsAmountTotal
29+
record.capsAmountRounded = roundPrice(record.capsAmount)
30+
record.capsAmountFrozenRounded = roundPrice(record.capsAmountFrozen)
31+
record.capsAmountTotalRounded = roundPrice(record.capsAmountTotal)
32+
record.updatedAt = date
33+
await record.save()
34+
} else {
35+
logger.error("Error in update accout : Balance not found")
36+
}
37+
}),
38+
)
3639
} catch (err) {
3740
logger.error("Error in update accout : " + err.toString())
3841
if (err.sql) logger.error("Error in update accout : " + JSON.stringify(err.sql))

src/mappings/mappingHandlers.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { SubstrateEvent } from "@subql/types"
22
import * as eventHandlers from "../eventHandlers"
3-
import { checkIfAnyBatch, checkIfTransfer, updateAccount } from "../helpers"
3+
import { checkIfAnyBatch, checkIfTransfer, updateAccounts } from "../helpers"
44

55
export async function handleEvent(event: SubstrateEvent): Promise<void> {
66
const key = `${event.event.section}.${event.event.method}`
@@ -19,12 +19,12 @@ export async function handleEvent(event: SubstrateEvent): Promise<void> {
1919
const isTransferCall = event.extrinsic && checkIfTransfer(event.extrinsic)
2020
if (!isBatchCall && !isTransferCall) {
2121
const [who] = event.event.data
22-
await updateAccount(who.toString())
22+
await updateAccounts([who.toString()])
2323
}
2424
break
2525
case "transactionPayment.TransactionFeePaid":
2626
const [whoPaid] = event.event.data
27-
await updateAccount(whoPaid.toString())
27+
await updateAccounts([whoPaid.toString()])
2828
break
2929
case "balances.Transfer":
3030
await eventHandlers.transferHandler(event)

0 commit comments

Comments
 (0)