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([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 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 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 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 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 142 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 283 const result = await collectionEvm.methods.transferCross(receiverCrossEth, 50).send({from: sender});284 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 291 const ownerBalance = await collectionEvm.methods.balanceOf(sender).call();292 expect(+ownerBalance).to.equal(150);293 294 const receiverBalance = await collectionEvm.methods.balanceOf(receiverEth).call();295 expect(+receiverBalance).to.equal(50);296 }297 298 {299 300 const result = await collectionEvm.methods.transferCross(receiverCrossSub, 50).send({from: sender});301 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 308 const senderBalance = await collection.getBalance({Ethereum: sender});309 expect(senderBalance).to.equal(100n);310 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 329 const receiver = testCase === 'transfer' ? receiverEth : receiverCrossEth;330 await expect(collectionEvm.methods[testCase](receiver, BALANCE_TO_TRANSFER).send({from: sender})).to.be.rejected;331 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});