1234567891011121314151617import {readFile} from 'fs/promises';18import {IKeyringPair} from '@polkadot/types/types';19import {EthUniqueHelper, itEth, usingEthPlaygrounds, expect} from '../util';202122async function proxyWrap(helper: EthUniqueHelper, wrapped: any, donor: IKeyringPair) {23 24 const owner = await helper.eth.createAccountWithBalance(donor);25 const web3 = helper.getWeb3();26 const proxyContract = new web3.eth.Contract(JSON.parse((await readFile(`${__dirname}/UniqueNFTProxy.abi`)).toString()), undefined, {27 from: owner,28 gas: helper.eth.DEFAULT_GAS,29 });30 const proxy = await proxyContract.deploy({data: (await readFile(`${__dirname}/UniqueNFTProxy.bin`)).toString(), arguments: [wrapped.options.address]}).send({from: owner});31 return proxy;32}3334describe('NFT (Via EVM proxy): Information getting', () => {35 let alice: IKeyringPair;36 let donor: IKeyringPair;3738 before(async function() {39 await usingEthPlaygrounds(async (helper, privateKey) => {40 donor = await privateKey({filename: __filename});41 [alice] = await helper.arrange.createAccounts([10n], donor);42 });43 });4445 itEth('totalSupply', async ({helper}) => {46 const collection = await helper.nft.mintCollection(alice, {name: 'test', description: 'test', tokenPrefix: 'test'});47 const caller = await helper.eth.createAccountWithBalance(donor);48 await collection.mintToken(alice, {Substrate: alice.address});4950 const address = helper.ethAddress.fromCollectionId(collection.collectionId);51 const evmCollection = await helper.ethNativeContract.collection(address, 'nft', caller);52 const contract = await proxyWrap(helper, evmCollection, donor);53 const totalSupply = await contract.methods.totalSupply().call();5455 expect(totalSupply).to.equal('1');56 });5758 itEth('balanceOf', async ({helper}) => {59 const collection = await helper.nft.mintCollection(alice, {name: 'test', description: 'test', tokenPrefix: 'test'});6061 const caller = await helper.eth.createAccountWithBalance(donor);62 await collection.mintMultipleTokens(alice, [63 {owner: {Ethereum: caller}},64 {owner: {Ethereum: caller}},65 {owner: {Ethereum: caller}},66 ]);6768 const address = helper.ethAddress.fromCollectionId(collection.collectionId);69 const evmCollection = await helper.ethNativeContract.collection(address, 'nft', caller);70 const contract = await proxyWrap(helper, evmCollection, donor);71 const balance = await contract.methods.balanceOf(caller).call();7273 expect(balance).to.equal('3');74 });7576 itEth('ownerOf', async ({helper}) => {77 const collection = await helper.nft.mintCollection(alice, {name: 'test', description: 'test', tokenPrefix: 'test'});7879 const caller = await helper.eth.createAccountWithBalance(donor);80 const {tokenId} = await collection.mintToken(alice, {Ethereum: caller});8182 const address = helper.ethAddress.fromCollectionId(collection.collectionId);83 const evmCollection = await helper.ethNativeContract.collection(address, 'nft', caller);84 const contract = await proxyWrap(helper, evmCollection, donor);85 const owner = await contract.methods.ownerOf(tokenId).call();8687 expect(owner).to.equal(caller);88 });89});9091describe('NFT (Via EVM proxy): Plain calls', () => {92 let alice: IKeyringPair;93 let donor: IKeyringPair;9495 before(async function() {96 await usingEthPlaygrounds(async (helper, privateKey) => {97 donor = await privateKey({filename: __filename});98 [alice] = await helper.arrange.createAccounts([10n], donor);99 });100 });101102 103 itEth('[eth] Can perform mint()', async ({helper}) => {104 const owner = await helper.eth.createAccountWithBalance(donor);105 const {collectionAddress} = await helper.eth.createERC721MetadataCompatibleNFTCollection(owner, 'A', 'A', 'A', '');106 const caller = await helper.eth.createAccountWithBalance(donor);107 const receiver = helper.eth.createAccount();108109 const collectionEvmOwned = await helper.ethNativeContract.collection(collectionAddress, 'nft', owner, true);110 const collectionEvm = await helper.ethNativeContract.collection(collectionAddress, 'nft', caller, true);111 const contract = await proxyWrap(helper, collectionEvm, donor);112 await collectionEvmOwned.methods.addCollectionAdmin(contract.options.address).send();113114 {115 const nextTokenId = await contract.methods.nextTokenId().call();116 const result = await contract.methods.mintWithTokenURI(receiver, nextTokenId, 'Test URI').send({from: caller});117 const tokenId = result.events.Transfer.returnValues.tokenId;118 expect(tokenId).to.be.equal('1');119120 const event = helper.eth.normalizeEvents(result.events)121 .find(event => event.event === 'Transfer')!;122 event.address = event.address.toLocaleLowerCase();123124 expect(event).to.be.deep.equal({125 address: collectionAddress.toLocaleLowerCase(),126 event: 'Transfer',127 args: {128 from: '0x0000000000000000000000000000000000000000',129 to: receiver,130 tokenId,131 },132 });133134 expect(await contract.methods.tokenURI(tokenId).call()).to.be.equal('Test URI');135 }136 });137138 itEth('[cross] Can perform mint()', async ({helper}) => {139 const owner = await helper.eth.createAccountWithBalance(donor);140 const {collectionAddress} = await helper.eth.createERC721MetadataCompatibleNFTCollection(owner, 'A', 'A', 'A', '');141 const caller = await helper.eth.createAccountWithBalance(donor);142 const receiver = helper.eth.createAccount();143144 const collectionEvmOwned = await helper.ethNativeContract.collection(collectionAddress, 'nft', owner);145 const collectionEvm = await helper.ethNativeContract.collection(collectionAddress, 'nft', caller);146 const contract = await proxyWrap(helper, collectionEvm, donor);147 const contractAddressCross = helper.ethCrossAccount.fromAddress(contract.options.address);148 await collectionEvmOwned.methods.addCollectionAdminCross(contractAddressCross).send();149150 {151 const nextTokenId = await contract.methods.nextTokenId().call();152 const result = await contract.methods.mintWithTokenURI(receiver, nextTokenId, 'Test URI').send({from: caller});153 const tokenId = result.events.Transfer.returnValues.tokenId;154 expect(tokenId).to.be.equal('1');155156 const event = helper.eth.normalizeEvents(result.events)157 .find(event => event.event === 'Transfer')!;158 event.address = event.address.toLocaleLowerCase();159160 expect(event).to.be.deep.equal({161 address: collectionAddress.toLocaleLowerCase(),162 event: 'Transfer',163 args: {164 from: '0x0000000000000000000000000000000000000000',165 to: receiver,166 tokenId,167 },168 });169170 expect(await contract.methods.tokenURI(tokenId).call()).to.be.equal('Test URI');171 }172 });173174 175 itEth.skip('Can perform mintBulk()', async ({helper}) => {176 const collection = await helper.nft.mintCollection(donor, {name: 'New', description: 'New collection', tokenPrefix: 'NEW'});177178 const caller = await helper.eth.createAccountWithBalance(donor, 30n);179 const receiver = helper.eth.createAccount();180181 const address = helper.ethAddress.fromCollectionId(collection.collectionId);182 const evmCollection = await helper.ethNativeContract.collection(address, 'nft', caller);183 const contract = await proxyWrap(helper, evmCollection, donor);184 await collection.addAdmin(donor, {Ethereum: contract.options.address});185186 {187 const nextTokenId = await contract.methods.nextTokenId().call();188 expect(nextTokenId).to.be.equal('1');189 const result = await contract.methods.mintBulkWithTokenURI(190 receiver,191 [192 [nextTokenId, 'Test URI 0'],193 [+nextTokenId + 1, 'Test URI 1'],194 [+nextTokenId + 2, 'Test URI 2'],195 ],196 ).send({from: caller});197 const events = helper.eth.normalizeEvents(result.events);198199 expect(events).to.be.deep.equal([200 {201 address,202 event: 'Transfer',203 args: {204 from: '0x0000000000000000000000000000000000000000',205 to: receiver,206 tokenId: nextTokenId,207 },208 },209 {210 address,211 event: 'Transfer',212 args: {213 from: '0x0000000000000000000000000000000000000000',214 to: receiver,215 tokenId: String(+nextTokenId + 1),216 },217 },218 {219 address,220 event: 'Transfer',221 args: {222 from: '0x0000000000000000000000000000000000000000',223 to: receiver,224 tokenId: String(+nextTokenId + 2),225 },226 },227 ]);228229 expect(await contract.methods.tokenURI(nextTokenId).call()).to.be.equal('Test URI 0');230 expect(await contract.methods.tokenURI(+nextTokenId + 1).call()).to.be.equal('Test URI 1');231 expect(await contract.methods.tokenURI(+nextTokenId + 2).call()).to.be.equal('Test URI 2');232 }233 });234235 itEth('Can perform burn()', async ({helper}) => {236 const collection = await helper.nft.mintCollection(alice, {name: 'test', description: 'test', tokenPrefix: 'test'});237 const caller = await helper.eth.createAccountWithBalance(donor);238239 const address = helper.ethAddress.fromCollectionId(collection.collectionId);240 const evmCollection = await helper.ethNativeContract.collection(address, 'nft', caller);241 const contract = await proxyWrap(helper, evmCollection, donor);242 const {tokenId} = await collection.mintToken(alice, {Ethereum: contract.options.address});243 await collection.addAdmin(alice, {Ethereum: contract.options.address});244245 {246 const result = await contract.methods.burn(tokenId).send({from: caller});247 const events = helper.eth.normalizeEvents(result.events);248249 expect(events).to.be.deep.equal([250 {251 address,252 event: 'Transfer',253 args: {254 from: contract.options.address,255 to: '0x0000000000000000000000000000000000000000',256 tokenId: tokenId.toString(),257 },258 },259 ]);260 }261 });262263 itEth('Can perform approve()', async ({helper}) => {264 const collection = await helper.nft.mintCollection(alice, {name: 'test', description: 'test', tokenPrefix: 'test'});265 const caller = await helper.eth.createAccountWithBalance(donor);266 const spender = helper.eth.createAccount();267268 const address = helper.ethAddress.fromCollectionId(collection.collectionId);269 const evmCollection = await helper.ethNativeContract.collection(address, 'nft', caller);270 const contract = await proxyWrap(helper, evmCollection, donor);271 const {tokenId} = await collection.mintToken(alice, {Ethereum: contract.options.address});272273 {274 const result = await contract.methods.approve(spender, tokenId).send({from: caller, gas: helper.eth.DEFAULT_GAS});275 const events = helper.eth.normalizeEvents(result.events);276277 expect(events).to.be.deep.equal([278 {279 address,280 event: 'Approval',281 args: {282 owner: contract.options.address,283 approved: spender,284 tokenId: tokenId.toString(),285 },286 },287 ]);288 }289 });290291 itEth('Can perform transferFrom()', async ({helper}) => {292 const collection = await helper.nft.mintCollection(alice, {name: 'test', description: 'test', tokenPrefix: 'test'});293 const caller = await helper.eth.createAccountWithBalance(donor);294 const owner = await helper.eth.createAccountWithBalance(donor);295296 const receiver = helper.eth.createAccount();297298 const address = helper.ethAddress.fromCollectionId(collection.collectionId);299 const evmCollection = await helper.ethNativeContract.collection(address, 'nft', caller);300 const contract = await proxyWrap(helper, evmCollection, donor);301 const {tokenId} = await collection.mintToken(alice, {Ethereum: owner});302303 await evmCollection.methods.approve(contract.options.address, tokenId).send({from: owner});304305 {306 const result = await contract.methods.transferFrom(owner, receiver, tokenId).send({from: caller});307 const events = helper.eth.normalizeEvents(result.events);308 expect(events).to.be.deep.equal([309 {310 address,311 event: 'Transfer',312 args: {313 from: owner,314 to: receiver,315 tokenId: tokenId.toString(),316 },317 },318 ]);319 }320321 {322 const balance = await contract.methods.balanceOf(receiver).call();323 expect(+balance).to.equal(1);324 }325326 {327 const balance = await contract.methods.balanceOf(contract.options.address).call();328 expect(+balance).to.equal(0);329 }330 });331332 itEth('Can perform transfer()', async ({helper}) => {333 const collection = await helper.nft.mintCollection(alice, {name: 'test', description: 'test', tokenPrefix: 'test'});334 const caller = await helper.eth.createAccountWithBalance(donor);335 const receiver = helper.eth.createAccount();336337 const address = helper.ethAddress.fromCollectionId(collection.collectionId);338 const evmCollection = await helper.ethNativeContract.collection(address, 'nft', caller);339 const contract = await proxyWrap(helper, evmCollection, donor);340 const {tokenId} = await collection.mintToken(alice, {Ethereum: contract.options.address});341342 {343 const result = await contract.methods.transfer(receiver, tokenId).send({from: caller});344 const events = helper.eth.normalizeEvents(result.events);345 expect(events).to.be.deep.equal([346 {347 address,348 event: 'Transfer',349 args: {350 from: contract.options.address,351 to: receiver,352 tokenId: tokenId.toString(),353 },354 },355 ]);356 }357358 {359 const balance = await contract.methods.balanceOf(contract.options.address).call();360 expect(+balance).to.equal(0);361 }362363 {364 const balance = await contract.methods.balanceOf(receiver).call();365 expect(+balance).to.equal(1);366 }367 });368});