Skip to content

Commit

Permalink
add derives for transfer
Browse files Browse the repository at this point in the history
Signed-off-by: Gregory Hill <[email protected]>
  • Loading branch information
gregdhill committed Jan 7, 2022
1 parent 2eea385 commit 75fa6ce
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 11 deletions.
26 changes: 22 additions & 4 deletions packages/apps-config/src/api/spec/interbtc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@

/* 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, from, map, Observable } from 'rxjs';

import { DeriveBalancesAll } from '@polkadot/api-derive/types';
import { memo } from '@polkadot/api-derive/util';
import { TypeRegistry, U128 } from '@polkadot/types';
import { AnyTuple } from '@polkadot/types-codec/types';
import { BN, formatBalance } from '@polkadot/util';

function balanceOf (number: number | string): U128 {
Expand Down Expand Up @@ -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
Expand All @@ -54,10 +56,26 @@ export function getBalance (
);
}

export function transferBalance (
instanceId: string,
api: ApiInterfaceRx
): Observable<SubmittableExtrinsicFunction<'rxjs', AnyTuple>> {
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
}
},

Expand Down
4 changes: 2 additions & 2 deletions packages/page-accounts/src/Accounts/Account.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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) && (
<Button
className='send-button'
icon='paper-plane'
label={t<string>('send')}
label={t<string>('Send')}
onClick={toggleTransfer}
/>
)}
Expand Down
2 changes: 1 addition & 1 deletion packages/page-accounts/src/Sidebar/AccountMenuButtons.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ function AccountMenuButtons ({ className = '', flags, isEditing, isEditingName,
)
: (
<Button.Group>
{isFunction(api.api.tx.balances?.transfer) && (
{isFunction(api.api.tx.balances?.transfer) || isFunction(api.api.derive.balances?.transfer) && (
<Button
icon='paper-plane'
isDisabled={isEditing}
Expand Down
8 changes: 4 additions & 4 deletions packages/page-accounts/src/modals/Transfer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -219,11 +219,11 @@ function Transfer ({ className = '', onClose, recipientId: propRecipientId, send
: [propRecipientId || recipientId, amount]
}
tx={
canToggleAll && isAll && isFunction(api.tx.balances?.transferAll)
? api.tx.balances?.transferAll
canToggleAll && isAll && isFunction(api.derive.balances?.transferAll)
? api.derive.balances?.transferAll
: isProtected
? api.tx.balances?.transferKeepAlive
: api.tx.balances?.transfer
? api.derive.balances?.transferKeepAlive
: api.derive.balances?.transfer
}
/>
</Modal.Actions>
Expand Down

0 comments on commit 75fa6ce

Please sign in to comment.