From 01e3bcbe14ec72659715c6ede163bcd269559930 Mon Sep 17 00:00:00 2001 From: dafuga Date: Mon, 14 Aug 2023 10:30:02 -0400 Subject: [PATCH] enhancement: displaying fee amount on confirmation page --- src/lib/evm.ts | 22 ++++++++++++++++++++-- src/pages/transfer/confirm.svelte | 16 ++++++++++++---- src/pages/transfer/index.svelte | 19 +++++++++++++++---- 3 files changed, 47 insertions(+), 10 deletions(-) diff --git a/src/lib/evm.ts b/src/lib/evm.ts index f03f40c1..b9ded41a 100644 --- a/src/lib/evm.ts +++ b/src/lib/evm.ts @@ -128,17 +128,35 @@ export async function transferNativeToEvm({nativeSession, evmAccount, amount}: T }) } -export async function transferEvmToNative({nativeSession, evmAccount, amount}: TransferParams) { +export async function estimateGas({nativeSession, evmAccount, amount}: TransferParams) { const targetEvmAddress = convertToEvmAddress(String(nativeSession.auth.actor)) + const gasPrice = await provider.getGasPrice() + const gas = await provider.estimateGas({ from: evmAccount.address, to: targetEvmAddress, value: ethers.utils.parseEther(amount), - gasPrice: await provider.getGasPrice(), + gasPrice, data: ethers.utils.formatBytes32String(''), }) + return {gas, gasPrice} +} + +export async function getGasAmount({nativeSession, evmAccount, amount}: TransferParams): Promise { + const { gas, gasPrice } = await estimateGas({nativeSession, evmAccount, amount}) + + const eosAmount = ethers.utils.formatEther(Number(gas) * Number(gasPrice)) + + return Asset.fromFloat(Number(eosAmount), '4,EOS') +} + +export async function transferEvmToNative({nativeSession, evmAccount, amount}: TransferParams) { + const targetEvmAddress = convertToEvmAddress(String(nativeSession.auth.actor)) + + const { gas } = await estimateGas({nativeSession, evmAccount, amount}) + return evmAccount.sendTransaction({ from: evmAccount.address, to: targetEvmAddress, diff --git a/src/pages/transfer/confirm.svelte b/src/pages/transfer/confirm.svelte index 5c614b0b..4be1fa80 100644 --- a/src/pages/transfer/confirm.svelte +++ b/src/pages/transfer/confirm.svelte @@ -1,6 +1,5 @@ @@ -85,8 +85,16 @@ {from?.name === 'EOS' ? $evmAccount?.address : $activeSession?.auth.actor} - Amount - {Asset.from(Number(amount), '4,EOS')} + Deposit Amount + {depositAmount} + + + Fee Amount + {feeAmount} + + + Received Amount + {feeAmount ? Asset.from(depositAmount.value - feeAmount.value, '4,EOS') : '0 EOS'}
diff --git a/src/pages/transfer/index.svelte b/src/pages/transfer/index.svelte index 83131b6e..e24ca483 100644 --- a/src/pages/transfer/index.svelte +++ b/src/pages/transfer/index.svelte @@ -1,10 +1,10 @@