Skip to content

Commit

Permalink
feat: fetching account balance for initial accounts
Browse files Browse the repository at this point in the history
  • Loading branch information
rabi-siddique committed May 29, 2024
1 parent 75f3708 commit 6d11f3a
Show file tree
Hide file tree
Showing 9 changed files with 169 additions and 227 deletions.
13 changes: 0 additions & 13 deletions genesis-local.json

This file was deleted.

130 changes: 0 additions & 130 deletions genesis-main.json

This file was deleted.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"pub": "subql publish",
"codegen": "subql codegen",
"start:docker": "docker-compose pull && docker-compose up --remove-orphans",
"dev": "network=local && subql codegen && subql build && docker-compose pull && docker-compose up --remove-orphans",
"dev": "subql codegen && subql build && docker-compose pull && docker-compose up --remove-orphans",
"prepack": "rm -rf dist && npm run build",
"test": "jest",
"subql": "subql codegen --help",
Expand Down Expand Up @@ -45,6 +45,7 @@
},
"dependencies": {
"@subql/types-cosmos": "^3.2.3",
"cross-fetch": "^4.0.0",
"@types/node": "^17.0.21",
"bech32": "^2.0.0",
"js-sha256": "^0.11.0",
Expand Down
21 changes: 9 additions & 12 deletions schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,10 @@ type ReserveMetrics @entity {
allocations: [ReserveAllocationMetrics] @derivedFrom(field: "reserveMetrics")
}

type Account @entity {
id: ID!
}

type IBCChannel @entity {
id: ID!
channelName: String!
Expand All @@ -289,7 +293,6 @@ type IBCTransfer @entity {
transferType: TransferType!
}

<<<<<<< HEAD
type VaultStatesDaily @entity {
id: ID!
blockHeightLast: BigInt!
Expand All @@ -299,17 +302,11 @@ type VaultStatesDaily @entity {
liquidating: BigInt!
liquidated: BigInt!
liquidatedClosed: BigInt!
=======
type Balance @entity {
id: ID!
address: String @index
balance: BigInt
denom: String @index
account: Account!
}

type Account @entity {
type Balances @entity {
id: ID!
balances: [Balance] @derivedFrom(field: "account")
>>>>>>> 31796b18 (feat: index transfer events)
}
address: String @index
balance: BigInt
denom: String @index
}
48 changes: 26 additions & 22 deletions src/mappings/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,27 @@ export const VAULT_STATES = {
LIQUIDATED_CLOSED: 'liquidatedClosed',
};

export const VALUE_KEY = b64encode('value');
export const STORE_KEY = b64encode('store');
export const VSTORAGE_VALUE = b64encode('vstorage');
export const KEY_KEY = b64encode('key');
export const STORE_NAME_KEY = b64encode('store_name');
export const SUBKEY_KEY = b64encode('store_subkey');
export const UNPROVED_VALUE_KEY = b64encode('unproved_value');
export const PACKET_DATA_KEY = 'packet_data';
export const PACKET_SRC_CHANNEL_KEY = 'packet_src_channel';
export const PACKET_DST_CHANNEL_KEY = 'packet_dst_channel';
export const PACKET_SRC_PORT_KEY = 'packet_src_port';
export const PACKET_DST_PORT_KEY = 'packet_dst_port';
export const ACTION_KEY = b64encode('action');
export const IBC_MESSAGE_TRANSFER_VALUE = b64encode('/ibc.applications.transfer.v1.MsgTransfer');
export const IBC_MESSAGE_RECEIVE_PACKET_VALUE = b64encode('/ibc.core.channel.v1.MsgRecvPacket');
export const RECEPIENT_KEY = b64encode('recipient');
export const SENDER_KEY = b64encode('sender');
export const RECEIVER_KEY = b64encode('receiver');
export const AMOUNT_KEY = b64encode('amount');
export const TRANSFER_PORT_VALUE = 'transfer';

export const BALANCE_FIELDS = {
amount: 'amount',
// Bank Events
Expand All @@ -39,33 +60,16 @@ export const BALANCE_FIELDS = {
coin_spent: 'spender',
transfer_recipient: 'recipient',
transfer_sender: 'sender',
burn: "burner",
burn: 'burner',
// Distribution Events
rewards: 'validator',
commission: 'validator',
proposer_reward: 'validator',
withdraw_rewards: 'validator',
withdraw_commission: 'validator',

};

export const VALUE_KEY = b64encode("value");
export const STORE_KEY = b64encode("store");
export const VSTORAGE_VALUE = b64encode("vstorage");
export const KEY_KEY = b64encode("key");
export const STORE_NAME_KEY = b64encode("store_name");
export const SUBKEY_KEY = b64encode("store_subkey");
export const UNPROVED_VALUE_KEY = b64encode("unproved_value");
export const PACKET_DATA_KEY = "packet_data";
export const PACKET_SRC_CHANNEL_KEY = "packet_src_channel";
export const PACKET_DST_CHANNEL_KEY = "packet_dst_channel";
export const PACKET_SRC_PORT_KEY = "packet_src_port";
export const PACKET_DST_PORT_KEY = "packet_dst_port";
export const ACTION_KEY = b64encode("action");
export const IBC_MESSAGE_TRANSFER_VALUE = b64encode("/ibc.applications.transfer.v1.MsgTransfer");
export const IBC_MESSAGE_RECEIVE_PACKET_VALUE = b64encode("/ibc.core.channel.v1.MsgRecvPacket");
export const RECEPIENT_KEY = b64encode("recipient");
export const SENDER_KEY = b64encode("sender");
export const RECEIVER_KEY = b64encode("receiver");
export const AMOUNT_KEY = b64encode("amount");
export const TRANSFER_PORT_VALUE = "transfer";
export const FETCH_ACCOUNTS_URL =
'http://localhost:1317/cosmos/auth/v1beta1/accounts';
export const GET_FETCH_BALANCE_URL = (address: string) =>
`http://localhost:1317/cosmos/bank/v1beta1/balances/${address}`;
44 changes: 44 additions & 0 deletions src/mappings/custom-types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
export type Balance = {
denom: string;
amount: string;
};

interface PubKey {
'@type': string;
key: string;
}

export type BaseAccount = {
'@type': string;
address: string;
pub_key: PubKey | null;
account_number: string;
sequence: string;
};

export type ModuleAccount = {
'@type': string;
base_account: {
address: string;
pub_key: PubKey | null;
account_number: string;
sequence: string;
};
name: string;
permissions: string[];
};

type Pagination = {
next_key: string | null;
total: string;
};

export type BalancesResponse = {
balances: Balance[];
pagination: Pagination;
};

export type AccountsResponse = {
accounts: (BaseAccount | ModuleAccount)[];
pagination: Pagination;
};
8 changes: 4 additions & 4 deletions src/mappings/events/balances.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Account, Balance } from '../../types';
import { Account, Balances } 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 Balance.getByFields([
const balance = await Balances.getByFields([
['address', '=', address],
['denom', '=', denom],
]);
Expand All @@ -88,7 +88,7 @@ export const balancesEventKit = () => {
denom: string,
primaryKey: string
) {
const newBalance = new Balance(primaryKey, address);
const newBalance = new Balances(primaryKey);
newBalance.address = address;
newBalance.balance = BigInt(0);
newBalance.denom = denom;
Expand Down Expand Up @@ -136,7 +136,7 @@ export const balancesEventKit = () => {
amount: bigint,
operation: Operation
): Promise<void> {
const balances = await Balance.getByFields([
const balances = await Balances.getByFields([
['address', '=', address],
['denom', '=', denom],
]);
Expand Down
Loading

0 comments on commit 6d11f3a

Please sign in to comment.