Skip to content

Commit

Permalink
fix: fixed use entire balance button
Browse files Browse the repository at this point in the history
fix: getting correct transfer fee working again
  • Loading branch information
dafuga committed Oct 27, 2023
1 parent 25144fe commit 9f2dff9
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 116 deletions.
20 changes: 9 additions & 11 deletions src/pages/transfer/form.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import type {TransferManager} from './managers/transferManager'
import {transferManagers} from './managers'
import type {EvmSession} from '~/lib/evm'
import { balances } from '~/stores/balances'
export let handleContinue: () => void
export let amount: string = ''
Expand Down Expand Up @@ -62,6 +63,8 @@
let generatingOptions = false
$: balance = $balances.find((balance) => balance.tokenKey === from?.key)
async function generateOptions(evmSession?: EvmSession) {
if (!!generatingOptions) return
Expand All @@ -76,14 +79,10 @@
// Only displaying accounts that support the current chain
if (!TransferManagerClass.supportedChains.includes($activeBlockchain?.id)) return
let accountBalance
if (!TransferManagerClass.evmRequired || evmSession) {
const transferManager = new (TransferManagerClass as unknown as new (
...args: any[]
) => TransferManager)($activeSession!, evmSession)
await transferManager.updateMainBalance()
accountBalance = await transferManager.balance()
}
const token = $tokens.find(token => token.name === transferManagerData.tokenName)
Expand Down Expand Up @@ -111,13 +110,12 @@
generateOptions()
$: {
transferManager?.balance().then((balance) => {
console.log({ balance, feeAmount, amount })
availableToReceive = CoreAsset.from(
(balance?.value || 0) - (feeAmount?.value || 0),
balance?.symbol || '4,EOS'
)
})
const balance = $balances.find((balance) => from?.key === balance.tokenKey)
const balanceAmount = balance?.quantity
availableToReceive = balanceAmount && CoreAsset.from(
(balanceAmount?.value || 0) - (feeAmount?.value || 0),
balanceAmount?.symbol || '4,EOS'
)
}
// Continue when the user presses enter
Expand Down
14 changes: 6 additions & 8 deletions src/pages/transfer/index.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import {startEvmSession} from '~/lib/evm'
import type {Token} from '~/stores/tokens'
import { balances } from '~/stores/balances'
let step = 'form'
let deposit: string = ''
Expand All @@ -27,6 +28,7 @@
let transferManager: TransferManager | undefined
$: systemContractSymbol = String($systemToken?.symbol)
$: balance = $balances?.find((balance) => balance.tokenKey === from?.key)?.quantity
$: {
const transferManagerData =
from?.name && to?.name ? transferManagers[`${from.name} - ${to?.name}`] : undefined
Expand All @@ -40,11 +42,7 @@
}
async function useEntireBalance() {
if (!from || !to) return
const balance = await transferManager?.balance()
if (!balance) return
if (!from || !to || !balance) return
const balanceValue = balance.value
Expand Down Expand Up @@ -96,7 +94,7 @@
}
try {
transferFee = await transferManager?.transferFee(transferAmount || received)
transferFee = await transferManager?.transferFee(transferAmount || received, from?.symbol)
} catch (error) {
if (
!error?.data?.message?.includes('insufficient funds for transfer') &&
Expand Down Expand Up @@ -152,8 +150,8 @@
// Eventually we may want to get the symbol from the transferManager instead of the systemToken
$: receivedAmount = isNaN(Number(received))
? undefined
: Asset.from(Number(received), systemContractSymbol)
$: depositAmount = Asset.from(Number(deposit), systemContractSymbol)
: Asset.from(Number(received), from?.symbol || systemContractSymbol)
$: depositAmount = Asset.from(Number(deposit), from?.symbol || systemContractSymbol)
</script>

<style type="scss">
Expand Down
16 changes: 3 additions & 13 deletions src/pages/transfer/managers/eosEvmBridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@ import {get} from 'svelte/store'
import {Transfer} from '~/abi-types'
import {getClient} from '~/api-client'
import {TransferManager} from './transferManager'
import {currentAccountBalance, evmBalance} from '~/store'
import {updateActiveAccount} from '~/stores/account-provider'
import {updateEvmBalance} from '~/stores/balances-provider'
import { balances } from '~/stores/balances'

export class EosEvmBridge extends TransferManager {
static supportedChains = ['eos']
Expand All @@ -21,7 +19,7 @@ export class EosEvmBridge extends TransferManager {
return this.evmSession.address
}

async transferFee() {
async transferFee(_amount: string, tokenSymbol: Asset.SymbolType = '4,EOS') {
const apiClient = getClient(this.nativeSession.chainId)

let apiResponse
Expand All @@ -36,6 +34,8 @@ export class EosEvmBridge extends TransferManager {
throw new Error('Failed to get config table from eosio.evm. Full error: ' + err)
}

console.log({rows: apiResponse.rows})

const config = apiResponse.rows[0]

return Asset.from(config.ingress_bridge_fee || '0.0000 EOS')
Expand All @@ -59,16 +59,6 @@ export class EosEvmBridge extends TransferManager {
})
}

async balance(tokenName?: string) {
const balance = get(balances).find((b) => b.tokenKey === tokenName)

return balance?.quantity
}

async receivingBalance() {
return get(evmBalance)
}

async updateBalances() {
updateEvmBalance()
updateActiveAccount()
Expand Down
19 changes: 3 additions & 16 deletions src/pages/transfer/managers/evmEosBridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,9 @@ import {Asset} from 'anchor-link'
import {ethers} from 'ethers'

import {convertToEvmAddress, getProvider} from '~/lib/evm'

import {TransferManager} from './transferManager'
import {updateEvmBalance} from '~/stores/balances-provider'
import {updateActiveAccount} from '~/stores/account-provider'
import {get} from 'svelte/store'
import {currentAccountBalance, evmBalance} from '~/store'

export class EvmEosBridge extends TransferManager {
static from = 'evm'
static fromDisplayString = 'EOS (EVM)'
static to = 'eos'
static toDisplayString = 'EOS'
static supportedChains = ['eos']
static evmRequired = true

Expand All @@ -25,12 +16,12 @@ export class EvmEosBridge extends TransferManager {
return String(this.nativeSession.auth.actor)
}

async transferFee(amount: string) {
async transferFee(amount: string, tokenSymbol: Asset.SymbolType = '4,EOS') {
const {gas, gasPrice} = await this.estimateGas(amount)

const eosAmount = ethers.utils.formatEther(Number(gas) * Number(gasPrice))
const feeAmount = ethers.utils.formatEther(Number(gas) * Number(gasPrice))

return Asset.fromFloat(Number(eosAmount), this.evmSession.getNativeToken().symbol)
return Asset.fromFloat(Number(amount), tokenSymbol)
}

async transfer(amount: string, _tokenSymbol: Asset.SymbolType, amountReceived?: string) {
Expand Down Expand Up @@ -70,8 +61,4 @@ export class EvmEosBridge extends TransferManager {

return {gas, gasPrice}
}

updateMainBalance() {
return updateEvmBalance()
}
}
24 changes: 0 additions & 24 deletions src/pages/transfer/managers/evmTelosBridge.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,11 @@
import {get} from 'svelte/store'

import {currentAccountBalance, evmBalance} from '~/store'
import {TransferManager} from './transferManager'
import {Asset, Name} from 'anchor-link'
import {TelosEvmWithdraw} from '~/abi-types'
import {systemToken} from '~/stores/tokens'
import {updateActiveAccount} from '~/stores/account-provider'
import {updateEvmBalance} from '~/stores/balances-provider'

export class EvmTelosBridge extends TransferManager {
static from = 'evm'
static fromDisplayString = 'TLOS (EVM)'
static to = 'telos'
static toDisplayString = 'TLOS'
static supportedChains = ['telos']
static evmRequired = true

Expand Down Expand Up @@ -45,21 +38,4 @@ export class EvmTelosBridge extends TransferManager {
},
})
}

async balance() {
return get(evmBalance)
}

async receivingBalance() {
return get(currentAccountBalance)
}

async updateBalances(): Promise<void> {
updateActiveAccount()
updateEvmBalance()
}

updateMainBalance() {
return updateEvmBalance()
}
}
27 changes: 0 additions & 27 deletions src/pages/transfer/managers/telosEvmBridge.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,9 @@
import {get} from 'svelte/store'

import {currentAccountBalance, evmBalance} from '~/store'
import {TransferManager} from './transferManager'
import {TelosEvmCreate, TelosEvmOpenWallet, Transfer} from '~/abi-types'
import {Asset, Name} from 'anchor-link'
import {updateActiveAccount} from '~/stores/account-provider'
import {updateEvmBalance} from '~/stores/balances-provider'
import {getTelosEvmAccount} from '~/lib/evm'

export class TelosEvmBridge extends TransferManager {
static from = 'telos'
static fromDisplayString = 'TLOS'
static to = 'evm'
static toDisplayString = 'TLOS (EVM)'
static supportedChains = ['telos']
static evmRequired = true

get fromAddress() {
Expand Down Expand Up @@ -52,23 +42,6 @@ export class TelosEvmBridge extends TransferManager {
})
}

async balance() {
return get(currentAccountBalance)
}

async receivingBalance() {
return get(evmBalance)
}

async updateMainBalance() {
return updateEvmBalance()
}

async updateBalances(): Promise<void> {
updateActiveAccount()
updateEvmBalance()
}

telosEvmOpenWalletAction() {
const action = TelosEvmOpenWallet.from({
account: this.nativeSession.auth.actor,
Expand Down
18 changes: 1 addition & 17 deletions src/pages/transfer/managers/transferManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,27 +34,11 @@ export abstract class TransferManager {
throw new Error('transfer() not implemented')
}

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

balance(tokenName?: string): Promise<Asset | undefined> {
throw new Error('balance() not implemented')
}

receivingBalance(): Promise<Asset | undefined> {
throw new Error('receivingBalance() not implemented')
}

updateBalances(): Promise<void> {
throw new Error('updateBalances() not implemented')
}

updateMainBalance(): Promise<void> {
throw new Error('updateMainBalance() not implemented')
}

async convertToUsd(amount: number): Promise<string> {
const systemTokenPrice = await waitForStoreValue(activePriceTicker)

Expand Down

0 comments on commit 9f2dff9

Please sign in to comment.