diff --git a/schema.graphql b/schema.graphql index d007be7a..a867fc94 100644 --- a/schema.graphql +++ b/schema.graphql @@ -242,9 +242,15 @@ type ReserveMetrics @entity { allocations: [ReserveAllocationMetrics] @derivedFrom(field: "reserveMetrics") } -type Balances @entity { +type Balance @entity { id: ID! address: String @index balance: BigInt denom: String @index -} \ No newline at end of file + account: Account! +} + +type Account @entity { + id: ID! + balances: [Balance] @derivedFrom(field: "account") +} diff --git a/src/mappings/events/balances.ts b/src/mappings/events/balances.ts index 9e574673..63ee67de 100644 --- a/src/mappings/events/balances.ts +++ b/src/mappings/events/balances.ts @@ -1,4 +1,4 @@ -import { Balances } from '../../types'; +import { Account, Balance } from '../../types'; import { BALANCE_FIELDS } from '../constants'; import { b64decode } from '../utils'; import { CosmosEvent } from '@subql/types-cosmos'; @@ -72,7 +72,7 @@ export const balancesEventKit = () => { address: string, denom: string ): Promise { - const balance = await Balances.getByFields([ + const balance = await Balance.getByFields([ ['address', '=', address], ['denom', '=', denom], ]); @@ -88,16 +88,25 @@ export const balancesEventKit = () => { denom: string, primaryKey: string ) { - const newBalance = new Balances(primaryKey); + const newBalance = new Balance(primaryKey, address); newBalance.address = address; newBalance.balance = BigInt(0); newBalance.denom = denom; - await newBalance.save(); + await createAccountIfNotExists(address); + logger.info(`Created new entry for address: ${address}`); } + async function createAccountIfNotExists(address: string): Promise { + const account = await Account.get(address); + if (!account) { + const newAccount = new Account(address); + await newAccount.save(); + } + } + function validateTransaction(amount: string | null): TransactionData { const result: TransactionData = { isValidTransaction: false, @@ -127,7 +136,7 @@ export const balancesEventKit = () => { amount: bigint, operation: Operation ): Promise { - const balances = await Balances.getByFields([ + const balances = await Balance.getByFields([ ['address', '=', address], ['denom', '=', denom], ]); diff --git a/src/mappings/mappingHandlers.ts b/src/mappings/mappingHandlers.ts index 46b5471f..7e8a5ed0 100644 --- a/src/mappings/mappingHandlers.ts +++ b/src/mappings/mappingHandlers.ts @@ -1,20 +1,6 @@ import { StateChangeEvent, - OraclePrice, - OraclePriceDaily, - PsmMetric, - PsmGovernance, - Wallet, - Vault, - VaultManagerMetrics, - VaultManagerGovernance, - ReserveMetrics, - ReserveAllocationMetrics, - BoardAux, - VaultManagerMetricsDaily, - PsmMetricDaily, - ReserveAllocationMetricsDaily, - Balances, + Balance, } from "../types"; import { CosmosBlock, CosmosEvent } from "@subql/types-cosmos"; import { @@ -33,7 +19,7 @@ import { STORE_NAME_KEY, SUBKEY_KEY, UNPROVED_VALUE_KEY, - BALANCE_FIELDS + BALANCE_FIELDS, } from "./constants"; import { psmEventKit } from "./events/psm"; import { boardAuxEventKit } from "./events/boardAux"; @@ -240,7 +226,7 @@ export async function initiateBalancesTable(block: CosmosBlock): Promise { for (let element of data.balances) { let newBalance; for (const coin of element.coins) { - newBalance = new Balances(`${element.address}-${coin.denom}`); + newBalance = new Balance(`${element.address}-${coin.denom}`, element.address); newBalance.address = element.address; newBalance.balance = BigInt(coin.amount); newBalance.denom = coin.denom;