Skip to content

Commit e7663fa

Browse files
committed
Add rawAmount field
1 parent 32e0b4b commit e7663fa

File tree

3 files changed

+31
-11
lines changed

3 files changed

+31
-11
lines changed

schema.graphql

+12-1
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,19 @@ type TokenBalance @entity {
6767
token: Token!
6868
"Account that holds the token"
6969
account: Account!
70-
"The amount of the token this account holds"
70+
71+
"""
72+
Amount: the amount of the token this account holds.
73+
This amount is what is available on chain.
74+
@deprecated(reason: "use rawAmount instead")
75+
"""
7176
amount: BigInt!
77+
78+
"""
79+
Raw amount: does not account for ignored contracts which can be buggy.
80+
Raw amount is what is available on chain.
81+
"""
82+
rawAmount: BigInt!
7283
}
7384

7485
"""

src/common/entity/balance.ts

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ export function getTokenBalance(token: Token, account: Account): TokenBalance {
99
tokenBalance.account = account.id
1010
tokenBalance.token = token.id
1111
tokenBalance.amount = ZERO_BI
12+
tokenBalance.rawAmount = ZERO_BI
1213
}
1314

1415
return tokenBalance

src/common/interaction.ts

+18-10
Original file line numberDiff line numberDiff line change
@@ -14,23 +14,23 @@ export function handleProductTransfer(event: TransferEvent): void {
1414
return
1515
}
1616

17-
if (shouldIgnoreContract(event.params.from) || shouldIgnoreContract(event.params.to)) {
18-
log.debug("Ignoring transfer from/to ignored contract: {}", [event.transaction.hash.toHexString()])
19-
return
20-
}
21-
2217
const tokenAddress = event.address
2318
fetchAndSaveTokenData(tokenAddress)
24-
2519
const statistic = getTokenStatistic(tokenAddress)
2620

2721
if (event.params.from.notEqual(SHARE_TOKEN_MINT_ADDRESS) && event.params.from.notEqual(BURN_ADDRESS)) {
28-
const balDiff = updateAccountBalance(tokenAddress, event.params.from, event.params.value.neg())
22+
const holder = event.params.from
23+
const rawAmountDiff = event.params.value.neg()
24+
const amountDiff = shouldIgnoreContract(holder) ? ZERO_BI : event.params.value.neg()
25+
const balDiff = updateAccountBalance(tokenAddress, holder, amountDiff, rawAmountDiff)
2926
statistic.holderCount = statistic.holderCount.plus(balDiff.holderCountChange())
3027
}
3128

3229
if (event.params.to.notEqual(SHARE_TOKEN_MINT_ADDRESS) && event.params.to.notEqual(BURN_ADDRESS)) {
33-
const balDiff = updateAccountBalance(tokenAddress, event.params.to, event.params.value)
30+
const receiver = event.params.to
31+
const rawAmountDiff = event.params.value
32+
const amountDiff = shouldIgnoreContract(receiver) ? ZERO_BI : event.params.value
33+
const balDiff = updateAccountBalance(tokenAddress, receiver, amountDiff, rawAmountDiff)
3434
statistic.holderCount = statistic.holderCount.plus(balDiff.holderCountChange())
3535
}
3636

@@ -44,22 +44,30 @@ export function handleProductTransfer(event: TransferEvent): void {
4444
statistic.save()
4545
}
4646

47-
function updateAccountBalance(tokenAddress: Bytes, accountAddress: Bytes, amountDiff: BigInt): BalanceDiff {
47+
function updateAccountBalance(tokenAddress: Bytes, accountAddress: Bytes, amountDiff: BigInt, rawAmountDiff: BigInt): BalanceDiff {
4848
const account = createAccount(accountAddress)
4949
const token = getToken(tokenAddress)
5050
const balance = getTokenBalance(token, account)
51+
5152
const before = balance.amount
5253
const after = balance.amount.plus(amountDiff)
5354
balance.amount = after
55+
56+
const rawBefore = balance.rawAmount
57+
const rawAfter = balance.rawAmount.plus(rawAmountDiff)
58+
balance.rawAmount = rawAfter
59+
5460
balance.save()
5561

56-
return new BalanceDiff(before, balance.amount)
62+
return new BalanceDiff(before, after, rawBefore, rawAfter)
5763
}
5864

5965
class BalanceDiff {
6066
constructor(
6167
public before: BigInt,
6268
public after: BigInt,
69+
public rawBefore: BigInt,
70+
public rawAfter: BigInt,
6371
) {}
6472

6573
public holderCountChange(): BigInt {

0 commit comments

Comments
 (0)