Skip to content

Commit

Permalink
Add Account and create relationship with Balance
Browse files Browse the repository at this point in the history
  • Loading branch information
toliaqat committed May 5, 2024
1 parent 98ef89c commit 13b19f2
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 10 deletions.
10 changes: 8 additions & 2 deletions schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -264,9 +264,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
}
account: Account!
}

type Account @entity {
id: ID!
balances: [Balance] @derivedFrom(field: "account")
}
11 changes: 6 additions & 5 deletions src/mappings/events/balances.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -72,7 +72,7 @@ export const balancesEventKit = () => {
address: string,
denom: string
): Promise<boolean> {
const balance = await Balances.getByFields([
const balance = await Balance.getByFields([
['address', '=', address],
['denom', '=', denom],
]);
Expand All @@ -88,13 +88,14 @@ 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 new Account(address).save();

logger.info(`Created new entry for address: ${address}`);
}

Expand Down Expand Up @@ -127,7 +128,7 @@ export const balancesEventKit = () => {
amount: bigint,
operation: Operation
): Promise<void> {
const balances = await Balances.getByFields([
const balances = await Balance.getByFields([
['address', '=', address],
['denom', '=', denom],
]);
Expand Down
6 changes: 3 additions & 3 deletions src/mappings/mappingHandlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import {
VaultManagerMetricsDaily,
PsmMetricDaily,
ReserveAllocationMetricsDaily,
Balances,
Balance,
} from "../types";
import { CosmosBlock, CosmosEvent } from "@subql/types-cosmos";
import {
Expand All @@ -38,7 +38,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";
Expand Down Expand Up @@ -245,7 +245,7 @@ export async function initiateBalancesTable(block: CosmosBlock): Promise<void> {
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;
Expand Down

0 comments on commit 13b19f2

Please sign in to comment.