Skip to content

Commit d444dc9

Browse files
committed
feat: Balance update improved for batchs & transfers
1 parent c79d694 commit d444dc9

File tree

8 files changed

+75
-11
lines changed

8 files changed

+75
-11
lines changed

project.yaml

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,24 @@ dataSources:
2121
mapping:
2222
file: "./dist/index.js"
2323
handlers:
24+
# Batch Calls
25+
- handler: batchCallHandler
26+
kind: substrate/CallHandler
27+
filter:
28+
module: utility
29+
method: batchAll
30+
success: true
31+
- handler: batchCallHandler
32+
kind: substrate/CallHandler
33+
filter:
34+
module: utility
35+
method: batch
36+
- handler: batchCallHandler
37+
kind: substrate/CallHandler
38+
filter:
39+
module: utility
40+
method: forceBatch
41+
2442
# Balance
2543
- handler: handleEvent
2644
kind: substrate/EventHandler
@@ -52,11 +70,6 @@ dataSources:
5270
filter:
5371
module: balances
5472
method: Slashed
55-
- handler: handleEvent
56-
kind: substrate/EventHandler
57-
filter:
58-
module: balances
59-
method: BalanceSet
6073
- handler: handleEvent
6174
kind: substrate/EventHandler
6275
filter:

src/callHandlers/batch.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { SubstrateExtrinsic } from "@subql/types"
2+
import { updateAccount } from "../helpers"
3+
4+
const balanceMethods = ["BalanceSet", "Deposit", "DustLost", "Endowed", "Reserved", "Slashed", "Unreserved", "Withdraw"]
5+
6+
export async function batchCallHandler(extrinsic: SubstrateExtrinsic): Promise<void> {
7+
if (!extrinsic.success) {
8+
return
9+
}
10+
11+
const signer = extrinsic.extrinsic.signer.toString()
12+
const events = extrinsic.events
13+
14+
const addressStack: string[] = []
15+
16+
for (const event of events) {
17+
const { data, method, section } = event.event
18+
if (section === "balances" && balanceMethods.includes(method)) {
19+
const [who] = data
20+
if (!addressStack.includes(who.toString()) && who.toString() !== signer) {
21+
addressStack.push(who.toString())
22+
}
23+
}
24+
}
25+
26+
await Promise.all(addressStack.map(async (address) => await updateAccount(address)))
27+
}

src/callHandlers/index.ts

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

src/eventHandlers/balances.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
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 } from "../helpers"
4+
import { updateAccount, 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)
9+
const signer = getSigner(event)
910
const [from, to, amount] = event.event.data
1011
await genericTransferHandler(from, to, amount, commonEventData)
11-
await updateAccount(from.toString())
12+
if (from.toString() !== signer) await updateAccount(from.toString())
1213
await updateAccount(to.toString())
1314
}
1415

src/helpers/account.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1+
import type { AccountInfo } from "@polkadot/types/interfaces"
2+
13
import { AccountEntity } from "../types/models/AccountEntity"
24
import { roundPrice } from "../helpers"
35

46
export const updateAccount = async (user: string) => {
57
try {
6-
const res: any = await api.query.system.account(user)
8+
const res = await api.query.system.account<AccountInfo>(user)
79
const balance = res.data
810
if (balance) {
911
const { feeFrozen, free, miscFrozen, reserved } = balance

src/helpers/event.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,18 @@ export const getCommonEventData = (event: SubstrateEvent): CommonEventData => {
3737
}
3838
}
3939

40+
export const checkIfTransfer = (extrinsic: SubstrateExtrinsic): boolean => {
41+
const { section, method } = extrinsic.extrinsic.method
42+
if (section === "balances" && ["transfer", "transferAll", "transferKeepAlive"].includes(method)) return true
43+
return false
44+
}
45+
46+
export const checkIfAnyBatch = (extrinsic: SubstrateExtrinsic): boolean => {
47+
const { section, method } = extrinsic.extrinsic.method
48+
if (section === "utility" && ["batch", "batchAll", "forceBatch"].includes(method)) return true
49+
return false
50+
}
51+
4052
const checkIfBatch = (extrinsic: SubstrateExtrinsic): boolean => {
4153
const { section, method } = extrinsic.extrinsic.method
4254
if (section === "utility" && method === "batch") return true

src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
//Exports all handler functions
2+
export * from "./callHandlers"
23
export * from "./mappings/mappingHandlers"

src/mappings/mappingHandlers.ts

Lines changed: 10 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 { updateAccount } from "../helpers"
3+
import { checkIfAnyBatch, checkIfTransfer, updateAccount } from "../helpers"
44

55
export async function handleEvent(event: SubstrateEvent): Promise<void> {
66
const key = `${event.event.section}.${event.event.method}`
@@ -15,9 +15,16 @@ export async function handleEvent(event: SubstrateEvent): Promise<void> {
1515
case "balances.Slashed":
1616
case "balances.Unreserved":
1717
case "balances.Withdraw":
18+
const isBatchCall = event.extrinsic && checkIfAnyBatch(event.extrinsic)
19+
const isTransferCall = event.extrinsic && checkIfTransfer(event.extrinsic)
20+
if (!isBatchCall && !isTransferCall) {
21+
const [who] = event.event.data
22+
await updateAccount(who.toString())
23+
}
24+
break
1825
case "transactionPayment.TransactionFeePaid":
19-
const [who] = event.event.data
20-
await updateAccount(who.toString())
26+
const [whoPaid] = event.event.data
27+
await updateAccount(whoPaid.toString())
2128
break
2229
case "balances.Transfer":
2330
await eventHandlers.transferHandler(event)

0 commit comments

Comments
 (0)