Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: default balance #149

Merged
merged 4 commits into from
Oct 1, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading