|
| 1 | +import assert from 'node:assert'; |
| 2 | + |
| 3 | +import { BigNumber, Polymesh } from '@polymeshassociation/polymesh-sdk'; |
| 4 | +import { createAsset } from '~/sdk/assets/createAsset'; |
| 5 | +import { createNftCollection } from '~/sdk/assets/createNftCollection'; |
| 6 | +import { KnownNftType, MetadataType } from '@polymeshassociation/polymesh-sdk/types'; |
| 7 | + |
| 8 | +/* |
| 9 | + This script showcases VenueFiltering related functionality. It: |
| 10 | + - Creates an asset with initial supply |
| 11 | + - Transfers some of the asset to a new DID |
| 12 | + - Force controller transfer for soma part back to default portfolio |
| 13 | + - Creates new portfolio |
| 14 | + - Force controller transfer to newly created portfolio |
| 15 | +*/ |
| 16 | + |
| 17 | +export const fungibleAssetControllerTransfer = async (sdk: Polymesh, targetDid: string): Promise<void> => { |
| 18 | + const asset = await createAsset(sdk, { initialSupply: new BigNumber(2000) }); |
| 19 | + |
| 20 | + assert(asset); |
| 21 | + |
| 22 | + const [identity, counterParty] = await Promise.all([ |
| 23 | + sdk.getSigningIdentity(), |
| 24 | + sdk.identities.getIdentity({ did: targetDid }), |
| 25 | + ]); |
| 26 | + assert(identity); |
| 27 | + const { account: counterPartyAccount } = await counterParty.getPrimaryAccount(); |
| 28 | + |
| 29 | + const transferTx = await sdk.settlements.addInstruction({ |
| 30 | + legs: [ |
| 31 | + { asset, from: identity, to: targetDid, amount: new BigNumber(1000) }, |
| 32 | + ], |
| 33 | + }); |
| 34 | + const instruction = await transferTx.run(); |
| 35 | + assert(transferTx.isSuccess); |
| 36 | + |
| 37 | + // affirm instruction |
| 38 | + const { pending } = await counterParty.getInstructions(); |
| 39 | + const counterInstruction = pending.find(({ id }) => id.eq(instruction.id)); |
| 40 | + assert(counterInstruction, 'the counter party should have the instruction as pending'); |
| 41 | + |
| 42 | + const affirmTx = await counterInstruction.affirm({}, { signingAccount: counterPartyAccount }); |
| 43 | + await affirmTx.run(); |
| 44 | + assert(affirmTx.isSuccess); |
| 45 | + |
| 46 | + const controllerTransferTx = await asset.controllerTransfer({ |
| 47 | + originPortfolio: targetDid, |
| 48 | + amount: new BigNumber(100), |
| 49 | + }); |
| 50 | + await controllerTransferTx.run(); |
| 51 | + |
| 52 | + assert(controllerTransferTx.isSuccess); |
| 53 | + |
| 54 | + const assetHolders = await asset.assetHolders.get(); |
| 55 | + |
| 56 | + const heldByIssuer = assetHolders.data.find(({ identity }) => identity.isEqual(identity)); |
| 57 | + assert(heldByIssuer); |
| 58 | + expect(heldByIssuer.balance.eq(new BigNumber(1100))); |
| 59 | + |
| 60 | + const heldByCounterParty = assetHolders.data.find(({ identity }) => identity.did === targetDid); |
| 61 | + assert(heldByCounterParty); |
| 62 | + expect(heldByCounterParty.balance.eq(new BigNumber(900))); |
| 63 | +}; |
| 64 | + |
| 65 | +/* |
| 66 | + This script showcases VenueFiltering related functionality. It: |
| 67 | + - Creates an asset with initial supply |
| 68 | + - Transfers some of the asset to a new DID |
| 69 | + - Force controller transfer for soma part back to default portfolio |
| 70 | + - Creates new portfolio |
| 71 | + - Force controller transfer to newly created portfolio |
| 72 | +*/ |
| 73 | +export const nonFungibleAssetControllerTransfer = async (sdk: Polymesh, targetDid: string): Promise<void> => { |
| 74 | + const collection = await createNftCollection(sdk, { |
| 75 | + ticker: 'TEST', |
| 76 | + nftType: KnownNftType.Derivative, |
| 77 | + }); |
| 78 | + |
| 79 | + assert(collection); |
| 80 | + |
| 81 | + const [identity, counterParty] = await Promise.all([ |
| 82 | + sdk.getSigningIdentity(), |
| 83 | + sdk.identities.getIdentity({ did: targetDid }), |
| 84 | + ]); |
| 85 | + assert(identity); |
| 86 | + assert(counterParty); |
| 87 | + |
| 88 | + const { account: counterPartyAccount } = await counterParty.getPrimaryAccount(); |
| 89 | + |
| 90 | + const issueTx = await collection.issue({ |
| 91 | + metadata: [ |
| 92 | + { |
| 93 | + type: MetadataType.Local, |
| 94 | + id: new BigNumber(1), |
| 95 | + value: 'https://example.com/nft/1', |
| 96 | + }, |
| 97 | + { |
| 98 | + type: MetadataType.Local, |
| 99 | + id: new BigNumber(2), |
| 100 | + value: '0x35987a0f9ae77012a5146a982966661b75cdeaa4161d1d62b1e18d39438e7396', |
| 101 | + }, |
| 102 | + ], |
| 103 | + }); |
| 104 | + |
| 105 | + const nft = await issueTx.run(); |
| 106 | + |
| 107 | + expect(nft.id).toEqual(new BigNumber(1)); |
| 108 | + |
| 109 | + const issueTx2 = await collection.issue({ |
| 110 | + metadata: [ |
| 111 | + { |
| 112 | + type: MetadataType.Local, |
| 113 | + id: new BigNumber(1), |
| 114 | + value: 'https://example.com/nft/1', |
| 115 | + }, |
| 116 | + { |
| 117 | + type: MetadataType.Local, |
| 118 | + id: new BigNumber(2), |
| 119 | + value: '0x35987a0f9ae77012a5146a982966661b75cdeaa4161d1d62b1e18d39438e7396', |
| 120 | + }, |
| 121 | + ], |
| 122 | + }); |
| 123 | + |
| 124 | + const nft2 = await issueTx2.run(); |
| 125 | + |
| 126 | + const transferTx = await sdk.settlements.addInstruction({ |
| 127 | + legs: [ |
| 128 | + { asset: collection, nfts: [nft, nft2], from: identity, to: targetDid }, |
| 129 | + ], |
| 130 | + }); |
| 131 | + const instruction = await transferTx.run(); |
| 132 | + assert(transferTx.isSuccess); |
| 133 | + |
| 134 | + // affirm instruction |
| 135 | + const { pending } = await counterParty.getInstructions(); |
| 136 | + const counterInstruction = pending.find(({ id }) => id.eq(instruction.id)); |
| 137 | + assert(counterInstruction, 'the counter party should have the instruction as pending'); |
| 138 | + |
| 139 | + const affirmTx = await counterInstruction.affirm({}, { signingAccount: counterPartyAccount }); |
| 140 | + await affirmTx.run(); |
| 141 | + assert(affirmTx.isSuccess); |
| 142 | + |
| 143 | + const controllerTransferTx = await collection.controllerTransfer({ |
| 144 | + originPortfolio: targetDid, |
| 145 | + nfts: [nft], |
| 146 | + }); |
| 147 | + await controllerTransferTx.run(); |
| 148 | + |
| 149 | + assert(controllerTransferTx.isSuccess); |
| 150 | + |
| 151 | + const assetHolders = await collection.assetHolders.get({}); |
| 152 | + |
| 153 | + let heldByIssuer = assetHolders.data.find(({ identity }) => identity.isEqual(identity)); |
| 154 | + assert(heldByIssuer); |
| 155 | + expect(heldByIssuer.nfts.length).toEqual(1); |
| 156 | + expect(heldByIssuer.nfts[0].id.eq(nft.id)); |
| 157 | + |
| 158 | + let heldByCounterParty = assetHolders.data.find(({ identity }) => identity.did === targetDid); |
| 159 | + assert(heldByCounterParty); |
| 160 | + expect(heldByCounterParty.nfts.length).toEqual(1); |
| 161 | + expect(heldByCounterParty.nfts[0].id.eq(nft2.id)); |
| 162 | + |
| 163 | + const createPortfolioTx = await sdk.identities.createPortfolio({ |
| 164 | + name: 'PORTFOLIO', |
| 165 | + }); |
| 166 | + const portfolio = await createPortfolioTx.run(); |
| 167 | + assert(createPortfolioTx.isSuccess); |
| 168 | + |
| 169 | + const controllerTransferTx2 = await collection.controllerTransfer({ originPortfolio: { identity, id: portfolio.id }, nfts: [nft2] }); |
| 170 | + await controllerTransferTx2.run(); |
| 171 | + |
| 172 | + assert(controllerTransferTx2.isSuccess); |
| 173 | + |
| 174 | + const assetHolders2 = await collection.assetHolders.get({}); |
| 175 | + |
| 176 | + heldByIssuer = assetHolders2.data.find(({ identity }) => identity.isEqual(identity)); |
| 177 | + assert(heldByIssuer); |
| 178 | + expect(heldByIssuer.nfts.length).toEqual(2); |
| 179 | + |
| 180 | + heldByCounterParty = assetHolders2.data.find(({ identity }) => identity.did === targetDid); |
| 181 | + expect(heldByCounterParty).toBeUndefined(); |
| 182 | + |
| 183 | + const portfolioCollections = await portfolio.getCollections({ collections: [collection] }); |
| 184 | + |
| 185 | + expect(portfolioCollections.length).toEqual(1); |
| 186 | + expect(portfolioCollections[0].collection.id).toEqual(collection.id); |
| 187 | + expect(portfolioCollections[0].free.length).toEqual(1); |
| 188 | + expect(portfolioCollections[0].free[0].id).toEqual(nft2.id); |
| 189 | +}; |
0 commit comments