Skip to content

Commit

Permalink
chore: changes to adjust pagination for fetching accounts
Browse files Browse the repository at this point in the history
  • Loading branch information
rabi-siddique committed Jun 3, 2024
1 parent c8750f8 commit 9eea11b
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 21 deletions.
1 change: 1 addition & 0 deletions src/mappings/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ export const IBC_MESSAGE_TRANSFER_VALUE = b64encode('/ibc.applications.transfer.
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 SPENDER_KEY = b64encode('spender');
export const RECEIVER_KEY = b64encode('receiver');
export const AMOUNT_KEY = b64encode('amount');
export const TRANSFER_PORT_VALUE = 'transfer';
Expand Down
4 changes: 2 additions & 2 deletions src/mappings/custom-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ export type Balance = {
amount: string;
};

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

export type BaseAccount = {
'@type': string;
Expand Down
7 changes: 4 additions & 3 deletions src/mappings/events/balances.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Account, Balances } from '../../types';
import { BALANCE_FIELDS } from '../constants';
import { b64decode } from '../utils';
import { CosmosEvent } from '@subql/types-cosmos';
import { AMOUNT_KEY, RECEIVER_KEY, SPENDER_KEY } from '../constants';

interface Attribute {
key: string;
Expand Down Expand Up @@ -33,11 +34,11 @@ export const balancesEventKit = () => {
}

function getData(cosmosEvent: CosmosEvent) {
let dataAlreadyDecoded = true;
const value = getAttributeValue(cosmosEvent.event, BALANCE_FIELDS.amount);
let dataAlreadyDecoded = false;
const value = getAttributeValue(cosmosEvent.event, AMOUNT_KEY);

if (!value) {
dataAlreadyDecoded = false;
dataAlreadyDecoded = true;
}

const data = dataAlreadyDecoded
Expand Down
68 changes: 52 additions & 16 deletions src/mappings/mappingHandlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,14 @@ import { boardAuxEventKit } from './events/boardAux';
import { priceFeedEventKit } from './events/priceFeed';
import { vaultsEventKit } from './events/vaults';
import { reservesEventKit } from './events/reserves';
import { AccountsResponse, BaseAccount, ModuleAccount, BalancesResponse, Balance, VestingAccount } from './custom-types';
import {
AccountsResponse,
BaseAccount,
ModuleAccount,
BalancesResponse,
Balance,
VestingAccount,
} from './custom-types';
import { Operation, balancesEventKit } from './events/balances';
import crossFetch from 'cross-fetch';

Expand Down Expand Up @@ -305,16 +312,26 @@ export const handleBalanceEvent = async (cosmosEvent: CosmosEvent): Promise<void
}
};

const fetchAccounts = async (): Promise<AccountsResponse | void> => {
const fetchAccounts = async (
offset: string,
limit: string,
): Promise<[(BaseAccount | ModuleAccount | VestingAccount)[], string | null]> => {
try {
const response = await crossFetch(FETCH_ACCOUNTS_URL);
const accounts: AccountsResponse = await response.json();
return accounts;
const url = new URL(FETCH_ACCOUNTS_URL);
url.searchParams.append('pagination.limit', limit);
url.searchParams.append('pagination.offset', offset);

const response = await crossFetch(url.toString());
const parsedResponse: AccountsResponse = await response.json();

const accounts: (BaseAccount | ModuleAccount | VestingAccount)[] = parsedResponse.accounts;

return [accounts, parsedResponse.pagination.next_key];
} catch (error) {
logger.error(`Error fetching accounts: ${error}`);
return [[], ''];
}
};

const extractAddresses = (accounts: (BaseAccount | ModuleAccount | VestingAccount)[]): Array<string> => {
return accounts
.map((account) => {
Expand Down Expand Up @@ -348,20 +365,39 @@ const saveAccountBalances = async (address: string, balances: Balance[]) => {
newBalance.denom = denom;

await newBalance.save();
logger.info(`Save Operation Successful`);
}
};

export const initiateBalancesTable = async (block: CosmosBlock): Promise<void> => {
const response = await fetchAccounts();
const fetchAndSaveBalances = async (accountAddresses: string[]) => {
const fetchPromises = accountAddresses.map(async (address) => {
const response = await fetchBalance(address);
if (response) {
return saveAccountBalances(address, response.balances);
}
});

if (response) {
const accountAddresses = extractAddresses(response.accounts).filter(Boolean);
for (let address of accountAddresses) {
const response = await fetchBalance(address);
await Promise.all(fetchPromises);
};

if (response) {
await saveAccountBalances(address, response.balances);
}
export const initiateBalancesTable = async (block: CosmosBlock): Promise<void> => {
let nextKey: string | null = null;
let offset: number = 0;
const limit: number = 1000;

logger.info('Initiate Balances Table');
do {
logger.info('Fetching Accounts');
const [accounts, nextPaginationKey] = await fetchAccounts(String(offset), String(limit));
logger.info(`Accounts Fetched: ${accounts.length}`);
logger.info(`Pagination Key ${nextKey}`);
nextKey = nextPaginationKey;
if (accounts) {
logger.info('Storing Balances');
const accountAddresses = extractAddresses(accounts).filter(Boolean);
await fetchAndSaveBalances(accountAddresses);
logger.info('Balances Stored Successfully');
offset += accounts.length;
}
}
} while (nextKey);
};

0 comments on commit 9eea11b

Please sign in to comment.