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 itEth('Can perform burn()', async ({helper}) => {106 const owner = await helper.eth.createAccountWithBalance(donor);107 const receiver = await helper.eth.createAccountWithBalance(donor);108 const collection = await helper.ft.mintCollection(alice);109 await collection.addAdmin(alice, {Ethereum: owner});110111 const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);112 const contract = helper.ethNativeContract.collection(collectionAddress, 'ft', owner);113 await contract.methods.mint(receiver, 100).send();114115 const result = await contract.methods.burnFrom(receiver, 49).send({from: receiver});116 117 const event = result.events.Transfer;118 expect(event.address).to.equal(collectionAddress);119 expect(event.returnValues.from).to.equal(receiver);120 expect(event.returnValues.to).to.equal('0x0000000000000000000000000000000000000000');121 expect(event.returnValues.value).to.equal('49');122123 const balance = await contract.methods.balanceOf(receiver).call();124 expect(balance).to.equal('51');125 });126127 itEth('Can perform approve()', async ({helper}) => {128 const owner = await helper.eth.createAccountWithBalance(donor);129 const spender = helper.eth.createAccount();130 const collection = await helper.ft.mintCollection(alice);131 await collection.mint(alice, 200n, {Ethereum: owner});132133 const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);134 const contract = helper.ethNativeContract.collection(collectionAddress, 'ft', owner);135136 {137 const result = await contract.methods.approve(spender, 100).send({from: owner});138139 const event = result.events.Approval;140 expect(event.address).to.be.equal(collectionAddress);141 expect(event.returnValues.owner).to.be.equal(owner);142 expect(event.returnValues.spender).to.be.equal(spender);143 expect(event.returnValues.value).to.be.equal('100');144 }145146 {147 const allowance = await contract.methods.allowance(owner, spender).call();148 expect(+allowance).to.equal(100);149 }150 });151152 itEth('Can perform burnFromCross()', async ({helper}) => {153 const sender = await helper.eth.createAccountWithBalance(donor, 100n);154155 const collection = await helper.ft.mintCollection(owner, {name: 'A', description: 'B', tokenPrefix: 'C'}, 0);156157 await collection.mint(owner, 200n, {Substrate: owner.address});158 await collection.approveTokens(owner, {Ethereum: sender}, 100n);159160 const address = helper.ethAddress.fromCollectionId(collection.collectionId);161 const contract = helper.ethNativeContract.collection(address, 'ft');162163 const fromBalanceBefore = await collection.getBalance({Substrate: owner.address});164 165 const ownerCross = helper.ethCrossAccount.fromKeyringPair(owner);166 const result = await contract.methods.burnFromCross(ownerCross, 49).send({from: sender});167 const events = result.events;168169 expect(events).to.be.like({170 Transfer: {171 address: helper.ethAddress.fromCollectionId(collection.collectionId),172 event: 'Transfer',173 returnValues: {174 from: helper.address.substrateToEth(owner.address),175 to: '0x0000000000000000000000000000000000000000',176 value: '49',177 },178 },179 Approval: {180 address: helper.ethAddress.fromCollectionId(collection.collectionId),181 returnValues: {182 owner: helper.address.substrateToEth(owner.address),183 spender: sender,184 value: '51',185 },186 event: 'Approval',187 },188 });189190 const fromBalanceAfter = await collection.getBalance({Substrate: owner.address});191 expect(fromBalanceBefore - fromBalanceAfter).to.be.eq(49n);192 });193194 itEth('Can perform transferFrom()', async ({helper}) => {195 const owner = await helper.eth.createAccountWithBalance(donor);196 const spender = await helper.eth.createAccountWithBalance(donor);197 const receiver = helper.eth.createAccount();198 const collection = await helper.ft.mintCollection(alice);199 await collection.mint(alice, 200n, {Ethereum: owner});200201 const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);202 const contract = helper.ethNativeContract.collection(collectionAddress, 'ft', owner);203204 await contract.methods.approve(spender, 100).send();205206 {207 const result = await contract.methods.transferFrom(owner, receiver, 49).send({from: spender});208 209 let event = result.events.Transfer;210 expect(event.address).to.be.equal(collectionAddress);211 expect(event.returnValues.from).to.be.equal(owner);212 expect(event.returnValues.to).to.be.equal(receiver);213 expect(event.returnValues.value).to.be.equal('49');214215 event = result.events.Approval;216 expect(event.address).to.be.equal(collectionAddress);217 expect(event.returnValues.owner).to.be.equal(owner);218 expect(event.returnValues.spender).to.be.equal(spender);219 expect(event.returnValues.value).to.be.equal('51');220 }221222 {223 const balance = await contract.methods.balanceOf(receiver).call();224 expect(+balance).to.equal(49);225 }226227 {228 const balance = await contract.methods.balanceOf(owner).call();229 expect(+balance).to.equal(151);230 }231 });232233 itEth('Can perform transfer()', async ({helper}) => {234 const owner = await helper.eth.createAccountWithBalance(donor);235 const receiver = await helper.eth.createAccountWithBalance(donor);236 const collection = await helper.ft.mintCollection(alice);237 await collection.mint(alice, 200n, {Ethereum: owner});238239 const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);240 const contract = helper.ethNativeContract.collection(collectionAddress, 'ft', owner);241242 {243 const result = await contract.methods.transfer(receiver, 50).send({from: owner});244 245 const event = result.events.Transfer;246 expect(event.address).to.be.equal(collectionAddress);247 expect(event.returnValues.from).to.be.equal(owner);248 expect(event.returnValues.to).to.be.equal(receiver);249 expect(event.returnValues.value).to.be.equal('50');250 }251252 {253 const balance = await contract.methods.balanceOf(owner).call();254 expect(+balance).to.equal(150);255 }256257 {258 const balance = await contract.methods.balanceOf(receiver).call();259 expect(+balance).to.equal(50);260 }261 });262263 itEth('Can perform transferFromCross()', async ({helper, privateKey}) => {264 const sender = await helper.eth.createAccountWithBalance(donor, 100n);265266 const collection = await helper.ft.mintCollection(owner, {name: 'A', description: 'B', tokenPrefix: 'C'}, 0);267268 const receiver = helper.eth.createAccount();269270 await collection.mint(owner, 200n, {Substrate: owner.address});271 await collection.approveTokens(owner, {Ethereum: sender}, 100n);272273 const address = helper.ethAddress.fromCollectionId(collection.collectionId);274 const contract = helper.ethNativeContract.collection(address, 'ft');275276 const from = helper.ethCrossAccount.fromKeyringPair(owner);277 const to = helper.ethCrossAccount.fromAddress(receiver);278279 const fromBalanceBefore = await collection.getBalance({Substrate: owner.address});280 const toBalanceBefore = await collection.getBalance({Ethereum: receiver});281 282 const result = await contract.methods.transferFromCross(from, to, 51).send({from: sender});283284 expect(result.events).to.be.like({285 Transfer: {286 address,287 event: 'Transfer',288 returnValues: {289 from: helper.address.substrateToEth(owner.address),290 to: receiver,291 value: '51',292 },293 },294 Approval: {295 address,296 event: 'Approval',297 returnValues: {298 owner: helper.address.substrateToEth(owner.address),299 spender: sender,300 value: '49',301 },302 }});303304 const fromBalanceAfter = await collection.getBalance({Substrate: owner.address});305 expect(fromBalanceBefore - fromBalanceAfter).to.be.eq(51n);306 const toBalanceAfter = await collection.getBalance({Ethereum: receiver});307 expect(toBalanceAfter - toBalanceBefore).to.be.eq(51n);308 });309});310311describe('Fungible: Fees', () => {312 let donor: IKeyringPair;313 let alice: IKeyringPair;314315 before(async function() {316 await usingEthPlaygrounds(async (helper, privateKey) => {317 donor = await privateKey({filename: __filename});318 [alice] = await helper.arrange.createAccounts([20n], donor);319 });320 });321 322 itEth('approve() call fee is less than 0.2UNQ', async ({helper}) => {323 const owner = await helper.eth.createAccountWithBalance(donor);324 const spender = helper.eth.createAccount();325 const collection = await helper.ft.mintCollection(alice);326 await collection.mint(alice, 200n, {Ethereum: owner});327328 const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);329 const contract = helper.ethNativeContract.collection(collectionAddress, 'ft', owner);330331 const cost = await helper.eth.recordCallFee(owner, () => contract.methods.approve(spender, 100).send({from: owner}));332 expect(cost < BigInt(0.2 * Number(helper.balance.getOneTokenNominal())));333 });334335 itEth('transferFrom() call fee is less than 0.2UNQ', async ({helper}) => {336 const owner = await helper.eth.createAccountWithBalance(donor);337 const spender = await helper.eth.createAccountWithBalance(donor);338 const collection = await helper.ft.mintCollection(alice);339 await collection.mint(alice, 200n, {Ethereum: owner});340341 const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);342 const contract = helper.ethNativeContract.collection(collectionAddress, 'ft', owner);343344 await contract.methods.approve(spender, 100).send({from: owner});345346 const cost = await helper.eth.recordCallFee(spender, () => contract.methods.transferFrom(owner, spender, 100).send({from: spender}));347 expect(cost < BigInt(0.2 * Number(helper.balance.getOneTokenNominal())));348 });349350 itEth('transfer() call fee is less than 0.2UNQ', async ({helper}) => {351 const owner = await helper.eth.createAccountWithBalance(donor);352 const receiver = helper.eth.createAccount();353 const collection = await helper.ft.mintCollection(alice);354 await collection.mint(alice, 200n, {Ethereum: owner});355356 const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);357 const contract = helper.ethNativeContract.collection(collectionAddress, 'ft', owner);358359 const cost = await helper.eth.recordCallFee(owner, () => contract.methods.transfer(receiver, 100).send({from: owner}));360 expect(cost < BigInt(0.2 * Number(helper.balance.getOneTokenNominal())));361 });362});363364describe('Fungible: Substrate calls', () => {365 let donor: IKeyringPair;366 let alice: IKeyringPair;367 let owner: IKeyringPair;368369 before(async function() {370 await usingEthPlaygrounds(async (helper, privateKey) => {371 donor = await privateKey({filename: __filename});372 [alice, owner] = await helper.arrange.createAccounts([20n, 20n], donor);373 });374 });375376 itEth('Events emitted for approve()', async ({helper}) => {377 const receiver = helper.eth.createAccount();378 const collection = await helper.ft.mintCollection(alice);379 await collection.mint(alice, 200n);380381 const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);382 const contract = helper.ethNativeContract.collection(collectionAddress, 'ft');383384 const events: any = [];385 contract.events.allEvents((_: any, event: any) => {386 events.push(event);387 });388 389 await collection.approveTokens(alice, {Ethereum: receiver}, 100n);390 if (events.length == 0) await helper.wait.newBlocks(1);391 const event = events[0];392393 expect(event.event).to.be.equal('Approval');394 expect(event.address).to.be.equal(collectionAddress);395 expect(event.returnValues.owner).to.be.equal(helper.address.substrateToEth(alice.address));396 expect(event.returnValues.spender).to.be.equal(receiver);397 expect(event.returnValues.value).to.be.equal('100');398 });399400 itEth('Events emitted for transferFrom()', async ({helper}) => {401 const [bob] = await helper.arrange.createAccounts([10n], donor);402 const receiver = helper.eth.createAccount();403 const collection = await helper.ft.mintCollection(alice);404 await collection.mint(alice, 200n);405 await collection.approveTokens(alice, {Substrate: bob.address}, 100n);406407 const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);408 const contract = helper.ethNativeContract.collection(collectionAddress, 'ft');409410 const events: any = [];411 contract.events.allEvents((_: any, event: any) => {412 events.push(event);413 });414415 await collection.transferFrom(bob, {Substrate: alice.address}, {Ethereum: receiver}, 51n);416 if (events.length == 0) await helper.wait.newBlocks(1);417 let event = events[0];418419 expect(event.event).to.be.equal('Transfer');420 expect(event.address).to.be.equal(collectionAddress);421 expect(event.returnValues.from).to.be.equal(helper.address.substrateToEth(alice.address));422 expect(event.returnValues.to).to.be.equal(receiver);423 expect(event.returnValues.value).to.be.equal('51');424425 event = events[1];426 expect(event.event).to.be.equal('Approval');427 expect(event.address).to.be.equal(collectionAddress);428 expect(event.returnValues.owner).to.be.equal(helper.address.substrateToEth(alice.address));429 expect(event.returnValues.spender).to.be.equal(helper.address.substrateToEth(bob.address));430 expect(event.returnValues.value).to.be.equal('49');431 });432433 itEth('Events emitted for transfer()', async ({helper}) => {434 const receiver = helper.eth.createAccount();435 const collection = await helper.ft.mintCollection(alice);436 await collection.mint(alice, 200n);437438 const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);439 const contract = helper.ethNativeContract.collection(collectionAddress, 'ft');440441 const events: any = [];442 contract.events.allEvents((_: any, event: any) => {443 events.push(event);444 });445 446 await collection.transfer(alice, {Ethereum:receiver}, 51n);447 if (events.length == 0) await helper.wait.newBlocks(1);448 const event = events[0];449450 expect(event.event).to.be.equal('Transfer');451 expect(event.address).to.be.equal(collectionAddress);452 expect(event.returnValues.from).to.be.equal(helper.address.substrateToEth(alice.address));453 expect(event.returnValues.to).to.be.equal(receiver);454 expect(event.returnValues.value).to.be.equal('51');455 });456457 itEth('Events emitted for transferFromCross()', async ({helper, privateKey}) => {458 const sender = await helper.eth.createAccountWithBalance(donor, 100n);459460 const collection = await helper.ft.mintCollection(owner, {name: 'A', description: 'B', tokenPrefix: 'C'}, 0);461462 const receiver = helper.eth.createAccount();463464 await collection.mint(owner, 200n, {Substrate: owner.address});465 await collection.approveTokens(owner, {Ethereum: sender}, 100n);466467 const address = helper.ethAddress.fromCollectionId(collection.collectionId);468 const contract = helper.ethNativeContract.collection(address, 'ft');469470 const from = helper.ethCrossAccount.fromKeyringPair(owner);471 const to = helper.ethCrossAccount.fromAddress(receiver);472 473 const result = await contract.methods.transferFromCross(from, to, 51).send({from: sender});474475 expect(result.events).to.be.like({476 Transfer: {477 address,478 event: 'Transfer',479 returnValues: {480 from: helper.address.substrateToEth(owner.address),481 to: receiver,482 value: '51',483 },484 },485 Approval: {486 address,487 event: 'Approval',488 returnValues: {489 owner: helper.address.substrateToEth(owner.address),490 spender: sender,491 value: '49',492 },493 }});494 });495});