From 629fcaf3c73d6aaeefa8bc12446a0487c9d7728c Mon Sep 17 00:00:00 2001 From: dafuga Date: Thu, 26 Oct 2023 23:15:09 -0700 Subject: [PATCH] chore: general polishing of transfer page --- src/lib/evm/index.ts | 13 ++++++++++++- src/pages/transfer/form.svelte | 2 +- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/src/lib/evm/index.ts b/src/lib/evm/index.ts index 49310597..31952896 100644 --- a/src/lib/evm/index.ts +++ b/src/lib/evm/index.ts @@ -132,13 +132,20 @@ export class EvmSession { const contract = new ethers.Contract(token.address, erc20_abi, this.signer); wei = await contract.balanceOf(this.address) + } else if (token?.nativeToken) { wei = await this.signer.getBalance() } else { throw new Error('Non native token must have an address.') } - return Asset.from(formatToken(ethers.utils.formatEther(wei), token.name)) + const decimals = token.decimals + + if (token.decimals === 18) { + return Asset.from(Number(ethers.utils.formatEther(wei)), token.name) + } else { + return Asset.from(fromWei(wei, decimals), token.name) + } } getBalances() { @@ -343,3 +350,7 @@ export async function startEvmSession(): Promise { return evmSession } + +export function fromWei(wei: BigNumber, decimals: number) { + return wei.toNumber() / Math.pow(10, decimals); +} diff --git a/src/pages/transfer/form.svelte b/src/pages/transfer/form.svelte index 16e198d4..f76714a2 100644 --- a/src/pages/transfer/form.svelte +++ b/src/pages/transfer/form.svelte @@ -103,7 +103,7 @@ $: { if (from) { - toOptions = fromOptions.filter((token) => token.name !== from?.name) + toOptions = fromOptions.filter((token) => token.name !== from?.name) // this needs to only show options which involve the same token } else { toOptions = fromOptions }