diff --git a/schema.graphql b/schema.graphql index b0e58c5..da2f70d 100644 --- a/schema.graphql +++ b/schema.graphql @@ -22,7 +22,7 @@ type Block @entity { transactions: [Transaction] @derivedFrom(field: "block") messages: [Message] @derivedFrom(field: "block") events: [Event] @derivedFrom(field: "block") - balancesOfAccountByDenom: [BalanceOfAccountByDenom] @derivedFrom(field: "lastUpdatedBlock") + balancesOfAccountByDenom: [Balance] @derivedFrom(field: "lastUpdatedBlock") } type Transaction @entity { @@ -35,13 +35,14 @@ type Transaction @entity { status: TxStatus! log: String timeoutHeight: BigInt @index + # NB: only the first signer! signerAddress: String @index messages: [Message] @derivedFrom(field: "transaction") } type Message @entity { id: ID! - typeUrl: String @index + typeUrl: String! @index json: String transaction: Transaction! block: Block! @@ -77,7 +78,7 @@ type Account @entity { chainId: String! @index nativeBalanceChanges: [NativeBalanceChange]! @derivedFrom(field: "account") genesisBalances: [GenesisBalance] @derivedFrom(field: "account") - balances: [BalanceOfAccountByDenom] @derivedFrom(field: "account") + balances: [Balance] @derivedFrom(field: "account") } type GenesisBalance @entity { @@ -106,7 +107,7 @@ type UnprocessedEntity @entity { block: Block } -type BalanceOfAccountByDenom @entity { +type Balance @entity { id: ID! account: Account! @index denom: String! @index diff --git a/src/mappings/primitives.ts b/src/mappings/primitives.ts index d095b38..926da8f 100644 --- a/src/mappings/primitives.ts +++ b/src/mappings/primitives.ts @@ -20,7 +20,7 @@ import { TxStatus, NativeBalanceChange, GenesisBalance, - BalanceOfAccountByDenom, + Balance, GenesisFile as GenesisEntity, } from "../types"; import { PREFIX } from "./constants"; @@ -33,7 +33,7 @@ import { stringify, trackUnprocessed, unprocessedEventHandler, - getBalanceOfAccountByDenomId, + getBalanceId, } from "./utils"; export async function handleGenesis(block: CosmosBlock): Promise { @@ -63,9 +63,9 @@ export async function handleGenesis(block: CosmosBlock): Promise { ) type EntityToSave = Omit; - const nativeBalances: Array> = []; + const nativeBalanceChanges: Array> = []; const genesisBalances: Array> = []; - const balancesByDenom: Array> = []; + const balances: Array> = []; type AmountByAccountAndDenom = Record { }> // here we are grouping the amount of each denom for each account - const amountByAccountAndDenom: AmountByAccountAndDenom = genesis.app_state.bank.balances.reduce((acc, item) => { - const amountByDenom: Record = item.coins.reduce((acc, item) => ({ + const amountByAccountAndDenom: AmountByAccountAndDenom = genesis.app_state.bank.balances.reduce((acc, balance) => { + const amountByDenom: Record = balance.coins.reduce((acc, coin) => ({ ...acc, - [item.denom]: BigInt(acc[item.denom] || 0) + BigInt(item.amount), + [coin.denom]: BigInt(acc[coin.denom] || 0) + BigInt(coin.amount), }), {} as Record) for (const [denom, amount] of Object.entries(amountByDenom)) { - const id = getBalanceOfAccountByDenomId(item.address, denom) + const id = getBalanceId(balance.address, denom) if (acc[id]) { acc[id].amount += amount } else { acc[id] = { amount, denom, - accountId: item.address, + accountId: balance.address, } } } @@ -97,7 +97,7 @@ export async function handleGenesis(block: CosmosBlock): Promise { }, {} as AmountByAccountAndDenom) for (const [id, {accountId, amount, denom}] of Object.entries(amountByAccountAndDenom)) { - nativeBalances.push({ + nativeBalanceChanges.push({ id, balanceOffset: amount.valueOf(), denom, @@ -113,7 +113,7 @@ export async function handleGenesis(block: CosmosBlock): Promise { accountId: accountId, }); - balancesByDenom.push({ + balances.push({ id, amount: amount, denom, @@ -124,8 +124,8 @@ export async function handleGenesis(block: CosmosBlock): Promise { await Promise.all([ store.bulkCreate('GenesisBalance', genesisBalances), - store.bulkCreate('NativeBalanceChange', nativeBalances), - store.bulkCreate('BalanceOfAccountByDenom', balancesByDenom) + store.bulkCreate('NativeBalanceChange', nativeBalanceChanges), + store.bulkCreate('Balance', balances) ]); await GenesisEntity.create({ diff --git a/src/mappings/utils.ts b/src/mappings/utils.ts index 07392a7..b2f7ac3 100644 --- a/src/mappings/utils.ts +++ b/src/mappings/utils.ts @@ -8,7 +8,7 @@ import { import { default as JSONBig } from "json-bigint"; import { Account, - BalanceOfAccountByDenom, + Balance, UnprocessedEntity, } from "../types"; @@ -130,27 +130,27 @@ export async function trackUnprocessed(error: Error, primitives: Primitives): Pr } } -export function getBalanceOfAccountByDenomId(address: string, denom: string): string { +export function getBalanceId(address: string, denom: string): string { return `${address}-${denom}`; } export async function updateAccountBalance(address: string, denom: string, offset: bigint, blockId: string): Promise { - let balanceOfAccountByDenom = await BalanceOfAccountByDenom.get(getBalanceOfAccountByDenomId(address, denom)); + let balance = await Balance.get(getBalanceId(address, denom)); - if (!balanceOfAccountByDenom) { - balanceOfAccountByDenom = BalanceOfAccountByDenom.create({ - id: getBalanceOfAccountByDenomId(address, denom), + if (!balance) { + balance = Balance.create({ + id: getBalanceId(address, denom), accountId: address, denom, amount: offset, lastUpdatedBlockId: blockId, }) } else { - balanceOfAccountByDenom.amount = balanceOfAccountByDenom.amount + offset; - balanceOfAccountByDenom.lastUpdatedBlockId = blockId; + balance.amount = balance.amount + offset; + balance.lastUpdatedBlockId = blockId; } - await balanceOfAccountByDenom.save(); + await balance.save(); - logger.debug(`[updateAccountBalance] (address): ${address}, (denom): ${denom}, (offset): ${offset}, (newBalance): ${balanceOfAccountByDenom?.amount}`); + logger.debug(`[updateAccountBalance] (address): ${address}, (denom): ${denom}, (offset): ${offset}, (newBalance): ${balance?.amount}`); }