1234567891011121314151617import {itEth, usingEthPlaygrounds, expect, EthUniqueHelper} from './util';18import {IKeyringPair} from '@polkadot/types/types';19import {Contract} from 'web3-eth-contract';202122describe('NFT: Information getting', () => {23 let donor: IKeyringPair;24 let alice: IKeyringPair;2526 before(async function() {27 await usingEthPlaygrounds(async (helper, privateKey) => {28 donor = await privateKey({filename: __filename});29 [alice] = await helper.arrange.createAccounts([10n], donor);30 });31 });3233 itEth('totalSupply', async ({helper}) => {34 const collection = await helper.nft.mintCollection(alice, {});35 await collection.mintToken(alice);3637 const caller = await helper.eth.createAccountWithBalance(donor);3839 const contract = helper.ethNativeContract.collectionById(collection.collectionId, 'nft', caller);40 const totalSupply = await contract.methods.totalSupply().call();4142 expect(totalSupply).to.equal('1');43 });4445 itEth('balanceOf', async ({helper}) => {46 const collection = await helper.nft.mintCollection(alice, {});47 const caller = await helper.eth.createAccountWithBalance(donor);4849 await collection.mintToken(alice, {Ethereum: caller});50 await collection.mintToken(alice, {Ethereum: caller});51 await collection.mintToken(alice, {Ethereum: caller});5253 const contract = helper.ethNativeContract.collectionById(collection.collectionId, 'nft', caller);54 const balance = await contract.methods.balanceOf(caller).call();5556 expect(balance).to.equal('3');57 });5859 itEth('ownerOf', async ({helper}) => {60 const collection = await helper.nft.mintCollection(alice, {});61 const caller = await helper.eth.createAccountWithBalance(donor);6263 const token = await collection.mintToken(alice, {Ethereum: caller});6465 const contract = helper.ethNativeContract.collectionById(collection.collectionId, 'nft', caller);6667 const owner = await contract.methods.ownerOf(token.tokenId).call();6869 expect(owner).to.equal(caller);70 });7172 itEth('name/symbol is available regardless of ERC721Metadata support', async ({helper}) => {73 const collection = await helper.nft.mintCollection(alice, {name: 'test', tokenPrefix: 'TEST'});74 const caller = helper.eth.createAccount();7576 const contract = helper.ethNativeContract.collectionById(collection.collectionId, 'nft', caller);7778 expect(await contract.methods.name().call()).to.equal('test');79 expect(await contract.methods.symbol().call()).to.equal('TEST');80 });81});8283describe('Check ERC721 token URI for NFT', () => {84 let donor: IKeyringPair;8586 before(async function() {87 await usingEthPlaygrounds(async (_helper, privateKey) => {88 donor = await privateKey({filename: __filename});89 });90 });9192 async function setup(helper: EthUniqueHelper, baseUri: string, propertyKey?: string, propertyValue?: string): Promise<{contract: Contract, nextTokenId: string}> {93 const owner = await helper.eth.createAccountWithBalance(donor);94 const receiver = helper.eth.createAccount();9596 const {collectionAddress} = await helper.eth.createERC721MetadataCompatibleNFTCollection(owner, 'Mint collection', 'a', 'b', baseUri);97 const contract = helper.ethNativeContract.collection(collectionAddress, 'nft', owner);9899 const result = await contract.methods.mint(receiver).send();100 const tokenId = result.events.Transfer.returnValues.tokenId;101 expect(tokenId).to.be.equal('1');102103 if (propertyKey && propertyValue) {104 105 await contract.methods.setProperty(tokenId, propertyKey, Buffer.from(propertyValue)).send();106 }107108 const event = result.events.Transfer;109 expect(event.address).to.be.equal(collectionAddress);110 expect(event.returnValues.from).to.be.equal('0x0000000000000000000000000000000000000000');111 expect(event.returnValues.to).to.be.equal(receiver);112 expect(event.returnValues.tokenId).to.be.equal(tokenId);113114 return {contract, nextTokenId: tokenId};115 }116117 itEth('Empty tokenURI', async ({helper}) => {118 const {contract, nextTokenId} = await setup(helper, '');119 expect(await contract.methods.tokenURI(nextTokenId).call()).to.be.equal('');120 });121122 itEth('TokenURI from url', async ({helper}) => {123 const {contract, nextTokenId} = await setup(helper, 'BaseURI_', 'URI', 'Token URI');124 expect(await contract.methods.tokenURI(nextTokenId).call()).to.be.equal('Token URI');125 });126127 itEth('TokenURI from baseURI', async ({helper}) => {128 const {contract, nextTokenId} = await setup(helper, 'BaseURI_');129 expect(await contract.methods.tokenURI(nextTokenId).call()).to.be.equal('BaseURI_');130 });131132 itEth('TokenURI from baseURI + suffix', async ({helper}) => {133 const suffix = '/some/suffix';134 const {contract, nextTokenId} = await setup(helper, 'BaseURI_', 'URISuffix', suffix);135 expect(await contract.methods.tokenURI(nextTokenId).call()).to.be.equal('BaseURI_' + suffix);136 });137});138139describe('NFT: Plain calls', () => {140 let donor: IKeyringPair;141 let alice: IKeyringPair;142 let bob: IKeyringPair;143 let charlie: IKeyringPair;144145 before(async function() {146 await usingEthPlaygrounds(async (helper, privateKey) => {147 donor = await privateKey({filename: __filename});148 [alice, bob, charlie] = await helper.arrange.createAccounts([100n, 100n, 100n], donor);149 });150 });151152 itEth('Can perform mint()', async ({helper}) => {153 const owner = await helper.eth.createAccountWithBalance(donor);154 const receiver = helper.eth.createAccount();155156 const {collectionAddress} = await helper.eth.createERC721MetadataCompatibleNFTCollection(owner, 'Mint collection', '6', '6', '');157 const contract = helper.ethNativeContract.collection(collectionAddress, 'nft', owner);158159 const result = await contract.methods.mintWithTokenURI(receiver, 'Test URI').send();160 const tokenId = result.events.Transfer.returnValues.tokenId;161 expect(tokenId).to.be.equal('1');162163 const event = result.events.Transfer;164 expect(event.address).to.be.equal(collectionAddress);165 expect(event.returnValues.from).to.be.equal('0x0000000000000000000000000000000000000000');166 expect(event.returnValues.to).to.be.equal(receiver);167168 expect(await contract.methods.tokenURI(tokenId).call()).to.be.equal('Test URI');169170 171 172 173 174 });175176 177 itEth.skip('Can perform mintBulk()', async ({helper}) => {178 const caller = await helper.eth.createAccountWithBalance(donor);179 const receiver = helper.eth.createAccount();180181 const collection = await helper.nft.mintCollection(alice);182 await collection.addAdmin(alice, {Ethereum: caller});183184 const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);185 const contract = helper.ethNativeContract.collection(collectionAddress, 'nft', caller);186 {187 const bulkSize = 3;188 const nextTokenId = await contract.methods.nextTokenId().call();189 expect(nextTokenId).to.be.equal('1');190 const result = await contract.methods.mintBulkWithTokenURI(191 receiver,192 Array.from({length: bulkSize}, (_, i) => (193 [+nextTokenId + i, `Test URI ${i}`]194 )),195 ).send({from: caller});196197 const events = result.events.Transfer.sort((a: any, b: any) => +a.returnValues.tokenId - b.returnValues.tokenId);198 for (let i = 0; i < bulkSize; i++) {199 const event = events[i];200 expect(event.address).to.equal(collectionAddress);201 expect(event.returnValues.from).to.equal('0x0000000000000000000000000000000000000000');202 expect(event.returnValues.to).to.equal(receiver);203 expect(event.returnValues.tokenId).to.equal(`${+nextTokenId + i}`);204205 expect(await contract.methods.tokenURI(+nextTokenId + i).call()).to.be.equal(`Test URI ${i}`);206 }207 }208 });209210 itEth('Can perform burn()', async ({helper}) => {211 const caller = await helper.eth.createAccountWithBalance(donor);212213 const collection = await helper.nft.mintCollection(alice, {});214 const {tokenId} = await collection.mintToken(alice, {Ethereum: caller});215216 const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);217 const contract = helper.ethNativeContract.collection(collectionAddress, 'nft', caller);218219 {220 const result = await contract.methods.burn(tokenId).send({from: caller});221222 const event = result.events.Transfer;223 expect(event.address).to.be.equal(collectionAddress);224 expect(event.returnValues.from).to.be.equal(caller);225 expect(event.returnValues.to).to.be.equal('0x0000000000000000000000000000000000000000');226 expect(event.returnValues.tokenId).to.be.equal(`${tokenId}`);227 }228 });229230 itEth('Can perform approve()', async ({helper}) => {231 const owner = await helper.eth.createAccountWithBalance(donor);232 const spender = helper.eth.createAccount();233234 const collection = await helper.nft.mintCollection(alice, {});235 const {tokenId} = await collection.mintToken(alice, {Ethereum: owner});236237 const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);238 const contract = helper.ethNativeContract.collection(collectionAddress, 'nft', owner);239240 {241 const result = await contract.methods.approve(spender, tokenId).send({from: owner});242243 const event = result.events.Approval;244 expect(event.address).to.be.equal(collectionAddress);245 expect(event.returnValues.owner).to.be.equal(owner);246 expect(event.returnValues.approved).to.be.equal(spender);247 expect(event.returnValues.tokenId).to.be.equal(`${tokenId}`);248 }249 });250251 itEth('Can perform burnFromCross()', async ({helper, privateKey}) => {252 const collection = await helper.nft.mintCollection(alice, {name: 'A', description: 'B', tokenPrefix: 'C'});253254 const owner = bob;255 const spender = await helper.eth.createAccountWithBalance(donor, 100n);256257 const token = await collection.mintToken(alice, {Substrate: owner.address});258259 const address = helper.ethAddress.fromCollectionId(collection.collectionId);260 const contract = helper.ethNativeContract.collection(address, 'nft');261262 {263 await token.approve(owner, {Ethereum: spender});264 const ownerCross = helper.ethCrossAccount.fromKeyringPair(owner);265 const result = await contract.methods.burnFromCross(ownerCross, token.tokenId).send({from: spender});266 const events = result.events.Transfer;267268 expect(events).to.be.like({269 address,270 event: 'Transfer',271 returnValues: {272 from: helper.address.substrateToEth(owner.address),273 to: '0x0000000000000000000000000000000000000000',274 tokenId: token.tokenId.toString(),275 },276 });277 }278 });279280 itEth('Can perform approveCross()', async ({helper, privateKey}) => {281 const collection = await helper.nft.mintCollection(alice, {name: 'A', description: 'B', tokenPrefix: 'C'});282283 const owner = await helper.eth.createAccountWithBalance(donor, 100n);284 const receiver = charlie;285286 const token = await collection.mintToken(alice, {Ethereum: owner});287288 const address = helper.ethAddress.fromCollectionId(collection.collectionId);289 const contract = helper.ethNativeContract.collection(address, 'nft');290291 {292 const recieverCross = helper.ethCrossAccount.fromKeyringPair(receiver);293 const result = await contract.methods.approveCross(recieverCross, token.tokenId).send({from: owner});294 const event = result.events.Approval;295 expect(event).to.be.like({296 address: helper.ethAddress.fromCollectionId(collection.collectionId),297 event: 'Approval',298 returnValues: {299 owner,300 approved: helper.address.substrateToEth(receiver.address),301 tokenId: token.tokenId.toString(),302 },303 });304 }305 });306307 itEth('Can perform transferFrom()', async ({helper}) => {308 const owner = await helper.eth.createAccountWithBalance(donor);309 const spender = await helper.eth.createAccountWithBalance(donor);310 const receiver = helper.eth.createAccount();311312 const collection = await helper.nft.mintCollection(alice, {});313 const {tokenId} = await collection.mintToken(alice, {Ethereum: owner});314315 const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);316 const contract = helper.ethNativeContract.collection(collectionAddress, 'nft', owner);317318 await contract.methods.approve(spender, tokenId).send({from: owner});319320 {321 const result = await contract.methods.transferFrom(owner, receiver, tokenId).send({from: spender});322323 const event = result.events.Transfer;324 expect(event.address).to.be.equal(collectionAddress);325 expect(event.returnValues.from).to.be.equal(owner);326 expect(event.returnValues.to).to.be.equal(receiver);327 expect(event.returnValues.tokenId).to.be.equal(`${tokenId}`);328 }329330 {331 const balance = await contract.methods.balanceOf(receiver).call();332 expect(+balance).to.equal(1);333 }334335 {336 const balance = await contract.methods.balanceOf(owner).call();337 expect(+balance).to.equal(0);338 }339 });340341 itEth('Can perform transfer()', async ({helper}) => {342 const collection = await helper.nft.mintCollection(alice, {});343 const owner = await helper.eth.createAccountWithBalance(donor);344 const receiver = helper.eth.createAccount();345346 const {tokenId} = await collection.mintToken(alice, {Ethereum: owner});347348 const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);349 const contract = helper.ethNativeContract.collection(collectionAddress, 'nft', owner);350351 {352 const result = await contract.methods.transfer(receiver, tokenId).send({from: owner});353354 const event = result.events.Transfer;355 expect(event.address).to.be.equal(collectionAddress);356 expect(event.returnValues.from).to.be.equal(owner);357 expect(event.returnValues.to).to.be.equal(receiver);358 expect(event.returnValues.tokenId).to.be.equal(`${tokenId}`);359 }360361 {362 const balance = await contract.methods.balanceOf(owner).call();363 expect(+balance).to.equal(0);364 }365366 {367 const balance = await contract.methods.balanceOf(receiver).call();368 expect(+balance).to.equal(1);369 }370 });371});372373describe('NFT: Fees', () => {374 let donor: IKeyringPair;375 let alice: IKeyringPair;376 let bob: IKeyringPair;377 let charlie: IKeyringPair;378379 before(async function() {380 await usingEthPlaygrounds(async (helper, privateKey) => {381 donor = await privateKey({filename: __filename});382 [alice, bob, charlie] = await helper.arrange.createAccounts([10n, 10n, 10n], donor);383 });384 });385386 itEth('approve() call fee is less than 0.2UNQ', async ({helper}) => {387 const owner = await helper.eth.createAccountWithBalance(donor);388 const spender = helper.eth.createAccount();389390 const collection = await helper.nft.mintCollection(alice, {});391 const {tokenId} = await collection.mintToken(alice, {Ethereum: owner});392393 const contract = helper.ethNativeContract.collectionById(collection.collectionId, 'nft', owner);394395 const cost = await helper.eth.recordCallFee(owner, () => contract.methods.approve(spender, tokenId).send({from: owner}));396 expect(cost < BigInt(0.2 * Number(helper.balance.getOneTokenNominal())));397 });398399 itEth('transferFrom() call fee is less than 0.2UNQ', async ({helper}) => {400 const owner = await helper.eth.createAccountWithBalance(donor);401 const spender = await helper.eth.createAccountWithBalance(donor);402403 const collection = await helper.nft.mintCollection(alice, {});404 const {tokenId} = await collection.mintToken(alice, {Ethereum: owner});405406 const contract = helper.ethNativeContract.collectionById(collection.collectionId, 'nft', owner);407408 await contract.methods.approve(spender, tokenId).send({from: owner});409410 const cost = await helper.eth.recordCallFee(spender, () => contract.methods.transferFrom(owner, spender, tokenId).send({from: spender}));411 expect(cost < BigInt(0.2 * Number(helper.balance.getOneTokenNominal())));412 });413414 itEth('Can perform transferFromCross()', async ({helper, privateKey}) => {415 const collectionMinter = alice;416 const owner = bob;417 const receiver = charlie;418 const collection = await helper.nft.mintCollection(collectionMinter, {name: 'A', description: 'B', tokenPrefix: 'C'});419420 const spender = await helper.eth.createAccountWithBalance(donor, 100n);421422 const token = await collection.mintToken(collectionMinter, {Substrate: owner.address});423424 const address = helper.ethAddress.fromCollectionId(collection.collectionId);425 const contract = helper.ethNativeContract.collection(address, 'nft');426427 await token.approve(owner, {Ethereum: spender});428429 {430 const ownerCross = helper.ethCrossAccount.fromKeyringPair(owner);431 const recieverCross = helper.ethCrossAccount.fromKeyringPair(receiver);432 const result = await contract.methods.transferFromCross(ownerCross, recieverCross, token.tokenId).send({from: spender});433 const event = result.events.Transfer;434 expect(event).to.be.like({435 address: helper.ethAddress.fromCollectionId(collection.collectionId),436 event: 'Transfer',437 returnValues: {438 from: helper.address.substrateToEth(owner.address),439 to: helper.address.substrateToEth(receiver.address),440 tokenId: token.tokenId.toString(),441 },442 });443 }444445 expect(await token.getOwner()).to.be.like({Substrate: receiver.address});446 });447448 itEth('transfer() call fee is less than 0.2UNQ', async ({helper}) => {449 const owner = await helper.eth.createAccountWithBalance(donor);450 const receiver = helper.eth.createAccount();451452 const collection = await helper.nft.mintCollection(alice, {});453 const {tokenId} = await collection.mintToken(alice, {Ethereum: owner});454455 const contract = helper.ethNativeContract.collectionById(collection.collectionId, 'nft', owner);456457 const cost = await helper.eth.recordCallFee(owner, () => contract.methods.transfer(receiver, tokenId).send({from: owner}));458 expect(cost < BigInt(0.2 * Number(helper.balance.getOneTokenNominal())));459 });460});461462describe('NFT: Substrate calls', () => {463 let donor: IKeyringPair;464 let alice: IKeyringPair;465466 before(async function() {467 await usingEthPlaygrounds(async (helper, privateKey) => {468 donor = await privateKey({filename: __filename});469 [alice] = await helper.arrange.createAccounts([20n], donor);470 });471 });472473 itEth('Events emitted for mint()', async ({helper}) => {474 const collection = await helper.nft.mintCollection(alice, {});475 const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);476 const contract = helper.ethNativeContract.collection(collectionAddress, 'nft');477478 const events: any = [];479 contract.events.allEvents((_: any, event: any) => {480 events.push(event);481 });482483 const {tokenId} = await collection.mintToken(alice);484 if (events.length == 0) await helper.wait.newBlocks(1);485 const event = events[0];486487 expect(event.event).to.be.equal('Transfer');488 expect(event.address).to.be.equal(collectionAddress);489 expect(event.returnValues.from).to.be.equal('0x0000000000000000000000000000000000000000');490 expect(event.returnValues.to).to.be.equal(helper.address.substrateToEth(alice.address));491 expect(event.returnValues.tokenId).to.be.equal(tokenId.toString());492 });493494 itEth('Events emitted for burn()', async ({helper}) => {495 const collection = await helper.nft.mintCollection(alice, {});496 const token = await collection.mintToken(alice);497498 const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);499 const contract = helper.ethNativeContract.collection(collectionAddress, 'nft');500501 const events: any = [];502 contract.events.allEvents((_: any, event: any) => {503 events.push(event);504 });505506 await token.burn(alice);507 if (events.length == 0) await helper.wait.newBlocks(1);508 const event = events[0];509510 expect(event.event).to.be.equal('Transfer');511 expect(event.address).to.be.equal(collectionAddress);512 expect(event.returnValues.from).to.be.equal(helper.address.substrateToEth(alice.address));513 expect(event.returnValues.to).to.be.equal('0x0000000000000000000000000000000000000000');514 expect(event.returnValues.tokenId).to.be.equal(token.tokenId.toString());515 });516517 itEth('Events emitted for approve()', async ({helper}) => {518 const receiver = helper.eth.createAccount();519520 const collection = await helper.nft.mintCollection(alice, {});521 const token = await collection.mintToken(alice);522523 const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);524 const contract = helper.ethNativeContract.collection(collectionAddress, 'nft');525526 const events: any = [];527 contract.events.allEvents((_: any, event: any) => {528 events.push(event);529 });530531 await token.approve(alice, {Ethereum: receiver});532 if (events.length == 0) await helper.wait.newBlocks(1);533 const event = events[0];534535 expect(event.event).to.be.equal('Approval');536 expect(event.address).to.be.equal(collectionAddress);537 expect(event.returnValues.owner).to.be.equal(helper.address.substrateToEth(alice.address));538 expect(event.returnValues.approved).to.be.equal(receiver);539 expect(event.returnValues.tokenId).to.be.equal(token.tokenId.toString());540 });541542 itEth('Events emitted for transferFrom()', async ({helper}) => {543 const [bob] = await helper.arrange.createAccounts([10n], donor);544 const receiver = helper.eth.createAccount();545546 const collection = await helper.nft.mintCollection(alice, {});547 const token = await collection.mintToken(alice);548 await token.approve(alice, {Substrate: bob.address});549550 const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);551 const contract = helper.ethNativeContract.collection(collectionAddress, 'nft');552553 const events: any = [];554 contract.events.allEvents((_: any, event: any) => {555 events.push(event);556 });557558 await token.transferFrom(bob, {Substrate: alice.address}, {Ethereum: receiver});559560 if (events.length == 0) await helper.wait.newBlocks(1);561 const event = events[0];562563 expect(event.address).to.be.equal(collectionAddress);564 expect(event.returnValues.from).to.be.equal(helper.address.substrateToEth(alice.address));565 expect(event.returnValues.to).to.be.equal(receiver);566 expect(event.returnValues.tokenId).to.be.equal(`${token.tokenId}`);567 });568569 itEth('Events emitted for transfer()', async ({helper}) => {570 const receiver = helper.eth.createAccount();571572 const collection = await helper.nft.mintCollection(alice, {});573 const token = await collection.mintToken(alice);574575 const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);576 const contract = helper.ethNativeContract.collection(collectionAddress, 'nft');577578 const events: any = [];579 contract.events.allEvents((_: any, event: any) => {580 events.push(event);581 });582583 await token.transfer(alice, {Ethereum: receiver});584585 if (events.length == 0) await helper.wait.newBlocks(1);586 const event = events[0];587588 expect(event.address).to.be.equal(collectionAddress);589 expect(event.returnValues.from).to.be.equal(helper.address.substrateToEth(alice.address));590 expect(event.returnValues.to).to.be.equal(receiver);591 expect(event.returnValues.tokenId).to.be.equal(`${token.tokenId}`);592 });593});594595describe('Common metadata', () => {596 let donor: IKeyringPair;597 let alice: IKeyringPair;598599 before(async function() {600 await usingEthPlaygrounds(async (helper, privateKey) => {601 donor = await privateKey({filename: __filename});602 [alice] = await helper.arrange.createAccounts([20n], donor);603 });604 });605606 itEth('Returns collection name', async ({helper}) => {607 const caller = await helper.eth.createAccountWithBalance(donor);608 const tokenPropertyPermissions = [{609 key: 'URI',610 permission: {611 mutable: true,612 collectionAdmin: true,613 tokenOwner: false,614 },615 }];616 const collection = await helper.nft.mintCollection(617 alice,618 {619 name: 'oh River',620 tokenPrefix: 'CHANGE',621 properties: [{key: 'ERC721Metadata', value: '1'}],622 tokenPropertyPermissions,623 },624 );625626 const contract = helper.ethNativeContract.collectionById(collection.collectionId, 'nft', caller);627 const name = await contract.methods.name().call();628 expect(name).to.equal('oh River');629 });630631 itEth('Returns symbol name', async ({helper}) => {632 const caller = await helper.eth.createAccountWithBalance(donor);633 const tokenPropertyPermissions = [{634 key: 'URI',635 permission: {636 mutable: true,637 collectionAdmin: true,638 tokenOwner: false,639 },640 }];641 const collection = await helper.nft.mintCollection(642 alice,643 {644 name: 'oh River',645 tokenPrefix: 'CHANGE',646 properties: [{key: 'ERC721Metadata', value: '1'}],647 tokenPropertyPermissions,648 },649 );650651 const contract = helper.ethNativeContract.collectionById(collection.collectionId, 'nft', caller);652 const symbol = await contract.methods.symbol().call();653 expect(symbol).to.equal('CHANGE');654 });655});