1234567891011121314151617import {GAS_ARGS, itWeb3, normalizeEvents} from '../util/helpers';18import {expect} from 'chai';19import {readFile} from 'fs/promises';20import {IKeyringPair} from '@polkadot/types/types';21import {EthUniqueHelper, itEth, usingEthPlaygrounds} from '../util/playgrounds';2223async function proxyWrap(helper: EthUniqueHelper, wrapped: any, donor: IKeyringPair) {24 25 const owner = await helper.eth.createAccountWithBalance(donor);26 const web3 = helper.getWeb3();27 const proxyContract = new web3.eth.Contract(JSON.parse((await readFile(`${__dirname}/UniqueNFTProxy.abi`)).toString()), undefined, {28 from: owner,29 ...GAS_ARGS,30 });31 const proxy = await proxyContract.deploy({data: (await readFile(`${__dirname}/UniqueNFTProxy.bin`)).toString(), arguments: [wrapped.options.address]}).send({from: owner});32 return proxy;33}3435describe('NFT (Via EVM proxy): Information getting', () => {36 let alice: IKeyringPair;37 let donor: IKeyringPair;3839 before(async function() {40 await usingEthPlaygrounds(async (helper, privateKey) => {41 donor = privateKey('//Alice');42 [alice] = await helper.arrange.createAccounts([10n], donor);43 });44 });4546 itEth('totalSupply', async ({helper}) => {47 const collection = await helper.nft.mintCollection(alice, {name: 'test', description: 'test', tokenPrefix: 'test'});48 const caller = await helper.eth.createAccountWithBalance(donor);49 await collection.mintToken(alice, {Substrate: alice.address});5051 const address = helper.ethAddress.fromCollectionId(collection.collectionId);52 const evmCollection = helper.ethNativeContract.collection(address, 'nft', caller);53 const contract = await proxyWrap(helper, evmCollection, donor);54 const totalSupply = await contract.methods.totalSupply().call();5556 expect(totalSupply).to.equal('1');57 });5859 itEth('balanceOf', async ({helper}) => {60 const collection = await helper.nft.mintCollection(alice, {name: 'test', description: 'test', tokenPrefix: 'test'});6162 const caller = await helper.eth.createAccountWithBalance(donor);63 await collection.mintMultipleTokens(alice, [64 {owner: {Ethereum: caller}},65 {owner: {Ethereum: caller}},66 {owner: {Ethereum: caller}},67 ]);6869 const address = helper.ethAddress.fromCollectionId(collection.collectionId);70 const evmCollection = helper.ethNativeContract.collection(address, 'nft', caller);71 const contract = await proxyWrap(helper, evmCollection, donor);72 const balance = await contract.methods.balanceOf(caller).call();7374 expect(balance).to.equal('3');75 });7677 itEth('ownerOf', async ({helper}) => {78 const collection = await helper.nft.mintCollection(alice, {name: 'test', description: 'test', tokenPrefix: 'test'});7980 const caller = await helper.eth.createAccountWithBalance(donor);81 const {tokenId} = await collection.mintToken(alice, {Ethereum: caller});8283 const address = helper.ethAddress.fromCollectionId(collection.collectionId);84 const evmCollection = helper.ethNativeContract.collection(address, 'nft', caller);85 const contract = await proxyWrap(helper, evmCollection, donor);86 const owner = await contract.methods.ownerOf(tokenId).call();8788 expect(owner).to.equal(caller);89 });90});9192describe('NFT (Via EVM proxy): Plain calls', () => {93 let alice: IKeyringPair;94 let donor: IKeyringPair;9596 before(async function() {97 await usingEthPlaygrounds(async (helper, privateKey) => {98 donor = privateKey('//Alice');99 [alice] = await helper.arrange.createAccounts([10n], donor);100 });101 });102103 itEth('Can perform mint()', async ({helper}) => {104 const owner = await helper.eth.createAccountWithBalance(donor);105 const {collectionAddress} = await helper.eth.createNonfungibleCollection(owner, 'A', 'A', 'A');106 const caller = await helper.eth.createAccountWithBalance(donor);107 const receiver = helper.eth.createAccount();108109 const collectionEvmOwned = helper.ethNativeContract.collection(collectionAddress, 'nft', owner);110 const collectionEvm = helper.ethNativeContract.collection(collectionAddress, 'nft', caller);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 expect(nextTokenId).to.be.equal('1');117 const result = await contract.methods.mintWithTokenURI(118 receiver,119 nextTokenId,120 'Test URI',121 ).send({from: caller});122 const events = normalizeEvents(result.events);123 events[0].address = events[0].address.toLocaleLowerCase();124125 expect(events).to.be.deep.equal([126 {127 address: collectionAddress.toLocaleLowerCase(),128 event: 'Transfer',129 args: {130 from: '0x0000000000000000000000000000000000000000',131 to: receiver,132 tokenId: nextTokenId,133 },134 },135 ]);136137 expect(await contract.methods.tokenURI(nextTokenId).call()).to.be.equal('Test URI');138 }139 });140141 142 itWeb3.skip('Can perform mintBulk()', async ({web3, api, privateKeyWrapper}) => {143 144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205 });206207 itEth('Can perform burn()', async ({helper}) => {208 const collection = await helper.nft.mintCollection(alice, {name: 'test', description: 'test', tokenPrefix: 'test'});209 const caller = await helper.eth.createAccountWithBalance(donor);210211 const address = helper.ethAddress.fromCollectionId(collection.collectionId);212 const evmCollection = helper.ethNativeContract.collection(address, 'nft', caller);213 const contract = await proxyWrap(helper, evmCollection, donor);214 const {tokenId} = await collection.mintToken(alice, {Ethereum: contract.options.address});215 await collection.addAdmin(alice, {Ethereum: contract.options.address});216217 {218 const result = await contract.methods.burn(tokenId).send({from: caller});219 const events = normalizeEvents(result.events);220221 expect(events).to.be.deep.equal([222 {223 address,224 event: 'Transfer',225 args: {226 from: contract.options.address,227 to: '0x0000000000000000000000000000000000000000',228 tokenId: tokenId.toString(),229 },230 },231 ]);232 }233 });234235 itEth('Can perform approve()', async ({helper}) => {236 const collection = await helper.nft.mintCollection(alice, {name: 'test', description: 'test', tokenPrefix: 'test'});237 const caller = await helper.eth.createAccountWithBalance(donor);238 const spender = helper.eth.createAccount();239240 const address = helper.ethAddress.fromCollectionId(collection.collectionId);241 const evmCollection = helper.ethNativeContract.collection(address, 'nft', caller);242 const contract = await proxyWrap(helper, evmCollection, donor);243 const {tokenId} = await collection.mintToken(alice, {Ethereum: contract.options.address});244245 {246 const result = await contract.methods.approve(spender, tokenId).send({from: caller, ...GAS_ARGS});247 const events = normalizeEvents(result.events);248249 expect(events).to.be.deep.equal([250 {251 address,252 event: 'Approval',253 args: {254 owner: contract.options.address,255 approved: spender,256 tokenId: tokenId.toString(),257 },258 },259 ]);260 }261 });262263 itEth('Can perform transferFrom()', 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 owner = await helper.eth.createAccountWithBalance(donor);267268 const receiver = helper.eth.createAccount();269270 const address = helper.ethAddress.fromCollectionId(collection.collectionId);271 const evmCollection = helper.ethNativeContract.collection(address, 'nft', caller);272 const contract = await proxyWrap(helper, evmCollection, donor);273 const {tokenId} = await collection.mintToken(alice, {Ethereum: owner});274275 await evmCollection.methods.approve(contract.options.address, tokenId).send({from: owner});276277 {278 const result = await contract.methods.transferFrom(owner, receiver, tokenId).send({from: caller});279 const events = normalizeEvents(result.events);280 expect(events).to.be.deep.equal([281 {282 address,283 event: 'Transfer',284 args: {285 from: owner,286 to: receiver,287 tokenId: tokenId.toString(),288 },289 },290 ]);291 }292293 {294 const balance = await contract.methods.balanceOf(receiver).call();295 expect(+balance).to.equal(1);296 }297298 {299 const balance = await contract.methods.balanceOf(contract.options.address).call();300 expect(+balance).to.equal(0);301 }302 });303304 itEth('Can perform transfer()', async ({helper}) => {305 const collection = await helper.nft.mintCollection(alice, {name: 'test', description: 'test', tokenPrefix: 'test'});306 const caller = await helper.eth.createAccountWithBalance(donor);307 const receiver = helper.eth.createAccount();308309 const address = helper.ethAddress.fromCollectionId(collection.collectionId);310 const evmCollection = helper.ethNativeContract.collection(address, 'nft', caller);311 const contract = await proxyWrap(helper, evmCollection, donor);312 const {tokenId} = await collection.mintToken(alice, {Ethereum: contract.options.address});313314 {315 const result = await contract.methods.transfer(receiver, tokenId).send({from: caller});316 const events = normalizeEvents(result.events);317 expect(events).to.be.deep.equal([318 {319 address,320 event: 'Transfer',321 args: {322 from: contract.options.address,323 to: receiver,324 tokenId: tokenId.toString(),325 },326 },327 ]);328 }329330 {331 const balance = await contract.methods.balanceOf(contract.options.address).call();332 expect(+balance).to.equal(0);333 }334335 {336 const balance = await contract.methods.balanceOf(receiver).call();337 expect(+balance).to.equal(1);338 }339 });340});