Skip to content

Commit

Permalink
fix: handling useEntireBalance case gracefully
Browse files Browse the repository at this point in the history
  • Loading branch information
dafuga committed Aug 14, 2023
1 parent 01e3bcb commit f7f1bb2
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 17 deletions.
4 changes: 3 additions & 1 deletion src/lib/evm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,10 +133,12 @@ export async function estimateGas({nativeSession, evmAccount, amount}: TransferP

const gasPrice = await provider.getGasPrice()

const reducedAmount = String(Number(amount) - 0.005)

const gas = await provider.estimateGas({
from: evmAccount.address,
to: targetEvmAddress,
value: ethers.utils.parseEther(amount),
value: ethers.utils.parseEther(reducedAmount),
gasPrice,
data: ethers.utils.formatBytes32String(''),
})
Expand Down
7 changes: 4 additions & 3 deletions src/pages/transfer/confirm.svelte
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<script lang="ts">
import {Asset} from '@greymass/eosio'
import type {Asset} from '@greymass/eosio'
import Button from '~/components/elements/button.svelte'
import {evmAccount, activeSession} from '~/store'
Expand All @@ -8,6 +8,7 @@
export let from: Token
export let to: Token
export let depositAmount: Asset
export let receivedAmount: Asset
export let feeAmount: Asset | undefined
export let handleConfirm: () => void
export let handleBack: () => void
Expand Down Expand Up @@ -90,11 +91,11 @@
</tr>
<tr>
<td>Fee Amount</td>
<td>{feeAmount}</td>
<td>{feeAmount || '0.0000 EOS' }</td>
</tr>
<tr>
<td>Received Amount</td>
<td>{feeAmount ? Asset.from(depositAmount.value - feeAmount.value, '4,EOS') : '0 EOS'}</td>
<td>{receivedAmount}</td>
</tr>
</table>
<div class="bottom-section">
Expand Down
44 changes: 31 additions & 13 deletions src/pages/transfer/index.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
import type {Token} from '~/stores/tokens'
let step = 'form'
let amount: string = ''
let deposit: string = ''
let received: string = ''
let errorMessage: string | undefined
let from: Token | undefined
let to: Token | undefined
Expand All @@ -32,13 +33,13 @@
nativeTransactResult = await transferNativeToEvm({
nativeSession: $activeSession!,
evmAccount: $evmAccount,
amount,
amount: received,
})
} else {
evmTransactResult = await transferEvmToNative({
nativeSession: $activeSession!,
evmAccount: $evmAccount,
amount,
amount: received,
})
}
} catch (error) {
Expand All @@ -55,21 +56,30 @@
errorMessage = undefined
nativeTransactResult = undefined
evmTransactResult = undefined
amount = ''
deposit = ''
received = ''
}
async function submitForm() {
if (!$evmAccount) {
return (errorMessage = 'An evm session is required.')
}
step = 'confirm'
transferFee = await getGasAmount({
nativeSession: $activeSession!,
evmAccount: $evmAccount,
amount,
})
try {
transferFee = await getGasAmount({
nativeSession: $activeSession!,
evmAccount: $evmAccount,
amount: deposit,
})
} catch (error) {
return (errorMessage = `Could not estimate gas. Error: ${
JSON.stringify(error) === '{}' ? error.message : JSON.stringify(error)
}`)
}
received = (parseFloat(deposit) - parseFloat(transferFee.value.toFixed(4))).toFixed(4)
}
let connectInterval: number | undefined
Expand Down Expand Up @@ -120,10 +130,18 @@
<div class="container">
{#if errorMessage}
<Error error={errorMessage} {handleBack} />
{:else if step === 'form' || !from || !to || !amount}
<Form handleContinue={submitForm} bind:amount bind:from bind:to />
{:else if step === 'form' || !from || !to || !deposit || !received}
<Form handleContinue={submitForm} bind:amount={deposit} bind:from bind:to />
{:else if step === 'confirm'}
<Confirm depositAmount={Asset.from(Number(amount), '4,EOS')} feeAmount={transferFee} {from} {to} handleConfirm={transfer} {handleBack} />
<Confirm
depositAmount={Asset.from(Number(deposit), '4,EOS')}
receivedAmount={Asset.from(Number(received), '4,EOS')}
feeAmount={transferFee}
{from}
{to}
handleConfirm={transfer}
{handleBack}
/>
{:else if (step === 'success' && nativeTransactResult) || evmTransactResult}
<Success {from} {to} {nativeTransactResult} {evmTransactResult} {handleBack} />
{/if}
Expand Down

0 comments on commit f7f1bb2

Please sign in to comment.