123456import privateKey from '../substrate/privateKey';7import { approveExpectSuccess, createCollectionExpectSuccess, createItemExpectSuccess, transferExpectSuccess, transferFromExpectSuccess } from '../util/helpers';8import { collectionIdToAddress, createEthAccount, itWeb3, normalizeEvents, recordEvents, subToEth, transferBalanceToEth } from './util/helpers';9import nonFungibleAbi from './nonFungibleAbi.json';10import { expect } from 'chai';1112describe('Information getting', () => {13 itWeb3('totalSupply', async ({ web3 }) => {14 const collection = await createCollectionExpectSuccess({15 mode: { type: 'NFT' },16 });17 const alice = privateKey('//Alice');1819 await createItemExpectSuccess(alice, collection, 'NFT', { substrate: alice.address });2021 const address = collectionIdToAddress(collection);22 const contract = new web3.eth.Contract(nonFungibleAbi as any, address);23 const totalSupply = await contract.methods.totalSupply().call();2425 26 expect(totalSupply).to.equal('0');27 });2829 itWeb3('balanceOf', async ({ web3 }) => {30 const collection = await createCollectionExpectSuccess({31 mode: { type: 'NFT' },32 });33 const alice = privateKey('//Alice');3435 const caller = createEthAccount(web3);36 await createItemExpectSuccess(alice, collection, 'NFT', { ethereum: caller });37 await createItemExpectSuccess(alice, collection, 'NFT', { ethereum: caller });38 await createItemExpectSuccess(alice, collection, 'NFT', { ethereum: caller });3940 const address = collectionIdToAddress(collection);41 const contract = new web3.eth.Contract(nonFungibleAbi as any, address);42 const balance = await contract.methods.balanceOf(caller).call();4344 expect(balance).to.equal('3');45 });4647 itWeb3('ownerOf', async ({ web3 }) => {48 const collection = await createCollectionExpectSuccess({49 mode: { type: 'NFT' },50 });51 const alice = privateKey('//Alice');5253 const caller = createEthAccount(web3);54 const tokenId = await createItemExpectSuccess(alice, collection, 'NFT', { ethereum: caller });5556 const address = collectionIdToAddress(collection);57 const contract = new web3.eth.Contract(nonFungibleAbi as any, address);58 const owner = await contract.methods.ownerOf(tokenId).call();5960 expect(owner).to.equal(caller);61 });62});6364describe('Plain calls', () => {65 itWeb3('Can perform approve()', async ({ web3, api }) => {66 const collection = await createCollectionExpectSuccess({67 mode: { type: 'NFT' },68 });69 const alice = privateKey('//Alice');7071 const owner = createEthAccount(web3);72 await transferBalanceToEth(api, alice, owner, 999999999999999);7374 const tokenId = await createItemExpectSuccess(alice, collection, 'NFT', { ethereum: owner });7576 const spender = createEthAccount(web3);7778 const address = collectionIdToAddress(collection);79 const contract = new web3.eth.Contract(nonFungibleAbi as any, address);8081 {82 const result = await contract.methods.approve(spender, tokenId).send({ from: owner, gas: '0x1000000', gasPrice: '0x01' });83 const events = normalizeEvents(result.events);8485 expect(events).to.be.deep.equal([86 {87 address,88 event: 'Approval',89 args: {90 owner,91 approved: spender,92 tokenId: tokenId.toString(),93 },94 },95 ]);96 }97 });9899 itWeb3('Can perform transferFrom()', async ({ web3, api }) => {100 const collection = await createCollectionExpectSuccess({101 mode: { type: 'NFT' },102 });103 const alice = privateKey('//Alice');104105 const owner = createEthAccount(web3);106 await transferBalanceToEth(api, alice, owner, 999999999999999);107108 const tokenId = await createItemExpectSuccess(alice, collection, 'NFT', { ethereum: owner });109110 const spender = createEthAccount(web3);111 await transferBalanceToEth(api, alice, spender, 999999999999999);112113 const receiver = createEthAccount(web3);114115 const address = collectionIdToAddress(collection);116 const contract = new web3.eth.Contract(nonFungibleAbi as any, address);117118 await contract.methods.approve(spender, tokenId).send({ from: owner, gas: '0x1000000', gasPrice: '0x01' });119120 {121 const result = await contract.methods.transferFrom(owner, receiver, tokenId).send({ from: spender, gas: '0x1000000', gasPrice: '0x01' });122 const events = normalizeEvents(result.events);123 expect(events).to.be.deep.equal([124 {125 address,126 event: 'Transfer',127 args: {128 from: owner,129 to: receiver,130 tokenId: tokenId.toString(),131 },132 },133 ]);134 }135136 {137 const balance = await contract.methods.balanceOf(receiver).call();138 expect(+balance).to.equal(1);139 }140141 {142 const balance = await contract.methods.balanceOf(owner).call();143 expect(+balance).to.equal(0);144 }145 });146147 itWeb3('Can perform transfer()', async ({ web3, api }) => {148 const collection = await createCollectionExpectSuccess({149 mode: { type: 'NFT' },150 });151 const alice = privateKey('//Alice');152153 const owner = createEthAccount(web3);154 await transferBalanceToEth(api, alice, owner, 999999999999999);155156 const tokenId = await createItemExpectSuccess(alice, collection, 'NFT', { ethereum: owner });157158 const receiver = createEthAccount(web3);159 await transferBalanceToEth(api, alice, receiver, 999999999999999);160161 const address = collectionIdToAddress(collection);162 const contract = new web3.eth.Contract(nonFungibleAbi as any, address);163164 {165 const result = await contract.methods.transfer(receiver, tokenId).send({ from: owner, gas: '0x1000000', gasPrice: '0x01' });166 console.log(result);167 const events = normalizeEvents(result.events);168 expect(events).to.be.deep.equal([169 {170 address,171 event: 'Transfer',172 args: {173 from: owner,174 to: receiver,175 tokenId: tokenId.toString(),176 },177 },178 ]);179 }180181 {182 const balance = await contract.methods.balanceOf(owner).call();183 expect(+balance).to.equal(0);184 }185186 {187 const balance = await contract.methods.balanceOf(receiver).call();188 expect(+balance).to.equal(1);189 }190 });191});192193describe('Substrate calls', () => {194 itWeb3('Events emitted for approve()', async ({ web3 }) => {195 const collection = await createCollectionExpectSuccess({196 mode: { type: 'NFT' },197 });198 const alice = privateKey('//Alice');199200 const receiver = createEthAccount(web3);201202 const tokenId = await createItemExpectSuccess(alice, collection, 'NFT');203204 const address = collectionIdToAddress(collection);205 const contract = new web3.eth.Contract(nonFungibleAbi as any, address);206207 const events = await recordEvents(contract, async () => {208 await approveExpectSuccess(collection, tokenId, alice, { ethereum: receiver }, 1);209 });210211 expect(events).to.be.deep.equal([212 {213 address,214 event: 'Approval',215 args: {216 owner: subToEth(alice.address),217 approved: receiver,218 tokenId: tokenId.toString(),219 },220 },221 ]);222 });223224 itWeb3('Events emitted for transferFrom()', async ({ web3 }) => {225 const collection = await createCollectionExpectSuccess({226 mode: { type: 'NFT' },227 });228 const alice = privateKey('//Alice');229 const bob = privateKey('//Bob');230231 const receiver = createEthAccount(web3);232233 const tokenId = await createItemExpectSuccess(alice, collection, 'NFT');234 await approveExpectSuccess(collection, tokenId, alice, bob, 1);235236 const address = collectionIdToAddress(collection);237 const contract = new web3.eth.Contract(nonFungibleAbi as any, address);238239 const events = await recordEvents(contract, async () => {240 await transferFromExpectSuccess(collection, tokenId, bob, alice, { ethereum: receiver }, 1, 'NFT');241 });242243 expect(events).to.be.deep.equal([244 {245 address,246 event: 'Transfer',247 args: {248 from: subToEth(alice.address),249 to: receiver,250 tokenId: tokenId.toString(),251 },252 },253 ]);254 });255256 itWeb3('Events emitted for transfer()', async ({ web3 }) => {257 const collection = await createCollectionExpectSuccess({258 mode: { type: 'NFT' },259 });260 const alice = privateKey('//Alice');261262 const receiver = createEthAccount(web3);263264 const tokenId = await createItemExpectSuccess(alice, collection, 'NFT');265266 const address = collectionIdToAddress(collection);267 const contract = new web3.eth.Contract(nonFungibleAbi as any, address);268269 const events = await recordEvents(contract, async () => {270 await transferExpectSuccess(collection, tokenId, alice, { ethereum: receiver }, 1, 'NFT');271 });272273 expect(events).to.be.deep.equal([274 {275 address,276 event: 'Transfer',277 args: {278 from: subToEth(alice.address),279 to: receiver,280 tokenId: tokenId.toString(),281 },282 },283 ]);284 });285});