1234567891011121314151617import {itEth, usingEthPlaygrounds, expect, EthUniqueHelper} from './util/playgrounds';18import {IKeyringPair} from '@polkadot/types/types';19import {Contract} from 'web3-eth-contract';2021describe('NFT: Information getting', () => {22 let donor: IKeyringPair;23 let alice: IKeyringPair;2425 before(async function() {26 await usingEthPlaygrounds(async (helper, privateKey) => {27 donor = privateKey('//Alice');28 [alice] = await helper.arrange.createAccounts([10n], donor);29 });30 });31 32 itEth('totalSupply', async ({helper}) => {33 const collection = await helper.nft.mintCollection(alice, {});34 await collection.mintToken(alice);3536 const caller = await helper.eth.createAccountWithBalance(donor);3738 const contract = helper.ethNativeContract.collectionById(collection.collectionId, 'nft', caller);39 const totalSupply = await contract.methods.totalSupply().call();4041 expect(totalSupply).to.equal('1');42 });4344 itEth('balanceOf', async ({helper}) => {45 const collection = await helper.nft.mintCollection(alice, {});46 const caller = await helper.eth.createAccountWithBalance(donor);4748 await collection.mintToken(alice, {Ethereum: caller});49 await collection.mintToken(alice, {Ethereum: caller});50 await collection.mintToken(alice, {Ethereum: caller});5152 const contract = helper.ethNativeContract.collectionById(collection.collectionId, 'nft', caller);53 const balance = await contract.methods.balanceOf(caller).call();5455 expect(balance).to.equal('3');56 });5758 itEth('ownerOf', async ({helper}) => {59 const collection = await helper.nft.mintCollection(alice, {});60 const caller = await helper.eth.createAccountWithBalance(donor);6162 const token = await collection.mintToken(alice, {Ethereum: caller});6364 const contract = helper.ethNativeContract.collectionById(collection.collectionId, 'nft', caller);6566 const owner = await contract.methods.ownerOf(token.tokenId).call();6768 expect(owner).to.equal(caller);69 });70});7172describe('Check ERC721 token URI for NFT', () => {73 let donor: IKeyringPair;7475 before(async function() {76 await usingEthPlaygrounds(async (_helper, privateKey) => {77 donor = privateKey('//Alice');78 });79 });8081 async function setup(helper: EthUniqueHelper, tokenPrefix: string, propertyKey?: string, propertyValue?: string): Promise<{contract: Contract, nextTokenId: string}> {82 const owner = await helper.eth.createAccountWithBalance(donor);83 const receiver = helper.eth.createAccount();8485 const collectionHelper = helper.ethNativeContract.collectionHelpers(owner);86 let result = await collectionHelper.methods.createERC721MetadataCompatibleCollection('Mint collection', 'a', 'b', tokenPrefix).send();87 const collectionAddress = helper.ethAddress.normalizeAddress(result.events.CollectionCreated.returnValues.collectionId);88 const contract = helper.ethNativeContract.collection(collectionAddress, 'nft', owner);89 90 const nextTokenId = await contract.methods.nextTokenId().call();91 expect(nextTokenId).to.be.equal('1');92 result = await contract.methods.mint(93 receiver,94 nextTokenId,95 ).send();9697 if (propertyKey && propertyValue) {98 99 await contract.methods.setProperty(nextTokenId, propertyKey, Buffer.from(propertyValue)).send();100 }101102 const event = result.events.Transfer;103 expect(event.address).to.be.equal(collectionAddress);104 expect(event.returnValues.from).to.be.equal('0x0000000000000000000000000000000000000000');105 expect(event.returnValues.to).to.be.equal(receiver);106 expect(event.returnValues.tokenId).to.be.equal(nextTokenId);107108 return {contract, nextTokenId};109 }110111 itEth('Empty tokenURI', async ({helper}) => {112 const {contract, nextTokenId} = await setup(helper, '');113 expect(await contract.methods.tokenURI(nextTokenId).call()).to.be.equal('');114 });115116 itEth('TokenURI from url', async ({helper}) => {117 const {contract, nextTokenId} = await setup(helper, 'BaseURI_', 'url', 'Token URI');118 expect(await contract.methods.tokenURI(nextTokenId).call()).to.be.equal('Token URI');119 });120121 itEth('TokenURI from baseURI + tokenId', async ({helper}) => {122 const {contract, nextTokenId} = await setup(helper, 'BaseURI_');123 expect(await contract.methods.tokenURI(nextTokenId).call()).to.be.equal('BaseURI_' + nextTokenId);124 });125126 itEth('TokenURI from baseURI + suffix', async ({helper}) => {127 const suffix = '/some/suffix';128 const {contract, nextTokenId} = await setup(helper, 'BaseURI_', 'suffix', suffix);129 expect(await contract.methods.tokenURI(nextTokenId).call()).to.be.equal('BaseURI_' + suffix);130 });131});132133describe('NFT: Plain calls', () => {134 let donor: IKeyringPair;135 let alice: IKeyringPair;136137 before(async function() {138 await usingEthPlaygrounds(async (helper, privateKey) => {139 donor = privateKey('//Alice');140 [alice] = await helper.arrange.createAccounts([10n], donor);141 });142 });143144 itEth('Can perform mint()', async ({helper}) => {145 const owner = await helper.eth.createAccountWithBalance(donor);146 const receiver = helper.eth.createAccount();147148 const {collectionAddress} = await helper.eth.createNonfungibleCollection(owner, 'Minty', '6', '6');149 const contract = helper.ethNativeContract.collection(collectionAddress, 'nft', owner);150 const nextTokenId = await contract.methods.nextTokenId().call();151152 expect(nextTokenId).to.be.equal('1');153 const result = await contract.methods.mintWithTokenURI(154 receiver,155 nextTokenId,156 'Test URI',157 ).send();158159 const event = result.events.Transfer;160 expect(event.address).to.be.equal(collectionAddress);161 expect(event.returnValues.from).to.be.equal('0x0000000000000000000000000000000000000000');162 expect(event.returnValues.to).to.be.equal(receiver);163 expect(event.returnValues.tokenId).to.be.equal(nextTokenId);164165 expect(await contract.methods.tokenURI(nextTokenId).call()).to.be.equal('Test URI');166167 168 169 170 171 });172173 174 175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239 itEth('Can perform burn()', async ({helper}) => {240 const caller = await helper.eth.createAccountWithBalance(donor);241242 const collection = await helper.nft.mintCollection(alice, {});243 const {tokenId} = await collection.mintToken(alice, {Ethereum: caller});244245 const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);246 const contract = helper.ethNativeContract.collection(collectionAddress, 'nft', caller);247248 {249 const result = await contract.methods.burn(tokenId).send({from: caller});250 251 const event = result.events.Transfer;252 expect(event.address).to.be.equal(collectionAddress);253 expect(event.returnValues.from).to.be.equal(caller);254 expect(event.returnValues.to).to.be.equal('0x0000000000000000000000000000000000000000');255 expect(event.returnValues.tokenId).to.be.equal(`${tokenId}`);256 }257 });258259 itEth('Can perform approve()', async ({helper}) => {260 const owner = await helper.eth.createAccountWithBalance(donor);261 const spender = helper.eth.createAccount();262263 const collection = await helper.nft.mintCollection(alice, {});264 const {tokenId} = await collection.mintToken(alice, {Ethereum: owner});265266 const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);267 const contract = helper.ethNativeContract.collection(collectionAddress, 'nft', owner);268269 {270 const result = await contract.methods.approve(spender, tokenId).send({from: owner});271272 const event = result.events.Approval;273 expect(event.address).to.be.equal(collectionAddress);274 expect(event.returnValues.owner).to.be.equal(owner);275 expect(event.returnValues.approved).to.be.equal(spender);276 expect(event.returnValues.tokenId).to.be.equal(`${tokenId}`);277 }278 });279280 itEth('Can perform transferFrom()', async ({helper}) => {281 const owner = await helper.eth.createAccountWithBalance(donor);282 const spender = await helper.eth.createAccountWithBalance(donor);283 const receiver = helper.eth.createAccount();284285 const collection = await helper.nft.mintCollection(alice, {});286 const {tokenId} = await collection.mintToken(alice, {Ethereum: owner});287288 const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);289 const contract = helper.ethNativeContract.collection(collectionAddress, 'nft', owner);290291 await contract.methods.approve(spender, tokenId).send({from: owner});292293 {294 const result = await contract.methods.transferFrom(owner, receiver, tokenId).send({from: spender});295296 const event = result.events.Transfer;297 expect(event.address).to.be.equal(collectionAddress);298 expect(event.returnValues.from).to.be.equal(owner);299 expect(event.returnValues.to).to.be.equal(receiver);300 expect(event.returnValues.tokenId).to.be.equal(`${tokenId}`);301 }302303 {304 const balance = await contract.methods.balanceOf(receiver).call();305 expect(+balance).to.equal(1);306 }307308 {309 const balance = await contract.methods.balanceOf(owner).call();310 expect(+balance).to.equal(0);311 }312 });313314 itEth('Can perform transfer()', async ({helper}) => {315 const collection = await helper.nft.mintCollection(alice, {});316 const owner = await helper.eth.createAccountWithBalance(donor);317 const receiver = helper.eth.createAccount();318319 const {tokenId} = await collection.mintToken(alice, {Ethereum: owner});320321 const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);322 const contract = helper.ethNativeContract.collection(collectionAddress, 'nft', owner);323324 {325 const result = await contract.methods.transfer(receiver, tokenId).send({from: owner});326327 const event = result.events.Transfer;328 expect(event.address).to.be.equal(collectionAddress);329 expect(event.returnValues.from).to.be.equal(owner);330 expect(event.returnValues.to).to.be.equal(receiver);331 expect(event.returnValues.tokenId).to.be.equal(`${tokenId}`);332 }333334 {335 const balance = await contract.methods.balanceOf(owner).call();336 expect(+balance).to.equal(0);337 }338339 {340 const balance = await contract.methods.balanceOf(receiver).call();341 expect(+balance).to.equal(1);342 }343 });344});345346describe('NFT: Fees', () => {347 let donor: IKeyringPair;348 let alice: IKeyringPair;349350 before(async function() {351 await usingEthPlaygrounds(async (helper, privateKey) => {352 donor = privateKey('//Alice');353 [alice] = await helper.arrange.createAccounts([10n], donor);354 });355 });356 357 itEth('approve() call fee is less than 0.2UNQ', async ({helper}) => {358 const owner = await helper.eth.createAccountWithBalance(donor);359 const spender = helper.eth.createAccount();360361 const collection = await helper.nft.mintCollection(alice, {});362 const {tokenId} = await collection.mintToken(alice, {Ethereum: owner});363364 const contract = helper.ethNativeContract.collectionById(collection.collectionId, 'nft', owner);365366 const cost = await helper.eth.recordCallFee(owner, () => contract.methods.approve(spender, tokenId).send({from: owner}));367 expect(cost < BigInt(0.2 * Number(helper.balance.getOneTokenNominal())));368 });369370 itEth('transferFrom() call fee is less than 0.2UNQ', async ({helper}) => {371 const owner = await helper.eth.createAccountWithBalance(donor);372 const spender = await helper.eth.createAccountWithBalance(donor);373374 const collection = await helper.nft.mintCollection(alice, {});375 const {tokenId} = await collection.mintToken(alice, {Ethereum: owner});376377 const contract = helper.ethNativeContract.collectionById(collection.collectionId, 'nft', owner);378379 await contract.methods.approve(spender, tokenId).send({from: owner});380381 const cost = await helper.eth.recordCallFee(spender, () => contract.methods.transferFrom(owner, spender, tokenId).send({from: spender}));382 expect(cost < BigInt(0.2 * Number(helper.balance.getOneTokenNominal())));383 });384385 itEth('transfer() call fee is less than 0.2UNQ', async ({helper}) => {386 const owner = await helper.eth.createAccountWithBalance(donor);387 const receiver = helper.eth.createAccount();388389 const collection = await helper.nft.mintCollection(alice, {});390 const {tokenId} = await collection.mintToken(alice, {Ethereum: owner});391392 const contract = helper.ethNativeContract.collectionById(collection.collectionId, 'nft', owner);393394 const cost = await helper.eth.recordCallFee(owner, () => contract.methods.transfer(receiver, tokenId).send({from: owner}));395 expect(cost < BigInt(0.2 * Number(helper.balance.getOneTokenNominal())));396 });397});398399describe('NFT: Substrate calls', () => {400 let donor: IKeyringPair;401 let alice: IKeyringPair;402403 before(async function() {404 await usingEthPlaygrounds(async (helper, privateKey) => {405 donor = privateKey('//Alice');406 [alice] = await helper.arrange.createAccounts([20n], donor);407 });408 });409410 itEth('Events emitted for mint()', async ({helper}) => {411 const collection = await helper.nft.mintCollection(alice, {});412 const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);413 const contract = helper.ethNativeContract.collection(collectionAddress, 'nft');414415 const events: any = [];416 contract.events.allEvents((_: any, event: any) => {417 events.push(event);418 });419 const {tokenId} = await collection.mintToken(alice);420421 const event = events[0];422 expect(event.event).to.be.equal('Transfer');423 expect(event.address).to.be.equal(collectionAddress);424 expect(event.returnValues.from).to.be.equal('0x0000000000000000000000000000000000000000');425 expect(event.returnValues.to).to.be.equal(helper.address.substrateToEth(alice.address));426 expect(event.returnValues.tokenId).to.be.equal(tokenId.toString());427 });428429 itEth('Events emitted for burn()', async ({helper}) => {430 const collection = await helper.nft.mintCollection(alice, {});431 const token = await collection.mintToken(alice);432433 const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);434 const contract = helper.ethNativeContract.collection(collectionAddress, 'nft');435 436 const events: any = [];437 contract.events.allEvents((_: any, event: any) => {438 events.push(event);439 });440441 await token.burn(alice);442443 const event = events[0];444 expect(event.event).to.be.equal('Transfer');445 expect(event.address).to.be.equal(collectionAddress);446 expect(event.returnValues.from).to.be.equal(helper.address.substrateToEth(alice.address));447 expect(event.returnValues.to).to.be.equal('0x0000000000000000000000000000000000000000');448 expect(event.returnValues.tokenId).to.be.equal(token.tokenId.toString());449 });450451 itEth('Events emitted for approve()', async ({helper}) => {452 const receiver = helper.eth.createAccount();453454 const collection = await helper.nft.mintCollection(alice, {});455 const token = await collection.mintToken(alice);456457 const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);458 const contract = helper.ethNativeContract.collection(collectionAddress, 'nft');459 460 const events: any = [];461 contract.events.allEvents((_: any, event: any) => {462 events.push(event);463 });464465 await token.approve(alice, {Ethereum: receiver});466467 const event = events[0];468 expect(event.event).to.be.equal('Approval');469 expect(event.address).to.be.equal(collectionAddress);470 expect(event.returnValues.owner).to.be.equal(helper.address.substrateToEth(alice.address));471 expect(event.returnValues.approved).to.be.equal(receiver);472 expect(event.returnValues.tokenId).to.be.equal(token.tokenId.toString());473 });474475 itEth('Events emitted for transferFrom()', async ({helper}) => {476 const [bob] = await helper.arrange.createAccounts([10n], donor);477 const receiver = helper.eth.createAccount();478479 const collection = await helper.nft.mintCollection(alice, {});480 const token = await collection.mintToken(alice);481 await token.approve(alice, {Substrate: bob.address});482483 const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);484 const contract = helper.ethNativeContract.collection(collectionAddress, 'nft');485 486 const events: any = [];487 contract.events.allEvents((_: any, event: any) => {488 events.push(event);489 });490491 await token.transferFrom(bob, {Substrate: alice.address}, {Ethereum: receiver});492 493 const event = events[0];494 expect(event.address).to.be.equal(collectionAddress);495 expect(event.returnValues.from).to.be.equal(helper.address.substrateToEth(alice.address));496 expect(event.returnValues.to).to.be.equal(receiver);497 expect(event.returnValues.tokenId).to.be.equal(`${token.tokenId}`);498 });499500 itEth('Events emitted for transfer()', async ({helper}) => {501 const receiver = helper.eth.createAccount();502503 const collection = await helper.nft.mintCollection(alice, {});504 const token = await collection.mintToken(alice);505506 const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);507 const contract = helper.ethNativeContract.collection(collectionAddress, 'nft');508 509 const events: any = [];510 contract.events.allEvents((_: any, event: any) => {511 events.push(event);512 });513514 await token.transfer(alice, {Ethereum: receiver});515 516 const event = events[0];517 expect(event.address).to.be.equal(collectionAddress);518 expect(event.returnValues.from).to.be.equal(helper.address.substrateToEth(alice.address));519 expect(event.returnValues.to).to.be.equal(receiver);520 expect(event.returnValues.tokenId).to.be.equal(`${token.tokenId}`);521 });522});523524describe('Common metadata', () => {525 let donor: IKeyringPair;526 let alice: IKeyringPair;527528 before(async function() {529 await usingEthPlaygrounds(async (helper, privateKey) => {530 donor = privateKey('//Alice');531 [alice] = await helper.arrange.createAccounts([20n], donor);532 });533 });534535 itEth('Returns collection name', async ({helper}) => {536 const caller = await helper.eth.createAccountWithBalance(donor);537 const collection = await helper.nft.mintCollection(alice, {name: 'oh River', tokenPrefix: 'CHANGE'});538539 const contract = helper.ethNativeContract.collectionById(collection.collectionId, 'nft', caller);540 const name = await contract.methods.name().call();541 expect(name).to.equal('oh River');542 });543544 itEth('Returns symbol name', async ({helper}) => {545 const caller = await helper.eth.createAccountWithBalance(donor);546 const collection = await helper.nft.mintCollection(alice, {name: 'oh River', tokenPrefix: 'CHANGE'});547548 const contract = helper.ethNativeContract.collectionById(collection.collectionId, 'nft', caller);549 const symbol = await contract.methods.symbol().call();550 expect(symbol).to.equal('CHANGE');551 });552});