1234567891011121314151617import {readFile} from 'fs/promises';18import {IKeyringPair} from '@polkadot/types/types';19import {EthUniqueHelper, itEth, usingEthPlaygrounds, expect} from '../util/playgrounds';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 = privateKey('//Alice');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 = privateKey('//Alice');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.createNonfungibleCollection(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 expect(nextTokenId).to.be.equal('1');116 const result = await contract.methods.mintWithTokenURI(117 receiver,118 nextTokenId,119 'Test URI',120 ).send({from: caller});121 const events = helper.eth.normalizeEvents(result.events);122 events[0].address = events[0].address.toLocaleLowerCase();123124 expect(events).to.be.deep.equal([125 {126 address: collectionAddress.toLocaleLowerCase(),127 event: 'Transfer',128 args: {129 from: '0x0000000000000000000000000000000000000000',130 to: receiver,131 tokenId: nextTokenId,132 },133 },134 ]);135136 expect(await contract.methods.tokenURI(nextTokenId).call()).to.be.equal('Test URI');137 }138 });139140 141 itEth.skip('Can perform mintBulk()', async ({helper, privateKey}) => {142 const api = helper.getApi();143 const web3 = helper.getWeb3();144 const privateKeyWrapper = privateKey;145 146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207 });208209 itEth('Can perform burn()', async ({helper}) => {210 const collection = await helper.nft.mintCollection(alice, {name: 'test', description: 'test', tokenPrefix: 'test'});211 const caller = await helper.eth.createAccountWithBalance(donor);212213 const address = helper.ethAddress.fromCollectionId(collection.collectionId);214 const evmCollection = helper.ethNativeContract.collection(address, 'nft', caller);215 const contract = await proxyWrap(helper, evmCollection, donor);216 const {tokenId} = await collection.mintToken(alice, {Ethereum: contract.options.address});217 await collection.addAdmin(alice, {Ethereum: contract.options.address});218219 {220 const result = await contract.methods.burn(tokenId).send({from: caller});221 const events = helper.eth.normalizeEvents(result.events);222223 expect(events).to.be.deep.equal([224 {225 address,226 event: 'Transfer',227 args: {228 from: contract.options.address,229 to: '0x0000000000000000000000000000000000000000',230 tokenId: tokenId.toString(),231 },232 },233 ]);234 }235 });236237 itEth('Can perform approve()', async ({helper}) => {238 const collection = await helper.nft.mintCollection(alice, {name: 'test', description: 'test', tokenPrefix: 'test'});239 const caller = await helper.eth.createAccountWithBalance(donor);240 const spender = helper.eth.createAccount();241242 const address = helper.ethAddress.fromCollectionId(collection.collectionId);243 const evmCollection = 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});246247 {248 const result = await contract.methods.approve(spender, tokenId).send({from: caller, gas: helper.eth.DEFAULT_GAS});249 const events = helper.eth.normalizeEvents(result.events);250251 expect(events).to.be.deep.equal([252 {253 address,254 event: 'Approval',255 args: {256 owner: contract.options.address,257 approved: spender,258 tokenId: tokenId.toString(),259 },260 },261 ]);262 }263 });264265 itEth('Can perform transferFrom()', async ({helper}) => {266 const collection = await helper.nft.mintCollection(alice, {name: 'test', description: 'test', tokenPrefix: 'test'});267 const caller = await helper.eth.createAccountWithBalance(donor);268 const owner = await helper.eth.createAccountWithBalance(donor);269270 const receiver = helper.eth.createAccount();271272 const address = helper.ethAddress.fromCollectionId(collection.collectionId);273 const evmCollection = helper.ethNativeContract.collection(address, 'nft', caller);274 const contract = await proxyWrap(helper, evmCollection, donor);275 const {tokenId} = await collection.mintToken(alice, {Ethereum: owner});276277 await evmCollection.methods.approve(contract.options.address, tokenId).send({from: owner});278279 {280 const result = await contract.methods.transferFrom(owner, receiver, tokenId).send({from: caller});281 const events = helper.eth.normalizeEvents(result.events);282 expect(events).to.be.deep.equal([283 {284 address,285 event: 'Transfer',286 args: {287 from: owner,288 to: receiver,289 tokenId: tokenId.toString(),290 },291 },292 ]);293 }294295 {296 const balance = await contract.methods.balanceOf(receiver).call();297 expect(+balance).to.equal(1);298 }299300 {301 const balance = await contract.methods.balanceOf(contract.options.address).call();302 expect(+balance).to.equal(0);303 }304 });305306 itEth('Can perform transfer()', async ({helper}) => {307 const collection = await helper.nft.mintCollection(alice, {name: 'test', description: 'test', tokenPrefix: 'test'});308 const caller = await helper.eth.createAccountWithBalance(donor);309 const receiver = helper.eth.createAccount();310311 const address = helper.ethAddress.fromCollectionId(collection.collectionId);312 const evmCollection = helper.ethNativeContract.collection(address, 'nft', caller);313 const contract = await proxyWrap(helper, evmCollection, donor);314 const {tokenId} = await collection.mintToken(alice, {Ethereum: contract.options.address});315316 {317 const result = await contract.methods.transfer(receiver, tokenId).send({from: caller});318 const events = helper.eth.normalizeEvents(result.events);319 expect(events).to.be.deep.equal([320 {321 address,322 event: 'Transfer',323 args: {324 from: contract.options.address,325 to: receiver,326 tokenId: tokenId.toString(),327 },328 },329 ]);330 }331332 {333 const balance = await contract.methods.balanceOf(contract.options.address).call();334 expect(+balance).to.equal(0);335 }336337 {338 const balance = await contract.methods.balanceOf(receiver).call();339 expect(+balance).to.equal(1);340 }341 });342});