1234567891011121314151617import {default as usingApi, submitTransactionAsync} from './substrate/substrate-api';18import {IKeyringPair} from '@polkadot/types/types';19import {20 createCollectionExpectSuccess,21 getBalance,22 createMultipleItemsExpectSuccess,23 isTokenExists,24 getLastTokenId,25 getAllowance,26 approve,27 transferFrom,28 createCollection,29 createRefungibleToken,30 transfer,31 burnItem,32 repartitionRFT,33 createCollectionWithPropsExpectSuccess,34 getDetailedCollectionInfo,35 normalizeAccountId,36 CrossAccountId,37 getCreateItemsResult,38 getDestroyItemsResult,39 getModuleNames,40 Pallets,41} from './util/helpers';4243import chai from 'chai';44import chaiAsPromised from 'chai-as-promised';45chai.use(chaiAsPromised);46const expect = chai.expect;4748let alice: IKeyringPair;49let bob: IKeyringPair;50515253describe('integration test: Refungible functionality:', async () => {54 before(async function() {55 await usingApi(async (api, privateKeyWrapper) => {56 alice = privateKeyWrapper('//Alice');57 bob = privateKeyWrapper('//Bob');58 if (!getModuleNames(api).includes(Pallets.ReFungible)) this.skip();59 });60 61 });62 63 it('Create refungible collection and token', async () => {64 await usingApi(async api => {65 const createCollectionResult = await createCollection(api, alice, {mode: {type: 'ReFungible'}});66 expect(createCollectionResult.success).to.be.true;67 const collectionId = createCollectionResult.collectionId;68 69 const itemCountBefore = await getLastTokenId(api, collectionId);70 const result = await createRefungibleToken(api, alice, collectionId, 100n);71 72 const itemCountAfter = await getLastTokenId(api, collectionId);73 74 75 76 expect(result.success).to.be.true;77 expect(itemCountAfter).to.be.equal(itemCountBefore + 1);78 expect(collectionId).to.be.equal(result.collectionId);79 expect(itemCountAfter.toString()).to.be.equal(result.itemId.toString());80 });81 });82 83 it('RPC method tokenOnewrs for refungible collection and token', async () => {84 await usingApi(async (api, privateKeyWrapper) => {85 const ethAcc = {Ethereum: '0x67fb3503a61b284dc83fa96dceec4192db47dc7c'};86 const facelessCrowd = Array.from(Array(7).keys()).map(i => normalizeAccountId(privateKeyWrapper(i.toString())));87 88 const createCollectionResult = await createCollection(api, alice, {mode: {type: 'ReFungible'}});89 const collectionId = createCollectionResult.collectionId;90 91 const result = await createRefungibleToken(api, alice, collectionId, 10_000n);92 const aliceTokenId = result.itemId;93 94 95 await transfer(api, collectionId, aliceTokenId, alice, bob, 1000n);96 await transfer(api, collectionId, aliceTokenId, alice, ethAcc, 900n);97 98 for (let i = 0; i < 7; i++) {99 await transfer(api, collectionId, aliceTokenId, alice, facelessCrowd[i], 50*(i+1));100 } 101 102 const owners = await api.rpc.unique.tokenOwners(collectionId, aliceTokenId);103 const ids = (owners.toJSON() as CrossAccountId[]).map(s => normalizeAccountId(s));104 105 const aliceID = normalizeAccountId(alice);106 const bobId = normalizeAccountId(bob);107 108 109 110 expect(ids).to.deep.include.members([aliceID, ethAcc, bobId, ...facelessCrowd]);111 expect(owners.length).to.be.equal(10);112 113 const eleven = privateKeyWrapper('11');114 expect(await transfer(api, collectionId, aliceTokenId, alice, eleven, 10n)).to.be.true;115 expect((await api.rpc.unique.tokenOwners(collectionId, aliceTokenId)).length).to.be.equal(10);116 });117 });118 119 it('Transfer token pieces', async () => {120 await usingApi(async api => {121 const collectionId = (await createCollection(api, alice, {mode: {type: 'ReFungible'}})).collectionId;122 const tokenId = (await createRefungibleToken(api, alice, collectionId, 100n)).itemId;123124 expect(await getBalance(api, collectionId, alice, tokenId)).to.be.equal(100n);125 expect(await transfer(api, collectionId, tokenId, alice, bob, 60n)).to.be.true;126127 expect(await getBalance(api, collectionId, alice, tokenId)).to.be.equal(40n);128 expect(await getBalance(api, collectionId, bob, tokenId)).to.be.equal(60n);129 await expect(transfer(api, collectionId, tokenId, alice, bob, 41n)).to.eventually.be.rejected;130 });131 });132133 it('Create multiple tokens', async () => {134 const collectionId = await createCollectionExpectSuccess({mode: {type: 'ReFungible'}});135 const args = [136 {ReFungible: {pieces: 1}},137 {ReFungible: {pieces: 2}},138 {ReFungible: {pieces: 100}},139 ];140 await createMultipleItemsExpectSuccess(alice, collectionId, args);141142 await usingApi(async api => { 143 const tokenId = await getLastTokenId(api, collectionId);144 expect(tokenId).to.be.equal(3);145 expect(await getBalance(api, collectionId, alice, tokenId)).to.be.equal(100n);146 });147 });148149 it('Burn some pieces', async () => {150 await usingApi(async api => { 151 const collectionId = (await createCollection(api, alice, {mode: {type: 'ReFungible'}})).collectionId;152 const tokenId = (await createRefungibleToken(api, alice, collectionId, 100n)).itemId;153 expect(await isTokenExists(api, collectionId, tokenId)).to.be.true;154 expect(await getBalance(api, collectionId, alice, tokenId)).to.be.equal(100n);155 expect(await burnItem(api, alice, collectionId, tokenId, 99n)).to.be.true;156 expect(await isTokenExists(api, collectionId, tokenId)).to.be.true;157 expect(await getBalance(api, collectionId, alice, tokenId)).to.be.equal(1n);158 });159 });160161 it('Burn all pieces', async () => {162 await usingApi(async api => { 163 const collectionId = (await createCollection(api, alice, {mode: {type: 'ReFungible'}})).collectionId;164 const tokenId = (await createRefungibleToken(api, alice, collectionId, 100n)).itemId;165 expect(await isTokenExists(api, collectionId, tokenId)).to.be.true;166 expect(await getBalance(api, collectionId, alice, tokenId)).to.be.equal(100n);167 expect(await burnItem(api, alice, collectionId, tokenId, 100n)).to.be.true;168 expect(await isTokenExists(api, collectionId, tokenId)).to.be.false;169 });170 });171172 it('Burn some pieces for multiple users', async () => {173 await usingApi(async api => { 174 const collectionId = (await createCollection(api, alice, {mode: {type: 'ReFungible'}})).collectionId;175 const tokenId = (await createRefungibleToken(api, alice, collectionId, 100n)).itemId;176 expect(await isTokenExists(api, collectionId, tokenId)).to.be.true;177178 expect(await getBalance(api, collectionId, alice, tokenId)).to.be.equal(100n);179 expect(await transfer(api, collectionId, tokenId, alice, bob, 60n)).to.be.true;180181 182 expect(await getBalance(api, collectionId, alice, tokenId)).to.be.equal(40n);183 expect(await getBalance(api, collectionId, bob, tokenId)).to.be.equal(60n);184 expect(await burnItem(api, alice, collectionId, tokenId, 40n)).to.be.true;185186 expect(await getBalance(api, collectionId, alice, tokenId)).to.be.equal(0n);187 expect(await isTokenExists(api, collectionId, tokenId)).to.be.true;188 expect(await burnItem(api, bob, collectionId, tokenId, 59n)).to.be.true;189190 expect(await getBalance(api, collectionId, bob, tokenId)).to.be.equal(1n);191 expect(await isTokenExists(api, collectionId, tokenId)).to.be.true;192 expect(await burnItem(api, bob, collectionId, tokenId, 1n)).to.be.true;193 194 expect(await isTokenExists(api, collectionId, tokenId)).to.be.false;195 });196 });197198 it('Set allowance for token', async () => {199 await usingApi(async api => {200 const collectionId = (await createCollection(api, alice, {mode: {type: 'ReFungible'}})).collectionId;201 const tokenId = (await createRefungibleToken(api, alice, collectionId, 100n)).itemId;202203 expect(await getBalance(api, collectionId, alice, tokenId)).to.be.equal(100n);204205 expect(await approve(api, collectionId, tokenId, alice, bob, 60n)).to.be.true;206 expect(await getAllowance(api, collectionId, alice, bob, tokenId)).to.be.equal(60n);207208 expect(await transferFrom(api, collectionId, tokenId, bob, alice, bob, 20n)).to.be.true;209 expect(await getBalance(api, collectionId, alice, tokenId)).to.be.equal(80n);210 expect(await getBalance(api, collectionId, bob, tokenId)).to.be.equal(20n);211 expect(await getAllowance(api, collectionId, alice, bob, tokenId)).to.be.equal(40n);212 });213 });214215 it('Repartition', async () => {216 await usingApi(async api => {217 const collectionId = (await createCollection(api, alice, {mode: {type: 'ReFungible'}})).collectionId;218 const tokenId = (await createRefungibleToken(api, alice, collectionId, 100n)).itemId;219220 expect(await repartitionRFT(api, collectionId, alice, tokenId, 200n)).to.be.true;221 expect(await getBalance(api, collectionId, alice, tokenId)).to.be.equal(200n);222223 expect(await transfer(api, collectionId, tokenId, alice, bob, 110n)).to.be.true;224 expect(await getBalance(api, collectionId, alice, tokenId)).to.be.equal(90n);225 expect(await getBalance(api, collectionId, bob, tokenId)).to.be.equal(110n);226227 await expect(repartitionRFT(api, collectionId, alice, tokenId, 80n)).to.eventually.be.rejected;228229 expect(await transfer(api, collectionId, tokenId, alice, bob, 90n)).to.be.true;230 expect(await getBalance(api, collectionId, alice, tokenId)).to.be.equal(0n);231 expect(await getBalance(api, collectionId, bob, tokenId)).to.be.equal(200n);232233 expect(await repartitionRFT(api, collectionId, bob, tokenId, 150n)).to.be.true;234 await expect(transfer(api, collectionId, tokenId, bob, alice, 160n)).to.eventually.be.rejected;235 });236 });237238 it('Repartition with increased amount', async () => {239 await usingApi(async api => {240 const collectionId = (await createCollection(api, alice, {mode: {type: 'ReFungible'}})).collectionId;241 const tokenId = (await createRefungibleToken(api, alice, collectionId, 100n)).itemId;242243 const tx = api.tx.unique.repartition(collectionId, tokenId, 200n);244 const events = await submitTransactionAsync(alice, tx);245 const substrateEvents = getCreateItemsResult(events);246 expect(substrateEvents).to.include.deep.members([247 {248 success: true,249 collectionId,250 itemId: tokenId,251 recipient: {Substrate: alice.address},252 amount: 100,253 },254 ]);255 });256 });257258 it('Repartition with decreased amount', async () => {259 await usingApi(async api => {260 const collectionId = (await createCollection(api, alice, {mode: {type: 'ReFungible'}})).collectionId;261 const tokenId = (await createRefungibleToken(api, alice, collectionId, 100n)).itemId;262263 const tx = api.tx.unique.repartition(collectionId, tokenId, 50n);264 const events = await submitTransactionAsync(alice, tx);265 const substrateEvents = getDestroyItemsResult(events);266 expect(substrateEvents).to.include.deep.members([267 {268 success: true,269 collectionId,270 itemId: tokenId,271 owner: {Substrate: alice.address},272 amount: 50,273 },274 ]);275 });276 });277 278 it('Сreate new collection with properties', async () => {279 await usingApi(async api => {280 const properties = [{key: 'key1', value: 'val1'}];281 const propertyPermissions = [{key: 'key1', permission: {tokenOwner: true, mutable: false, collectionAdmin: true}}];282 const collectionId = await createCollectionWithPropsExpectSuccess({name: 'A', description: 'B', tokenPrefix: 'C', mode: {type: 'ReFungible'},283 properties: properties,284 propPerm: propertyPermissions, 285 });286 const collection = (await getDetailedCollectionInfo(api, collectionId))!;287 expect(collection.properties.toHuman()).to.be.deep.equal(properties);288 expect(collection.tokenPropertyPermissions.toHuman()).to.be.deep.equal(propertyPermissions);289 });290 });291});292