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 itEth('Can perform burnFrom()', async ({helper}) => {235 const collection = await helper.rft.mintCollection(minter, {name: 'A', description: 'B', tokenPrefix: 'C'});236237 const owner = await helper.eth.createAccountWithBalance(donor, 100n);238 const spender = await helper.eth.createAccountWithBalance(donor, 100n);239240 const token = await collection.mintToken(minter, 100n, {Ethereum: owner});241242 const address = helper.ethAddress.fromCollectionId(collection.collectionId);243 const contract = helper.ethNativeContract.collection(address, 'rft');244245 const tokenAddress = helper.ethAddress.fromTokenId(collection.collectionId, token.tokenId);246 const tokenContract = helper.ethNativeContract.rftToken(tokenAddress, owner);247 await tokenContract.methods.repartition(15).send();248 await tokenContract.methods.approve(spender, 15).send();249250 {251 const result = await contract.methods.burnFrom(owner, token.tokenId).send({from: spender});252 const event = result.events.Transfer;253 expect(event).to.be.like({254 address: helper.ethAddress.fromCollectionId(collection.collectionId),255 event: 'Transfer',256 returnValues: {257 from: owner,258 to: '0x0000000000000000000000000000000000000000',259 tokenId: token.tokenId.toString(),260 },261 });262 }263264 expect(await collection.getTokenBalance(token.tokenId, {Ethereum: owner})).to.be.eq(0n);265 });266267 itEth('Can perform burnFromCross()', async ({helper}) => {268 const collection = await helper.rft.mintCollection(minter, {name: 'A', description: 'B', tokenPrefix: 'C'});269 270 const owner = bob;271 const spender = await helper.eth.createAccountWithBalance(donor, 100n);272273 const token = await collection.mintToken(minter, 100n, {Substrate: owner.address});274275 const address = helper.ethAddress.fromCollectionId(collection.collectionId);276 const contract = helper.ethNativeContract.collection(address, 'rft');277278 await token.repartition(owner, 15n);279 await token.approve(owner, {Ethereum: spender}, 15n);280281 {282 const ownerCross = helper.ethCrossAccount.fromKeyringPair(owner);283 const result = await contract.methods.burnFromCross(ownerCross, token.tokenId).send({from: spender});284 const event = result.events.Transfer;285 expect(event).to.be.like({286 address: helper.ethAddress.fromCollectionId(collection.collectionId),287 event: 'Transfer',288 returnValues: {289 from: helper.address.substrateToEth(owner.address),290 to: '0x0000000000000000000000000000000000000000',291 tokenId: token.tokenId.toString(),292 },293 });294 }295296 expect(await collection.getTokenBalance(token.tokenId, {Substrate: owner.address})).to.be.eq(0n);297 });298299 itEth('Can perform transferFromCross()', async ({helper}) => {300 const collection = await helper.rft.mintCollection(minter, {name: 'A', description: 'B', tokenPrefix: 'C'});301302 const owner = bob;303 const spender = await helper.eth.createAccountWithBalance(donor, 100n);304 const receiver = charlie;305306 const token = await collection.mintToken(minter, 100n, {Substrate: owner.address});307308 const address = helper.ethAddress.fromCollectionId(collection.collectionId);309 const contract = helper.ethNativeContract.collection(address, 'rft');310311 await token.repartition(owner, 15n);312 await token.approve(owner, {Ethereum: spender}, 15n);313314 {315 const ownerCross = helper.ethCrossAccount.fromKeyringPair(owner);316 const recieverCross = helper.ethCrossAccount.fromKeyringPair(receiver);317 const result = await contract.methods.transferFromCross(ownerCross, recieverCross, token.tokenId).send({from: spender});318 const event = result.events.Transfer;319 expect(event).to.be.like({320 address: helper.ethAddress.fromCollectionId(collection.collectionId),321 event: 'Transfer',322 returnValues: {323 from: helper.address.substrateToEth(owner.address),324 to: helper.address.substrateToEth(receiver.address),325 tokenId: token.tokenId.toString(),326 },327 });328 }329330 expect(await token.getTop10Owners()).to.be.like([{Substrate: receiver.address}]);331 });332333 itEth('Can perform transfer()', async ({helper}) => {334 const caller = await helper.eth.createAccountWithBalance(donor);335 const receiver = helper.eth.createAccount();336 const {collectionAddress} = await helper.eth.createRFTCollection(caller, 'Transferry', '6', '6');337 const contract = helper.ethNativeContract.collection(collectionAddress, 'rft', caller);338339 const result = await contract.methods.mint(caller).send();340 const tokenId = result.events.Transfer.returnValues.tokenId;341342 {343 const result = await contract.methods.transfer(receiver, tokenId).send();344345 const event = result.events.Transfer;346 expect(event.address).to.equal(collectionAddress);347 expect(event.returnValues.from).to.equal(caller);348 expect(event.returnValues.to).to.equal(receiver);349 expect(event.returnValues.tokenId).to.equal(tokenId.toString());350 }351352 {353 const balance = await contract.methods.balanceOf(caller).call();354 expect(+balance).to.equal(0);355 }356357 {358 const balance = await contract.methods.balanceOf(receiver).call();359 expect(+balance).to.equal(1);360 }361 });362 363 itEth('Can perform transferCross()', async ({helper}) => {364 const caller = await helper.eth.createAccountWithBalance(donor);365 const receiver = await helper.eth.createAccountWithBalance(donor);366 const to = helper.ethCrossAccount.fromAddress(receiver);367 const toSubstrate = helper.ethCrossAccount.fromKeyringPair(minter);368 const collection = await helper.rft.mintCollection(minter, {});369 const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);370 const contract = helper.ethNativeContract.collection(collectionAddress, 'rft', caller);371372 const {tokenId} = await collection.mintToken(minter, 1n, {Ethereum: caller});373374 {375 const result = await contract.methods.transferCross(to, tokenId).send({from: caller});376377 const event = result.events.Transfer;378 expect(event.address).to.equal(collectionAddress);379 expect(event.returnValues.from).to.equal(caller);380 expect(event.returnValues.to).to.equal(receiver);381 expect(event.returnValues.tokenId).to.equal(tokenId.toString());382 }383384 {385 const balance = await contract.methods.balanceOf(caller).call();386 expect(+balance).to.equal(0);387 }388389 {390 const balance = await contract.methods.balanceOf(receiver).call();391 expect(+balance).to.equal(1);392 }393 394 {395 const substrateResult = await contract.methods.transferCross(toSubstrate, tokenId).send({from: receiver});396 397398 const event = substrateResult.events.Transfer;399 expect(event.address).to.be.equal(collectionAddress);400 expect(event.returnValues.from).to.be.equal(receiver);401 expect(event.returnValues.to).to.be.equal(helper.address.substrateToEth(minter.address));402 expect(event.returnValues.tokenId).to.be.equal(`${tokenId}`);403 }404405 {406 const balance = await contract.methods.balanceOf(receiver).call();407 expect(+balance).to.equal(0);408 }409410 {411 const balance = await helper.nft.getTokensByAddress(collection.collectionId, {Substrate: minter.address});412 expect(balance).to.be.contain(tokenId);413 }414 });415416 itEth('transfer event on transfer from partial ownership to full ownership', async ({helper}) => {417 const caller = await helper.eth.createAccountWithBalance(donor);418 const receiver = helper.eth.createAccount();419 const {collectionId, collectionAddress} = await helper.eth.createRFTCollection(caller, 'Transferry-Partial-to-Full', '6', '6');420 const contract = helper.ethNativeContract.collection(collectionAddress, 'rft', caller);421422 const result = await contract.methods.mint(caller).send();423 const tokenId = result.events.Transfer.returnValues.tokenId;424425 const tokenContract = helper.ethNativeContract.rftTokenById(collectionId, tokenId, caller);426427 await tokenContract.methods.repartition(2).send();428 await tokenContract.methods.transfer(receiver, 1).send();429430 const events: any = [];431 contract.events.allEvents((_: any, event: any) => {432 events.push(event);433 });434435 await tokenContract.methods.transfer(receiver, 1).send();436 if (events.length == 0) await helper.wait.newBlocks(1);437 const event = events[0];438439 expect(event.address).to.equal(collectionAddress);440 expect(event.returnValues.from).to.equal('0xFFfFfFffFFfffFFfFFfFFFFFffFFFffffFfFFFfF');441 expect(event.returnValues.to).to.equal(receiver);442 expect(event.returnValues.tokenId).to.equal(tokenId.toString());443 });444445 itEth('transfer event on transfer from full ownership to partial ownership', async ({helper}) => {446 const caller = await helper.eth.createAccountWithBalance(donor);447 const receiver = helper.eth.createAccount();448 const {collectionId, collectionAddress} = await helper.eth.createRFTCollection(caller, 'Transferry-Full-to-Partial', '6', '6');449 const contract = helper.ethNativeContract.collection(collectionAddress, 'rft', caller);450451 const result = await contract.methods.mint(caller).send();452 const tokenId = result.events.Transfer.returnValues.tokenId;453454 const tokenContract = helper.ethNativeContract.rftTokenById(collectionId, tokenId, caller);455456 await tokenContract.methods.repartition(2).send();457458 const events: any = [];459 contract.events.allEvents((_: any, event: any) => {460 events.push(event);461 });462463 await tokenContract.methods.transfer(receiver, 1).send();464 if (events.length == 0) await helper.wait.newBlocks(1);465 const event = events[0];466467 expect(event.address).to.equal(collectionAddress);468 expect(event.returnValues.from).to.equal(caller);469 expect(event.returnValues.to).to.equal('0xFFfFfFffFFfffFFfFFfFFFFFffFFFffffFfFFFfF');470 expect(event.returnValues.tokenId).to.equal(tokenId.toString());471 });472});473474describe('RFT: Fees', () => {475 let donor: IKeyringPair;476477 before(async function() {478 await usingEthPlaygrounds(async (helper, privateKey) => {479 requirePalletsOrSkip(this, helper, [Pallets.ReFungible]);480481 donor = await privateKey({filename: __filename});482 });483 });484485 itEth('transferFrom() call fee is less than 0.2UNQ', async ({helper}) => {486 const caller = await helper.eth.createAccountWithBalance(donor);487 const receiver = helper.eth.createAccount();488 const {collectionAddress} = await helper.eth.createRFTCollection(caller, 'Feeful-Transfer-From', '6', '6');489 const contract = helper.ethNativeContract.collection(collectionAddress, 'rft', caller);490491 const result = await contract.methods.mint(caller).send();492 const tokenId = result.events.Transfer.returnValues.tokenId;493494 const cost = await helper.eth.recordCallFee(caller, () => contract.methods.transferFrom(caller, receiver, tokenId).send());495 expect(cost < BigInt(0.2 * Number(helper.balance.getOneTokenNominal())));496 expect(cost > 0n);497 });498499 itEth('transfer() call fee is less than 0.2UNQ', async ({helper}) => {500 const caller = await helper.eth.createAccountWithBalance(donor);501 const receiver = helper.eth.createAccount();502 const {collectionAddress} = await helper.eth.createRFTCollection(caller, 'Feeful-Transfer', '6', '6');503 const contract = helper.ethNativeContract.collection(collectionAddress, 'rft', caller);504505 const result = await contract.methods.mint(caller).send();506 const tokenId = result.events.Transfer.returnValues.tokenId;507508 const cost = await helper.eth.recordCallFee(caller, () => contract.methods.transfer(receiver, tokenId).send());509 expect(cost < BigInt(0.2 * Number(helper.balance.getOneTokenNominal())));510 expect(cost > 0n);511 });512});513514describe('Common metadata', () => {515 let donor: IKeyringPair;516 let alice: IKeyringPair;517518 before(async function() {519 await usingEthPlaygrounds(async (helper, privateKey) => {520 requirePalletsOrSkip(this, helper, [Pallets.ReFungible]);521522 donor = await privateKey({filename: __filename});523 [alice] = await helper.arrange.createAccounts([20n], donor);524 });525 });526527 itEth('Returns collection name', async ({helper}) => {528 const caller = helper.eth.createAccount();529 const tokenPropertyPermissions = [{530 key: 'URI',531 permission: {532 mutable: true,533 collectionAdmin: true,534 tokenOwner: false,535 },536 }];537 const collection = await helper.rft.mintCollection(538 alice,539 {540 name: 'Leviathan',541 tokenPrefix: '11',542 properties: [{key: 'ERC721Metadata', value: '1'}],543 tokenPropertyPermissions,544 },545 );546547 const contract = helper.ethNativeContract.collectionById(collection.collectionId, 'rft', caller);548 const name = await contract.methods.name().call();549 expect(name).to.equal('Leviathan');550 });551552 itEth('Returns symbol name', async ({helper}) => {553 const caller = await helper.eth.createAccountWithBalance(donor);554 const tokenPropertyPermissions = [{555 key: 'URI',556 permission: {557 mutable: true,558 collectionAdmin: true,559 tokenOwner: false,560 },561 }];562 const {collectionId} = await helper.rft.mintCollection(563 alice,564 {565 name: 'Leviathan',566 tokenPrefix: '12',567 properties: [{key: 'ERC721Metadata', value: '1'}],568 tokenPropertyPermissions,569 },570 );571572 const contract = helper.ethNativeContract.collectionById(collectionId, 'rft', caller);573 const symbol = await contract.methods.symbol().call();574 expect(symbol).to.equal('12');575 });576});