Skip to content

Commit

Permalink
refactor: moving balances to balances store
Browse files Browse the repository at this point in the history
  • Loading branch information
dafuga committed Oct 24, 2023
1 parent 6f921fa commit 7f39ca4
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 45 deletions.
6 changes: 4 additions & 2 deletions src/components/elements/input/token/selector.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,10 @@
$: {
if (tokenOptions) {
filteredTokens = tokenOptions.map(tokenOption => {
const usdt = $tokens?.find(token => token.contract === 'tethertether')
const token = $tokens?.find(token => tokenOption.tokenName === token.name)
const token = $tokens?.find(token =>
String(tokenOption.tokenName) === String(token.name) &&
String(tokenOption.tokenContract) === String(token.contract)
)
if (!token) {
throw new Error(`Token ${tokenOption.tokenName} not found.`)
Expand Down
7 changes: 1 addition & 6 deletions src/components/elements/input/token/selector/row.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,7 @@
let balance
$: {
if (token.balance) {
balance = token.balance
} else {
balance =
$balances && $balances.find((balance) => balance.tokenKey === token.key)?.quantity
}
balance = $balances && $balances.find((balance) => balance.tokenKey === token.key)?.quantity
if (typeof balance === 'string') {
formattedTokenBalance = balance
Expand Down
30 changes: 6 additions & 24 deletions src/pages/transfer/form.svelte
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<script lang="ts">
import {Asset as CoreAsset} from '@greymass/eosio'
import {activeEvmSession, activeSession, activeBlockchain, currentAccountBalance} from '~/store'
import {Token, TokenOption, systemToken} from '~/stores/tokens'
import type {Token, TokenOption} from '~/stores/tokens'
import Label from '~/components/elements/input/label.svelte'
import Form from '~/components/elements/form.svelte'
Expand Down Expand Up @@ -73,8 +73,6 @@
Object.values(transferManagers).map(async (transferManagerData) => {
const TransferManagerClass = transferManagerData.transferClass
if (!$systemToken) return
// Only displaying accounts that support the current chain
if (!TransferManagerClass.supportedChains.includes($activeBlockchain?.id)) return
Expand All @@ -89,8 +87,9 @@
}
fromOptions.push({
tokenName: String(transferManagerData.token),
label: TransferManagerClass.fromDisplayString,
tokenName: String(transferManagerData.tokenName),
tokenContract: String(transferManagerData.tokenContract),
label: transferManagerData.fromLabel,
})
})
)
Expand All @@ -106,24 +105,9 @@
}
}
let lastBalanceValue = $currentAccountBalance?.value
$: {
// Regenerate options if the balance changes
if ($activeEvmSession && lastBalanceValue !== $currentAccountBalance?.value) {
generateOptions($activeEvmSession)
lastBalanceValue = $currentAccountBalance?.value
}
}
generateOptions()
$: {
// Regenerate options if the user connects to evm wallet
if ($activeEvmSession) {
generateOptions($activeEvmSession)
} else {
generateOptions()
}
}
$: console.log({ fromOptions })
$: {
transferManager?.balance().then((balance) => {
Expand Down Expand Up @@ -234,7 +218,6 @@
onTokenSelect={handleFromChange}
selectedToken={from}
tokenOptions={fromOptions}
showTokensWithoutBalance
/>
</div>
<Label align="left">Amount</Label>
Expand All @@ -258,7 +241,6 @@
onTokenSelect={handleToChange}
selectedToken={to}
tokenOptions={toOptions}
showTokensWithoutBalance
/>
</div>
{#if receivedAmount && receivedAmount.value > 0 && feeAmount && feeAmount.value > 0}
Expand Down
4 changes: 0 additions & 4 deletions src/pages/transfer/managers/eosEvmBridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,6 @@ import {updateActiveAccount} from '~/stores/account-provider'
import {updateEvmBalance} from '~/stores/balances-provider'

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

Expand Down
51 changes: 43 additions & 8 deletions src/pages/transfer/managers/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Asset, AssetType } from 'anchor-link'
import type { NameType } from 'anchor-link'

import {EvmTelosBridge} from './evmTelosBridge'
import {EosEvmBridge} from './eosEvmBridge'
Expand All @@ -8,32 +8,67 @@ import type {TransferManager} from './transferManager'

interface TransferType {
transferClass: typeof TransferManager
token: AssetType
tokenName: NameType
tokenContract: NameType
from: string
fromLabel: string
to: string
toLabel: string
}

export const transferManagers: {[key: string]: TransferType} = {
'EOS - EOS (EVM)': {
transferClass: EosEvmBridge,
token: 'EOS'
tokenName: 'EOS',
tokenContract: 'eosio.token',
from: 'eos',
fromLabel: 'EOS',
to: 'evm',
toLabel: 'EOS (EVM)',
},
'EOS (EVM) - EOS': {
transferClass: EvmEosBridge,
token: 'EOS'
tokenName: 'EOS',
tokenContract: 'eosio.token',
from: 'evm',
fromLabel: 'EOS (EVM)',
to: 'eos',
toLabel: 'EOS',
},
'USDT - USDT (EVM)': {
transferClass: EosEvmBridge,
token: 'USDT'
tokenName: 'USDT',
tokenContract: 'tethertether',
from: 'usdt',
fromLabel: 'USDT',
to: 'evm',
toLabel: 'USDT (EVM)',
},
'USDT (EVM) - USDT': {
transferClass: EvmEosBridge,
token: 'USDT'
tokenName: 'USDT',
tokenContract: 'tethertether',
from: 'evm',
fromLabel: 'USDT (EVM)',
to: 'usdt',
toLabel: 'USDT',
},
'TLOS - TLOS (EVM)': {
transferClass: TelosEvmBridge,
token: 'TLOS'
tokenName: 'TLOS',
tokenContract: 'tethertether',
from: 'tlos',
fromLabel: 'TLOS',
to: 'evm',
toLabel: 'TLOS (EVM)',
},
'TLOS (EVM) - TLOS': {
transferClass: EvmTelosBridge,
token: 'TLOS'
tokenName: 'TLOS',
tokenContract: 'tethertether',
from: 'evm',
fromLabel: 'TLOS (EVM)',
to: 'tlos',
toLabel: 'TLOS',
},
}
3 changes: 2 additions & 1 deletion src/stores/tokens.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ export interface Token {
}

export interface TokenOption {
tokenName: string
tokenName: NameType
tokenContract: NameType
label?: string
}

Expand Down

0 comments on commit 7f39ca4

Please sign in to comment.