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 203ce60
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 33 deletions.
32 changes: 25 additions & 7 deletions packages/apps-config/src/api/spec/interbtc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -32,7 +32,7 @@ function defaultAccountBalance (): DeriveBalancesAll {
} as any;
}

export function getBalance (
export function getBalance(
instanceId: string,
api: ApiInterfaceRx
): () => Observable<DeriveBalancesAll> {
Expand All @@ -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
16 changes: 8 additions & 8 deletions packages/page-accounts/src/Accounts/Account.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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;
}
Expand All @@ -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
Expand All @@ -89,7 +89,7 @@ function createClearDemocracyTx (api: ApiPromise, address: string, unlockableIds
: null;
}

async function showLedgerAddress (getLedger: () => Ledger, meta: KeyringJson$Meta): Promise<void> {
async function showLedgerAddress(getLedger: () => Ledger, meta: KeyringJson$Meta): Promise<void> {
const ledger = getLedger();

await ledger.getAddress(true, meta.accountOffset as number || 0, meta.addressOffset as number || 0);
Expand All @@ -99,7 +99,7 @@ const transformRecovery = {
transform: (opt: Option<RecoveryConfig>) => opt.unwrapOr(null)
};

function Account ({ account: { address, meta }, className = '', delegation, filter, isFavorite, proxy, setBalance, toggleFavorite }: Props): React.ReactElement<Props> | null {
function Account({ account: { address, meta }, className = '', delegation, filter, isFavorite, proxy, setBalance, toggleFavorite }: Props): React.ReactElement<Props> | null {
const { t } = useTranslation();
const [isExpanded, toggleIsExpanded] = useToggle(false);
const { theme } = useContext(ThemeContext as React.Context<ThemeDef>);
Expand Down Expand Up @@ -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;
Expand Down 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
4 changes: 2 additions & 2 deletions packages/page-accounts/src/Sidebar/AccountMenuButtons.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ interface Props {
recipientId: string;
}

function AccountMenuButtons ({ className = '', flags, isEditing, isEditingName, onCancel, onForgetAddress, onSaveName, onSaveTags, onUpdateName, recipientId, toggleIsEditingName, toggleIsEditingTags }: Props): React.ReactElement<Props> {
function AccountMenuButtons({ className = '', flags, isEditing, isEditingName, onCancel, onForgetAddress, onSaveName, onSaveTags, onUpdateName, recipientId, toggleIsEditingName, toggleIsEditingTags }: Props): React.ReactElement<Props> {
const { t } = useTranslation();
const [isTransferOpen, toggleIsTransferOpen] = useToggle();
const api = useApi();
Expand Down 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
14 changes: 7 additions & 7 deletions packages/page-accounts/src/modals/Transfer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ interface Props {
senderId?: string;
}

function isRefcount (accountInfo: AccountInfoWithProviders | AccountInfoWithRefCount): accountInfo is AccountInfoWithRefCount {
function isRefcount(accountInfo: AccountInfoWithProviders | AccountInfoWithRefCount): accountInfo is AccountInfoWithRefCount {
return !!(accountInfo as AccountInfoWithRefCount).refcount;
}

async function checkPhishing (_senderId: string | null, recipientId: string | null): Promise<[string | null, string | null]> {
async function checkPhishing(_senderId: string | null, recipientId: string | null): Promise<[string | null, string | null]> {
return [
// not being checked atm
// senderId
Expand All @@ -40,7 +40,7 @@ async function checkPhishing (_senderId: string | null, recipientId: string | nu
];
}

function Transfer ({ className = '', onClose, recipientId: propRecipientId, senderId: propSenderId }: Props): React.ReactElement<Props> {
function Transfer({ className = '', onClose, recipientId: propRecipientId, senderId: propSenderId }: Props): React.ReactElement<Props> {
const { t } = useTranslation();
const { api } = useApi();
const [amount, setAmount] = useState<BN | undefined>(BN_ZERO);
Expand Down 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
18 changes: 10 additions & 8 deletions packages/react-components/src/Status/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ interface Props {
className?: string;
}

function iconName (status: string): IconName {
function iconName(status: string): IconName {
switch (status) {
case 'error':
return 'ban';
Expand All @@ -37,7 +37,7 @@ function iconName (status: string): IconName {
}
}

function signerIconName (status: QueueTxStatus): IconName {
function signerIconName(status: QueueTxStatus): IconName {
switch (status) {
case 'cancelled':
return 'ban';
Expand All @@ -58,15 +58,15 @@ function signerIconName (status: QueueTxStatus): IconName {
return 'exclamation-triangle';

case 'queued':
// case 'retracted':
// case 'retracted':
return 'random';

default:
return 'spinner';
}
}

function renderStatus ({ account, action, id, message, removeItem, status }: QueueStatus): React.ReactNode {
function renderStatus({ account, action, id, message, removeItem, status }: QueueStatus): React.ReactNode {
return (
<div
className={`item ${status}`}
Expand Down Expand Up @@ -100,9 +100,11 @@ function renderStatus ({ account, action, id, message, removeItem, status }: Que
);
}

function renderItem ({ error, extrinsic, id, removeItem, rpc, status }: QueueTx): React.ReactNode {
function renderItem({ error, extrinsic, id, removeItem, rpc, status }: QueueTx): React.ReactNode {
let { method, section } = rpc;

console.log(extrinsic)

if (extrinsic) {
const found = extrinsic.registry.findMetaCall(extrinsic.callIndex);

Expand Down Expand Up @@ -147,17 +149,17 @@ function renderItem ({ error, extrinsic, id, removeItem, rpc, status }: QueueTx)
);
}

function filterSt (stqueue?: QueueStatus[]): QueueStatus[] {
function filterSt(stqueue?: QueueStatus[]): QueueStatus[] {
return (stqueue || []).filter(({ isCompleted }) => !isCompleted);
}

function filterTx (txqueue?: QueueTx[]): [QueueTx[], QueueTx[]] {
function filterTx(txqueue?: QueueTx[]): [QueueTx[], QueueTx[]] {
const allTx = (txqueue || []).filter(({ status }) => !['completed', 'incomplete'].includes(status));

return [allTx, allTx.filter(({ status }) => STATUS_COMPLETE.includes(status))];
}

function Status ({ className = '' }: Props): React.ReactElement<Props> | null {
function Status({ className = '' }: Props): React.ReactElement<Props> | null {
const { stqueue, txqueue } = useContext(StatusContext);
const [allSt, setAllSt] = useState<QueueStatus[]>([]);
const [[allTx, completedTx], setAllTx] = useState<[QueueTx[], QueueTx[]]>([[], []]);
Expand Down
4 changes: 3 additions & 1 deletion packages/react-signer/src/Transaction.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,15 @@ interface Props {
tip?: BN;
}

function Transaction ({ accountId, className, currentItem: { extrinsic, isUnsigned, payload }, isSendable, onError, tip }: Props): React.ReactElement<Props> | null {
function Transaction({ accountId, className, currentItem: { extrinsic, isUnsigned, payload }, isSendable, onError, tip }: Props): React.ReactElement<Props> | null {
const { t } = useTranslation();

if (!extrinsic) {
return null;
}

console.log(extrinsic);

const { meta, method, section } = extrinsic.registry.findMetaCall(extrinsic.callIndex);
const args = meta?.args.map(({ name }) => name).join(', ') || '';

Expand Down

0 comments on commit 203ce60

Please sign in to comment.