Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add derives for transfer #6799

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 21 additions & 4 deletions packages/apps-config/src/api/spec/interbtc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@

/* 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, SubmittableExtrinsic } 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 'rxjs';

import { ApiPromise } from '@polkadot/api';
import { DeriveBalancesAll } from '@polkadot/api-derive/types';
import { memo } from '@polkadot/api-derive/util';
import { TypeRegistry, U128 } from '@polkadot/types';
Expand Down Expand Up @@ -63,10 +63,27 @@ export function getBalance (
);
}

export function transferBalance (
account: string,
amount: number,
api: ApiPromise
): SubmittableExtrinsic<'promise'> | undefined {
if (!api) {
return;
}
Comment on lines +71 to +73
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Even though there is a check for the api not to be undefined before calling this function, unless this if is added the app crashes

Copy link
Member

@jacogr jacogr Feb 7, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The signature for all derives are -

// ReturnType is here for illustrative purposes
type ReturnType = string;

function myDerive (instanceId: string, api: ApiInterfaceRx): Observable<ReturnType> {
  return memo(instanceId, (): Observable<ReturnType> => {
    // we work with observables here
    return api.rpc.chain.getHeader().pipe(
      switchMap((header) => of(header.toString()))
    );    
  });
}

So in your case you received 2 params, not 3 (and not an ApiPromise instance, they don't exist in derives)


const nativeToken = api.registry.chainTokens[0] || formatBalance.getDefaults().unit;

return 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
5 changes: 3 additions & 2 deletions packages/page-accounts/src/Accounts/Account.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -652,11 +652,12 @@ function Account ({ account: { address, meta }, className = '', delegation, filt
isLogo
type='address'
/>
{isFunction(api.api.tx.balances?.transfer) && (
{ /* eslint-disable-next-line @typescript-eslint/no-unsafe-member-access */ }
{(isFunction(api.api.tx.balances?.transfer) || isFunction((api.api.derive.balances as any).transfer)) && (
<Button
className='send-button'
icon='paper-plane'
label={t<string>('send')}
label={t<string>('Send')}
onClick={toggleTransfer}
/>
)}
Expand Down
3 changes: 2 additions & 1 deletion packages/page-accounts/src/Sidebar/AccountMenuButtons.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,8 @@ function AccountMenuButtons ({ className = '', flags, isEditing, isEditingName,
)
: (
<Button.Group>
{isFunction(api.api.tx.balances?.transfer) && (
{/* eslint-disable-next-line @typescript-eslint/no-unsafe-member-access */}
{(isFunction(api.api.tx.balances?.transfer) || isFunction((api.api.derive.balances as any).transfer)) && (
<Button
icon='paper-plane'
isDisabled={isEditing}
Expand Down
25 changes: 18 additions & 7 deletions packages/page-accounts/src/modals/Transfer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import type { BN } from '@polkadot/util';
import React, { useEffect, useState } from 'react';
import styled from 'styled-components';

import { transferBalance } from '@polkadot/apps-config/api/spec/interbtc';
import { checkAddress } from '@polkadot/phishing';
import { InputAddress, InputBalance, MarkError, MarkWarning, Modal, Toggle, TxButton } from '@polkadot/react-components';
import { useApi, useCall } from '@polkadot/react-hooks';
Expand Down Expand Up @@ -208,22 +209,32 @@ function Transfer ({ className = '', onClose, recipientId: propRecipientId, send
<TxButton
accountId={propSenderId || senderId}
icon='paper-plane'
isDisabled={!hasAvailable || !(propRecipientId || recipientId) || !amount || !!recipientPhish}
isDisabled={!hasAvailable || !(propRecipientId || recipientId) || !amount || !!recipientPhish || !api}
label={t<string>('Make Transfer')}
onStart={onClose}
params={
canToggleAll && isAll
? isFunction(api.tx.balances?.transferAll)
? [propRecipientId || recipientId, false]
: [propRecipientId || recipientId, maxTransfer]
: [propRecipientId || recipientId, amount]
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
: !!api && isFunction((api.derive.balances as any).transferAll)
? [propRecipientId || recipientId, amount, api]
: [propRecipientId || recipientId, amount]
}
tx={
canToggleAll && isAll && isFunction(api.tx.balances?.transferAll)
? api.tx.balances?.transferAll
: isProtected
? api.tx.balances?.transferKeepAlive
: api.tx.balances?.transfer
canToggleAll && isAll
? isFunction(api.tx.balances?.transferAll)
? api.tx.balances?.transferAll || transferBalance
: isProtected
? api.tx.balances?.transferKeepAlive || transferBalance
: api.tx.balances?.transfer || transferBalance
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
: !!api && isFunction((api.derive.balances as any).transferAll)
? transferBalance
: isProtected
? transferBalance
: transferBalance
Comment on lines +233 to +237
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Although the derive is defined in the apps-config for interBtc, it's empty when used here. transferBalance is what the derive is supposed to be, and it works this way. How can this be correctly implemented using the derive?

}
/>
</Modal.Actions>
Expand Down