1234567891011121314151617import {Pallets, requirePalletsOrSkip} from '../util';18import {expect, itEth, usingEthPlaygrounds} from './util';19import {IKeyringPair} from '@polkadot/types/types';2021describe('Refungible: Information getting', () => {22 let donor: IKeyringPair;2324 before(async function() {25 await usingEthPlaygrounds(async (helper, privateKey) => {26 requirePalletsOrSkip(this, helper, [Pallets.ReFungible]);2728 donor = await privateKey({filename: __filename});29 });30 });3132 itEth('totalSupply', async ({helper}) => {33 const caller = await helper.eth.createAccountWithBalance(donor);34 const {collectionAddress} = await helper.eth.createRFTCollection(caller, 'TotalSupply', '6', '6');35 const contract = helper.ethNativeContract.collection(collectionAddress, 'rft', caller);3637 await contract.methods.mint(caller).send();3839 const totalSupply = await contract.methods.totalSupply().call();40 expect(totalSupply).to.equal('1');41 });4243 itEth('balanceOf', async ({helper}) => {44 const caller = await helper.eth.createAccountWithBalance(donor);45 const {collectionAddress} = await helper.eth.createRFTCollection(caller, 'BalanceOf', '6', '6');46 const contract = helper.ethNativeContract.collection(collectionAddress, 'rft', caller);4748 await contract.methods.mint(caller).send();49 await contract.methods.mint(caller).send();50 await contract.methods.mint(caller).send();5152 const balance = await contract.methods.balanceOf(caller).call();53 expect(balance).to.equal('3');54 });5556 itEth('ownerOf', async ({helper}) => {57 const caller = await helper.eth.createAccountWithBalance(donor);58 const {collectionAddress} = await helper.eth.createRFTCollection(caller, 'OwnerOf', '6', '6');59 const contract = helper.ethNativeContract.collection(collectionAddress, 'rft', caller);6061 const result = await contract.methods.mint(caller).send();62 const tokenId = result.events.Transfer.returnValues.tokenId;6364 const owner = await contract.methods.ownerOf(tokenId).call();65 expect(owner).to.equal(caller);66 });6768 itEth('ownerOf after burn', async ({helper}) => {69 const caller = await helper.eth.createAccountWithBalance(donor);70 const receiver = helper.eth.createAccount();71 const {collectionId, collectionAddress} = await helper.eth.createRFTCollection(caller, 'OwnerOf-AfterBurn', '6', '6');72 const contract = helper.ethNativeContract.collection(collectionAddress, 'rft', caller);7374 const result = await contract.methods.mint(caller).send();75 const tokenId = result.events.Transfer.returnValues.tokenId;76 const tokenContract = helper.ethNativeContract.rftTokenById(collectionId, tokenId, caller);7778 await tokenContract.methods.repartition(2).send();79 await tokenContract.methods.transfer(receiver, 1).send();8081 await tokenContract.methods.burnFrom(caller, 1).send();8283 const owner = await contract.methods.ownerOf(tokenId).call();84 expect(owner).to.equal(receiver);85 });8687 itEth('ownerOf for partial ownership', async ({helper}) => {88 const caller = await helper.eth.createAccountWithBalance(donor);89 const receiver = helper.eth.createAccount();90 const {collectionId, collectionAddress} = await helper.eth.createRFTCollection(caller, 'Partial-OwnerOf', '6', '6');91 const contract = helper.ethNativeContract.collection(collectionAddress, 'rft', caller);9293 const result = await contract.methods.mint(caller).send();94 const tokenId = result.events.Transfer.returnValues.tokenId;95 const tokenContract = helper.ethNativeContract.rftTokenById(collectionId, tokenId, caller);9697 await tokenContract.methods.repartition(2).send();98 await tokenContract.methods.transfer(receiver, 1).send();99100 const owner = await contract.methods.ownerOf(tokenId).call();101 expect(owner).to.equal('0xFFfFfFffFFfffFFfFFfFFFFFffFFFffffFfFFFfF');102 });103});104105describe('Refungible: Plain calls', () => {106 let donor: IKeyringPair;107 let minter: IKeyringPair;108 let bob: IKeyringPair;109 let charlie: IKeyringPair;110111 before(async function() {112 await usingEthPlaygrounds(async (helper, privateKey) => {113 requirePalletsOrSkip(this, helper, [Pallets.ReFungible]);114115 donor = await privateKey({filename: __filename});116 [minter, bob, charlie] = await helper.arrange.createAccounts([100n, 100n, 100n], donor);117 });118 });119120 itEth('Can perform mint()', async ({helper}) => {121 const owner = await helper.eth.createAccountWithBalance(donor);122 const receiver = helper.eth.createAccount();123 const {collectionAddress} = await helper.eth.createERC721MetadataCompatibleRFTCollection(owner, 'Minty', '6', '6', '');124 const contract = helper.ethNativeContract.collection(collectionAddress, 'rft', owner);125126 const result = await contract.methods.mintWithTokenURI(receiver, 'Test URI').send();127128 const event = result.events.Transfer;129 expect(event.address).to.equal(collectionAddress);130 expect(event.returnValues.from).to.equal('0x0000000000000000000000000000000000000000');131 expect(event.returnValues.to).to.equal(receiver);132 const tokenId = event.returnValues.tokenId;133 expect(tokenId).to.be.equal('1');134135 expect(await contract.methods.tokenURI(tokenId).call()).to.be.equal('Test URI');136 });137138 itEth.skip('Can perform mintBulk()', async ({helper}) => {139 const owner = await helper.eth.createAccountWithBalance(donor);140 const receiver = helper.eth.createAccount();141 const {collectionAddress} = await helper.eth.createERC721MetadataCompatibleRFTCollection(owner, 'MintBulky', '6', '6', '');142 const contract = helper.ethNativeContract.collection(collectionAddress, 'rft', owner);143144 {145 const nextTokenId = await contract.methods.nextTokenId().call();146 expect(nextTokenId).to.be.equal('1');147 const result = await contract.methods.mintBulkWithTokenURI(148 receiver,149 [150 [nextTokenId, 'Test URI 0'],151 [+nextTokenId + 1, 'Test URI 1'],152 [+nextTokenId + 2, 'Test URI 2'],153 ],154 ).send();155156 const events = result.events.Transfer;157 for (let i = 0; i < 2; i++) {158 const event = events[i];159 expect(event.address).to.equal(collectionAddress);160 expect(event.returnValues.from).to.equal('0x0000000000000000000000000000000000000000');161 expect(event.returnValues.to).to.equal(receiver);162 expect(event.returnValues.tokenId).to.equal(String(+nextTokenId + i));163 }164165 expect(await contract.methods.tokenURI(nextTokenId).call()).to.be.equal('Test URI 0');166 expect(await contract.methods.tokenURI(+nextTokenId + 1).call()).to.be.equal('Test URI 1');167 expect(await contract.methods.tokenURI(+nextTokenId + 2).call()).to.be.equal('Test URI 2');168 }169 });170171 itEth('Can perform burn()', async ({helper}) => {172 const caller = await helper.eth.createAccountWithBalance(donor);173 const {collectionAddress} = await helper.eth.createRFTCollection(caller, 'Burny', '6', '6');174 const contract = helper.ethNativeContract.collection(collectionAddress, 'rft', caller);175176 const result = await contract.methods.mint(caller).send();177 const tokenId = result.events.Transfer.returnValues.tokenId;178 {179 const result = await contract.methods.burn(tokenId).send();180 const event = result.events.Transfer;181 expect(event.address).to.equal(collectionAddress);182 expect(event.returnValues.from).to.equal(caller);183 expect(event.returnValues.to).to.equal('0x0000000000000000000000000000000000000000');184 expect(event.returnValues.tokenId).to.equal(tokenId.toString());185 }186 });187188 itEth('Can perform transferFrom()', async ({helper}) => {189 const caller = await helper.eth.createAccountWithBalance(donor);190 const receiver = helper.eth.createAccount();191 const {collectionId, collectionAddress} = await helper.eth.createRFTCollection(caller, 'TransferFromy', '6', '6');192 const contract = helper.ethNativeContract.collection(collectionAddress, 'rft', caller);193194 const result = await contract.methods.mint(caller).send();195 const tokenId = result.events.Transfer.returnValues.tokenId;196197 const tokenAddress = helper.ethAddress.fromTokenId(collectionId, tokenId);198199 const tokenContract = helper.ethNativeContract.rftToken(tokenAddress, caller);200 await tokenContract.methods.repartition(15).send();201202 {203 const tokenEvents: any = [];204 tokenContract.events.allEvents((_: any, event: any) => {205 tokenEvents.push(event);206 });207 const result = await contract.methods.transferFrom(caller, receiver, tokenId).send();208 if (tokenEvents.length == 0) await helper.wait.newBlocks(1);209210 let event = result.events.Transfer;211 expect(event.address).to.equal(collectionAddress);212 expect(event.returnValues.from).to.equal(caller);213 expect(event.returnValues.to).to.equal(receiver);214 expect(event.returnValues.tokenId).to.equal(tokenId.toString());215216 event = tokenEvents[0];217 expect(event.address).to.equal(tokenAddress);218 expect(event.returnValues.from).to.equal(caller);219 expect(event.returnValues.to).to.equal(receiver);220 expect(event.returnValues.value).to.equal('15');221 }222223 {224 const balance = await contract.methods.balanceOf(receiver).call();225 expect(+balance).to.equal(1);226 }227228 {229 const balance = await contract.methods.balanceOf(caller).call();230 expect(+balance).to.equal(0);231 }232 });233234 235 itEth('Can perform burnFrom()', async ({helper}) => {236 const collection = await helper.rft.mintCollection(minter, {name: 'A', description: 'B', tokenPrefix: 'C'});237238 const owner = await helper.eth.createAccountWithBalance(donor, 100n);239 const spender = await helper.eth.createAccountWithBalance(donor, 100n);240241 const token = await collection.mintToken(minter, 100n, {Ethereum: owner});242243 const address = helper.ethAddress.fromCollectionId(collection.collectionId);244 const contract = helper.ethNativeContract.collection(address, 'rft', spender, true);245246 const tokenAddress = helper.ethAddress.fromTokenId(collection.collectionId, token.tokenId);247 const tokenContract = helper.ethNativeContract.rftToken(tokenAddress, owner);248 await tokenContract.methods.repartition(15).send();249 await tokenContract.methods.approve(spender, 15).send();250251 {252 const result = await contract.methods.burnFrom(owner, token.tokenId).send();253 const event = result.events.Transfer;254 expect(event).to.be.like({255 address: helper.ethAddress.fromCollectionId(collection.collectionId),256 event: 'Transfer',257 returnValues: {258 from: owner,259 to: '0x0000000000000000000000000000000000000000',260 tokenId: token.tokenId.toString(),261 },262 });263 }264265 expect(await collection.getTokenBalance(token.tokenId, {Ethereum: owner})).to.be.eq(0n);266 });267268 itEth('Can perform burnFromCross()', async ({helper}) => {269 const collection = await helper.rft.mintCollection(minter, {name: 'A', description: 'B', tokenPrefix: 'C'});270 271 const owner = bob;272 const spender = await helper.eth.createAccountWithBalance(donor, 100n);273274 const token = await collection.mintToken(minter, 100n, {Substrate: owner.address});275276 const address = helper.ethAddress.fromCollectionId(collection.collectionId);277 const contract = helper.ethNativeContract.collection(address, 'rft');278279 await token.repartition(owner, 15n);280 await token.approve(owner, {Ethereum: spender}, 15n);281282 {283 const ownerCross = helper.ethCrossAccount.fromKeyringPair(owner);284 const result = await contract.methods.burnFromCross(ownerCross, token.tokenId).send({from: spender});285 const event = result.events.Transfer;286 expect(event).to.be.like({287 address: helper.ethAddress.fromCollectionId(collection.collectionId),288 event: 'Transfer',289 returnValues: {290 from: helper.address.substrateToEth(owner.address),291 to: '0x0000000000000000000000000000000000000000',292 tokenId: token.tokenId.toString(),293 },294 });295 }296297 expect(await collection.getTokenBalance(token.tokenId, {Substrate: owner.address})).to.be.eq(0n);298 });299300 itEth('Can perform transferFromCross()', async ({helper}) => {301 const collection = await helper.rft.mintCollection(minter, {name: 'A', description: 'B', tokenPrefix: 'C'});302303 const owner = bob;304 const spender = await helper.eth.createAccountWithBalance(donor, 100n);305 const receiver = charlie;306307 const token = await collection.mintToken(minter, 100n, {Substrate: owner.address});308309 const address = helper.ethAddress.fromCollectionId(collection.collectionId);310 const contract = helper.ethNativeContract.collection(address, 'rft');311312 await token.repartition(owner, 15n);313 await token.approve(owner, {Ethereum: spender}, 15n);314315 {316 const ownerCross = helper.ethCrossAccount.fromKeyringPair(owner);317 const recieverCross = helper.ethCrossAccount.fromKeyringPair(receiver);318 const result = await contract.methods.transferFromCross(ownerCross, recieverCross, token.tokenId).send({from: spender});319 const event = result.events.Transfer;320 expect(event).to.be.like({321 address: helper.ethAddress.fromCollectionId(collection.collectionId),322 event: 'Transfer',323 returnValues: {324 from: helper.address.substrateToEth(owner.address),325 to: helper.address.substrateToEth(receiver.address),326 tokenId: token.tokenId.toString(),327 },328 });329 }330331 expect(await token.getTop10Owners()).to.be.like([{Substrate: receiver.address}]);332 });333334 itEth('Can perform transfer()', async ({helper}) => {335 const caller = await helper.eth.createAccountWithBalance(donor);336 const receiver = helper.eth.createAccount();337 const {collectionAddress} = await helper.eth.createRFTCollection(caller, 'Transferry', '6', '6');338 const contract = helper.ethNativeContract.collection(collectionAddress, 'rft', caller);339340 const result = await contract.methods.mint(caller).send();341 const tokenId = result.events.Transfer.returnValues.tokenId;342343 {344 const result = await contract.methods.transfer(receiver, tokenId).send();345346 const event = result.events.Transfer;347 expect(event.address).to.equal(collectionAddress);348 expect(event.returnValues.from).to.equal(caller);349 expect(event.returnValues.to).to.equal(receiver);350 expect(event.returnValues.tokenId).to.equal(tokenId.toString());351 }352353 {354 const balance = await contract.methods.balanceOf(caller).call();355 expect(+balance).to.equal(0);356 }357358 {359 const balance = await contract.methods.balanceOf(receiver).call();360 expect(+balance).to.equal(1);361 }362 });363 364 itEth('Can perform transferCross()', async ({helper}) => {365 const caller = await helper.eth.createAccountWithBalance(donor);366 const receiver = await helper.eth.createAccountWithBalance(donor);367 const to = helper.ethCrossAccount.fromAddress(receiver);368 const toSubstrate = helper.ethCrossAccount.fromKeyringPair(minter);369 const collection = await helper.rft.mintCollection(minter, {});370 const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);371 const contract = helper.ethNativeContract.collection(collectionAddress, 'rft', caller);372373 const {tokenId} = await collection.mintToken(minter, 1n, {Ethereum: caller});374375 {376 const result = await contract.methods.transferCross(to, tokenId).send({from: caller});377378 const event = result.events.Transfer;379 expect(event.address).to.equal(collectionAddress);380 expect(event.returnValues.from).to.equal(caller);381 expect(event.returnValues.to).to.equal(receiver);382 expect(event.returnValues.tokenId).to.equal(tokenId.toString());383 }384385 {386 const balance = await contract.methods.balanceOf(caller).call();387 expect(+balance).to.equal(0);388 }389390 {391 const balance = await contract.methods.balanceOf(receiver).call();392 expect(+balance).to.equal(1);393 }394 395 {396 const substrateResult = await contract.methods.transferCross(toSubstrate, tokenId).send({from: receiver});397 398399 const event = substrateResult.events.Transfer;400 expect(event.address).to.be.equal(collectionAddress);401 expect(event.returnValues.from).to.be.equal(receiver);402 expect(event.returnValues.to).to.be.equal(helper.address.substrateToEth(minter.address));403 expect(event.returnValues.tokenId).to.be.equal(`${tokenId}`);404 }405406 {407 const balance = await contract.methods.balanceOf(receiver).call();408 expect(+balance).to.equal(0);409 }410411 {412 const balance = await helper.nft.getTokensByAddress(collection.collectionId, {Substrate: minter.address});413 expect(balance).to.be.contain(tokenId);414 }415 });416417 itEth('transfer event on transfer from partial ownership to full ownership', async ({helper}) => {418 const caller = await helper.eth.createAccountWithBalance(donor);419 const receiver = helper.eth.createAccount();420 const {collectionId, collectionAddress} = await helper.eth.createRFTCollection(caller, 'Transferry-Partial-to-Full', '6', '6');421 const contract = helper.ethNativeContract.collection(collectionAddress, 'rft', caller);422423 const result = await contract.methods.mint(caller).send();424 const tokenId = result.events.Transfer.returnValues.tokenId;425426 const tokenContract = helper.ethNativeContract.rftTokenById(collectionId, tokenId, caller);427428 await tokenContract.methods.repartition(2).send();429 await tokenContract.methods.transfer(receiver, 1).send();430431 const events: any = [];432 contract.events.allEvents((_: any, event: any) => {433 events.push(event);434 });435436 await tokenContract.methods.transfer(receiver, 1).send();437 if (events.length == 0) await helper.wait.newBlocks(1);438 const event = events[0];439440 expect(event.address).to.equal(collectionAddress);441 expect(event.returnValues.from).to.equal('0xFFfFfFffFFfffFFfFFfFFFFFffFFFffffFfFFFfF');442 expect(event.returnValues.to).to.equal(receiver);443 expect(event.returnValues.tokenId).to.equal(tokenId.toString());444 });445446 itEth('transfer event on transfer from full ownership to partial ownership', async ({helper}) => {447 const caller = await helper.eth.createAccountWithBalance(donor);448 const receiver = helper.eth.createAccount();449 const {collectionId, collectionAddress} = await helper.eth.createRFTCollection(caller, 'Transferry-Full-to-Partial', '6', '6');450 const contract = helper.ethNativeContract.collection(collectionAddress, 'rft', caller);451452 const result = await contract.methods.mint(caller).send();453 const tokenId = result.events.Transfer.returnValues.tokenId;454455 const tokenContract = helper.ethNativeContract.rftTokenById(collectionId, tokenId, caller);456457 await tokenContract.methods.repartition(2).send();458459 const events: any = [];460 contract.events.allEvents((_: any, event: any) => {461 events.push(event);462 });463464 await tokenContract.methods.transfer(receiver, 1).send();465 if (events.length == 0) await helper.wait.newBlocks(1);466 const event = events[0];467468 expect(event.address).to.equal(collectionAddress);469 expect(event.returnValues.from).to.equal(caller);470 expect(event.returnValues.to).to.equal('0xFFfFfFffFFfffFFfFFfFFFFFffFFFffffFfFFFfF');471 expect(event.returnValues.tokenId).to.equal(tokenId.toString());472 });473});474475describe('RFT: Fees', () => {476 let donor: IKeyringPair;477478 before(async function() {479 await usingEthPlaygrounds(async (helper, privateKey) => {480 requirePalletsOrSkip(this, helper, [Pallets.ReFungible]);481482 donor = await privateKey({filename: __filename});483 });484 });485486 itEth('transferFrom() call fee is less than 0.2UNQ', async ({helper}) => {487 const caller = await helper.eth.createAccountWithBalance(donor);488 const receiver = helper.eth.createAccount();489 const {collectionAddress} = await helper.eth.createRFTCollection(caller, 'Feeful-Transfer-From', '6', '6');490 const contract = helper.ethNativeContract.collection(collectionAddress, 'rft', caller);491492 const result = await contract.methods.mint(caller).send();493 const tokenId = result.events.Transfer.returnValues.tokenId;494495 const cost = await helper.eth.recordCallFee(caller, () => contract.methods.transferFrom(caller, receiver, tokenId).send());496 expect(cost < BigInt(0.2 * Number(helper.balance.getOneTokenNominal())));497 expect(cost > 0n);498 });499500 itEth('transfer() call fee is less than 0.2UNQ', async ({helper}) => {501 const caller = await helper.eth.createAccountWithBalance(donor);502 const receiver = helper.eth.createAccount();503 const {collectionAddress} = await helper.eth.createRFTCollection(caller, 'Feeful-Transfer', '6', '6');504 const contract = helper.ethNativeContract.collection(collectionAddress, 'rft', caller);505506 const result = await contract.methods.mint(caller).send();507 const tokenId = result.events.Transfer.returnValues.tokenId;508509 const cost = await helper.eth.recordCallFee(caller, () => contract.methods.transfer(receiver, tokenId).send());510 expect(cost < BigInt(0.2 * Number(helper.balance.getOneTokenNominal())));511 expect(cost > 0n);512 });513});514515describe('Common metadata', () => {516 let donor: IKeyringPair;517 let alice: IKeyringPair;518519 before(async function() {520 await usingEthPlaygrounds(async (helper, privateKey) => {521 requirePalletsOrSkip(this, helper, [Pallets.ReFungible]);522523 donor = await privateKey({filename: __filename});524 [alice] = await helper.arrange.createAccounts([20n], donor);525 });526 });527528 itEth('Returns collection name', async ({helper}) => {529 const caller = helper.eth.createAccount();530 const tokenPropertyPermissions = [{531 key: 'URI',532 permission: {533 mutable: true,534 collectionAdmin: true,535 tokenOwner: false,536 },537 }];538 const collection = await helper.rft.mintCollection(539 alice,540 {541 name: 'Leviathan',542 tokenPrefix: '11',543 properties: [{key: 'ERC721Metadata', value: '1'}],544 tokenPropertyPermissions,545 },546 );547548 const contract = helper.ethNativeContract.collectionById(collection.collectionId, 'rft', caller);549 const name = await contract.methods.name().call();550 expect(name).to.equal('Leviathan');551 });552553 itEth('Returns symbol name', async ({helper}) => {554 const caller = await helper.eth.createAccountWithBalance(donor);555 const tokenPropertyPermissions = [{556 key: 'URI',557 permission: {558 mutable: true,559 collectionAdmin: true,560 tokenOwner: false,561 },562 }];563 const {collectionId} = await helper.rft.mintCollection(564 alice,565 {566 name: 'Leviathan',567 tokenPrefix: '12',568 properties: [{key: 'ERC721Metadata', value: '1'}],569 tokenPropertyPermissions,570 },571 );572573 const contract = helper.ethNativeContract.collectionById(collectionId, 'rft', caller);574 const symbol = await contract.methods.symbol().call();575 expect(symbol).to.equal('12');576 });577});