From fba6c421d87ce85015a83bf0bb5328c87ae5d041 Mon Sep 17 00:00:00 2001 From: Dean Sallinen <7519573+deansallinen@users.noreply.github.com> Date: Tue, 1 Oct 2024 08:35:15 -0700 Subject: [PATCH 1/3] fix: default balance --- .../[network]/api/account/[[name]]/+server.ts | 54 +++++++++---------- 1 file changed, 26 insertions(+), 28 deletions(-) diff --git a/src/routes/[network]/api/account/[[name]]/+server.ts b/src/routes/[network]/api/account/[[name]]/+server.ts index dfa892f2..c0ffffa2 100644 --- a/src/routes/[network]/api/account/[[name]]/+server.ts +++ b/src/routes/[network]/api/account/[[name]]/+server.ts @@ -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 { 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 }); @@ -22,30 +18,32 @@ export async function GET({ fetch, params }) { const network = getNetwork(chain, fetch); const { system: systemContract } = network.contracts; - const requests: [ - Promise, - Promise, - Promise, - Promise - ] = [ - network.client.v1.chain.get_account(params.name), - systemContract.table('delband').all({ scope: params.name }), - systemContract.table('rexbal').get(params.name), - loadBalances(network, params.name, fetch) - ]; - try { const headers = getCacheHeaders(5); - const [account_data, delegated, rex, balances] = await Promise.all(requests); + const [account_data, delegated, rex, 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), + 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 the core liquid balance as a default if it exists if (!balances.length && chain.systemToken) { - 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 - }); + if (account_data.core_liquid_balance) { + balances.push({ + contract: chain.systemToken.contract.toString(), + amount: account_data.core_liquid_balance.quantity, + decimals: account_data.core_liquid_balance.symbol.precision.toString(), + currency: account_data.core_liquid_balance.symbol.code.toString() + }); + } else { + balances.push({ + contract: chain.systemToken.contract.toString(), + amount: '0', + decimals: '0', + currency: '' + }); + } } return json( @@ -62,7 +60,7 @@ export async function GET({ fetch, params }) { console.error(error); return json({ error: 'Unable to load account.' }, { status: 500 }); } -} +}; async function loadBalances( network: NetworkState, From a8d368129103d02ec2ee26d4a0908b47a57a5df6 Mon Sep 17 00:00:00 2001 From: Dean Sallinen <7519573+deansallinen@users.noreply.github.com> Date: Tue, 1 Oct 2024 11:04:21 -0700 Subject: [PATCH 2/3] fix: use network symbol as default --- .../[network]/api/account/[[name]]/+server.ts | 24 +++++++------------ 1 file changed, 8 insertions(+), 16 deletions(-) diff --git a/src/routes/[network]/api/account/[[name]]/+server.ts b/src/routes/[network]/api/account/[[name]]/+server.ts index c0ffffa2..fb0087a2 100644 --- a/src/routes/[network]/api/account/[[name]]/+server.ts +++ b/src/routes/[network]/api/account/[[name]]/+server.ts @@ -3,7 +3,7 @@ import { getChainDefinitionFromParams, getNetwork, NetworkState } from '$lib/sta import { getCacheHeaders } from '$lib/utils'; import type { LightAPIBalanceResponse, LightAPIBalanceRow } from '$lib/types.js'; import type { RequestHandler } from './$types'; -import { type NameType } from '@wharfkit/antelope'; +import { Asset, type NameType } from '@wharfkit/antelope'; export const GET: RequestHandler = async ({ fetch, params }) => { const chain = getChainDefinitionFromParams(params.network); @@ -29,21 +29,13 @@ export const GET: RequestHandler = async ({ fetch, params }) => { // If no response from the light API, add the core liquid balance as a default if it exists if (!balances.length && chain.systemToken) { - if (account_data.core_liquid_balance) { - balances.push({ - contract: chain.systemToken.contract.toString(), - amount: account_data.core_liquid_balance.quantity, - decimals: account_data.core_liquid_balance.symbol.precision.toString(), - currency: account_data.core_liquid_balance.symbol.code.toString() - }); - } else { - balances.push({ - contract: chain.systemToken.contract.toString(), - amount: '0', - decimals: '0', - currency: '' - }); - } + const symbol = Asset.Symbol.from(network.config.symbol); + balances.push({ + contract: String(chain.systemToken.contract), + amount: '0', + decimals: String(symbol.precision), + currency: String(symbol.code) + }); } return json( From ca11c1392dbef836fcb78c7b5687888371f94b14 Mon Sep 17 00:00:00 2001 From: Dean Sallinen <7519573+deansallinen@users.noreply.github.com> Date: Tue, 1 Oct 2024 11:06:29 -0700 Subject: [PATCH 3/3] chore: update comment --- src/routes/[network]/api/account/[[name]]/+server.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/routes/[network]/api/account/[[name]]/+server.ts b/src/routes/[network]/api/account/[[name]]/+server.ts index fb0087a2..8311d72d 100644 --- a/src/routes/[network]/api/account/[[name]]/+server.ts +++ b/src/routes/[network]/api/account/[[name]]/+server.ts @@ -27,7 +27,7 @@ export const GET: RequestHandler = async ({ fetch, params }) => { loadBalances(network, params.name, fetch) ]); - // If no response from the light API, add the core liquid balance as a default if it exists + // 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({