Skip to content

Commit

Permalink
Merge pull request #149 from greymass/fix-default-balances
Browse files Browse the repository at this point in the history
fix: default balance
  • Loading branch information
aaroncox authored Oct 1, 2024
2 parents 80b8760 + 052f6ea commit 1354503
Showing 1 changed file with 17 additions and 28 deletions.
45 changes: 17 additions & 28 deletions src/routes/[network]/api/account/[[name]]/+server.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
import { json } from '@sveltejs/kit';

import { getChainDefinitionFromParams, getNetwork, NetworkState } from '$lib/state/network.svelte';
import { type API, type NameType } from '@wharfkit/antelope';
import type { TokenBalance } from '@wharfkit/common';
import type { ChainDefinition } from '@wharfkit/session';
import { chainMapper } from '$lib/wharf/chains.js';
import { getCacheHeaders } from '$lib/utils';
import { SystemContract } from '@wharfkit/account';
import type { LightAPIBalanceResponse, LightAPIBalanceRow } from '$lib/types.js';
import type { RequestHandler } from './$types';
import { Asset, type NameType } from '@wharfkit/antelope';

export async function GET({ fetch, params }) {
export const GET: RequestHandler = async ({ fetch, params }) => {
const chain = getChainDefinitionFromParams(params.network);
if (!chain) {
return json({ error: 'Invalid chain specified' }, { status: 400 });
Expand All @@ -22,31 +18,24 @@ export async function GET({ fetch, params }) {
const network = getNetwork(chain, fetch);
const { system: systemContract } = network.contracts;

const requests: [
Promise<API.v1.AccountObject>,
Promise<SystemContract.Types.delegated_bandwidth[]>,
Promise<SystemContract.Types.rex_balance>,
Promise<SystemContract.Types.rex_fund>,
Promise<LightAPIBalanceRow[]>
] = [
network.client.v1.chain.get_account(params.name),
systemContract.table('delband').all({ scope: params.name }),
systemContract.table('rexbal').get(params.name),
systemContract.table('rexfund').get(params.name),
loadBalances(network, params.name, fetch)
];

try {
const headers = getCacheHeaders(5);
const [account_data, delegated, rexbal, rexfund, balances] = await Promise.all(requests);
const [account_data, delegated, rexbal, rexfund, balances] = await Promise.all([
network.client.v1.chain.get_account(params.name),
systemContract.table('delband').all({ scope: params.name }),
systemContract.table('rexbal').get(params.name),
systemContract.table('rexfund').get(params.name),
loadBalances(network, params.name, fetch)
]);

// If no response from the light API, add the core liquid balance as a default
// If no response from the light API, add a default balance of zero
if (!balances.length && chain.systemToken) {
const symbol = Asset.Symbol.from(network.config.symbol);
balances.push({
contract: chain.systemToken.contract,
amount: account_data.core_liquid_balance.quantity,
decimals: account_data.core_liquid_balance.symbol.precision,
currency: account_data.core_liquid_balance.symbol.code
contract: String(chain.systemToken.contract),
amount: '0',
decimals: String(symbol.precision),
currency: String(symbol.code)
});
}

Expand All @@ -65,7 +54,7 @@ export async function GET({ fetch, params }) {
console.error(error);
return json({ error: 'Unable to load account.' }, { status: 500 });
}
}
};

async function loadBalances(
network: NetworkState,
Expand Down

0 comments on commit 1354503

Please sign in to comment.