|
| 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 | +}); |
0 commit comments