Skip to content

Commit

Permalink
Add rawAmount field
Browse files Browse the repository at this point in the history
  • Loading branch information
prevostc committed Dec 19, 2024
1 parent 32e0b4b commit e7663fa
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 11 deletions.
13 changes: 12 additions & 1 deletion schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,19 @@ type TokenBalance @entity {
token: Token!
"Account that holds the token"
account: Account!
"The amount of the token this account holds"

"""
Amount: the amount of the token this account holds.
This amount is what is available on chain.
@deprecated(reason: "use rawAmount instead")
"""
amount: BigInt!

"""
Raw amount: does not account for ignored contracts which can be buggy.
Raw amount is what is available on chain.
"""
rawAmount: BigInt!
}

"""
Expand Down
1 change: 1 addition & 0 deletions src/common/entity/balance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export function getTokenBalance(token: Token, account: Account): TokenBalance {
tokenBalance.account = account.id
tokenBalance.token = token.id
tokenBalance.amount = ZERO_BI
tokenBalance.rawAmount = ZERO_BI
}

return tokenBalance
Expand Down
28 changes: 18 additions & 10 deletions src/common/interaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,23 @@ export function handleProductTransfer(event: TransferEvent): void {
return
}

if (shouldIgnoreContract(event.params.from) || shouldIgnoreContract(event.params.to)) {
log.debug("Ignoring transfer from/to ignored contract: {}", [event.transaction.hash.toHexString()])
return
}

const tokenAddress = event.address
fetchAndSaveTokenData(tokenAddress)

const statistic = getTokenStatistic(tokenAddress)

if (event.params.from.notEqual(SHARE_TOKEN_MINT_ADDRESS) && event.params.from.notEqual(BURN_ADDRESS)) {
const balDiff = updateAccountBalance(tokenAddress, event.params.from, event.params.value.neg())
const holder = event.params.from
const rawAmountDiff = event.params.value.neg()
const amountDiff = shouldIgnoreContract(holder) ? ZERO_BI : event.params.value.neg()
const balDiff = updateAccountBalance(tokenAddress, holder, amountDiff, rawAmountDiff)
statistic.holderCount = statistic.holderCount.plus(balDiff.holderCountChange())
}

if (event.params.to.notEqual(SHARE_TOKEN_MINT_ADDRESS) && event.params.to.notEqual(BURN_ADDRESS)) {
const balDiff = updateAccountBalance(tokenAddress, event.params.to, event.params.value)
const receiver = event.params.to
const rawAmountDiff = event.params.value
const amountDiff = shouldIgnoreContract(receiver) ? ZERO_BI : event.params.value
const balDiff = updateAccountBalance(tokenAddress, receiver, amountDiff, rawAmountDiff)
statistic.holderCount = statistic.holderCount.plus(balDiff.holderCountChange())
}

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

function updateAccountBalance(tokenAddress: Bytes, accountAddress: Bytes, amountDiff: BigInt): BalanceDiff {
function updateAccountBalance(tokenAddress: Bytes, accountAddress: Bytes, amountDiff: BigInt, rawAmountDiff: BigInt): BalanceDiff {
const account = createAccount(accountAddress)
const token = getToken(tokenAddress)
const balance = getTokenBalance(token, account)

const before = balance.amount
const after = balance.amount.plus(amountDiff)
balance.amount = after

const rawBefore = balance.rawAmount
const rawAfter = balance.rawAmount.plus(rawAmountDiff)
balance.rawAmount = rawAfter

balance.save()

return new BalanceDiff(before, balance.amount)
return new BalanceDiff(before, after, rawBefore, rawAfter)
}

class BalanceDiff {
constructor(
public before: BigInt,
public after: BigInt,
public rawBefore: BigInt,
public rawAfter: BigInt,
) {}

public holderCountChange(): BigInt {
Expand Down

0 comments on commit e7663fa

Please sign in to comment.