1234567891011121314151617import {readFile} from 'fs/promises';18import {IKeyringPair} from '@polkadot/types/types';19import {EthUniqueHelper, itEth, usingEthPlaygrounds, expect} from '../util';20import {makeNames} from '../../util';2122const {dirname} = makeNames(import.meta.url);232425async function proxyWrap(helper: EthUniqueHelper, wrapped: any, donor: IKeyringPair) {26 27 const owner = await helper.eth.createAccountWithBalance(donor);28 const web3 = helper.getWeb3();29 const proxyContract = new web3.eth.Contract(JSON.parse((await readFile(`${dirname}/UniqueNFTProxy.abi`)).toString()), undefined, {30 from: owner,31 gas: helper.eth.DEFAULT_GAS,32 });33 const proxy = await proxyContract.deploy({data: (await readFile(`${dirname}/UniqueNFTProxy.bin`)).toString(), arguments: [wrapped.options.address]}).send({from: owner});34 return proxy;35}3637describe('NFT (Via EVM proxy): Information getting', () => {38 let alice: IKeyringPair;39 let donor: IKeyringPair;4041 before(async function() {42 await usingEthPlaygrounds(async (helper, privateKey) => {43 donor = await privateKey({url: import.meta.url});44 [alice] = await helper.arrange.createAccounts([10n], donor);45 });46 });4748 itEth('totalSupply', async ({helper}) => {49 const collection = await helper.nft.mintCollection(alice, {name: 'test', description: 'test', tokenPrefix: 'test'});50 const caller = await helper.eth.createAccountWithBalance(donor);51 await collection.mintToken(alice, {Substrate: alice.address});5253 const address = helper.ethAddress.fromCollectionId(collection.collectionId);54 const evmCollection = await helper.ethNativeContract.collection(address, 'nft', caller);55 const contract = await proxyWrap(helper, evmCollection, donor);56 const totalSupply = await contract.methods.totalSupply().call();5758 expect(totalSupply).to.equal('1');59 });6061 itEth('balanceOf', async ({helper}) => {62 const collection = await helper.nft.mintCollection(alice, {name: 'test', description: 'test', tokenPrefix: 'test'});6364 const caller = await helper.eth.createAccountWithBalance(donor);65 await collection.mintMultipleTokens(alice, [66 {owner: {Ethereum: caller}},67 {owner: {Ethereum: caller}},68 {owner: {Ethereum: caller}},69 ]);7071 const address = helper.ethAddress.fromCollectionId(collection.collectionId);72 const evmCollection = await helper.ethNativeContract.collection(address, 'nft', caller);73 const contract = await proxyWrap(helper, evmCollection, donor);74 const balance = await contract.methods.balanceOf(caller).call();7576 expect(balance).to.equal('3');77 });7879 itEth('ownerOf', async ({helper}) => {80 const collection = await helper.nft.mintCollection(alice, {name: 'test', description: 'test', tokenPrefix: 'test'});8182 const caller = await helper.eth.createAccountWithBalance(donor);83 const {tokenId} = await collection.mintToken(alice, {Ethereum: caller});8485 const address = helper.ethAddress.fromCollectionId(collection.collectionId);86 const evmCollection = await helper.ethNativeContract.collection(address, 'nft', caller);87 const contract = await proxyWrap(helper, evmCollection, donor);88 const owner = await contract.methods.ownerOf(tokenId).call();8990 expect(owner).to.equal(caller);91 });92});9394describe('NFT (Via EVM proxy): Plain calls', () => {95 let alice: IKeyringPair;96 let donor: IKeyringPair;9798 before(async function() {99 await usingEthPlaygrounds(async (helper, privateKey) => {100 donor = await privateKey({url: import.meta.url});101 [alice] = await helper.arrange.createAccounts([10n], donor);102 });103 });104105 106 itEth('[eth] Can perform mint()', async ({helper}) => {107 const owner = await helper.eth.createAccountWithBalance(donor);108 const {collectionAddress} = await helper.eth.createERC721MetadataCompatibleNFTCollection(owner, 'A', 'A', 'A', '');109 const caller = await helper.eth.createAccountWithBalance(donor);110 const receiver = helper.eth.createAccount();111112 const collectionEvmOwned = await helper.ethNativeContract.collection(collectionAddress, 'nft', owner, true);113 const collectionEvm = await helper.ethNativeContract.collection(collectionAddress, 'nft', caller, true);114 const contract = await proxyWrap(helper, collectionEvm, donor);115 await collectionEvmOwned.methods.addCollectionAdmin(contract.options.address).send();116117 {118 const nextTokenId = await contract.methods.nextTokenId().call();119 const result = await contract.methods.mintWithTokenURI(receiver, nextTokenId, 'Test URI').send({from: caller});120 const tokenId = result.events.Transfer.returnValues.tokenId;121 expect(tokenId).to.be.equal('1');122123 const event = helper.eth.normalizeEvents(result.events)124 .find(event => event.event === 'Transfer')!;125 event.address = event.address.toLocaleLowerCase();126127 expect(event).to.be.deep.equal({128 address: collectionAddress.toLocaleLowerCase(),129 event: 'Transfer',130 args: {131 from: '0x0000000000000000000000000000000000000000',132 to: receiver,133 tokenId,134 },135 });136137 expect(await contract.methods.tokenURI(tokenId).call()).to.be.equal('Test URI');138 }139 });140141 itEth('[cross] Can perform mint()', async ({helper}) => {142 const owner = await helper.eth.createAccountWithBalance(donor);143 const {collectionAddress} = await helper.eth.createERC721MetadataCompatibleNFTCollection(owner, 'A', 'A', 'A', '');144 const caller = await helper.eth.createAccountWithBalance(donor);145 const receiver = helper.eth.createAccount();146147 const collectionEvmOwned = await helper.ethNativeContract.collection(collectionAddress, 'nft', owner);148 const collectionEvm = await helper.ethNativeContract.collection(collectionAddress, 'nft', caller);149 const contract = await proxyWrap(helper, collectionEvm, donor);150 const contractAddressCross = helper.ethCrossAccount.fromAddress(contract.options.address);151 await collectionEvmOwned.methods.addCollectionAdminCross(contractAddressCross).send();152153 {154 const nextTokenId = await contract.methods.nextTokenId().call();155 const result = await contract.methods.mintWithTokenURI(receiver, nextTokenId, 'Test URI').send({from: caller});156 const tokenId = result.events.Transfer.returnValues.tokenId;157 expect(tokenId).to.be.equal('1');158159 const event = helper.eth.normalizeEvents(result.events)160 .find(event => event.event === 'Transfer')!;161 event.address = event.address.toLocaleLowerCase();162163 expect(event).to.be.deep.equal({164 address: collectionAddress.toLocaleLowerCase(),165 event: 'Transfer',166 args: {167 from: '0x0000000000000000000000000000000000000000',168 to: receiver,169 tokenId,170 },171 });172173 expect(await contract.methods.tokenURI(tokenId).call()).to.be.equal('Test URI');174 }175 });176177 178 itEth.skip('Can perform mintBulk()', async ({helper}) => {179 const collection = await helper.nft.mintCollection(donor, {name: 'New', description: 'New collection', tokenPrefix: 'NEW'});180181 const caller = await helper.eth.createAccountWithBalance(donor);182 const receiver = helper.eth.createAccount();183184 const address = helper.ethAddress.fromCollectionId(collection.collectionId);185 const evmCollection = await helper.ethNativeContract.collection(address, 'nft', caller);186 const contract = await proxyWrap(helper, evmCollection, donor);187 await collection.addAdmin(donor, {Ethereum: contract.options.address});188189 {190 const nextTokenId = await contract.methods.nextTokenId().call();191 expect(nextTokenId).to.be.equal('1');192 const result = await contract.methods.mintBulkWithTokenURI(193 receiver,194 [195 [nextTokenId, 'Test URI 0'],196 [+nextTokenId + 1, 'Test URI 1'],197 [+nextTokenId + 2, 'Test URI 2'],198 ],199 ).send({from: caller});200 const events = helper.eth.normalizeEvents(result.events);201202 expect(events).to.be.deep.equal([203 {204 address,205 event: 'Transfer',206 args: {207 from: '0x0000000000000000000000000000000000000000',208 to: receiver,209 tokenId: nextTokenId,210 },211 },212 {213 address,214 event: 'Transfer',215 args: {216 from: '0x0000000000000000000000000000000000000000',217 to: receiver,218 tokenId: String(+nextTokenId + 1),219 },220 },221 {222 address,223 event: 'Transfer',224 args: {225 from: '0x0000000000000000000000000000000000000000',226 to: receiver,227 tokenId: String(+nextTokenId + 2),228 },229 },230 ]);231232 expect(await contract.methods.tokenURI(nextTokenId).call()).to.be.equal('Test URI 0');233 expect(await contract.methods.tokenURI(+nextTokenId + 1).call()).to.be.equal('Test URI 1');234 expect(await contract.methods.tokenURI(+nextTokenId + 2).call()).to.be.equal('Test URI 2');235 }236 });237238 itEth('Can perform burn()', async ({helper}) => {239 const collection = await helper.nft.mintCollection(alice, {name: 'test', description: 'test', tokenPrefix: 'test'});240 const caller = await helper.eth.createAccountWithBalance(donor);241242 const address = helper.ethAddress.fromCollectionId(collection.collectionId);243 const evmCollection = await helper.ethNativeContract.collection(address, 'nft', caller);244 const contract = await proxyWrap(helper, evmCollection, donor);245 const {tokenId} = await collection.mintToken(alice, {Ethereum: contract.options.address});246 await collection.addAdmin(alice, {Ethereum: contract.options.address});247248 {249 const result = await contract.methods.burn(tokenId).send({from: caller});250 const events = helper.eth.normalizeEvents(result.events);251252 expect(events).to.be.deep.equal([253 {254 address,255 event: 'Transfer',256 args: {257 from: contract.options.address,258 to: '0x0000000000000000000000000000000000000000',259 tokenId: tokenId.toString(),260 },261 },262 ]);263 }264 });265266 itEth('Can perform approve()', async ({helper}) => {267 const collection = await helper.nft.mintCollection(alice, {name: 'test', description: 'test', tokenPrefix: 'test'});268 const caller = await helper.eth.createAccountWithBalance(donor);269 const spender = helper.eth.createAccount();270271 const address = helper.ethAddress.fromCollectionId(collection.collectionId);272 const evmCollection = await helper.ethNativeContract.collection(address, 'nft', caller);273 const contract = await proxyWrap(helper, evmCollection, donor);274 const {tokenId} = await collection.mintToken(alice, {Ethereum: contract.options.address});275276 {277 const result = await contract.methods.approve(spender, tokenId).send({from: caller, gas: helper.eth.DEFAULT_GAS});278 const events = helper.eth.normalizeEvents(result.events);279280 expect(events).to.be.deep.equal([281 {282 address,283 event: 'Approval',284 args: {285 owner: contract.options.address,286 approved: spender,287 tokenId: tokenId.toString(),288 },289 },290 ]);291 }292 });293294 itEth('Can perform transferFrom()', async ({helper}) => {295 const collection = await helper.nft.mintCollection(alice, {name: 'test', description: 'test', tokenPrefix: 'test'});296 const caller = await helper.eth.createAccountWithBalance(donor);297 const owner = await helper.eth.createAccountWithBalance(donor);298299 const receiver = helper.eth.createAccount();300301 const address = helper.ethAddress.fromCollectionId(collection.collectionId);302 const evmCollection = await helper.ethNativeContract.collection(address, 'nft', caller);303 const contract = await proxyWrap(helper, evmCollection, donor);304 const {tokenId} = await collection.mintToken(alice, {Ethereum: owner});305306 await evmCollection.methods.approve(contract.options.address, tokenId).send({from: owner});307308 {309 const result = await contract.methods.transferFrom(owner, receiver, tokenId).send({from: caller});310 const events = helper.eth.normalizeEvents(result.events);311 expect(events).to.be.deep.equal([312 {313 address,314 event: 'Transfer',315 args: {316 from: owner,317 to: receiver,318 tokenId: tokenId.toString(),319 },320 },321 ]);322 }323324 {325 const balance = await contract.methods.balanceOf(receiver).call();326 expect(+balance).to.equal(1);327 }328329 {330 const balance = await contract.methods.balanceOf(contract.options.address).call();331 expect(+balance).to.equal(0);332 }333 });334335 itEth('Can perform transfer()', async ({helper}) => {336 const collection = await helper.nft.mintCollection(alice, {name: 'test', description: 'test', tokenPrefix: 'test'});337 const caller = await helper.eth.createAccountWithBalance(donor);338 const receiver = helper.eth.createAccount();339340 const address = helper.ethAddress.fromCollectionId(collection.collectionId);341 const evmCollection = await helper.ethNativeContract.collection(address, 'nft', caller);342 const contract = await proxyWrap(helper, evmCollection, donor);343 const {tokenId} = await collection.mintToken(alice, {Ethereum: contract.options.address});344345 {346 const result = await contract.methods.transfer(receiver, tokenId).send({from: caller});347 const events = helper.eth.normalizeEvents(result.events);348 expect(events).to.be.deep.equal([349 {350 address,351 event: 'Transfer',352 args: {353 from: contract.options.address,354 to: receiver,355 tokenId: tokenId.toString(),356 },357 },358 ]);359 }360361 {362 const balance = await contract.methods.balanceOf(contract.options.address).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});