Skip to content

Commit c881345

Browse files
committed
tests: rest controller transfer
1 parent 0341f67 commit c881345

File tree

4 files changed

+136
-2
lines changed

4 files changed

+136
-2
lines changed
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
import { TestFactory } from '~/helpers';
2+
import { RestClient } from '~/rest';
3+
import { controllerTransferParams, createAssetParams } from '~/rest/assets/params';
4+
import { ProcessMode } from '~/rest/common';
5+
import { Identity } from '~/rest/identities/interfaces';
6+
import { RestSuccessResult } from '~/rest/interfaces';
7+
import { fungibleInstructionParams } from '~/rest/settlements/params';
8+
import { expectBasicTxInfo } from '../utils';
9+
10+
const handles = ['issuer', 'holder'];
11+
let factory: TestFactory;
12+
13+
describe('Fungible AssetController transfer', () => {
14+
let restClient: RestClient;
15+
let signer: string;
16+
let issuer: Identity;
17+
let holder: Identity;
18+
let assetParams: ReturnType<typeof createAssetParams>;
19+
let assetId: string;
20+
21+
beforeAll(async () => {
22+
factory = await TestFactory.create({ handles });
23+
({ restClient } = factory);
24+
issuer = factory.getSignerIdentity(handles[0]);
25+
holder = factory.getSignerIdentity(handles[1]);
26+
27+
signer = issuer.signer;
28+
29+
assetParams = createAssetParams({
30+
options: { processMode: ProcessMode.Submit, signer },
31+
});
32+
});
33+
34+
afterAll(async () => {
35+
await factory.close();
36+
});
37+
38+
it('should create and fetch the Asset', async () => {
39+
assetId = await restClient.assets.createAndGetAssetId(assetParams);
40+
41+
const asset = await restClient.assets.getAsset(assetId);
42+
43+
expect(asset).toMatchObject({
44+
name: assetParams.name,
45+
assetType: assetParams.assetType,
46+
});
47+
});
48+
49+
it('should transfer the asset to holder', async () => {
50+
const transferToHolderTx = await restClient.settlements.createDirectInstruction(fungibleInstructionParams(assetId, issuer.did, holder.did, {
51+
options: { processMode: ProcessMode.Submit, signer },
52+
}));
53+
54+
// should have created an instruction
55+
expect((transferToHolderTx as RestSuccessResult).instruction).toBeDefined();
56+
57+
const txData = await restClient.settlements.affirmInstruction(
58+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
59+
(transferToHolderTx as any).instruction,
60+
{
61+
options: { processMode: ProcessMode.Submit, signer: holder.signer },
62+
}
63+
);
64+
65+
expect(txData).toMatchObject({
66+
transactions: expect.arrayContaining([
67+
{
68+
transactionTag: 'settlement.affirmInstructionWithCount',
69+
type: 'single',
70+
...expectBasicTxInfo,
71+
},
72+
]),
73+
});
74+
75+
const { results } = await restClient.assets.getAssetHolders(assetId);
76+
77+
expect(results.length).toEqual(2);
78+
expect(results).toContainEqual(expect.objectContaining({
79+
identity: issuer.did,
80+
balance: '99990',
81+
}));
82+
expect(results).toContainEqual(expect.objectContaining({
83+
identity: holder.did,
84+
balance: '10',
85+
}));
86+
});
87+
88+
it('should run controller transfer and return the asset back to the issuer', async () => {
89+
const controllerTransferTx = await restClient.assets.controllerTransfer(assetId, controllerTransferParams({ did: holder.did, id: '0' }, 10, {
90+
options: { processMode: ProcessMode.Submit, signer },
91+
})) as RestSuccessResult;
92+
93+
expect(controllerTransferTx).toMatchObject({
94+
transactions: expect.arrayContaining([
95+
{
96+
transactionTag: 'asset.controllerTransfer',
97+
type: 'single',
98+
...expectBasicTxInfo,
99+
},
100+
]),
101+
});
102+
103+
104+
const { results } = await restClient.assets.getAssetHolders(assetId);
105+
106+
expect(results.length).toEqual(1);
107+
expect(results).toContainEqual(expect.objectContaining({
108+
identity: expect.objectContaining({
109+
did: issuer.did,
110+
}),
111+
balance: expect.objectContaining({
112+
amount: '100000',
113+
}),
114+
}));
115+
});
116+
});

tests/src/__tests__/sdk/assets/controllerTransfer.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { LocalSigningManager } from '@polymeshassociation/local-signing-manager';
2-
import { BigNumber, Polymesh } from '@polymeshassociation/polymesh-sdk';
2+
import { Polymesh } from '@polymeshassociation/polymesh-sdk';
33
import { FungibleAsset } from '@polymeshassociation/polymesh-sdk/types';
44

55
import { TestFactory } from '~/helpers';

tests/src/rest/assets/client.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,11 @@ import {
88
setAssetDocumentParams,
99
setMetadataParams,
1010
transferAssetOwnershipParams,
11+
controllerTransferParams,
1112
} from '~/rest/assets/params';
1213
import { RestClient } from '~/rest/client';
1314
import { TxBase } from '~/rest/common';
14-
import { PostResult, RestSuccessResult } from '~/rest/interfaces';
15+
import { PostResult, ResultSet, RestSuccessResult } from '~/rest/interfaces';
1516

1617
export class Assets {
1718
constructor(private client: RestClient) {}
@@ -133,4 +134,12 @@ export class Assets {
133134
public async unfreeze(asset: string, params: TxBase): Promise<PostResult> {
134135
return this.client.post(`assets/${asset}/unfreeze`, { ...params });
135136
}
137+
138+
public async controllerTransfer(asset: string, params: ReturnType<typeof controllerTransferParams>): Promise<PostResult> {
139+
return this.client.post(`assets/${asset}/controller-transfer`, params);
140+
}
141+
142+
public async getAssetHolders(asset: string): Promise<ResultSet<Record<string, unknown>>> {
143+
return this.client.get(`assets/${asset}/holders`);
144+
}
136145
}

tests/src/rest/assets/params.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,3 +105,12 @@ export const issueAssetParams = (amount: number, base: TxBase, extras: TxExtras
105105
...extras,
106106
...base,
107107
} as const);
108+
109+
110+
export const controllerTransferParams = (origin: { did: string, id: string }, amount: number, base: TxBase, extras: TxExtras = {}) =>
111+
({
112+
origin,
113+
amount,
114+
...extras,
115+
...base,
116+
} as const);

0 commit comments

Comments
 (0)