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 = 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 = 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 = 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 itEth('Can perform mint()', async ({helper}) => {103 const owner = await helper.eth.createAccountWithBalance(donor);104 const {collectionAddress} = await helper.eth.createERC721MetadataCompatibleNFTCollection(owner, 'A', 'A', 'A', '');105 const caller = await helper.eth.createAccountWithBalance(donor);106 const receiver = helper.eth.createAccount();107108 const collectionEvmOwned = helper.ethNativeContract.collection(collectionAddress, 'nft', owner);109 const collectionEvm = helper.ethNativeContract.collection(collectionAddress, 'nft', caller);110 const contract = await proxyWrap(helper, collectionEvm, donor);111 await collectionEvmOwned.methods.addCollectionAdmin(contract.options.address).send();112113 {114 const nextTokenId = await contract.methods.nextTokenId().call();115 const result = await contract.methods.mintWithTokenURI(receiver, nextTokenId, 'Test URI').send({from: caller});116 const tokenId = result.events.Transfer.returnValues.tokenId;117 expect(tokenId).to.be.equal('1');118119 const events = helper.eth.normalizeEvents(result.events);120 events[0].address = events[0].address.toLocaleLowerCase();121122 expect(events).to.be.deep.equal([123 {124 address: collectionAddress.toLocaleLowerCase(),125 event: 'Transfer',126 args: {127 from: '0x0000000000000000000000000000000000000000',128 to: receiver,129 tokenId,130 },131 },132 ]);133134 expect(await contract.methods.tokenURI(tokenId).call()).to.be.equal('Test URI');135 }136 });137138 139 itEth.skip('Can perform mintBulk()', async ({helper}) => {140 const collection = await helper.nft.mintCollection(donor, {name: 'New', description: 'New collection', tokenPrefix: 'NEW'});141142 const caller = await helper.eth.createAccountWithBalance(donor, 30n);143 const receiver = helper.eth.createAccount();144145 const address = helper.ethAddress.fromCollectionId(collection.collectionId);146 const evmCollection = helper.ethNativeContract.collection(address, 'nft', caller);147 const contract = await proxyWrap(helper, evmCollection, donor);148 await collection.addAdmin(donor, {Ethereum: contract.options.address});149150 {151 const nextTokenId = await contract.methods.nextTokenId().call();152 expect(nextTokenId).to.be.equal('1');153 const result = await contract.methods.mintBulkWithTokenURI(154 receiver,155 [156 [nextTokenId, 'Test URI 0'],157 [+nextTokenId + 1, 'Test URI 1'],158 [+nextTokenId + 2, 'Test URI 2'],159 ],160 ).send({from: caller});161 const events = helper.eth.normalizeEvents(result.events);162163 expect(events).to.be.deep.equal([164 {165 address,166 event: 'Transfer',167 args: {168 from: '0x0000000000000000000000000000000000000000',169 to: receiver,170 tokenId: nextTokenId,171 },172 },173 {174 address,175 event: 'Transfer',176 args: {177 from: '0x0000000000000000000000000000000000000000',178 to: receiver,179 tokenId: String(+nextTokenId + 1),180 },181 },182 {183 address,184 event: 'Transfer',185 args: {186 from: '0x0000000000000000000000000000000000000000',187 to: receiver,188 tokenId: String(+nextTokenId + 2),189 },190 },191 ]);192193 expect(await contract.methods.tokenURI(nextTokenId).call()).to.be.equal('Test URI 0');194 expect(await contract.methods.tokenURI(+nextTokenId + 1).call()).to.be.equal('Test URI 1');195 expect(await contract.methods.tokenURI(+nextTokenId + 2).call()).to.be.equal('Test URI 2');196 }197 });198199 itEth('Can perform burn()', async ({helper}) => {200 const collection = await helper.nft.mintCollection(alice, {name: 'test', description: 'test', tokenPrefix: 'test'});201 const caller = await helper.eth.createAccountWithBalance(donor);202203 const address = helper.ethAddress.fromCollectionId(collection.collectionId);204 const evmCollection = helper.ethNativeContract.collection(address, 'nft', caller);205 const contract = await proxyWrap(helper, evmCollection, donor);206 const {tokenId} = await collection.mintToken(alice, {Ethereum: contract.options.address});207 await collection.addAdmin(alice, {Ethereum: contract.options.address});208209 {210 const result = await contract.methods.burn(tokenId).send({from: caller});211 const events = helper.eth.normalizeEvents(result.events);212213 expect(events).to.be.deep.equal([214 {215 address,216 event: 'Transfer',217 args: {218 from: contract.options.address,219 to: '0x0000000000000000000000000000000000000000',220 tokenId: tokenId.toString(),221 },222 },223 ]);224 }225 });226227 itEth('Can perform approve()', async ({helper}) => {228 const collection = await helper.nft.mintCollection(alice, {name: 'test', description: 'test', tokenPrefix: 'test'});229 const caller = await helper.eth.createAccountWithBalance(donor);230 const spender = helper.eth.createAccount();231232 const address = helper.ethAddress.fromCollectionId(collection.collectionId);233 const evmCollection = helper.ethNativeContract.collection(address, 'nft', caller);234 const contract = await proxyWrap(helper, evmCollection, donor);235 const {tokenId} = await collection.mintToken(alice, {Ethereum: contract.options.address});236237 {238 const result = await contract.methods.approve(spender, tokenId).send({from: caller, gas: helper.eth.DEFAULT_GAS});239 const events = helper.eth.normalizeEvents(result.events);240241 expect(events).to.be.deep.equal([242 {243 address,244 event: 'Approval',245 args: {246 owner: contract.options.address,247 approved: spender,248 tokenId: tokenId.toString(),249 },250 },251 ]);252 }253 });254255 itEth('Can perform transferFrom()', async ({helper}) => {256 const collection = await helper.nft.mintCollection(alice, {name: 'test', description: 'test', tokenPrefix: 'test'});257 const caller = await helper.eth.createAccountWithBalance(donor);258 const owner = await helper.eth.createAccountWithBalance(donor);259260 const receiver = helper.eth.createAccount();261262 const address = helper.ethAddress.fromCollectionId(collection.collectionId);263 const evmCollection = helper.ethNativeContract.collection(address, 'nft', caller);264 const contract = await proxyWrap(helper, evmCollection, donor);265 const {tokenId} = await collection.mintToken(alice, {Ethereum: owner});266267 await evmCollection.methods.approve(contract.options.address, tokenId).send({from: owner});268269 {270 const result = await contract.methods.transferFrom(owner, receiver, tokenId).send({from: caller});271 const events = helper.eth.normalizeEvents(result.events);272 expect(events).to.be.deep.equal([273 {274 address,275 event: 'Transfer',276 args: {277 from: owner,278 to: receiver,279 tokenId: tokenId.toString(),280 },281 },282 ]);283 }284285 {286 const balance = await contract.methods.balanceOf(receiver).call();287 expect(+balance).to.equal(1);288 }289290 {291 const balance = await contract.methods.balanceOf(contract.options.address).call();292 expect(+balance).to.equal(0);293 }294 });295296 itEth('Can perform transfer()', async ({helper}) => {297 const collection = await helper.nft.mintCollection(alice, {name: 'test', description: 'test', tokenPrefix: 'test'});298 const caller = await helper.eth.createAccountWithBalance(donor);299 const receiver = helper.eth.createAccount();300301 const address = helper.ethAddress.fromCollectionId(collection.collectionId);302 const evmCollection = helper.ethNativeContract.collection(address, 'nft', caller);303 const contract = await proxyWrap(helper, evmCollection, donor);304 const {tokenId} = await collection.mintToken(alice, {Ethereum: contract.options.address});305306 {307 const result = await contract.methods.transfer(receiver, tokenId).send({from: caller});308 const events = helper.eth.normalizeEvents(result.events);309 expect(events).to.be.deep.equal([310 {311 address,312 event: 'Transfer',313 args: {314 from: contract.options.address,315 to: receiver,316 tokenId: tokenId.toString(),317 },318 },319 ]);320 }321322 {323 const balance = await contract.methods.balanceOf(contract.options.address).call();324 expect(+balance).to.equal(0);325 }326327 {328 const balance = await contract.methods.balanceOf(receiver).call();329 expect(+balance).to.equal(1);330 }331 });332});