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 authored and rabi-siddique committed May 6, 2024
1 parent 41d43dd commit d3bdefd
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 24 deletions.
10 changes: 8 additions & 2 deletions schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
account: Account!
}

type Account @entity {
id: ID!
balances: [Balance] @derivedFrom(field: "account")
}
19 changes: 14 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,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<void> {
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,
Expand Down Expand Up @@ -127,7 +136,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
20 changes: 3 additions & 17 deletions src/mappings/mappingHandlers.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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";
Expand Down Expand Up @@ -240,7 +226,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 d3bdefd

Please sign in to comment.