difftreelog
Add checks to mintCross tests
in: master
3 files changed
tests/src/eth/fungible.test.tsdiffbeforeafterboth1// Copyright 2019-2022 Unique Network (Gibraltar) Ltd.2// This file is part of Unique Network.34// Unique Network is free software: you can redistribute it and/or modify5// it under the terms of the GNU General Public License as published by6// the Free Software Foundation, either version 3 of the License, or7// (at your option) any later version.89// Unique Network is distributed in the hope that it will be useful,10// but WITHOUT ANY WARRANTY; without even the implied warranty of11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the12// GNU General Public License for more details.1314// You should have received a copy of the GNU General Public License15// along with Unique Network. If not, see <http://www.gnu.org/licenses/>.1617import {expect, itEth, usingEthPlaygrounds} from './util';18import {IKeyringPair} from '@polkadot/types/types';1920describe('Fungible: Information getting', () => {21 let donor: IKeyringPair;22 let alice: IKeyringPair;2324 before(async function() {25 await usingEthPlaygrounds(async (helper, privateKey) => {26 donor = await privateKey({filename: __filename});27 [alice] = await helper.arrange.createAccounts([20n], donor);28 });29 });3031 itEth('totalSupply', async ({helper}) => {32 const caller = await helper.eth.createAccountWithBalance(donor);33 const collection = await helper.ft.mintCollection(alice);34 await collection.mint(alice, 200n);3536 const contract = helper.ethNativeContract.collectionById(collection.collectionId, 'ft', caller);37 const totalSupply = await contract.methods.totalSupply().call();38 expect(totalSupply).to.equal('200');39 });4041 itEth('balanceOf', async ({helper}) => {42 const caller = await helper.eth.createAccountWithBalance(donor);43 const collection = await helper.ft.mintCollection(alice);44 await collection.mint(alice, 200n, {Ethereum: caller});4546 const contract = helper.ethNativeContract.collectionById(collection.collectionId, 'ft', caller);47 const balance = await contract.methods.balanceOf(caller).call();48 expect(balance).to.equal('200');49 });50});5152describe('Fungible: Plain calls', () => {53 let donor: IKeyringPair;54 let alice: IKeyringPair;55 let owner: IKeyringPair;5657 before(async function() {58 await usingEthPlaygrounds(async (helper, privateKey) => {59 donor = await privateKey({filename: __filename});60 [alice, owner] = await helper.arrange.createAccounts([30n, 20n], donor);61 });62 });6364 itEth('Can perform mint()', async ({helper}) => {65 const owner = await helper.eth.createAccountWithBalance(donor);66 const receiver = helper.eth.createAccount();67 const collection = await helper.ft.mintCollection(alice);68 await collection.addAdmin(alice, {Ethereum: owner});6970 const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);71 const contract = helper.ethNativeContract.collection(collectionAddress, 'ft', owner);7273 const result = await contract.methods.mint(receiver, 100).send();74 75 const event = result.events.Transfer;76 expect(event.address).to.equal(collectionAddress);77 expect(event.returnValues.from).to.equal('0x0000000000000000000000000000000000000000');78 expect(event.returnValues.to).to.equal(receiver);79 expect(event.returnValues.value).to.equal('100');80 });81 82 83 itEth('Can perform mintCross()', async ({helper}) => {84 const receiverCross = helper.ethCrossAccount.fromKeyringPair(owner);85 const ethOwner = await helper.eth.createAccountWithBalance(donor);86 const collection = await helper.ft.mintCollection(alice);87 await collection.addAdmin(alice, {Ethereum: ethOwner});8889 const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);90 const contract = helper.ethNativeContract.collection(collectionAddress, 'ft', ethOwner);9192 const result = await contract.methods.mintCross(receiverCross, 100).send();93 94 const event = result.events.Transfer;95 expect(event.address).to.equal(collectionAddress);96 expect(event.returnValues.from).to.equal('0x0000000000000000000000000000000000000000');97 expect(event.returnValues.to).to.equal(helper.address.substrateToEth(owner.address));98 expect(event.returnValues.value).to.equal('100');99 });100101 itEth('Can perform mintBulk()', async ({helper}) => {102 const owner = await helper.eth.createAccountWithBalance(donor);103 const bulkSize = 3;104 const receivers = [...new Array(bulkSize)].map(() => helper.eth.createAccount());105 const collection = await helper.ft.mintCollection(alice);106 await collection.addAdmin(alice, {Ethereum: owner});107108 const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);109 const contract = helper.ethNativeContract.collection(collectionAddress, 'ft', owner);110111 const result = await contract.methods.mintBulk(Array.from({length: bulkSize}, (_, i) => (112 [receivers[i], (i + 1) * 10]113 ))).send();114 const events = result.events.Transfer.sort((a: any, b: any) => +a.returnValues.value - b.returnValues.value);115 for (let i = 0; i < bulkSize; i++) {116 const event = events[i];117 expect(event.address).to.equal(collectionAddress);118 expect(event.returnValues.from).to.equal('0x0000000000000000000000000000000000000000');119 expect(event.returnValues.to).to.equal(receivers[i]);120 expect(event.returnValues.value).to.equal(String(10 * (i + 1)));121 }122 });123124 // Soft-deprecated125 itEth('Can perform burn()', async ({helper}) => {126 const owner = await helper.eth.createAccountWithBalance(donor);127 const receiver = await helper.eth.createAccountWithBalance(donor);128 const collection = await helper.ft.mintCollection(alice);129 await collection.addAdmin(alice, {Ethereum: owner});130131 const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);132 const contract = helper.ethNativeContract.collection(collectionAddress, 'ft', owner, true);133 await contract.methods.mint(receiver, 100).send();134135 const result = await contract.methods.burnFrom(receiver, 49).send({from: receiver});136 137 const event = result.events.Transfer;138 expect(event.address).to.equal(collectionAddress);139 expect(event.returnValues.from).to.equal(receiver);140 expect(event.returnValues.to).to.equal('0x0000000000000000000000000000000000000000');141 expect(event.returnValues.value).to.equal('49');142143 const balance = await contract.methods.balanceOf(receiver).call();144 expect(balance).to.equal('51');145 });146147 itEth('Can perform approve()', async ({helper}) => {148 const owner = await helper.eth.createAccountWithBalance(donor);149 const spender = helper.eth.createAccount();150 const collection = await helper.ft.mintCollection(alice);151 await collection.mint(alice, 200n, {Ethereum: owner});152153 const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);154 const contract = helper.ethNativeContract.collection(collectionAddress, 'ft', owner);155156 {157 const result = await contract.methods.approve(spender, 100).send({from: owner});158159 const event = result.events.Approval;160 expect(event.address).to.be.equal(collectionAddress);161 expect(event.returnValues.owner).to.be.equal(owner);162 expect(event.returnValues.spender).to.be.equal(spender);163 expect(event.returnValues.value).to.be.equal('100');164 }165166 {167 const allowance = await contract.methods.allowance(owner, spender).call();168 expect(+allowance).to.equal(100);169 }170 });171172 itEth('Can perform burnFromCross()', async ({helper}) => {173 const sender = await helper.eth.createAccountWithBalance(donor, 100n);174175 const collection = await helper.ft.mintCollection(owner, {name: 'A', description: 'B', tokenPrefix: 'C'}, 0);176177 await collection.mint(owner, 200n, {Substrate: owner.address});178 await collection.approveTokens(owner, {Ethereum: sender}, 100n);179180 const address = helper.ethAddress.fromCollectionId(collection.collectionId);181 const contract = helper.ethNativeContract.collection(address, 'ft');182183 const fromBalanceBefore = await collection.getBalance({Substrate: owner.address});184 185 const ownerCross = helper.ethCrossAccount.fromKeyringPair(owner);186 const result = await contract.methods.burnFromCross(ownerCross, 49).send({from: sender});187 const events = result.events;188189 expect(events).to.be.like({190 Transfer: {191 address: helper.ethAddress.fromCollectionId(collection.collectionId),192 event: 'Transfer',193 returnValues: {194 from: helper.address.substrateToEth(owner.address),195 to: '0x0000000000000000000000000000000000000000',196 value: '49',197 },198 },199 Approval: {200 address: helper.ethAddress.fromCollectionId(collection.collectionId),201 returnValues: {202 owner: helper.address.substrateToEth(owner.address),203 spender: sender,204 value: '51',205 },206 event: 'Approval',207 },208 });209210 const fromBalanceAfter = await collection.getBalance({Substrate: owner.address});211 expect(fromBalanceBefore - fromBalanceAfter).to.be.eq(49n);212 });213214 itEth('Can perform transferFrom()', async ({helper}) => {215 const owner = await helper.eth.createAccountWithBalance(donor);216 const spender = await helper.eth.createAccountWithBalance(donor);217 const receiver = helper.eth.createAccount();218 const collection = await helper.ft.mintCollection(alice);219 await collection.mint(alice, 200n, {Ethereum: owner});220221 const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);222 const contract = helper.ethNativeContract.collection(collectionAddress, 'ft', owner);223224 await contract.methods.approve(spender, 100).send();225226 {227 const result = await contract.methods.transferFrom(owner, receiver, 49).send({from: spender});228 229 let event = result.events.Transfer;230 expect(event.address).to.be.equal(collectionAddress);231 expect(event.returnValues.from).to.be.equal(owner);232 expect(event.returnValues.to).to.be.equal(receiver);233 expect(event.returnValues.value).to.be.equal('49');234235 event = result.events.Approval;236 expect(event.address).to.be.equal(collectionAddress);237 expect(event.returnValues.owner).to.be.equal(owner);238 expect(event.returnValues.spender).to.be.equal(spender);239 expect(event.returnValues.value).to.be.equal('51');240 }241242 {243 const balance = await contract.methods.balanceOf(receiver).call();244 expect(+balance).to.equal(49);245 }246247 {248 const balance = await contract.methods.balanceOf(owner).call();249 expect(+balance).to.equal(151);250 }251 });252253 itEth('Can perform transferCross()', async ({helper}) => {254 const sender = await helper.eth.createAccountWithBalance(donor);255 const receiverEth = await helper.eth.createAccountWithBalance(donor);256 const receiverCrossEth = helper.ethCrossAccount.fromAddress(receiverEth);257 const receiverCrossSub = helper.ethCrossAccount.fromKeyringPair(donor);258 const collection = await helper.ft.mintCollection(alice);259 await collection.mint(alice, 200n, {Ethereum: sender});260261 const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);262 const collectionEvm = helper.ethNativeContract.collection(collectionAddress, 'ft', sender);263264 {265 // Can transferCross to ethereum address:266 const result = await collectionEvm.methods.transferCross(receiverCrossEth, 50).send({from: sender});267 // Check events:268 const event = result.events.Transfer;269 expect(event.address).to.be.equal(collectionAddress);270 expect(event.returnValues.from).to.be.equal(sender);271 expect(event.returnValues.to).to.be.equal(receiverEth);272 expect(event.returnValues.value).to.be.equal('50');273 // Sender's balance decreased:274 const ownerBalance = await collectionEvm.methods.balanceOf(sender).call();275 expect(+ownerBalance).to.equal(150);276 // Receiver's balance increased:277 const receiverBalance = await collectionEvm.methods.balanceOf(receiverEth).call();278 expect(+receiverBalance).to.equal(50);279 }280 281 {282 // Can transferCross to substrate address:283 const result = await collectionEvm.methods.transferCross(receiverCrossSub, 50).send({from: sender});284 // Check events:285 const event = result.events.Transfer;286 expect(event.address).to.be.equal(collectionAddress);287 expect(event.returnValues.from).to.be.equal(sender);288 expect(event.returnValues.to).to.be.equal(helper.address.substrateToEth(donor.address));289 expect(event.returnValues.value).to.be.equal('50');290 // Sender's balance decreased:291 const senderBalance = await collection.getBalance({Ethereum: sender});292 expect(senderBalance).to.equal(100n);293 // Receiver's balance increased:294 const balance = await collection.getBalance({Substrate: donor.address});295 expect(balance).to.equal(50n);296 }297 });298299 ['transfer', 'transferCross'].map(testCase => itEth(`Cannot ${testCase} incorrect amount`, async ({helper}) => {300 const sender = await helper.eth.createAccountWithBalance(donor);301 const receiverEth = await helper.eth.createAccountWithBalance(donor);302 const receiverCrossEth = helper.ethCrossAccount.fromAddress(receiverEth);303 const BALANCE = 200n;304 const BALANCE_TO_TRANSFER = BALANCE + 100n;305306 const collection = await helper.ft.mintCollection(alice);307 await collection.mint(alice, BALANCE, {Ethereum: sender});308 const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);309 const collectionEvm = helper.ethNativeContract.collection(collectionAddress, 'ft', sender);310311 // 1. Cannot transfer more than have312 const receiver = testCase === 'transfer' ? receiverEth : receiverCrossEth;313 await expect(collectionEvm.methods[testCase](receiver, BALANCE_TO_TRANSFER).send({from: sender})).to.be.rejected;314 // 2. Zero transfer allowed (EIP-20):315 await collectionEvm.methods[testCase](receiver, 0n).send({from: sender});316 }));317 318 319 itEth('Can perform transfer()', async ({helper}) => {320 const owner = await helper.eth.createAccountWithBalance(donor);321 const receiver = await helper.eth.createAccountWithBalance(donor);322 const collection = await helper.ft.mintCollection(alice);323 await collection.mint(alice, 200n, {Ethereum: owner});324325 const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);326 const contract = helper.ethNativeContract.collection(collectionAddress, 'ft', owner);327328 {329 const result = await contract.methods.transfer(receiver, 50).send({from: owner});330 331 const event = result.events.Transfer;332 expect(event.address).to.be.equal(collectionAddress);333 expect(event.returnValues.from).to.be.equal(owner);334 expect(event.returnValues.to).to.be.equal(receiver);335 expect(event.returnValues.value).to.be.equal('50');336 }337338 {339 const balance = await contract.methods.balanceOf(owner).call();340 expect(+balance).to.equal(150);341 }342343 {344 const balance = await contract.methods.balanceOf(receiver).call();345 expect(+balance).to.equal(50);346 }347 });348349 itEth('Can perform transferFromCross()', async ({helper}) => {350 const sender = await helper.eth.createAccountWithBalance(donor, 100n);351352 const collection = await helper.ft.mintCollection(owner, {name: 'A', description: 'B', tokenPrefix: 'C'}, 0);353354 const receiver = helper.eth.createAccount();355356 await collection.mint(owner, 200n, {Substrate: owner.address});357 await collection.approveTokens(owner, {Ethereum: sender}, 100n);358359 const address = helper.ethAddress.fromCollectionId(collection.collectionId);360 const contract = helper.ethNativeContract.collection(address, 'ft');361362 const from = helper.ethCrossAccount.fromKeyringPair(owner);363 const to = helper.ethCrossAccount.fromAddress(receiver);364365 const fromBalanceBefore = await collection.getBalance({Substrate: owner.address});366 const toBalanceBefore = await collection.getBalance({Ethereum: receiver});367 368 const result = await contract.methods.transferFromCross(from, to, 51).send({from: sender});369370 expect(result.events).to.be.like({371 Transfer: {372 address,373 event: 'Transfer',374 returnValues: {375 from: helper.address.substrateToEth(owner.address),376 to: receiver,377 value: '51',378 },379 },380 Approval: {381 address,382 event: 'Approval',383 returnValues: {384 owner: helper.address.substrateToEth(owner.address),385 spender: sender,386 value: '49',387 },388 }});389390 const fromBalanceAfter = await collection.getBalance({Substrate: owner.address});391 expect(fromBalanceBefore - fromBalanceAfter).to.be.eq(51n);392 const toBalanceAfter = await collection.getBalance({Ethereum: receiver});393 expect(toBalanceAfter - toBalanceBefore).to.be.eq(51n);394 });395});396397describe('Fungible: Fees', () => {398 let donor: IKeyringPair;399 let alice: IKeyringPair;400401 before(async function() {402 await usingEthPlaygrounds(async (helper, privateKey) => {403 donor = await privateKey({filename: __filename});404 [alice] = await helper.arrange.createAccounts([20n], donor);405 });406 });407 408 itEth('approve() call fee is less than 0.2UNQ', async ({helper}) => {409 const owner = await helper.eth.createAccountWithBalance(donor);410 const spender = helper.eth.createAccount();411 const collection = await helper.ft.mintCollection(alice);412 await collection.mint(alice, 200n, {Ethereum: owner});413414 const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);415 const contract = helper.ethNativeContract.collection(collectionAddress, 'ft', owner);416417 const cost = await helper.eth.recordCallFee(owner, () => contract.methods.approve(spender, 100).send({from: owner}));418 expect(cost < BigInt(0.2 * Number(helper.balance.getOneTokenNominal())));419 });420421 itEth('transferFrom() call fee is less than 0.2UNQ', async ({helper}) => {422 const owner = await helper.eth.createAccountWithBalance(donor);423 const spender = await helper.eth.createAccountWithBalance(donor);424 const collection = await helper.ft.mintCollection(alice);425 await collection.mint(alice, 200n, {Ethereum: owner});426427 const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);428 const contract = helper.ethNativeContract.collection(collectionAddress, 'ft', owner);429430 await contract.methods.approve(spender, 100).send({from: owner});431432 const cost = await helper.eth.recordCallFee(spender, () => contract.methods.transferFrom(owner, spender, 100).send({from: spender}));433 expect(cost < BigInt(0.2 * Number(helper.balance.getOneTokenNominal())));434 });435436 itEth('transfer() call fee is less than 0.2UNQ', async ({helper}) => {437 const owner = await helper.eth.createAccountWithBalance(donor);438 const receiver = helper.eth.createAccount();439 const collection = await helper.ft.mintCollection(alice);440 await collection.mint(alice, 200n, {Ethereum: owner});441442 const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);443 const contract = helper.ethNativeContract.collection(collectionAddress, 'ft', owner);444445 const cost = await helper.eth.recordCallFee(owner, () => contract.methods.transfer(receiver, 100).send({from: owner}));446 expect(cost < BigInt(0.2 * Number(helper.balance.getOneTokenNominal())));447 });448});449450describe('Fungible: Substrate calls', () => {451 let donor: IKeyringPair;452 let alice: IKeyringPair;453 let owner: IKeyringPair;454455 before(async function() {456 await usingEthPlaygrounds(async (helper, privateKey) => {457 donor = await privateKey({filename: __filename});458 [alice, owner] = await helper.arrange.createAccounts([20n, 20n], donor);459 });460 });461462 itEth('Events emitted for approve()', async ({helper}) => {463 const receiver = helper.eth.createAccount();464 const collection = await helper.ft.mintCollection(alice);465 await collection.mint(alice, 200n);466467 const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);468 const contract = helper.ethNativeContract.collection(collectionAddress, 'ft');469470 const events: any = [];471 contract.events.allEvents((_: any, event: any) => {472 events.push(event);473 });474 475 await collection.approveTokens(alice, {Ethereum: receiver}, 100n);476 if (events.length == 0) await helper.wait.newBlocks(1);477 const event = events[0];478479 expect(event.event).to.be.equal('Approval');480 expect(event.address).to.be.equal(collectionAddress);481 expect(event.returnValues.owner).to.be.equal(helper.address.substrateToEth(alice.address));482 expect(event.returnValues.spender).to.be.equal(receiver);483 expect(event.returnValues.value).to.be.equal('100');484 });485486 itEth('Events emitted for transferFrom()', async ({helper}) => {487 const [bob] = await helper.arrange.createAccounts([10n], donor);488 const receiver = helper.eth.createAccount();489 const collection = await helper.ft.mintCollection(alice);490 await collection.mint(alice, 200n);491 await collection.approveTokens(alice, {Substrate: bob.address}, 100n);492493 const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);494 const contract = helper.ethNativeContract.collection(collectionAddress, 'ft');495496 const events: any = [];497 contract.events.allEvents((_: any, event: any) => {498 events.push(event);499 });500501 await collection.transferFrom(bob, {Substrate: alice.address}, {Ethereum: receiver}, 51n);502 if (events.length == 0) await helper.wait.newBlocks(1);503 let event = events[0];504505 expect(event.event).to.be.equal('Transfer');506 expect(event.address).to.be.equal(collectionAddress);507 expect(event.returnValues.from).to.be.equal(helper.address.substrateToEth(alice.address));508 expect(event.returnValues.to).to.be.equal(receiver);509 expect(event.returnValues.value).to.be.equal('51');510511 event = events[1];512 expect(event.event).to.be.equal('Approval');513 expect(event.address).to.be.equal(collectionAddress);514 expect(event.returnValues.owner).to.be.equal(helper.address.substrateToEth(alice.address));515 expect(event.returnValues.spender).to.be.equal(helper.address.substrateToEth(bob.address));516 expect(event.returnValues.value).to.be.equal('49');517 });518519 itEth('Events emitted for transfer()', async ({helper}) => {520 const receiver = helper.eth.createAccount();521 const collection = await helper.ft.mintCollection(alice);522 await collection.mint(alice, 200n);523524 const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);525 const contract = helper.ethNativeContract.collection(collectionAddress, 'ft');526527 const events: any = [];528 contract.events.allEvents((_: any, event: any) => {529 events.push(event);530 });531 532 await collection.transfer(alice, {Ethereum:receiver}, 51n);533 if (events.length == 0) await helper.wait.newBlocks(1);534 const event = events[0];535536 expect(event.event).to.be.equal('Transfer');537 expect(event.address).to.be.equal(collectionAddress);538 expect(event.returnValues.from).to.be.equal(helper.address.substrateToEth(alice.address));539 expect(event.returnValues.to).to.be.equal(receiver);540 expect(event.returnValues.value).to.be.equal('51');541 });542543 itEth('Events emitted for transferFromCross()', async ({helper}) => {544 const sender = await helper.eth.createAccountWithBalance(donor, 100n);545546 const collection = await helper.ft.mintCollection(owner, {name: 'A', description: 'B', tokenPrefix: 'C'}, 0);547548 const receiver = helper.eth.createAccount();549550 await collection.mint(owner, 200n, {Substrate: owner.address});551 await collection.approveTokens(owner, {Ethereum: sender}, 100n);552553 const address = helper.ethAddress.fromCollectionId(collection.collectionId);554 const contract = helper.ethNativeContract.collection(address, 'ft');555556 const from = helper.ethCrossAccount.fromKeyringPair(owner);557 const to = helper.ethCrossAccount.fromAddress(receiver);558 559 const result = await contract.methods.transferFromCross(from, to, 51).send({from: sender});560561 expect(result.events).to.be.like({562 Transfer: {563 address,564 event: 'Transfer',565 returnValues: {566 from: helper.address.substrateToEth(owner.address),567 to: receiver,568 value: '51',569 },570 },571 Approval: {572 address,573 event: 'Approval',574 returnValues: {575 owner: helper.address.substrateToEth(owner.address),576 spender: sender,577 value: '49',578 },579 }});580 });581});1// Copyright 2019-2022 Unique Network (Gibraltar) Ltd.2// This file is part of Unique Network.34// Unique Network is free software: you can redistribute it and/or modify5// it under the terms of the GNU General Public License as published by6// the Free Software Foundation, either version 3 of the License, or7// (at your option) any later version.89// Unique Network is distributed in the hope that it will be useful,10// but WITHOUT ANY WARRANTY; without even the implied warranty of11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the12// GNU General Public License for more details.1314// You should have received a copy of the GNU General Public License15// along with Unique Network. If not, see <http://www.gnu.org/licenses/>.1617import {expect, itEth, usingEthPlaygrounds} from './util';18import {IKeyringPair} from '@polkadot/types/types';1920describe('Fungible: Information getting', () => {21 let donor: IKeyringPair;22 let alice: IKeyringPair;2324 before(async function() {25 await usingEthPlaygrounds(async (helper, privateKey) => {26 donor = await privateKey({filename: __filename});27 [alice] = await helper.arrange.createAccounts([20n], donor);28 });29 });3031 itEth('totalSupply', async ({helper}) => {32 const caller = await helper.eth.createAccountWithBalance(donor);33 const collection = await helper.ft.mintCollection(alice);34 await collection.mint(alice, 200n);3536 const contract = helper.ethNativeContract.collectionById(collection.collectionId, 'ft', caller);37 const totalSupply = await contract.methods.totalSupply().call();38 expect(totalSupply).to.equal('200');39 });4041 itEth('balanceOf', async ({helper}) => {42 const caller = await helper.eth.createAccountWithBalance(donor);43 const collection = await helper.ft.mintCollection(alice);44 await collection.mint(alice, 200n, {Ethereum: caller});4546 const contract = helper.ethNativeContract.collectionById(collection.collectionId, 'ft', caller);47 const balance = await contract.methods.balanceOf(caller).call();48 expect(balance).to.equal('200');49 });50});5152describe('Fungible: Plain calls', () => {53 let donor: IKeyringPair;54 let alice: IKeyringPair;55 let owner: IKeyringPair;5657 before(async function() {58 await usingEthPlaygrounds(async (helper, privateKey) => {59 donor = await privateKey({filename: __filename});60 [alice, owner] = await helper.arrange.createAccounts([30n, 20n], donor);61 });62 });6364 itEth('Can perform mint()', async ({helper}) => {65 const owner = await helper.eth.createAccountWithBalance(donor);66 const receiver = helper.eth.createAccount();67 const collection = await helper.ft.mintCollection(alice);68 await collection.addAdmin(alice, {Ethereum: owner});6970 const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);71 const contract = helper.ethNativeContract.collection(collectionAddress, 'ft', owner);7273 const result = await contract.methods.mint(receiver, 100).send();74 75 const event = result.events.Transfer;76 expect(event.address).to.equal(collectionAddress);77 expect(event.returnValues.from).to.equal('0x0000000000000000000000000000000000000000');78 expect(event.returnValues.to).to.equal(receiver);79 expect(event.returnValues.value).to.equal('100');80 });81 82 [83 'substrate' as const,84 'ethereum' as const,85 ].map(testCase => {86 itEth(`Can perform mintCross() for ${testCase} address`, async ({helper}) => {87 // 1. Create receiver depending on the test case:88 const receiverEth = helper.eth.createAccount();89 const receiverCrossEth = helper.ethCrossAccount.fromAddress(receiverEth);90 const receiverSub = owner;91 const receiverCrossSub = helper.ethCrossAccount.fromKeyringPair(owner);9293 const ethOwner = await helper.eth.createAccountWithBalance(donor);94 const collection = await helper.ft.mintCollection(alice);95 await collection.addAdmin(alice, {Ethereum: ethOwner});96 97 const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);98 const collectionEvm = helper.ethNativeContract.collection(collectionAddress, 'ft', ethOwner);99 100 // 2. Mint tokens:101 const result = await collectionEvm.methods.mintCross(testCase === 'ethereum' ? receiverCrossEth : receiverCrossSub, 100).send();102 103 const event = result.events.Transfer;104 expect(event.address).to.equal(collectionAddress);105 expect(event.returnValues.from).to.equal('0x0000000000000000000000000000000000000000');106 expect(event.returnValues.to).to.equal(testCase === 'ethereum' ? receiverEth : helper.address.substrateToEth(receiverSub.address));107 expect(event.returnValues.value).to.equal('100');108109 // 3. Get balance depending on the test case:110 let balance;111 if (testCase === 'ethereum') balance = await collection.getBalance({Ethereum: receiverEth});112 else if (testCase === 'substrate') balance = await collection.getBalance({Substrate: receiverSub.address});113 // 3.1 Check balance:114 expect(balance).to.eq(100n);115 });116 });117118 itEth('Can perform mintBulk()', async ({helper}) => {119 const owner = await helper.eth.createAccountWithBalance(donor);120 const bulkSize = 3;121 const receivers = [...new Array(bulkSize)].map(() => helper.eth.createAccount());122 const collection = await helper.ft.mintCollection(alice);123 await collection.addAdmin(alice, {Ethereum: owner});124125 const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);126 const contract = helper.ethNativeContract.collection(collectionAddress, 'ft', owner);127128 const result = await contract.methods.mintBulk(Array.from({length: bulkSize}, (_, i) => (129 [receivers[i], (i + 1) * 10]130 ))).send();131 const events = result.events.Transfer.sort((a: any, b: any) => +a.returnValues.value - b.returnValues.value);132 for (let i = 0; i < bulkSize; i++) {133 const event = events[i];134 expect(event.address).to.equal(collectionAddress);135 expect(event.returnValues.from).to.equal('0x0000000000000000000000000000000000000000');136 expect(event.returnValues.to).to.equal(receivers[i]);137 expect(event.returnValues.value).to.equal(String(10 * (i + 1)));138 }139 });140141 // Soft-deprecated142 itEth('Can perform burn()', async ({helper}) => {143 const owner = await helper.eth.createAccountWithBalance(donor);144 const receiver = await helper.eth.createAccountWithBalance(donor);145 const collection = await helper.ft.mintCollection(alice);146 await collection.addAdmin(alice, {Ethereum: owner});147148 const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);149 const contract = helper.ethNativeContract.collection(collectionAddress, 'ft', owner, true);150 await contract.methods.mint(receiver, 100).send();151152 const result = await contract.methods.burnFrom(receiver, 49).send({from: receiver});153 154 const event = result.events.Transfer;155 expect(event.address).to.equal(collectionAddress);156 expect(event.returnValues.from).to.equal(receiver);157 expect(event.returnValues.to).to.equal('0x0000000000000000000000000000000000000000');158 expect(event.returnValues.value).to.equal('49');159160 const balance = await contract.methods.balanceOf(receiver).call();161 expect(balance).to.equal('51');162 });163164 itEth('Can perform approve()', async ({helper}) => {165 const owner = await helper.eth.createAccountWithBalance(donor);166 const spender = helper.eth.createAccount();167 const collection = await helper.ft.mintCollection(alice);168 await collection.mint(alice, 200n, {Ethereum: owner});169170 const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);171 const contract = helper.ethNativeContract.collection(collectionAddress, 'ft', owner);172173 {174 const result = await contract.methods.approve(spender, 100).send({from: owner});175176 const event = result.events.Approval;177 expect(event.address).to.be.equal(collectionAddress);178 expect(event.returnValues.owner).to.be.equal(owner);179 expect(event.returnValues.spender).to.be.equal(spender);180 expect(event.returnValues.value).to.be.equal('100');181 }182183 {184 const allowance = await contract.methods.allowance(owner, spender).call();185 expect(+allowance).to.equal(100);186 }187 });188189 itEth('Can perform burnFromCross()', async ({helper}) => {190 const sender = await helper.eth.createAccountWithBalance(donor, 100n);191192 const collection = await helper.ft.mintCollection(owner, {name: 'A', description: 'B', tokenPrefix: 'C'}, 0);193194 await collection.mint(owner, 200n, {Substrate: owner.address});195 await collection.approveTokens(owner, {Ethereum: sender}, 100n);196197 const address = helper.ethAddress.fromCollectionId(collection.collectionId);198 const contract = helper.ethNativeContract.collection(address, 'ft');199200 const fromBalanceBefore = await collection.getBalance({Substrate: owner.address});201 202 const ownerCross = helper.ethCrossAccount.fromKeyringPair(owner);203 const result = await contract.methods.burnFromCross(ownerCross, 49).send({from: sender});204 const events = result.events;205206 expect(events).to.be.like({207 Transfer: {208 address: helper.ethAddress.fromCollectionId(collection.collectionId),209 event: 'Transfer',210 returnValues: {211 from: helper.address.substrateToEth(owner.address),212 to: '0x0000000000000000000000000000000000000000',213 value: '49',214 },215 },216 Approval: {217 address: helper.ethAddress.fromCollectionId(collection.collectionId),218 returnValues: {219 owner: helper.address.substrateToEth(owner.address),220 spender: sender,221 value: '51',222 },223 event: 'Approval',224 },225 });226227 const fromBalanceAfter = await collection.getBalance({Substrate: owner.address});228 expect(fromBalanceBefore - fromBalanceAfter).to.be.eq(49n);229 });230231 itEth('Can perform transferFrom()', async ({helper}) => {232 const owner = await helper.eth.createAccountWithBalance(donor);233 const spender = await helper.eth.createAccountWithBalance(donor);234 const receiver = helper.eth.createAccount();235 const collection = await helper.ft.mintCollection(alice);236 await collection.mint(alice, 200n, {Ethereum: owner});237238 const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);239 const contract = helper.ethNativeContract.collection(collectionAddress, 'ft', owner);240241 await contract.methods.approve(spender, 100).send();242243 {244 const result = await contract.methods.transferFrom(owner, receiver, 49).send({from: spender});245 246 let event = result.events.Transfer;247 expect(event.address).to.be.equal(collectionAddress);248 expect(event.returnValues.from).to.be.equal(owner);249 expect(event.returnValues.to).to.be.equal(receiver);250 expect(event.returnValues.value).to.be.equal('49');251252 event = result.events.Approval;253 expect(event.address).to.be.equal(collectionAddress);254 expect(event.returnValues.owner).to.be.equal(owner);255 expect(event.returnValues.spender).to.be.equal(spender);256 expect(event.returnValues.value).to.be.equal('51');257 }258259 {260 const balance = await contract.methods.balanceOf(receiver).call();261 expect(+balance).to.equal(49);262 }263264 {265 const balance = await contract.methods.balanceOf(owner).call();266 expect(+balance).to.equal(151);267 }268 });269270 itEth('Can perform transferCross()', async ({helper}) => {271 const sender = await helper.eth.createAccountWithBalance(donor);272 const receiverEth = await helper.eth.createAccountWithBalance(donor);273 const receiverCrossEth = helper.ethCrossAccount.fromAddress(receiverEth);274 const receiverCrossSub = helper.ethCrossAccount.fromKeyringPair(donor);275 const collection = await helper.ft.mintCollection(alice);276 await collection.mint(alice, 200n, {Ethereum: sender});277278 const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);279 const collectionEvm = helper.ethNativeContract.collection(collectionAddress, 'ft', sender);280281 {282 // Can transferCross to ethereum address:283 const result = await collectionEvm.methods.transferCross(receiverCrossEth, 50).send({from: sender});284 // Check events:285 const event = result.events.Transfer;286 expect(event.address).to.be.equal(collectionAddress);287 expect(event.returnValues.from).to.be.equal(sender);288 expect(event.returnValues.to).to.be.equal(receiverEth);289 expect(event.returnValues.value).to.be.equal('50');290 // Sender's balance decreased:291 const ownerBalance = await collectionEvm.methods.balanceOf(sender).call();292 expect(+ownerBalance).to.equal(150);293 // Receiver's balance increased:294 const receiverBalance = await collectionEvm.methods.balanceOf(receiverEth).call();295 expect(+receiverBalance).to.equal(50);296 }297 298 {299 // Can transferCross to substrate address:300 const result = await collectionEvm.methods.transferCross(receiverCrossSub, 50).send({from: sender});301 // Check events:302 const event = result.events.Transfer;303 expect(event.address).to.be.equal(collectionAddress);304 expect(event.returnValues.from).to.be.equal(sender);305 expect(event.returnValues.to).to.be.equal(helper.address.substrateToEth(donor.address));306 expect(event.returnValues.value).to.be.equal('50');307 // Sender's balance decreased:308 const senderBalance = await collection.getBalance({Ethereum: sender});309 expect(senderBalance).to.equal(100n);310 // Receiver's balance increased:311 const balance = await collection.getBalance({Substrate: donor.address});312 expect(balance).to.equal(50n);313 }314 });315316 ['transfer', 'transferCross'].map(testCase => itEth(`Cannot ${testCase} incorrect amount`, async ({helper}) => {317 const sender = await helper.eth.createAccountWithBalance(donor);318 const receiverEth = await helper.eth.createAccountWithBalance(donor);319 const receiverCrossEth = helper.ethCrossAccount.fromAddress(receiverEth);320 const BALANCE = 200n;321 const BALANCE_TO_TRANSFER = BALANCE + 100n;322323 const collection = await helper.ft.mintCollection(alice);324 await collection.mint(alice, BALANCE, {Ethereum: sender});325 const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);326 const collectionEvm = helper.ethNativeContract.collection(collectionAddress, 'ft', sender);327328 // 1. Cannot transfer more than have329 const receiver = testCase === 'transfer' ? receiverEth : receiverCrossEth;330 await expect(collectionEvm.methods[testCase](receiver, BALANCE_TO_TRANSFER).send({from: sender})).to.be.rejected;331 // 2. Zero transfer allowed (EIP-20):332 await collectionEvm.methods[testCase](receiver, 0n).send({from: sender});333 }));334 335 336 itEth('Can perform transfer()', async ({helper}) => {337 const owner = await helper.eth.createAccountWithBalance(donor);338 const receiver = await helper.eth.createAccountWithBalance(donor);339 const collection = await helper.ft.mintCollection(alice);340 await collection.mint(alice, 200n, {Ethereum: owner});341342 const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);343 const contract = helper.ethNativeContract.collection(collectionAddress, 'ft', owner);344345 {346 const result = await contract.methods.transfer(receiver, 50).send({from: owner});347 348 const event = result.events.Transfer;349 expect(event.address).to.be.equal(collectionAddress);350 expect(event.returnValues.from).to.be.equal(owner);351 expect(event.returnValues.to).to.be.equal(receiver);352 expect(event.returnValues.value).to.be.equal('50');353 }354355 {356 const balance = await contract.methods.balanceOf(owner).call();357 expect(+balance).to.equal(150);358 }359360 {361 const balance = await contract.methods.balanceOf(receiver).call();362 expect(+balance).to.equal(50);363 }364 });365366 itEth('Can perform transferFromCross()', async ({helper}) => {367 const sender = await helper.eth.createAccountWithBalance(donor, 100n);368369 const collection = await helper.ft.mintCollection(owner, {name: 'A', description: 'B', tokenPrefix: 'C'}, 0);370371 const receiver = helper.eth.createAccount();372373 await collection.mint(owner, 200n, {Substrate: owner.address});374 await collection.approveTokens(owner, {Ethereum: sender}, 100n);375376 const address = helper.ethAddress.fromCollectionId(collection.collectionId);377 const contract = helper.ethNativeContract.collection(address, 'ft');378379 const from = helper.ethCrossAccount.fromKeyringPair(owner);380 const to = helper.ethCrossAccount.fromAddress(receiver);381382 const fromBalanceBefore = await collection.getBalance({Substrate: owner.address});383 const toBalanceBefore = await collection.getBalance({Ethereum: receiver});384 385 const result = await contract.methods.transferFromCross(from, to, 51).send({from: sender});386387 expect(result.events).to.be.like({388 Transfer: {389 address,390 event: 'Transfer',391 returnValues: {392 from: helper.address.substrateToEth(owner.address),393 to: receiver,394 value: '51',395 },396 },397 Approval: {398 address,399 event: 'Approval',400 returnValues: {401 owner: helper.address.substrateToEth(owner.address),402 spender: sender,403 value: '49',404 },405 }});406407 const fromBalanceAfter = await collection.getBalance({Substrate: owner.address});408 expect(fromBalanceBefore - fromBalanceAfter).to.be.eq(51n);409 const toBalanceAfter = await collection.getBalance({Ethereum: receiver});410 expect(toBalanceAfter - toBalanceBefore).to.be.eq(51n);411 });412});413414describe('Fungible: Fees', () => {415 let donor: IKeyringPair;416 let alice: IKeyringPair;417418 before(async function() {419 await usingEthPlaygrounds(async (helper, privateKey) => {420 donor = await privateKey({filename: __filename});421 [alice] = await helper.arrange.createAccounts([20n], donor);422 });423 });424 425 itEth('approve() call fee is less than 0.2UNQ', async ({helper}) => {426 const owner = await helper.eth.createAccountWithBalance(donor);427 const spender = helper.eth.createAccount();428 const collection = await helper.ft.mintCollection(alice);429 await collection.mint(alice, 200n, {Ethereum: owner});430431 const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);432 const contract = helper.ethNativeContract.collection(collectionAddress, 'ft', owner);433434 const cost = await helper.eth.recordCallFee(owner, () => contract.methods.approve(spender, 100).send({from: owner}));435 expect(cost < BigInt(0.2 * Number(helper.balance.getOneTokenNominal())));436 });437438 itEth('transferFrom() call fee is less than 0.2UNQ', async ({helper}) => {439 const owner = await helper.eth.createAccountWithBalance(donor);440 const spender = await helper.eth.createAccountWithBalance(donor);441 const collection = await helper.ft.mintCollection(alice);442 await collection.mint(alice, 200n, {Ethereum: owner});443444 const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);445 const contract = helper.ethNativeContract.collection(collectionAddress, 'ft', owner);446447 await contract.methods.approve(spender, 100).send({from: owner});448449 const cost = await helper.eth.recordCallFee(spender, () => contract.methods.transferFrom(owner, spender, 100).send({from: spender}));450 expect(cost < BigInt(0.2 * Number(helper.balance.getOneTokenNominal())));451 });452453 itEth('transfer() call fee is less than 0.2UNQ', async ({helper}) => {454 const owner = await helper.eth.createAccountWithBalance(donor);455 const receiver = helper.eth.createAccount();456 const collection = await helper.ft.mintCollection(alice);457 await collection.mint(alice, 200n, {Ethereum: owner});458459 const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);460 const contract = helper.ethNativeContract.collection(collectionAddress, 'ft', owner);461462 const cost = await helper.eth.recordCallFee(owner, () => contract.methods.transfer(receiver, 100).send({from: owner}));463 expect(cost < BigInt(0.2 * Number(helper.balance.getOneTokenNominal())));464 });465});466467describe('Fungible: Substrate calls', () => {468 let donor: IKeyringPair;469 let alice: IKeyringPair;470 let owner: IKeyringPair;471472 before(async function() {473 await usingEthPlaygrounds(async (helper, privateKey) => {474 donor = await privateKey({filename: __filename});475 [alice, owner] = await helper.arrange.createAccounts([20n, 20n], donor);476 });477 });478479 itEth('Events emitted for approve()', async ({helper}) => {480 const receiver = helper.eth.createAccount();481 const collection = await helper.ft.mintCollection(alice);482 await collection.mint(alice, 200n);483484 const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);485 const contract = helper.ethNativeContract.collection(collectionAddress, 'ft');486487 const events: any = [];488 contract.events.allEvents((_: any, event: any) => {489 events.push(event);490 });491 492 await collection.approveTokens(alice, {Ethereum: receiver}, 100n);493 if (events.length == 0) await helper.wait.newBlocks(1);494 const event = events[0];495496 expect(event.event).to.be.equal('Approval');497 expect(event.address).to.be.equal(collectionAddress);498 expect(event.returnValues.owner).to.be.equal(helper.address.substrateToEth(alice.address));499 expect(event.returnValues.spender).to.be.equal(receiver);500 expect(event.returnValues.value).to.be.equal('100');501 });502503 itEth('Events emitted for transferFrom()', async ({helper}) => {504 const [bob] = await helper.arrange.createAccounts([10n], donor);505 const receiver = helper.eth.createAccount();506 const collection = await helper.ft.mintCollection(alice);507 await collection.mint(alice, 200n);508 await collection.approveTokens(alice, {Substrate: bob.address}, 100n);509510 const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);511 const contract = helper.ethNativeContract.collection(collectionAddress, 'ft');512513 const events: any = [];514 contract.events.allEvents((_: any, event: any) => {515 events.push(event);516 });517518 await collection.transferFrom(bob, {Substrate: alice.address}, {Ethereum: receiver}, 51n);519 if (events.length == 0) await helper.wait.newBlocks(1);520 let event = events[0];521522 expect(event.event).to.be.equal('Transfer');523 expect(event.address).to.be.equal(collectionAddress);524 expect(event.returnValues.from).to.be.equal(helper.address.substrateToEth(alice.address));525 expect(event.returnValues.to).to.be.equal(receiver);526 expect(event.returnValues.value).to.be.equal('51');527528 event = events[1];529 expect(event.event).to.be.equal('Approval');530 expect(event.address).to.be.equal(collectionAddress);531 expect(event.returnValues.owner).to.be.equal(helper.address.substrateToEth(alice.address));532 expect(event.returnValues.spender).to.be.equal(helper.address.substrateToEth(bob.address));533 expect(event.returnValues.value).to.be.equal('49');534 });535536 itEth('Events emitted for transfer()', async ({helper}) => {537 const receiver = helper.eth.createAccount();538 const collection = await helper.ft.mintCollection(alice);539 await collection.mint(alice, 200n);540541 const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);542 const contract = helper.ethNativeContract.collection(collectionAddress, 'ft');543544 const events: any = [];545 contract.events.allEvents((_: any, event: any) => {546 events.push(event);547 });548 549 await collection.transfer(alice, {Ethereum:receiver}, 51n);550 if (events.length == 0) await helper.wait.newBlocks(1);551 const event = events[0];552553 expect(event.event).to.be.equal('Transfer');554 expect(event.address).to.be.equal(collectionAddress);555 expect(event.returnValues.from).to.be.equal(helper.address.substrateToEth(alice.address));556 expect(event.returnValues.to).to.be.equal(receiver);557 expect(event.returnValues.value).to.be.equal('51');558 });559560 itEth('Events emitted for transferFromCross()', async ({helper}) => {561 const sender = await helper.eth.createAccountWithBalance(donor, 100n);562563 const collection = await helper.ft.mintCollection(owner, {name: 'A', description: 'B', tokenPrefix: 'C'}, 0);564565 const receiver = helper.eth.createAccount();566567 await collection.mint(owner, 200n, {Substrate: owner.address});568 await collection.approveTokens(owner, {Ethereum: sender}, 100n);569570 const address = helper.ethAddress.fromCollectionId(collection.collectionId);571 const contract = helper.ethNativeContract.collection(address, 'ft');572573 const from = helper.ethCrossAccount.fromKeyringPair(owner);574 const to = helper.ethCrossAccount.fromAddress(receiver);575 576 const result = await contract.methods.transferFromCross(from, to, 51).send({from: sender});577578 expect(result.events).to.be.like({579 Transfer: {580 address,581 event: 'Transfer',582 returnValues: {583 from: helper.address.substrateToEth(owner.address),584 to: receiver,585 value: '51',586 },587 },588 Approval: {589 address,590 event: 'Approval',591 returnValues: {592 owner: helper.address.substrateToEth(owner.address),593 spender: sender,594 value: '49',595 },596 }});597 });598});tests/src/eth/nonFungible.test.tsdiffbeforeafterboth--- a/tests/src/eth/nonFungible.test.ts
+++ b/tests/src/eth/nonFungible.test.ts
@@ -175,55 +175,82 @@
// expect(tokenUri).to.be.equal(`https://offchain-service.local/token-info/${nextTokenId}`);
});
- itEth('Can perform mintCross()', async ({helper}) => {
- const caller = await helper.eth.createAccountWithBalance(donor);
- const receiverCross = helper.ethCrossAccount.fromKeyringPair(bob);
- const properties = Array(5).fill(0).map((_, i) => { return {key: `key_${i}`, value: Buffer.from(`value_${i}`)}; });
- const permissions: ITokenPropertyPermission[] = properties
- .map(p => {
- return {
- key: p.key, permission: {
- tokenOwner: true,
- collectionAdmin: true,
- mutable: true,
- },
- };
- });
+ // TODO combine all minting tests in one place
+ [
+ 'substrate' as const,
+ 'ethereum' as const,
+ ].map(testCase => {
+ itEth(`Can perform mintCross() for ${testCase} address`, async ({helper}) => {
+ const collectionAdmin = await helper.eth.createAccountWithBalance(donor);
+
+ const receiverEth = helper.eth.createAccount();
+ const receiverCrossEth = helper.ethCrossAccount.fromAddress(receiverEth);
+ const receiverSub = bob;
+ const receiverCrossSub = helper.ethCrossAccount.fromKeyringPair(receiverSub);
+
+ // const receiverCross = helper.ethCrossAccount.fromKeyringPair(bob);
+ const properties = Array(5).fill(0).map((_, i) => { return {key: `key_${i}`, value: Buffer.from(`value_${i}`)}; });
+ const permissions: ITokenPropertyPermission[] = properties
+ .map(p => {
+ return {
+ key: p.key, permission: {
+ tokenOwner: true,
+ collectionAdmin: true,
+ mutable: true,
+ },
+ };
+ });
- const collection = await helper.nft.mintCollection(minter, {
- tokenPrefix: 'ethp',
- tokenPropertyPermissions: permissions,
- });
- await collection.addAdmin(minter, {Ethereum: caller});
+ const collection = await helper.nft.mintCollection(minter, {
+ tokenPrefix: 'ethp',
+ tokenPropertyPermissions: permissions,
+ });
+ await collection.addAdmin(minter, {Ethereum: collectionAdmin});
- const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);
- const contract = helper.ethNativeContract.collection(collectionAddress, 'nft', caller, true);
- let expectedTokenId = await contract.methods.nextTokenId().call();
- let result = await contract.methods.mintCross(receiverCross, []).send();
- let tokenId = result.events.Transfer.returnValues.tokenId;
- expect(tokenId).to.be.equal(expectedTokenId);
+ const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);
+ const contract = helper.ethNativeContract.collection(collectionAddress, 'nft', collectionAdmin, true);
+ let expectedTokenId = await contract.methods.nextTokenId().call();
+ let result = await contract.methods.mintCross(testCase === 'ethereum' ? receiverCrossEth : receiverCrossSub, []).send();
+ let tokenId = result.events.Transfer.returnValues.tokenId;
+ expect(tokenId).to.be.equal(expectedTokenId);
- let event = result.events.Transfer;
- expect(event.address).to.be.equal(collectionAddress);
- expect(event.returnValues.from).to.be.equal('0x0000000000000000000000000000000000000000');
- expect(event.returnValues.to).to.be.equal(helper.address.substrateToEth(bob.address));
- expect(await contract.methods.properties(tokenId, []).call()).to.be.like([]);
+ let event = result.events.Transfer;
+ expect(event.address).to.be.equal(collectionAddress);
+ expect(event.returnValues.from).to.be.equal('0x0000000000000000000000000000000000000000');
+ expect(event.returnValues.to).to.be.equal(testCase === 'ethereum' ? receiverEth : helper.address.substrateToEth(bob.address));
+ expect(await contract.methods.properties(tokenId, []).call()).to.be.like([]);
- expectedTokenId = await contract.methods.nextTokenId().call();
- result = await contract.methods.mintCross(receiverCross, properties).send();
- event = result.events.Transfer;
- expect(event.address).to.be.equal(collectionAddress);
- expect(event.returnValues.from).to.be.equal('0x0000000000000000000000000000000000000000');
- expect(event.returnValues.to).to.be.equal(helper.address.substrateToEth(bob.address));
- expect(await contract.methods.properties(tokenId, []).call()).to.be.like([]);
+ expectedTokenId = await contract.methods.nextTokenId().call();
+ result = await contract.methods.mintCross(testCase === 'ethereum' ? receiverCrossEth : receiverCrossSub, properties).send();
+ event = result.events.Transfer;
+ expect(event.address).to.be.equal(collectionAddress);
+ expect(event.returnValues.from).to.be.equal('0x0000000000000000000000000000000000000000');
+ expect(event.returnValues.to).to.be.equal(testCase === 'ethereum' ? receiverEth : helper.address.substrateToEth(bob.address));
+ expect(await contract.methods.properties(tokenId, []).call()).to.be.like([]);
- tokenId = result.events.Transfer.returnValues.tokenId;
+ tokenId = result.events.Transfer.returnValues.tokenId;
- expect(tokenId).to.be.equal(expectedTokenId);
+ expect(tokenId).to.be.equal(expectedTokenId);
- expect(await contract.methods.properties(tokenId, []).call()).to.be.like(properties
- .map(p => { return helper.ethProperty.property(p.key, p.value.toString()); }));
+ expect(await contract.methods.properties(tokenId, []).call()).to.be.like(properties
+ .map(p => { return helper.ethProperty.property(p.key, p.value.toString()); }));
+
+ expect(await helper.nft.getTokenOwner(collection.collectionId, tokenId))
+ .to.deep.eq(testCase === 'ethereum' ? {Ethereum: receiverEth.toLowerCase()} : {Substrate: receiverSub.address});
+ });
+ });
+
+ itEth('Non-owner and non admin cannot mintCross', async ({helper}) => {
+ const nonOwner = await helper.eth.createAccountWithBalance(donor);
+ const nonOwnerCross = helper.ethCrossAccount.fromAddress(nonOwner);
+
+ const collection = await helper.nft.mintCollection(minter);
+ const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);
+ const collectionEvm = helper.ethNativeContract.collection(collectionAddress, 'nft');
+
+ await expect(collectionEvm.methods.mintCross(nonOwnerCross, []).call({from: nonOwner}))
+ .to.be.rejectedWith('PublicMintingNotAllowed');
});
//TODO: CORE-302 add eth methods
tests/src/eth/reFungible.test.tsdiffbeforeafterboth--- a/tests/src/eth/reFungible.test.ts
+++ b/tests/src/eth/reFungible.test.ts
@@ -137,48 +137,61 @@
expect(await contract.methods.tokenURI(tokenId).call()).to.be.equal('Test URI');
});
- itEth('Can perform mintCross()', async ({helper}) => {
- const caller = await helper.eth.createAccountWithBalance(donor);
- const receiverCross = helper.ethCrossAccount.fromKeyringPair(bob);
- const properties = Array(5).fill(0).map((_, i) => { return {key: `key_${i}`, value: Buffer.from(`value_${i}`)}; });
- const permissions: ITokenPropertyPermission[] = properties.map(p => { return {key: p.key, permission: {tokenOwner: true,
- collectionAdmin: true,
- mutable: true}}; });
+ [
+ 'substrate' as const,
+ 'ethereum' as const,
+ ].map(testCase => {
+ itEth(`Can perform mintCross() for ${testCase} address`, async ({helper}) => {
+ const collectionAdmin = await helper.eth.createAccountWithBalance(donor);
+
+ const receiverEth = helper.eth.createAccount();
+ const receiverCrossEth = helper.ethCrossAccount.fromAddress(receiverEth);
+ const receiverSub = bob;
+ const receiverCrossSub = helper.ethCrossAccount.fromKeyringPair(receiverSub);
+
+ const properties = Array(5).fill(0).map((_, i) => { return {key: `key_${i}`, value: Buffer.from(`value_${i}`)}; });
+ const permissions: ITokenPropertyPermission[] = properties.map(p => { return {key: p.key, permission: {tokenOwner: true,
+ collectionAdmin: true,
+ mutable: true}}; });
- const collection = await helper.rft.mintCollection(minter, {
- tokenPrefix: 'ethp',
- tokenPropertyPermissions: permissions,
- });
- await collection.addAdmin(minter, {Ethereum: caller});
+ const collection = await helper.rft.mintCollection(minter, {
+ tokenPrefix: 'ethp',
+ tokenPropertyPermissions: permissions,
+ });
+ await collection.addAdmin(minter, {Ethereum: collectionAdmin});
- const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);
- const contract = helper.ethNativeContract.collection(collectionAddress, 'rft', caller, true);
- let expectedTokenId = await contract.methods.nextTokenId().call();
- let result = await contract.methods.mintCross(receiverCross, []).send();
- let tokenId = result.events.Transfer.returnValues.tokenId;
- expect(tokenId).to.be.equal(expectedTokenId);
+ const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);
+ const contract = helper.ethNativeContract.collection(collectionAddress, 'rft', collectionAdmin, true);
+ let expectedTokenId = await contract.methods.nextTokenId().call();
+ let result = await contract.methods.mintCross(testCase === 'ethereum' ? receiverCrossEth : receiverCrossSub, []).send();
+ let tokenId = result.events.Transfer.returnValues.tokenId;
+ expect(tokenId).to.be.equal(expectedTokenId);
- let event = result.events.Transfer;
- expect(event.address).to.be.equal(collectionAddress);
- expect(event.returnValues.from).to.be.equal('0x0000000000000000000000000000000000000000');
- expect(event.returnValues.to).to.be.equal(helper.address.substrateToEth(bob.address));
- expect(await contract.methods.properties(tokenId, []).call()).to.be.like([]);
+ let event = result.events.Transfer;
+ expect(event.address).to.be.equal(collectionAddress);
+ expect(event.returnValues.from).to.be.equal('0x0000000000000000000000000000000000000000');
+ expect(event.returnValues.to).to.be.equal(testCase === 'ethereum' ? receiverEth : helper.address.substrateToEth(bob.address));
+ expect(await contract.methods.properties(tokenId, []).call()).to.be.like([]);
- expectedTokenId = await contract.methods.nextTokenId().call();
- result = await contract.methods.mintCross(receiverCross, properties).send();
- event = result.events.Transfer;
- expect(event.address).to.be.equal(collectionAddress);
- expect(event.returnValues.from).to.be.equal('0x0000000000000000000000000000000000000000');
- expect(event.returnValues.to).to.be.equal(helper.address.substrateToEth(bob.address));
- expect(await contract.methods.properties(tokenId, []).call()).to.be.like([]);
+ expectedTokenId = await contract.methods.nextTokenId().call();
+ result = await contract.methods.mintCross(testCase === 'ethereum' ? receiverCrossEth : receiverCrossSub, properties).send();
+ event = result.events.Transfer;
+ expect(event.address).to.be.equal(collectionAddress);
+ expect(event.returnValues.from).to.be.equal('0x0000000000000000000000000000000000000000');
+ expect(event.returnValues.to).to.be.equal(testCase === 'ethereum' ? receiverEth : helper.address.substrateToEth(bob.address));
+ expect(await contract.methods.properties(tokenId, []).call()).to.be.like([]);
- tokenId = result.events.Transfer.returnValues.tokenId;
+ tokenId = result.events.Transfer.returnValues.tokenId;
- expect(tokenId).to.be.equal(expectedTokenId);
+ expect(tokenId).to.be.equal(expectedTokenId);
- expect(await contract.methods.properties(tokenId, []).call()).to.be.like(properties
- .map(p => { return helper.ethProperty.property(p.key, p.value.toString()); }));
+ expect(await contract.methods.properties(tokenId, []).call()).to.be.like(properties
+ .map(p => { return helper.ethProperty.property(p.key, p.value.toString()); }));
+
+ expect(await helper.nft.getTokenOwner(collection.collectionId, tokenId))
+ .to.deep.eq(testCase === 'ethereum' ? {Ethereum: receiverEth.toLowerCase()} : {Substrate: receiverSub.address});
+ });
});
itEth.skip('Can perform mintBulk()', async ({helper}) => {