1234567891011121314151617import {IKeyringPair} from '@polkadot/types/types';18import {itSub, Pallets, requirePalletsOrSkip, usingPlaygrounds, expect} from './util';1920const MAX_REFUNGIBLE_PIECES = 1_000_000_000_000_000_000_000n;2122describe('integration test: Refungible functionality:', () => {23 let donor: IKeyringPair;24 let alice: IKeyringPair;25 let bob: IKeyringPair;2627 before(async function() {28 await usingPlaygrounds(async (helper, privateKey) => {29 requirePalletsOrSkip(this, helper, [Pallets.ReFungible]);3031 donor = await privateKey({filename: __filename});32 [alice, bob] = await helper.arrange.createAccounts([100n, 10n], donor);33 });34 });35 36 itSub('Create refungible collection and token', async ({helper}) => {37 const collection = await helper.rft.mintCollection(alice, {name: 'test', description: 'test', tokenPrefix: 'test'});3839 const itemCountBefore = await collection.getLastTokenId();40 const token = await collection.mintToken(alice, 100n);41 42 const itemCountAfter = await collection.getLastTokenId();43 44 45 expect(token?.tokenId).to.be.gte(itemCountBefore);46 expect(itemCountAfter).to.be.equal(itemCountBefore + 1);47 expect(itemCountAfter.toString()).to.be.equal(token?.tokenId.toString());48 });49 50 itSub('Checking RPC methods when interacting with maximum allowed values (MAX_REFUNGIBLE_PIECES)', async ({helper}) => {51 const collection = await helper.rft.mintCollection(alice, {name: 'test', description: 'test', tokenPrefix: 'test'});52 53 const token = await collection.mintToken(alice, MAX_REFUNGIBLE_PIECES);54 55 expect(await collection.getTokenBalance(token.tokenId, {Substrate: alice.address})).to.be.equal(MAX_REFUNGIBLE_PIECES);56 57 await collection.transferToken(alice, token.tokenId, {Substrate: bob.address}, MAX_REFUNGIBLE_PIECES);58 expect(await collection.getTokenBalance(token.tokenId, {Substrate: bob.address})).to.be.equal(MAX_REFUNGIBLE_PIECES);59 expect(await token.getTotalPieces()).to.be.equal(MAX_REFUNGIBLE_PIECES);60 61 await expect(collection.mintToken(alice, MAX_REFUNGIBLE_PIECES + 1n))62 .to.eventually.be.rejectedWith(/refungible\.WrongRefungiblePieces/);63 });64 65 itSub('RPC method tokenOwners for refungible collection and token', async ({helper}) => {66 const ethAcc = {Ethereum: '0x67fb3503a61b284dc83fa96dceec4192db47dc7c'};67 const facelessCrowd = (await helper.arrange.createAccounts(Array(7).fill(0n), donor)).map(keyring => {return {Substrate: keyring.address};});6869 const collection = await helper.rft.mintCollection(alice, {name: 'test', description: 'test', tokenPrefix: 'test'});7071 const token = await collection.mintToken(alice, 10_000n);7273 await token.transfer(alice, {Substrate: bob.address}, 1000n);74 await token.transfer(alice, ethAcc, 900n);75 76 for (let i = 0; i < 7; i++) {77 await token.transfer(alice, facelessCrowd[i], 50n * BigInt(i + 1));78 } 7980 const owners = await token.getTop10Owners();8182 83 expect(owners).to.deep.include.members([{Substrate: alice.address}, ethAcc, {Substrate: bob.address}, ...facelessCrowd]);84 expect(owners.length).to.be.equal(10);85 86 const [eleven] = await helper.arrange.createAccounts([0n], donor);87 expect(await token.transfer(alice, {Substrate: eleven.address}, 10n)).to.be.true;88 expect((await token.getTop10Owners()).length).to.be.equal(10);89 });90 91 itSub('Transfer token pieces', async ({helper}) => {92 const collection = await helper.rft.mintCollection(alice, {name: 'test', description: 'test', tokenPrefix: 'test'});93 const token = await collection.mintToken(alice, 100n);9495 expect(await token.getBalance({Substrate: alice.address})).to.be.equal(100n);96 expect(await token.transfer(alice, {Substrate: bob.address}, 60n)).to.be.true;97 98 expect(await token.getBalance({Substrate: alice.address})).to.be.equal(40n);99 expect(await token.getBalance({Substrate: bob.address})).to.be.equal(60n);100 101 await expect(token.transfer(alice, {Substrate: bob.address}, 41n))102 .to.eventually.be.rejectedWith(/common\.TokenValueTooLow/);103 });104105 itSub('Create multiple tokens', async ({helper}) => {106 const collection = await helper.rft.mintCollection(alice, {name: 'test', description: 'test', tokenPrefix: 'test'});107 108 109 110 111 112 113 await helper.rft.mintMultipleTokensWithOneOwner(alice, collection.collectionId, {Substrate: alice.address}, [114 {pieces: 1n}, 115 {pieces: 2n}, 116 {pieces: 100n},117 ]);118 const lastTokenId = await collection.getLastTokenId();119 expect(lastTokenId).to.be.equal(3);120 expect(await collection.getTokenBalance(lastTokenId, {Substrate: alice.address})).to.be.equal(100n);121 });122123 itSub('Burn some pieces', async ({helper}) => {124 const collection = await helper.rft.mintCollection(alice, {name: 'test', description: 'test', tokenPrefix: 'test'});125 const token = await collection.mintToken(alice, 100n);126 expect(await collection.doesTokenExist(token.tokenId)).to.be.true;127 expect(await token.getBalance({Substrate: alice.address})).to.be.equal(100n);128 expect(await token.burn(alice, 99n)).to.be.true;129 expect(await collection.doesTokenExist(token.tokenId)).to.be.true;130 expect(await token.getBalance({Substrate: alice.address})).to.be.equal(1n);131 });132133 itSub('Burn all pieces', async ({helper}) => {134 const collection = await helper.rft.mintCollection(alice, {name: 'test', description: 'test', tokenPrefix: 'test'});135 const token = await collection.mintToken(alice, 100n);136 137 expect(await collection.doesTokenExist(token.tokenId)).to.be.true;138 expect(await token.getBalance({Substrate: alice.address})).to.be.equal(100n);139140 expect(await token.burn(alice, 100n)).to.be.true;141 expect(await collection.doesTokenExist(token.tokenId)).to.be.false;142 });143144 itSub('Burn some pieces for multiple users', async ({helper}) => {145 const collection = await helper.rft.mintCollection(alice, {name: 'test', description: 'test', tokenPrefix: 'test'});146 const token = await collection.mintToken(alice, 100n);147148 expect(await collection.doesTokenExist(token.tokenId)).to.be.true;149 150 expect(await token.getBalance({Substrate: alice.address})).to.be.equal(100n);151 expect(await token.transfer(alice, {Substrate: bob.address}, 60n)).to.be.true;152153 expect(await token.getBalance({Substrate: alice.address})).to.be.equal(40n);154 expect(await token.getBalance({Substrate: bob.address})).to.be.equal(60n);155156 expect(await token.burn(alice, 40n)).to.be.true;157158 expect(await collection.doesTokenExist(token.tokenId)).to.be.true;159 expect(await token.getBalance({Substrate: alice.address})).to.be.equal(0n);160161 expect(await token.burn(bob, 59n)).to.be.true;162163 expect(await token.getBalance({Substrate: bob.address})).to.be.equal(1n);164 expect(await collection.doesTokenExist(token.tokenId)).to.be.true;165166 expect(await token.burn(bob, 1n)).to.be.true;167168 expect(await collection.doesTokenExist(token.tokenId)).to.be.false;169 });170171 itSub('Set allowance for token', async ({helper}) => {172 const collection = await helper.rft.mintCollection(alice, {name: 'test', description: 'test', tokenPrefix: 'test'});173 const token = await collection.mintToken(alice, 100n);174 175 expect(await token.getBalance({Substrate: alice.address})).to.be.equal(100n);176177 expect(await token.approve(alice, {Substrate: bob.address}, 60n)).to.be.true;178 expect(await token.getApprovedPieces({Substrate: alice.address}, {Substrate: bob.address})).to.be.equal(60n);179180 expect(await token.transferFrom(bob, {Substrate: alice.address}, {Substrate: bob.address}, 20n)).to.be.true;181 expect(await token.getBalance({Substrate: alice.address})).to.be.equal(80n);182 expect(await token.getBalance({Substrate: bob.address})).to.be.equal(20n);183 expect(await token.getApprovedPieces({Substrate: alice.address}, {Substrate: bob.address})).to.be.equal(40n);184 });185186 itSub('Repartition', async ({helper}) => {187 const collection = await helper.rft.mintCollection(alice, {name: 'test', description: 'test', tokenPrefix: 'test'});188 const token = await collection.mintToken(alice, 100n);189190 expect(await token.repartition(alice, 200n)).to.be.true;191 expect(await token.getBalance({Substrate: alice.address})).to.be.equal(200n);192 expect(await token.getTotalPieces()).to.be.equal(200n);193 194 expect(await token.transfer(alice, {Substrate: bob.address}, 110n)).to.be.true;195 expect(await token.getBalance({Substrate: alice.address})).to.be.equal(90n);196 expect(await token.getBalance({Substrate: bob.address})).to.be.equal(110n);197 198 await expect(token.repartition(alice, 80n))199 .to.eventually.be.rejectedWith(/refungible\.RepartitionWhileNotOwningAllPieces/);200 201 expect(await token.transfer(alice, {Substrate: bob.address}, 90n)).to.be.true;202 expect(await token.getBalance({Substrate: alice.address})).to.be.equal(0n);203 expect(await token.getBalance({Substrate: bob.address})).to.be.equal(200n);204205 expect(await token.repartition(bob, 150n)).to.be.true;206 await expect(token.transfer(bob, {Substrate: alice.address}, 160n))207 .to.eventually.be.rejectedWith(/common\.TokenValueTooLow/);208 });209210 itSub('Repartition with increased amount', async ({helper}) => {211 const collection = await helper.rft.mintCollection(alice, {name: 'test', description: 'test', tokenPrefix: 'test'});212 const token = await collection.mintToken(alice, 100n);213 await token.repartition(alice, 200n);214 const chainEvents = helper.chainLog.slice(-1)[0].events;215 const event = chainEvents.find((event: any) => event.section === 'common' && event.method === 'ItemCreated');216 expect(event).to.deep.include({217 section: 'common',218 method: 'ItemCreated',219 index: [66, 2],220 data: [221 collection.collectionId,222 token.tokenId,223 {substrate: alice.address}, 224 100n,225 ],226 });227 });228229 itSub('Repartition with decreased amount', async ({helper}) => {230 const collection = await helper.rft.mintCollection(alice, {name: 'test', description: 'test', tokenPrefix: 'test'});231 const token = await collection.mintToken(alice, 100n);232 await token.repartition(alice, 50n);233 const chainEvents = helper.chainLog.slice(-1)[0].events;234 const event = chainEvents.find((event: any) => event.section === 'common' && event.method === 'ItemDestroyed');235 expect(event).to.deep.include({236 section: 'common',237 method: 'ItemDestroyed',238 index: [66, 3],239 data: [240 collection.collectionId,241 token.tokenId,242 {substrate: alice.address}, 243 50n,244 ],245 });246 });247 248 itSub('Create new collection with properties', async ({helper}) => {249 const properties = [{key: 'key1', value: 'val1'}];250 const tokenPropertyPermissions = [{key: 'key1', permission: {tokenOwner: true, mutable: false, collectionAdmin: true}}];251 const collection = await helper.rft.mintCollection(alice, {name: 'test', description: 'test', tokenPrefix: 'test', properties, tokenPropertyPermissions});252 const info = await collection.getData();253 expect(info?.raw.properties).to.be.deep.equal(properties);254 expect(info?.raw.tokenPropertyPermissions).to.be.deep.equal(tokenPropertyPermissions);255 });256});257258describe('Refungible negative tests', () => {259 let donor: IKeyringPair;260 let alice: IKeyringPair;261 let bob: IKeyringPair;262 let charlie: IKeyringPair;263264 before(async function() {265 await usingPlaygrounds(async (helper, privateKey) => {266 requirePalletsOrSkip(this, helper, [Pallets.ReFungible]);267268 donor = await privateKey({filename: __filename});269 [alice, bob, charlie] = await helper.arrange.createAccounts([100n, 100n, 100n], donor);270 });271 });272273 itSub('Cannot transfer incorrect amount of token pieces', async ({helper}) => {274 const collection = await helper.rft.mintCollection(alice, {name: 'test', description: 'test', tokenPrefix: 'test'});275 const tokenAlice = await collection.mintToken(alice, 10n, {Substrate: alice.address});276 const tokenBob = await collection.mintToken(alice, 10n, {Substrate: bob.address});277278 279 await expect(tokenBob.transfer(alice, {Substrate: charlie.address}, 0n)).to.be.rejected;280 await expect(tokenBob.transfer(alice, {Substrate: charlie.address}, 1n)).to.be.rejected;281 await expect(tokenBob.transfer(alice, {Substrate: charlie.address}, 10n)).to.be.rejected;282 await expect(tokenBob.transfer(alice, {Substrate: charlie.address}, 100n)).to.be.rejected;283 284 285 await expect(collection.transferToken(alice, 100, {Substrate: charlie.address}, 0n)).to.be.rejected;286 await expect(collection.transferToken(alice, 100, {Substrate: charlie.address}, 1n)).to.be.rejected;287288 289 await expect(tokenAlice.transfer(alice, {Substrate: charlie.address}, 0n)).to.be.rejectedWith('common.ZeroTransferNotAllowed');290291 expect(await tokenAlice.getTop10Owners()).to.deep.eq([{Substrate: alice.address}]);292 expect(await tokenBob.getTop10Owners()).to.deep.eq([{Substrate: bob.address}]);293 expect(await tokenAlice.getBalance({Substrate: alice.address})).to.eq(10n);294 expect(await tokenBob.getBalance({Substrate: bob.address})).to.eq(10n);295 expect(await tokenBob.getBalance({Substrate: charlie.address})).to.eq(0n);296 });297});