diff --git a/packages/apps-config/src/api/spec/interbtc.ts b/packages/apps-config/src/api/spec/interbtc.ts index 126b5ca67862..1765437a0d19 100644 --- a/packages/apps-config/src/api/spec/interbtc.ts +++ b/packages/apps-config/src/api/spec/interbtc.ts @@ -3,23 +3,23 @@ /* eslint-disable @typescript-eslint/no-unsafe-argument, @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-assignment */ -import type { Observable } from 'rxjs'; -import type { ApiInterfaceRx } from '@polkadot/api/types'; +import type { ApiInterfaceRx, SubmittableExtrinsicFunction } from '@polkadot/api/types'; import type { OverrideBundleDefinition } from '@polkadot/types/types'; import interbtc from '@interlay/interbtc-types'; -import { combineLatest, map } from 'rxjs'; +import { combineLatest, map, Observable, from } from 'rxjs'; import { DeriveBalancesAll } from '@polkadot/api-derive/types'; import { memo } from '@polkadot/api-derive/util'; import { TypeRegistry, U128 } from '@polkadot/types'; import { BN, formatBalance } from '@polkadot/util'; +import { AnyTuple } from '@polkadot/types-codec/types'; -function balanceOf (number: number | string): U128 { +function balanceOf(number: number | string): U128 { return new U128(new TypeRegistry(), number); } -function defaultAccountBalance (): DeriveBalancesAll { +function defaultAccountBalance(): DeriveBalancesAll { // eslint-disable-next-line @typescript-eslint/no-unsafe-return return { accountNonce: new BN(1), @@ -32,7 +32,7 @@ function defaultAccountBalance (): DeriveBalancesAll { } as any; } -export function getBalance ( +export function getBalance( instanceId: string, api: ApiInterfaceRx ): () => Observable { @@ -45,6 +45,8 @@ export function getBalance ( map(([data]: [any]): DeriveBalancesAll => { return { ...defaultAccountBalance(), + accountId: api.registry.createType("AccountId", account), + availableBalance: data.free, freeBalance: data.free, lockedBalance: data.frozen, reservedBalance: data.reserved @@ -54,10 +56,26 @@ export function getBalance ( ); } +export function transferBalance( + instanceId: string, + api: ApiInterfaceRx +): Observable> { + const nativeToken = api.registry.chainTokens[0] || formatBalance.getDefaults().unit; + + return memo( + instanceId, + (account: string, amount: number) => + api.tx.tokens.transfer(account, { Token: nativeToken }, amount) + ); +} + const definitions: OverrideBundleDefinition = { derives: { balances: { - all: getBalance + all: getBalance, + transfer: transferBalance, + transferAll: transferBalance, + transferKeepAlive: transferBalance, } }, diff --git a/packages/page-accounts/src/Accounts/Account.tsx b/packages/page-accounts/src/Accounts/Account.tsx index 30fce8563479..99139774a643 100644 --- a/packages/page-accounts/src/Accounts/Account.tsx +++ b/packages/page-accounts/src/Accounts/Account.tsx @@ -54,7 +54,7 @@ interface DemocracyUnlockable { ids: BN[]; } -function calcVisible (filter: string, name: string, tags: string[]): boolean { +function calcVisible(filter: string, name: string, tags: string[]): boolean { if (filter.length === 0) { return true; } @@ -66,7 +66,7 @@ function calcVisible (filter: string, name: string, tags: string[]): boolean { }, name.toLowerCase().includes(_filter)); } -function calcUnbonding (stakingInfo?: DeriveStakingAccount) { +function calcUnbonding(stakingInfo?: DeriveStakingAccount) { if (!stakingInfo?.unlocking) { return BN_ZERO; } @@ -79,7 +79,7 @@ function calcUnbonding (stakingInfo?: DeriveStakingAccount) { return total; } -function createClearDemocracyTx (api: ApiPromise, address: string, unlockableIds: BN[]): SubmittableExtrinsic<'promise'> | null { +function createClearDemocracyTx(api: ApiPromise, address: string, unlockableIds: BN[]): SubmittableExtrinsic<'promise'> | null { return api.tx.utility ? api.tx.utility.batch( unlockableIds @@ -89,7 +89,7 @@ function createClearDemocracyTx (api: ApiPromise, address: string, unlockableIds : null; } -async function showLedgerAddress (getLedger: () => Ledger, meta: KeyringJson$Meta): Promise { +async function showLedgerAddress(getLedger: () => Ledger, meta: KeyringJson$Meta): Promise { const ledger = getLedger(); await ledger.getAddress(true, meta.accountOffset as number || 0, meta.addressOffset as number || 0); @@ -99,7 +99,7 @@ const transformRecovery = { transform: (opt: Option) => opt.unwrapOr(null) }; -function Account ({ account: { address, meta }, className = '', delegation, filter, isFavorite, proxy, setBalance, toggleFavorite }: Props): React.ReactElement | null { +function Account({ account: { address, meta }, className = '', delegation, filter, isFavorite, proxy, setBalance, toggleFavorite }: Props): React.ReactElement | null { const { t } = useTranslation(); const [isExpanded, toggleIsExpanded] = useToggle(false); const { theme } = useContext(ThemeContext as React.Context); @@ -393,7 +393,7 @@ function Account ({ account: { address, meta }, className = '', delegation, filt /> ]) ].filter((i) => i), - [_clearDemocracyLocks, _showOnHardware, _vestingVest, api, delegation, democracyUnlockTx, genesisHash, identity, isDevelopment, isEditable, isEthereum, isExternal, isHardware, isInjected, isMultisig, multiInfos, onSetGenesisHash, proxy, recoveryInfo, t, toggleBackup, toggleDelegate, toggleDerive, toggleForget, toggleIdentityMain, toggleIdentitySub, toggleMultisig, togglePassword, toggleProxyOverview, toggleRecoverAccount, toggleRecoverSetup, toggleUndelegate, vestingVestTx]); + [_clearDemocracyLocks, _showOnHardware, _vestingVest, api, delegation, democracyUnlockTx, genesisHash, identity, isDevelopment, isEditable, isEthereum, isExternal, isHardware, isInjected, isMultisig, multiInfos, onSetGenesisHash, proxy, recoveryInfo, t, toggleBackup, toggleDelegate, toggleDerive, toggleForget, toggleIdentityMain, toggleIdentitySub, toggleMultisig, togglePassword, toggleProxyOverview, toggleRecoverAccount, toggleRecoverSetup, toggleUndelegate, vestingVestTx]); if (!isVisible) { return null; @@ -653,11 +653,11 @@ function Account ({ account: { address, meta }, className = '', delegation, filt isLogo type='address' /> - {isFunction(api.api.tx.balances?.transfer) && ( + {isFunction(api.api.tx.balances?.transfer) || isFunction(api.api.derive.balances?.transfer) && (