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

fix: add assets::transferAll call #405

Merged
merged 1 commit into from
Sep 18, 2024
Merged
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
27 changes: 27 additions & 0 deletions packages/txwrapper-dev/src/constants/constants.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { assetHubKusamaMetadataHex } from '../metadata/asset-hub-kusama';
import { assetHubPolkadotMetadataHex } from '../metadata/asset-hub-polkadot';
import { assetHubWestendMetadataHex } from '../metadata/asset-hub-westend';
import { astarMetadataHex } from '../metadata/astar';
import { kusamaMetadataHex } from '../metadata/kusama';
import { polkadotMetadataHex } from '../metadata/polkadot';
Expand All @@ -12,12 +13,14 @@ import {
memoizedPolkadotGetRegistry,
memoizedWestendGetRegistry,
} from '../registries';
import { memoizedAssetHubWestendGetRegistry } from '../registries/westendAssetHubRegistry';

export const KUSAMA_SPEC_VERSION = 102005;
export const POLKADOT_SPEC_VERSION = 9430;
export const ASSET_HUB_KUSAMA_SPEC_VERSION = 1002005;
export const ASSET_HUB_POLKADOT_SPEC_VERSION = 1002004;
export const WESTEND_SPEC_VERSION = 1013000;
export const ASSET_HUB_WESTEND_SPEC_VERSION = 1016000;
export const ASTAR_SPEC_VERSION = 72;

/**
Expand Down Expand Up @@ -98,6 +101,21 @@ export const WESTEND_TEST_BASE_TX_INFO = {
transactionVersion: 6,
};

export const ASSET_HUB_WESTEND_TEST_BASE_TX_INFO = {
address: 'HNZata7iMYWmk5RvZRTiAsSDhV8366zq2YGb3tLH5Upf74F', // seed "//Alice"
blockHash:
'0x1fc7493f3c1e9ac758a183839906475f8363aafb1b1d3e910fe16fab4ae1b582',
blockNumber: 4302222,
eraPeriod: 2400,
genesisHash:
'0xe3777fa922cafbff200cadeaea1a76bd7898ad5b89f7848999058b50e715f636',
metadataRpc: assetHubWestendMetadataHex,
nonce: 2,
specVersion: ASSET_HUB_WESTEND_SPEC_VERSION,
tip: 0,
transactionVersion: 6,
};

export const ASTAR_TEST_BASE_TX_INFO = {
address: 'XYJkwdMbojBtNzngQxLHEdciMDE5baLihb5RWTL3QHGfnwy',
blockHash:
Expand Down Expand Up @@ -155,6 +173,15 @@ export const WESTEND_TEST_OPTIONS = {
westendMetadataHex,
),
};

export const ASSET_HUB_WESTEND_TEST_OPTIONS = {
metadataRpc: assetHubWestendMetadataHex,
registry: memoizedAssetHubWestendGetRegistry(
ASSET_HUB_WESTEND_SPEC_VERSION,
assetHubWestendMetadataHex,
),
};

export const ASTAR_TEST_OPTIONS = {
metadataRpc: astarMetadataHex,
registry: memoizedAstarGetRegistry(ASTAR_SPEC_VERSION, astarMetadataHex),
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './assetHubWestendMetadataHex';
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { TypeRegistry } from '@polkadot/types';
import { getSpecTypes } from '@polkadot/types-known';
import memoizee from 'memoizee';

import { mockGetRegistryBase } from './mockGetRegistry';

/**
* Get the Westend type registry for a given spec version for testing purposes
*
* @param specVersion
* @param metadataRpc
*/
export function getRegistryAssetHubWestend(
specVersion: number,
metadataRpc: `0x${string}`,
): TypeRegistry {
return mockGetRegistryBase({
chainProperties: {
ss58Format: 42,
tokenDecimals: 12,
tokenSymbol: 'WND',
},
specTypes: getSpecTypes(
new TypeRegistry(),
'westmint',
'westmint',
specVersion,
),
metadataRpc,
});
}

export const memoizedAssetHubWestendGetRegistry = memoizee(
getRegistryAssetHubWestend,
);
1 change: 1 addition & 0 deletions packages/txwrapper-substrate/src/methods/assets/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export * from './approveTransfer';
export * from './cancelApproval';
export * from './transfer';
export * from './transferAll';
export * from './transferApproved';
export * from './transferKeepAlive';
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import {
ASSET_HUB_WESTEND_TEST_BASE_TX_INFO,
ASSET_HUB_WESTEND_TEST_OPTIONS,
itHasCorrectBaseTxInfo,
} from '@substrate/txwrapper-dev';

import { TEST_METHOD_ARGS } from '../../test-helpers';
import { transferAll } from './transferAll';

describe('assets:transferAll', () => {
it('should work', () => {
const unsigned = transferAll(
TEST_METHOD_ARGS.assets.transferAll,
ASSET_HUB_WESTEND_TEST_BASE_TX_INFO,
ASSET_HUB_WESTEND_TEST_OPTIONS,
);

itHasCorrectBaseTxInfo(unsigned);
expect(unsigned.method).toBe(
'0x322028008eaf04151687736326c9fea17e25fc5287613693c912909cb226aa4794f26a4801',
);
});
});
47 changes: 47 additions & 0 deletions packages/txwrapper-substrate/src/methods/assets/transferAll.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import {
Args,
BaseTxInfo,
defineMethod,
OptionsWithMeta,
UnsignedTransaction,
} from '@substrate/txwrapper-core';

export interface AssetsTransferAllArgs extends Args {
/**
* The identifier of the asset to have some amount transferred.
*/
id: number;
/**
* The account to be credited.
*/
dest: string;
/**
* Limit the amount of the asset transferred to keep the account from being reaped.
*/
keepAlive: boolean;
}

/**
* Move some assets from the sender account to another.
*
* @param args - Arguments specific to this method.
* @param info - Information required to construct the transaction.
* @param options - Registry and metadata used for constructing the method.
*/
export function transferAll(
args: AssetsTransferAllArgs,
info: BaseTxInfo,
options: OptionsWithMeta,
): UnsignedTransaction {
return defineMethod(
{
method: {
args,
name: 'transferAll',
pallet: 'assets',
},
...info,
},
options,
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ export const TEST_METHOD_ARGS = {
target: '14E5nqKAp3oAJcmzgZhUD2RcptBeUBScxKHgJKU4HPNcKVf3', // seed "//Bob"
amount: 1234,
},
transferAll: {
id: 10,
dest: '14E5nqKAp3oAJcmzgZhUD2RcptBeUBScxKHgJKU4HPNcKVf3', // seed "//Bob"
keepAlive: true,
},
transferKeepAlive: {
id: 10,
target: '14E5nqKAp3oAJcmzgZhUD2RcptBeUBScxKHgJKU4HPNcKVf3', // seed "//Bob"
Expand Down