123456import privateKey from '../substrate/privateKey';7import { approveExpectSuccess, burnItemExpectSuccess, createCollectionExpectSuccess, createItemExpectSuccess, transferExpectSuccess, transferFromExpectSuccess } from '../util/helpers';8import { collectionIdToAddress, createEthAccount, createEthAccountWithBalance, GAS_ARGS, itWeb3, normalizeEvents, recordEvents, subToEth, transferBalanceToEth } from './util/helpers';9import nonFungibleAbi from './nonFungibleAbi.json';10import { expect } from 'chai';11import waitNewBlocks from '../substrate/wait-new-blocks';12import { submitTransactionAsync } from '../substrate/substrate-api';1314describe('NFT: Information getting', () => {15 itWeb3('totalSupply', async ({ api, web3 }) => {16 const collection = await createCollectionExpectSuccess({17 mode: { type: 'NFT' },18 });19 const alice = privateKey('//Alice');20 const caller = await createEthAccountWithBalance(api, web3);2122 await createItemExpectSuccess(alice, collection, 'NFT', { substrate: alice.address });2324 const address = collectionIdToAddress(collection);25 const contract = new web3.eth.Contract(nonFungibleAbi as any, address, {from: caller, ...GAS_ARGS});26 const totalSupply = await contract.methods.totalSupply().call();2728 29 expect(totalSupply).to.equal('0');30 });3132 itWeb3('balanceOf', async ({ api, web3 }) => {33 const collection = await createCollectionExpectSuccess({34 mode: { type: 'NFT' },35 });36 const alice = privateKey('//Alice');3738 const caller = await createEthAccountWithBalance(api, web3);39 await createItemExpectSuccess(alice, collection, 'NFT', { ethereum: caller });40 await createItemExpectSuccess(alice, collection, 'NFT', { ethereum: caller });41 await createItemExpectSuccess(alice, collection, 'NFT', { ethereum: caller });4243 const address = collectionIdToAddress(collection);44 const contract = new web3.eth.Contract(nonFungibleAbi as any, address, {from: caller, ...GAS_ARGS});45 const balance = await contract.methods.balanceOf(caller).call();4647 expect(balance).to.equal('3');48 });4950 itWeb3('ownerOf', async ({ api, web3 }) => {51 const collection = await createCollectionExpectSuccess({52 mode: { type: 'NFT' },53 });54 const alice = privateKey('//Alice');5556 const caller = await createEthAccountWithBalance(api, web3);57 const tokenId = await createItemExpectSuccess(alice, collection, 'NFT', { ethereum: caller });5859 const address = collectionIdToAddress(collection);60 const contract = new web3.eth.Contract(nonFungibleAbi as any, address, {from: caller, ...GAS_ARGS});61 const owner = await contract.methods.ownerOf(tokenId).call();6263 expect(owner).to.equal(caller);64 });65});6667describe('NFT: Plain calls', () => {68 itWeb3('Can perform mint()', async ({ web3, api }) => {69 const collection = await createCollectionExpectSuccess({70 mode: { type: 'NFT' },71 });72 const alice = privateKey('//Alice');7374 const caller = await createEthAccountWithBalance(api, web3);75 const changeAdminTx = api.tx.nft.addCollectionAdmin(collection, { ethereum: caller });76 await submitTransactionAsync(alice, changeAdminTx);77 const receiver = createEthAccount(web3);7879 const address = collectionIdToAddress(collection);80 const contract = new web3.eth.Contract(nonFungibleAbi as any, address, {from: caller, ...GAS_ARGS});8182 {83 const nextTokenId = await contract.methods.nextTokenId().call();84 expect(nextTokenId).to.be.equal('1');85 const result = await contract.methods.mintWithTokenURI(86 receiver,87 nextTokenId,88 'Test URI',89 ).send({from: caller});90 const events = normalizeEvents(result.events);9192 expect(events).to.be.deep.equal([93 {94 address,95 event: 'Transfer',96 args: {97 from: '0x0000000000000000000000000000000000000000',98 to: receiver,99 tokenId: nextTokenId,100 },101 },102 ]);103104 await waitNewBlocks(api, 1);105 expect(await contract.methods.tokenURI(nextTokenId).call()).to.be.equal('Test URI');106 }107 });108109 itWeb3('Can perform burn()', async ({ web3, api }) => {110 const collection = await createCollectionExpectSuccess({111 mode: {type: 'NFT'},112 });113 const alice = privateKey('//Alice');114115 const owner = await createEthAccountWithBalance(api, web3);116117 const tokenId = await createItemExpectSuccess(alice, collection, 'NFT', { ethereum: owner });118 119 const address = collectionIdToAddress(collection);120 const contract = new web3.eth.Contract(nonFungibleAbi as any, address, {from: owner, ...GAS_ARGS});121122 {123 const result = await contract.methods.burn(tokenId).send({ from: owner });124 const events = normalizeEvents(result.events);125 126 expect(events).to.be.deep.equal([127 {128 address,129 event: 'Transfer',130 args: {131 from: owner,132 to: '0x0000000000000000000000000000000000000000',133 tokenId: tokenId.toString(),134 },135 },136 ]);137 }138 });139140 itWeb3('Can perform approve()', async ({ web3, api }) => {141 const collection = await createCollectionExpectSuccess({142 mode: { type: 'NFT' },143 });144 const alice = privateKey('//Alice');145146 const owner = createEthAccount(web3);147 await transferBalanceToEth(api, alice, owner, 999999999999999);148149 const tokenId = await createItemExpectSuccess(alice, collection, 'NFT', { ethereum: owner });150151 const spender = createEthAccount(web3);152153 const address = collectionIdToAddress(collection);154 const contract = new web3.eth.Contract(nonFungibleAbi as any, address);155156 {157 const result = await contract.methods.approve(spender, tokenId).send({ from: owner, gas: '0x1000000', gasPrice: '0x01' });158 const events = normalizeEvents(result.events);159160 expect(events).to.be.deep.equal([161 {162 address,163 event: 'Approval',164 args: {165 owner,166 approved: spender,167 tokenId: tokenId.toString(),168 },169 },170 ]);171 }172 });173174 itWeb3('Can perform transferFrom()', async ({ web3, api }) => {175 const collection = await createCollectionExpectSuccess({176 mode: { type: 'NFT' },177 });178 const alice = privateKey('//Alice');179180 const owner = createEthAccount(web3);181 await transferBalanceToEth(api, alice, owner, 999999999999999);182183 const tokenId = await createItemExpectSuccess(alice, collection, 'NFT', { ethereum: owner });184185 const spender = createEthAccount(web3);186 await transferBalanceToEth(api, alice, spender, 999999999999999);187188 const receiver = createEthAccount(web3);189190 const address = collectionIdToAddress(collection);191 const contract = new web3.eth.Contract(nonFungibleAbi as any, address, {from: owner, ...GAS_ARGS});192193 await contract.methods.approve(spender, tokenId).send({ from: owner });194195 {196 const result = await contract.methods.transferFrom(owner, receiver, tokenId).send({ from: spender });197 const events = normalizeEvents(result.events);198 expect(events).to.be.deep.equal([199 {200 address,201 event: 'Transfer',202 args: {203 from: owner,204 to: receiver,205 tokenId: tokenId.toString(),206 },207 },208 ]);209 }210211 {212 const balance = await contract.methods.balanceOf(receiver).call();213 expect(+balance).to.equal(1);214 }215216 {217 const balance = await contract.methods.balanceOf(owner).call();218 expect(+balance).to.equal(0);219 }220 });221222 itWeb3('Can perform transfer()', async ({ web3, api }) => {223 const collection = await createCollectionExpectSuccess({224 mode: { type: 'NFT' },225 });226 const alice = privateKey('//Alice');227228 const owner = createEthAccount(web3);229 await transferBalanceToEth(api, alice, owner, 999999999999999);230231 const tokenId = await createItemExpectSuccess(alice, collection, 'NFT', { ethereum: owner });232233 const receiver = createEthAccount(web3);234 await transferBalanceToEth(api, alice, receiver, 999999999999999);235236 const address = collectionIdToAddress(collection);237 const contract = new web3.eth.Contract(nonFungibleAbi as any, address, {from: owner, ...GAS_ARGS});238239 {240 const result = await contract.methods.transfer(receiver, tokenId).send({ from: owner });241 await waitNewBlocks(api, 1);242 const events = normalizeEvents(result.events);243 expect(events).to.be.deep.equal([244 {245 address,246 event: 'Transfer',247 args: {248 from: owner,249 to: receiver,250 tokenId: tokenId.toString(),251 },252 },253 ]);254 }255256 {257 const balance = await contract.methods.balanceOf(owner).call();258 expect(+balance).to.equal(0);259 }260261 {262 const balance = await contract.methods.balanceOf(receiver).call();263 expect(+balance).to.equal(1);264 }265 });266});267268describe('NFT: Substrate calls', () => {269 itWeb3('Events emitted for mint()', async ({ web3 }) => {270 const collection = await createCollectionExpectSuccess({271 mode: { type: 'NFT' },272 });273 const alice = privateKey('//Alice');274275 const address = collectionIdToAddress(collection);276 const contract = new web3.eth.Contract(nonFungibleAbi as any, address);277278 let tokenId: number;279 const events = await recordEvents(contract, async () => {280 tokenId = await createItemExpectSuccess(alice, collection, 'NFT');281 });282283 expect(events).to.be.deep.equal([284 {285 address,286 event: 'Transfer',287 args: {288 from: '0x0000000000000000000000000000000000000000',289 to: subToEth(alice.address),290 tokenId: tokenId!.toString(),291 },292 },293 ]);294 });295296 itWeb3('Events emitted for burn()', async ({ web3 }) => {297 const collection = await createCollectionExpectSuccess({298 mode: { type: 'NFT' },299 });300 const alice = privateKey('//Alice');301302 const address = collectionIdToAddress(collection);303 const contract = new web3.eth.Contract(nonFungibleAbi as any, address);304305 const tokenId = await createItemExpectSuccess(alice, collection, 'NFT');306 const events = await recordEvents(contract, async () => {307 await burnItemExpectSuccess(alice, collection, tokenId);308 });309310 expect(events).to.be.deep.equal([311 {312 address,313 event: 'Transfer',314 args: {315 from: subToEth(alice.address),316 to: '0x0000000000000000000000000000000000000000',317 tokenId: tokenId.toString(),318 },319 },320 ]);321 });322323 itWeb3('Events emitted for approve()', async ({ web3 }) => {324 const collection = await createCollectionExpectSuccess({325 mode: { type: 'NFT' },326 });327 const alice = privateKey('//Alice');328329 const receiver = createEthAccount(web3);330331 const tokenId = await createItemExpectSuccess(alice, collection, 'NFT');332333 const address = collectionIdToAddress(collection);334 const contract = new web3.eth.Contract(nonFungibleAbi as any, address);335336 const events = await recordEvents(contract, async () => {337 await approveExpectSuccess(collection, tokenId, alice, { ethereum: receiver }, 1);338 });339340 expect(events).to.be.deep.equal([341 {342 address,343 event: 'Approval',344 args: {345 owner: subToEth(alice.address),346 approved: receiver,347 tokenId: tokenId.toString(),348 },349 },350 ]);351 });352353 itWeb3('Events emitted for transferFrom()', async ({ web3 }) => {354 const collection = await createCollectionExpectSuccess({355 mode: { type: 'NFT' },356 });357 const alice = privateKey('//Alice');358 const bob = privateKey('//Bob');359360 const receiver = createEthAccount(web3);361362 const tokenId = await createItemExpectSuccess(alice, collection, 'NFT');363 await approveExpectSuccess(collection, tokenId, alice, bob, 1);364365 const address = collectionIdToAddress(collection);366 const contract = new web3.eth.Contract(nonFungibleAbi as any, address);367368 const events = await recordEvents(contract, async () => {369 await transferFromExpectSuccess(collection, tokenId, bob, alice, { ethereum: receiver }, 1, 'NFT');370 });371372 expect(events).to.be.deep.equal([373 {374 address,375 event: 'Transfer',376 args: {377 from: subToEth(alice.address),378 to: receiver,379 tokenId: tokenId.toString(),380 },381 },382 ]);383 });384385 itWeb3('Events emitted for transfer()', async ({ web3 }) => {386 const collection = await createCollectionExpectSuccess({387 mode: { type: 'NFT' },388 });389 const alice = privateKey('//Alice');390391 const receiver = createEthAccount(web3);392393 const tokenId = await createItemExpectSuccess(alice, collection, 'NFT');394395 const address = collectionIdToAddress(collection);396 const contract = new web3.eth.Contract(nonFungibleAbi as any, address);397398 const events = await recordEvents(contract, async () => {399 await transferExpectSuccess(collection, tokenId, alice, { ethereum: receiver }, 1, 'NFT');400 });401402 expect(events).to.be.deep.equal([403 {404 address,405 event: 'Transfer',406 args: {407 from: subToEth(alice.address),408 to: receiver,409 tokenId: tokenId.toString(),410 },411 },412 ]);413 });414});