Skip to content

Commit

Permalink
fix: fixed confirm page
Browse files Browse the repository at this point in the history
  • Loading branch information
dafuga committed Oct 28, 2023
1 parent 9f2dff9 commit 280ef85
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 29 deletions.
8 changes: 5 additions & 3 deletions src/pages/transfer/confirm.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@
import TokenImage from '~/components/elements/image/token.svelte'
import {systemTokenKey} from '~/stores/tokens'
import type {TransferManager} from './managers/transferManager'
import type { TransferType } from './managers'
import type { TransferManager } from './managers/transferManager'
export let transferManager: TransferManager
export let transferManagerData: TransferType
export let depositAmount: Asset
export let receivedAmount: Asset
export let feeAmount: Asset | undefined
Expand Down Expand Up @@ -136,11 +138,11 @@

<table>
<tr>
<td>From {transferManager.fromDisplayString}</td>
<td>From {transferManagerData.fromLabel}</td>
<td>{transferManager.fromAddress}</td>
</tr>
<tr>
<td>To {transferManager.toDisplayString}</td>
<td>To {transferManagerData.toLabel}</td>
<td>{transferManager.toAddress}</td>
</tr>
<tr>
Expand Down
24 changes: 15 additions & 9 deletions src/pages/transfer/form.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,14 @@
let generatingOptions = false
$: console.log({ balances: $balances })
$: balance = $balances.find((balance) => balance.tokenKey === from?.key)
$: console.log({ balance, from })
console.log({ tokens: $tokens })
async function generateOptions(evmSession?: EvmSession) {
if (!!generatingOptions) return
Expand All @@ -79,13 +85,10 @@
// Only displaying accounts that support the current chain
if (!TransferManagerClass.supportedChains.includes($activeBlockchain?.id)) return
if (!TransferManagerClass.evmRequired || evmSession) {
const transferManager = new (TransferManagerClass as unknown as new (
...args: any[]
) => TransferManager)($activeSession!, evmSession)
}
const token = $tokens.find(token => token.name === transferManagerData.tokenName)
const token = $tokens?.find(token =>
token.name === transferManagerData.tokenName &&
String(token.contract) === transferManagerData.tokenContract &&
token.chainId.equals($activeBlockchain?.chainId))
if (!token) {
console.error(`Token ${transferManagerData.tokenName} not found`)
Expand All @@ -107,10 +110,13 @@
}
}
generateOptions()
$: {
if ($tokens) {
generateOptions()
}
}
$: {
const balance = $balances.find((balance) => from?.key === balance.tokenKey)
const balanceAmount = balance?.quantity
availableToReceive = balanceAmount && CoreAsset.from(
(balanceAmount?.value || 0) - (feeAmount?.value || 0),
Expand Down
15 changes: 10 additions & 5 deletions src/pages/transfer/index.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import Success from './success.svelte'
import Error from './error.svelte'
import {systemToken} from '~/stores/tokens'
import {transferManagers} from './managers'
import {TransferType, transferManagers} from './managers'
import type {TransferManager} from './managers/transferManager'
import {startEvmSession} from '~/lib/evm'
Expand All @@ -25,13 +25,16 @@
let to: Token | undefined
let transactResult: TransactResult | ethers.providers.TransactionResponse | undefined
let transferFee: Asset | undefined
let transferManagerData: TransferType | undefined
let transferManager: TransferManager | undefined
$: systemContractSymbol = String($systemToken?.symbol)
$: balance = $balances?.find((balance) => balance.tokenKey === from?.key)?.quantity
$: receivingBalance = $balances?.find((balance) => balance.tokenKey === to?.key)?.quantity
$: {
const transferManagerData =
transferManagerData =
from?.name && to?.name ? transferManagers[`${from.name} - ${to?.name}`] : undefined
const TransferManagerClass = transferManagerData?.transferClass
if (TransferManagerClass) {
Expand All @@ -51,7 +54,7 @@
const transferFeeValue = transferFee?.value || 0
received = (
(balanceValue || 0) - (transferFeeValue === 0 ? 0 : transferFeeValue - 0.0001)
(balanceValue || 0) - (transferFeeValue === 0 ? 0 : transferFeeValue)
)?.toFixed(4)
}
Expand Down Expand Up @@ -96,6 +99,7 @@
try {
transferFee = await transferManager?.transferFee(transferAmount || received, from?.symbol)
} catch (error) {
console.log({ error })
if (
!error?.data?.message?.includes('insufficient funds for transfer') &&
!error?.data?.message?.includes('gas required exceeds allowance')
Expand Down Expand Up @@ -177,17 +181,18 @@
bind:from
bind:to
/>
{:else if step === 'confirm' && receivedAmount}
{:else if step === 'confirm' && receivedAmount && transferManagerData}
<Confirm
{depositAmount}
{receivedAmount}
{transferManager}
{transferManagerData}
feeAmount={transferFee}
handleConfirm={transfer}
{handleBack}
/>
{:else if step === 'success' && transactResult}
<Success {transactResult} {transferManager} {handleBack} />
<Success {transactResult} {transferManager} {balance} {receivingBalance} {handleBack} />
{/if}
</div>
</Page>
4 changes: 3 additions & 1 deletion src/pages/transfer/managers/eosEvmBridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@ export class EosEvmBridge extends TransferManager {

const config = apiResponse.rows[0]

return Asset.from(config.ingress_bridge_fee || '0.0000 EOS')
const ingressBridgeFee = Asset.from(config.ingress_bridge_fee || '0.0000 EOS')

return Asset.from(ingressBridgeFee.value, tokenSymbol)
}

transfer(amount: string, tokenSymbol: Asset.SymbolType = '4,EOS') {
Expand Down
6 changes: 3 additions & 3 deletions src/pages/transfer/managers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {EvmEosBridge} from './evmEosBridge'
import {TelosEvmBridge} from './telosEvmBridge'
import type {TransferManager} from './transferManager'

interface TransferType {
export interface TransferType {
transferClass: typeof TransferManager
tokenName: string
tokenContract: NameType
Expand All @@ -29,7 +29,7 @@ export const transferManagers: {[key: string]: TransferType} = {
'EOS (EVM) - EOS': {
transferClass: EvmEosBridge,
tokenName: 'EOS (EVM)',
tokenContract: 'eosio.token',
tokenContract: 'eosio.evm',
from: 'evm',
fromLabel: 'EOS (EVM)',
to: 'eos',
Expand All @@ -47,7 +47,7 @@ export const transferManagers: {[key: string]: TransferType} = {
'USDT (EVM) - USDT': {
transferClass: EvmEosBridge,
tokenName: 'USDT (EVM)',
tokenContract: 'tethertether',
tokenContract: 'eosio.evm',
from: 'evm',
fromLabel: 'USDT (EVM)',
to: 'usdt',
Expand Down
1 change: 1 addition & 0 deletions src/pages/transfer/managers/telosEvmBridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {Asset, Name} from 'anchor-link'
import {getTelosEvmAccount} from '~/lib/evm'

export class TelosEvmBridge extends TransferManager {
static supportedChains = ['telos']
static evmRequired = true

get fromAddress() {
Expand Down
4 changes: 1 addition & 3 deletions src/pages/transfer/managers/transferManager.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import {Asset} from 'anchor-link'
import type {LinkSession, TransactResult} from 'anchor-link'
import type {ethers} from 'ethers'
import {get} from 'svelte/store'

import type {EvmSession} from '~/lib/evm'
import {valueInFiat} from '~/lib/fiat'
import {activePriceTicker, waitForStoreValue} from '~/store'
import {systemToken} from '~/stores/tokens'

export abstract class TransferManager {
self: typeof TransferManager
Expand Down Expand Up @@ -35,7 +33,7 @@ export abstract class TransferManager {
}

transferFee(_amount?: string, tokenSymbol?: Asset.SymbolType): Promise<Asset> {
return Promise.resolve(Asset.from(0, get(systemToken)?.symbol || '4,EOS'))
return Promise.resolve(Asset.from(0, tokenSymbol|| '4,EOS'))
}
/* eslint-enable @typescript-eslint/no-unused-vars */

Expand Down
14 changes: 9 additions & 5 deletions src/pages/transfer/success.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,21 @@
import {activeBlockchain} from '~/store'
import type {TransferManager} from './managers/transferManager'
import { updateBalances } from '~/stores/balances-provider'
import { activeSession } from '~/store'
export let transferManager: TransferManager
export let transactResult: TransactResult | ethers.providers.TransactionResponse
export let handleBack: () => void
export let balance: Asset | undefined
export let receivingBalance: Asset | undefined
let refreshInterval: NodeJS.Timeout
async function awaitBalancesUpdate() {
// Create a copy of the initial value
const initialSendingBalance = await transferManager.balance()
const initialReceivedBalance = await transferManager.receivingBalance()
const initialSendingBalance = balance
const initialReceivedBalance = receivingBalance
if (!initialSendingBalance) return
if (!initialReceivedBalance) return
Expand All @@ -27,8 +31,8 @@
// Start an interval to continously monitor for changes to that value
refreshInterval = setInterval(async () => {
currentSendingBalance = await transferManager.balance()
currentReceivedBalance = await transferManager.receivingBalance()
currentSendingBalance = balance
currentReceivedBalance = receivingBalance
// If the balances changed, stop the interval
if (
Expand All @@ -38,7 +42,7 @@
clearInterval(refreshInterval)
}
// Fetch the balances
transferManager.updateBalances()
updateBalances($activeSession!)
}, 1000)
// Timeout after 30 seconds
Expand Down

0 comments on commit 280ef85

Please sign in to comment.