1234567891011121314151617import {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([20n, 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 });8182 itEth('Can perform mintBulk()', async ({helper}) => {83 const owner = await helper.eth.createAccountWithBalance(donor);84 const bulkSize = 3;85 const receivers = [...new Array(bulkSize)].map(() => helper.eth.createAccount());86 const collection = await helper.ft.mintCollection(alice);87 await collection.addAdmin(alice, {Ethereum: owner});8889 const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);90 const contract = helper.ethNativeContract.collection(collectionAddress, 'ft', owner);9192 const result = await contract.methods.mintBulk(Array.from({length: bulkSize}, (_, i) => (93 [receivers[i], (i + 1) * 10]94 ))).send();95 const events = result.events.Transfer.sort((a: any, b: any) => +a.returnValues.value - b.returnValues.value);96 for (let i = 0; i < bulkSize; i++) {97 const event = events[i];98 expect(event.address).to.equal(collectionAddress);99 expect(event.returnValues.from).to.equal('0x0000000000000000000000000000000000000000');100 expect(event.returnValues.to).to.equal(receivers[i]);101 expect(event.returnValues.value).to.equal(String(10 * (i + 1)));102 }103 });104105 106 itEth('Can perform burn()', async ({helper}) => {107 const owner = await helper.eth.createAccountWithBalance(donor);108 const receiver = await helper.eth.createAccountWithBalance(donor);109 const collection = await helper.ft.mintCollection(alice);110 await collection.addAdmin(alice, {Ethereum: owner});111112 const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);113 const contract = helper.ethNativeContract.collection(collectionAddress, 'ft', owner, true);114 await contract.methods.mint(receiver, 100).send();115116 const result = await contract.methods.burnFrom(receiver, 49).send({from: receiver});117 118 const event = result.events.Transfer;119 expect(event.address).to.equal(collectionAddress);120 expect(event.returnValues.from).to.equal(receiver);121 expect(event.returnValues.to).to.equal('0x0000000000000000000000000000000000000000');122 expect(event.returnValues.value).to.equal('49');123124 const balance = await contract.methods.balanceOf(receiver).call();125 expect(balance).to.equal('51');126 });127128 itEth('Can perform approve()', async ({helper}) => {129 const owner = await helper.eth.createAccountWithBalance(donor);130 const spender = helper.eth.createAccount();131 const collection = await helper.ft.mintCollection(alice);132 await collection.mint(alice, 200n, {Ethereum: owner});133134 const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);135 const contract = helper.ethNativeContract.collection(collectionAddress, 'ft', owner);136137 {138 const result = await contract.methods.approve(spender, 100).send({from: owner});139140 const event = result.events.Approval;141 expect(event.address).to.be.equal(collectionAddress);142 expect(event.returnValues.owner).to.be.equal(owner);143 expect(event.returnValues.spender).to.be.equal(spender);144 expect(event.returnValues.value).to.be.equal('100');145 }146147 {148 const allowance = await contract.methods.allowance(owner, spender).call();149 expect(+allowance).to.equal(100);150 }151 });152153 itEth('Can perform burnFromCross()', async ({helper}) => {154 const sender = await helper.eth.createAccountWithBalance(donor, 100n);155156 const collection = await helper.ft.mintCollection(owner, {name: 'A', description: 'B', tokenPrefix: 'C'}, 0);157158 await collection.mint(owner, 200n, {Substrate: owner.address});159 await collection.approveTokens(owner, {Ethereum: sender}, 100n);160161 const address = helper.ethAddress.fromCollectionId(collection.collectionId);162 const contract = helper.ethNativeContract.collection(address, 'ft');163164 const fromBalanceBefore = await collection.getBalance({Substrate: owner.address});165 166 const ownerCross = helper.ethCrossAccount.fromKeyringPair(owner);167 const result = await contract.methods.burnFromCross(ownerCross, 49).send({from: sender});168 const events = result.events;169170 expect(events).to.be.like({171 Transfer: {172 address: helper.ethAddress.fromCollectionId(collection.collectionId),173 event: 'Transfer',174 returnValues: {175 from: helper.address.substrateToEth(owner.address),176 to: '0x0000000000000000000000000000000000000000',177 value: '49',178 },179 },180 Approval: {181 address: helper.ethAddress.fromCollectionId(collection.collectionId),182 returnValues: {183 owner: helper.address.substrateToEth(owner.address),184 spender: sender,185 value: '51',186 },187 event: 'Approval',188 },189 });190191 const fromBalanceAfter = await collection.getBalance({Substrate: owner.address});192 expect(fromBalanceBefore - fromBalanceAfter).to.be.eq(49n);193 });194195 itEth('Can perform transferFrom()', async ({helper}) => {196 const owner = await helper.eth.createAccountWithBalance(donor);197 const spender = await helper.eth.createAccountWithBalance(donor);198 const receiver = helper.eth.createAccount();199 const collection = await helper.ft.mintCollection(alice);200 await collection.mint(alice, 200n, {Ethereum: owner});201202 const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);203 const contract = helper.ethNativeContract.collection(collectionAddress, 'ft', owner);204205 await contract.methods.approve(spender, 100).send();206207 {208 const result = await contract.methods.transferFrom(owner, receiver, 49).send({from: spender});209 210 let event = result.events.Transfer;211 expect(event.address).to.be.equal(collectionAddress);212 expect(event.returnValues.from).to.be.equal(owner);213 expect(event.returnValues.to).to.be.equal(receiver);214 expect(event.returnValues.value).to.be.equal('49');215216 event = result.events.Approval;217 expect(event.address).to.be.equal(collectionAddress);218 expect(event.returnValues.owner).to.be.equal(owner);219 expect(event.returnValues.spender).to.be.equal(spender);220 expect(event.returnValues.value).to.be.equal('51');221 }222223 {224 const balance = await contract.methods.balanceOf(receiver).call();225 expect(+balance).to.equal(49);226 }227228 {229 const balance = await contract.methods.balanceOf(owner).call();230 expect(+balance).to.equal(151);231 }232 });233234 itEth('Can perform transferCross()', async ({helper}) => {235 const sender = await helper.eth.createAccountWithBalance(donor);236 const receiverEth = await helper.eth.createAccountWithBalance(donor);237 const receiverCrossEth = helper.ethCrossAccount.fromAddress(receiverEth);238 const receiverCrossSub = helper.ethCrossAccount.fromKeyringPair(donor);239 const collection = await helper.ft.mintCollection(alice);240 await collection.mint(alice, 200n, {Ethereum: sender});241242 const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);243 const collectionEvm = helper.ethNativeContract.collection(collectionAddress, 'ft', sender);244245 {246 247 const result = await collectionEvm.methods.transferCross(receiverCrossEth, 50).send({from: sender});248 249 const event = result.events.Transfer;250 expect(event.address).to.be.equal(collectionAddress);251 expect(event.returnValues.from).to.be.equal(sender);252 expect(event.returnValues.to).to.be.equal(receiverEth);253 expect(event.returnValues.value).to.be.equal('50');254 255 const ownerBalance = await collectionEvm.methods.balanceOf(sender).call();256 expect(+ownerBalance).to.equal(150);257 258 const receiverBalance = await collectionEvm.methods.balanceOf(receiverEth).call();259 expect(+receiverBalance).to.equal(50);260 }261 262 {263 264 const result = await collectionEvm.methods.transferCross(receiverCrossSub, 50).send({from: sender});265 266 const event = result.events.Transfer;267 expect(event.address).to.be.equal(collectionAddress);268 expect(event.returnValues.from).to.be.equal(sender);269 expect(event.returnValues.to).to.be.equal(helper.address.substrateToEth(donor.address));270 expect(event.returnValues.value).to.be.equal('50');271 272 const senderBalance = await collection.getBalance({Ethereum: sender});273 expect(senderBalance).to.equal(100n);274 275 const balance = await collection.getBalance({Substrate: donor.address});276 expect(balance).to.equal(50n);277 }278 });279280 ['transfer', 'transferCross'].map(testCase => itEth(`Cannot ${testCase} incorrect amount`, async ({helper}) => {281 const sender = await helper.eth.createAccountWithBalance(donor);282 const receiverEth = await helper.eth.createAccountWithBalance(donor);283 const receiverCrossEth = helper.ethCrossAccount.fromAddress(receiverEth);284 const BALANCE = 200n;285 const BALANCE_TO_TRANSFER = BALANCE + 100n;286287 const collection = await helper.ft.mintCollection(alice);288 await collection.mint(alice, BALANCE, {Ethereum: sender});289 const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);290 const collectionEvm = helper.ethNativeContract.collection(collectionAddress, 'ft', sender);291292 293 const receiver = testCase === 'transfer' ? receiverEth : receiverCrossEth;294 await expect(collectionEvm.methods[testCase](receiver, BALANCE_TO_TRANSFER).send({from: sender})).to.be.rejected;295 296 await collectionEvm.methods[testCase](receiver, 0n).send({from: sender});297 }));298 299 300 itEth('Can perform transfer()', async ({helper}) => {301 const owner = await helper.eth.createAccountWithBalance(donor);302 const receiver = await helper.eth.createAccountWithBalance(donor);303 const collection = await helper.ft.mintCollection(alice);304 await collection.mint(alice, 200n, {Ethereum: owner});305306 const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);307 const contract = helper.ethNativeContract.collection(collectionAddress, 'ft', owner);308309 {310 const result = await contract.methods.transfer(receiver, 50).send({from: owner});311 312 const event = result.events.Transfer;313 expect(event.address).to.be.equal(collectionAddress);314 expect(event.returnValues.from).to.be.equal(owner);315 expect(event.returnValues.to).to.be.equal(receiver);316 expect(event.returnValues.value).to.be.equal('50');317 }318319 {320 const balance = await contract.methods.balanceOf(owner).call();321 expect(+balance).to.equal(150);322 }323324 {325 const balance = await contract.methods.balanceOf(receiver).call();326 expect(+balance).to.equal(50);327 }328 });329330 itEth('Can perform transferFromCross()', async ({helper}) => {331 const sender = await helper.eth.createAccountWithBalance(donor, 100n);332333 const collection = await helper.ft.mintCollection(owner, {name: 'A', description: 'B', tokenPrefix: 'C'}, 0);334335 const receiver = helper.eth.createAccount();336337 await collection.mint(owner, 200n, {Substrate: owner.address});338 await collection.approveTokens(owner, {Ethereum: sender}, 100n);339340 const address = helper.ethAddress.fromCollectionId(collection.collectionId);341 const contract = helper.ethNativeContract.collection(address, 'ft');342343 const from = helper.ethCrossAccount.fromKeyringPair(owner);344 const to = helper.ethCrossAccount.fromAddress(receiver);345346 const fromBalanceBefore = await collection.getBalance({Substrate: owner.address});347 const toBalanceBefore = await collection.getBalance({Ethereum: receiver});348 349 const result = await contract.methods.transferFromCross(from, to, 51).send({from: sender});350351 expect(result.events).to.be.like({352 Transfer: {353 address,354 event: 'Transfer',355 returnValues: {356 from: helper.address.substrateToEth(owner.address),357 to: receiver,358 value: '51',359 },360 },361 Approval: {362 address,363 event: 'Approval',364 returnValues: {365 owner: helper.address.substrateToEth(owner.address),366 spender: sender,367 value: '49',368 },369 }});370371 const fromBalanceAfter = await collection.getBalance({Substrate: owner.address});372 expect(fromBalanceBefore - fromBalanceAfter).to.be.eq(51n);373 const toBalanceAfter = await collection.getBalance({Ethereum: receiver});374 expect(toBalanceAfter - toBalanceBefore).to.be.eq(51n);375 });376});377378describe('Fungible: Fees', () => {379 let donor: IKeyringPair;380 let alice: IKeyringPair;381382 before(async function() {383 await usingEthPlaygrounds(async (helper, privateKey) => {384 donor = await privateKey({filename: __filename});385 [alice] = await helper.arrange.createAccounts([20n], donor);386 });387 });388 389 itEth('approve() call fee is less than 0.2UNQ', async ({helper}) => {390 const owner = await helper.eth.createAccountWithBalance(donor);391 const spender = helper.eth.createAccount();392 const collection = await helper.ft.mintCollection(alice);393 await collection.mint(alice, 200n, {Ethereum: owner});394395 const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);396 const contract = helper.ethNativeContract.collection(collectionAddress, 'ft', owner);397398 const cost = await helper.eth.recordCallFee(owner, () => contract.methods.approve(spender, 100).send({from: owner}));399 expect(cost < BigInt(0.2 * Number(helper.balance.getOneTokenNominal())));400 });401402 itEth('transferFrom() call fee is less than 0.2UNQ', async ({helper}) => {403 const owner = await helper.eth.createAccountWithBalance(donor);404 const spender = await helper.eth.createAccountWithBalance(donor);405 const collection = await helper.ft.mintCollection(alice);406 await collection.mint(alice, 200n, {Ethereum: owner});407408 const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);409 const contract = helper.ethNativeContract.collection(collectionAddress, 'ft', owner);410411 await contract.methods.approve(spender, 100).send({from: owner});412413 const cost = await helper.eth.recordCallFee(spender, () => contract.methods.transferFrom(owner, spender, 100).send({from: spender}));414 expect(cost < BigInt(0.2 * Number(helper.balance.getOneTokenNominal())));415 });416417 itEth('transfer() call fee is less than 0.2UNQ', async ({helper}) => {418 const owner = await helper.eth.createAccountWithBalance(donor);419 const receiver = helper.eth.createAccount();420 const collection = await helper.ft.mintCollection(alice);421 await collection.mint(alice, 200n, {Ethereum: owner});422423 const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);424 const contract = helper.ethNativeContract.collection(collectionAddress, 'ft', owner);425426 const cost = await helper.eth.recordCallFee(owner, () => contract.methods.transfer(receiver, 100).send({from: owner}));427 expect(cost < BigInt(0.2 * Number(helper.balance.getOneTokenNominal())));428 });429});430431describe('Fungible: Substrate calls', () => {432 let donor: IKeyringPair;433 let alice: IKeyringPair;434 let owner: IKeyringPair;435436 before(async function() {437 await usingEthPlaygrounds(async (helper, privateKey) => {438 donor = await privateKey({filename: __filename});439 [alice, owner] = await helper.arrange.createAccounts([20n, 20n], donor);440 });441 });442443 itEth('Events emitted for approve()', async ({helper}) => {444 const receiver = helper.eth.createAccount();445 const collection = await helper.ft.mintCollection(alice);446 await collection.mint(alice, 200n);447448 const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);449 const contract = helper.ethNativeContract.collection(collectionAddress, 'ft');450451 const events: any = [];452 contract.events.allEvents((_: any, event: any) => {453 events.push(event);454 });455 456 await collection.approveTokens(alice, {Ethereum: receiver}, 100n);457 if (events.length == 0) await helper.wait.newBlocks(1);458 const event = events[0];459460 expect(event.event).to.be.equal('Approval');461 expect(event.address).to.be.equal(collectionAddress);462 expect(event.returnValues.owner).to.be.equal(helper.address.substrateToEth(alice.address));463 expect(event.returnValues.spender).to.be.equal(receiver);464 expect(event.returnValues.value).to.be.equal('100');465 });466467 itEth('Events emitted for transferFrom()', async ({helper}) => {468 const [bob] = await helper.arrange.createAccounts([10n], donor);469 const receiver = helper.eth.createAccount();470 const collection = await helper.ft.mintCollection(alice);471 await collection.mint(alice, 200n);472 await collection.approveTokens(alice, {Substrate: bob.address}, 100n);473474 const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);475 const contract = helper.ethNativeContract.collection(collectionAddress, 'ft');476477 const events: any = [];478 contract.events.allEvents((_: any, event: any) => {479 events.push(event);480 });481482 await collection.transferFrom(bob, {Substrate: alice.address}, {Ethereum: receiver}, 51n);483 if (events.length == 0) await helper.wait.newBlocks(1);484 let event = events[0];485486 expect(event.event).to.be.equal('Transfer');487 expect(event.address).to.be.equal(collectionAddress);488 expect(event.returnValues.from).to.be.equal(helper.address.substrateToEth(alice.address));489 expect(event.returnValues.to).to.be.equal(receiver);490 expect(event.returnValues.value).to.be.equal('51');491492 event = events[1];493 expect(event.event).to.be.equal('Approval');494 expect(event.address).to.be.equal(collectionAddress);495 expect(event.returnValues.owner).to.be.equal(helper.address.substrateToEth(alice.address));496 expect(event.returnValues.spender).to.be.equal(helper.address.substrateToEth(bob.address));497 expect(event.returnValues.value).to.be.equal('49');498 });499500 itEth('Events emitted for transfer()', async ({helper}) => {501 const receiver = helper.eth.createAccount();502 const collection = await helper.ft.mintCollection(alice);503 await collection.mint(alice, 200n);504505 const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);506 const contract = helper.ethNativeContract.collection(collectionAddress, 'ft');507508 const events: any = [];509 contract.events.allEvents((_: any, event: any) => {510 events.push(event);511 });512 513 await collection.transfer(alice, {Ethereum:receiver}, 51n);514 if (events.length == 0) await helper.wait.newBlocks(1);515 const event = events[0];516517 expect(event.event).to.be.equal('Transfer');518 expect(event.address).to.be.equal(collectionAddress);519 expect(event.returnValues.from).to.be.equal(helper.address.substrateToEth(alice.address));520 expect(event.returnValues.to).to.be.equal(receiver);521 expect(event.returnValues.value).to.be.equal('51');522 });523524 itEth('Events emitted for transferFromCross()', async ({helper}) => {525 const sender = await helper.eth.createAccountWithBalance(donor, 100n);526527 const collection = await helper.ft.mintCollection(owner, {name: 'A', description: 'B', tokenPrefix: 'C'}, 0);528529 const receiver = helper.eth.createAccount();530531 await collection.mint(owner, 200n, {Substrate: owner.address});532 await collection.approveTokens(owner, {Ethereum: sender}, 100n);533534 const address = helper.ethAddress.fromCollectionId(collection.collectionId);535 const contract = helper.ethNativeContract.collection(address, 'ft');536537 const from = helper.ethCrossAccount.fromKeyringPair(owner);538 const to = helper.ethCrossAccount.fromAddress(receiver);539 540 const result = await contract.methods.transferFromCross(from, to, 51).send({from: sender});541542 expect(result.events).to.be.like({543 Transfer: {544 address,545 event: 'Transfer',546 returnValues: {547 from: helper.address.substrateToEth(owner.address),548 to: receiver,549 value: '51',550 },551 },552 Approval: {553 address,554 event: 'Approval',555 returnValues: {556 owner: helper.address.substrateToEth(owner.address),557 spender: sender,558 value: '49',559 },560 }});561 });562});