Skip to content

Commit

Permalink
enhancement: useEntireBalance now takes into account transferFee
Browse files Browse the repository at this point in the history
  • Loading branch information
dafuga committed Sep 1, 2023
1 parent 6dabfe2 commit d027cd9
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 20 deletions.
17 changes: 6 additions & 11 deletions src/pages/transfer/form.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
export let to: Token | undefined
export let evmBalance: CoreAsset | undefined
export let feeAmount: CoreAsset | undefined
export let depositAmount: CoreAsset | undefined
export let receivedAmount: CoreAsset | undefined
export let useEntireBalance: () => void
let validAmount = false
Expand All @@ -30,16 +32,6 @@
to = token
}
function useEntireBalance() {
let value
if (from?.name === 'EOS (EVM)') {
value = evmBalance?.value
} else if (from?.name === 'EOS') {
value = $currentAccountBalance?.value
}
amount = ((value || 0) - (feeAmount?.value || 0))?.toFixed(4)
}
$: readyToContinue = from && to && validAmount && $evmAccount
function onContinue() {
Expand Down Expand Up @@ -128,7 +120,7 @@
}
.label-container {
padding: 10px 20px;
padding: 3px 8px;
}
}
Expand Down Expand Up @@ -198,6 +190,9 @@
<div class="label-container">
<Label align="left" >Transfer Fee: {String(feeAmount) }</Label>
</div>
<div class="label-container">
<Label align="left" >Total transferred: {String(depositAmount) }</Label>
</div>
{/if}
</div>
</div>
Expand Down
42 changes: 33 additions & 9 deletions src/pages/transfer/index.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import {Asset, TransactResult} from 'anchor-link'
import type {ethers} from 'ethers'
import {activeSession, evmAccount} from '~/store'
import {currentAccountBalance, evmAccount, activeSession} from '~/store'
import {
transferNativeToEvm,
Expand Down Expand Up @@ -31,6 +31,21 @@
let transferFee: Asset | undefined
let evmBalance: Asset | undefined
async function useEntireBalance() {
if (!from || !to) return
let value
if (from?.name === 'EOS (EVM)') {
value = evmBalance?.value
} else if (from?.name === 'EOS') {
value = $currentAccountBalance?.value
}
await estimateTransferFee(String(value))
received = ((value || 0) - (transferFee?.value || 0))?.toFixed(4)
}
async function transfer() {
if (!$evmAccount) {
return (errorMessage = 'An evm session is required.')
Expand Down Expand Up @@ -78,8 +93,7 @@
deposit = (parseFloat(received) + parseFloat(transferFee?.value.toFixed(4) || '')).toFixed(4)
}
async function estimateTransferFee(): Promise<Asset | undefined> {
console.log({from, to, deposit})
async function estimateTransferFee(transferAmount?: string): Promise<Asset | undefined> {
if (!$evmAccount) {
errorMessage = 'An evm session is required.'
return
Expand All @@ -94,7 +108,7 @@
transferFee = await getGasAmount({
nativeSession: $activeSession!,
evmAccount: $evmAccount,
amount: deposit,
amount: transferAmount || received,
})
}
} catch (error) {
Expand Down Expand Up @@ -147,7 +161,6 @@
}
function updateBalances() {
console.log('updateBalances')
updateAccount($activeSession!.auth.actor, $activeSession!.chainId)
getEvmBalance()
}
Expand All @@ -162,10 +175,19 @@
connectEvmWallet()
$: {
if (from && to && deposit !== '') {
if (from && to && received !== '' && Number(received) > 0) {
estimateTransferFee()
}
}
$: {
if (transferFee && received !== '') {
deposit = (parseFloat(received) + parseFloat(transferFee?.value.toFixed(4))).toFixed(4)
}
}
$: receivedAmount = Asset.from(Number(received), '4,EOS')
$: depositAmount = Asset.from(Number(deposit), '4,EOS')
</script>

<style type="scss">
Expand All @@ -183,16 +205,18 @@
<Form
handleContinue={submitForm}
feeAmount={transferFee}
receivedAmount={Asset.from(Number(received), '4,EOS')}
{depositAmount}
{receivedAmount}
{evmBalance}
{useEntireBalance}
bind:amount={received}
bind:from
bind:to
/>
{:else if step === 'confirm'}
<Confirm
depositAmount={Asset.from(Number(deposit), '4,EOS')}
receivedAmount={Asset.from(Number(received), '4,EOS')}
{depositAmount}
{receivedAmount}
feeAmount={transferFee}
{from}
{to}
Expand Down

0 comments on commit d027cd9

Please sign in to comment.