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
rabi-siddique committed May 6, 2024
1 parent 22c1235 commit 9656875
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 23 deletions.
8 changes: 7 additions & 1 deletion schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -269,9 +269,15 @@ type IBCTransfer @entity {
transferType: TransferType!
}

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
22 changes: 5 additions & 17 deletions src/mappings/mappingHandlers.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,9 @@
import {
StateChangeEvent,
IBCChannel,
Balance,
IBCTransfer,
TransferType,
OraclePrice,
OraclePriceDaily,
PsmGovernance,
Wallet,
Vault,
VaultManagerMetrics,
VaultManagerGovernance,
ReserveMetrics,
ReserveAllocationMetrics,
BoardAux,
VaultManagerMetricsDaily,
ReserveAllocationMetricsDaily,
Balances,
IBCChannel,
TransferType
} from "../types";
import { CosmosBlock, CosmosEvent } from "@subql/types-cosmos";
import {
Expand All @@ -41,7 +29,7 @@ import {
PACKET_DST_PORT_KEY,
PACKET_SRC_PORT_KEY,
TRANSFER_PORT_VALUE,
BALANCE_FIELDS
BALANCE_FIELDS,
} from "./constants";
import { psmEventKit } from "./events/psm";
import { boardAuxEventKit } from "./events/boardAux";
Expand Down Expand Up @@ -340,7 +328,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 9656875

Please sign in to comment.