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;5556 before(async function() {57 await usingEthPlaygrounds(async (helper, privateKey) => {58 donor = await privateKey({filename: __filename});59 [alice] = await helper.arrange.createAccounts([20n], donor);60 });61 });6263 itEth('Can perform mint()', async ({helper}) => {64 const owner = await helper.eth.createAccountWithBalance(donor);65 const receiver = helper.eth.createAccount();66 const collection = await helper.ft.mintCollection(alice);67 await collection.addAdmin(alice, {Ethereum: owner});6869 const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);70 const contract = helper.ethNativeContract.collection(collectionAddress, 'ft', owner);7172 const result = await contract.methods.mint(receiver, 100).send();73 74 const event = result.events.Transfer;75 expect(event.address).to.equal(collectionAddress);76 expect(event.returnValues.from).to.equal('0x0000000000000000000000000000000000000000');77 expect(event.returnValues.to).to.equal(receiver);78 expect(event.returnValues.value).to.equal('100');79 });8081 itEth('Can perform mintBulk()', async ({helper}) => {82 const owner = await helper.eth.createAccountWithBalance(donor);83 const bulkSize = 3;84 const receivers = [...new Array(bulkSize)].map(() => helper.eth.createAccount());85 const collection = await helper.ft.mintCollection(alice);86 await collection.addAdmin(alice, {Ethereum: owner});8788 const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);89 const contract = helper.ethNativeContract.collection(collectionAddress, 'ft', owner);9091 const result = await contract.methods.mintBulk(Array.from({length: bulkSize}, (_, i) => (92 [receivers[i], (i + 1) * 10]93 ))).send();94 const events = result.events.Transfer.sort((a: any, b: any) => +a.returnValues.value - b.returnValues.value);95 for (let i = 0; i < bulkSize; i++) {96 const event = events[i];97 expect(event.address).to.equal(collectionAddress);98 expect(event.returnValues.from).to.equal('0x0000000000000000000000000000000000000000');99 expect(event.returnValues.to).to.equal(receivers[i]);100 expect(event.returnValues.value).to.equal(String(10 * (i + 1)));101 }102 });103104 itEth('Can perform burn()', async ({helper}) => {105 const owner = await helper.eth.createAccountWithBalance(donor);106 const receiver = await helper.eth.createAccountWithBalance(donor);107 const collection = await helper.ft.mintCollection(alice);108 await collection.addAdmin(alice, {Ethereum: owner});109110 const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);111 const contract = helper.ethNativeContract.collection(collectionAddress, 'ft', owner);112 await contract.methods.mint(receiver, 100).send();113114 const result = await contract.methods.burnFrom(receiver, 49).send({from: receiver});115 116 const event = result.events.Transfer;117 expect(event.address).to.equal(collectionAddress);118 expect(event.returnValues.from).to.equal(receiver);119 expect(event.returnValues.to).to.equal('0x0000000000000000000000000000000000000000');120 expect(event.returnValues.value).to.equal('49');121122 const balance = await contract.methods.balanceOf(receiver).call();123 expect(balance).to.equal('51');124 });125126 itEth('Can perform approve()', async ({helper}) => {127 const owner = await helper.eth.createAccountWithBalance(donor);128 const spender = helper.eth.createAccount();129 const collection = await helper.ft.mintCollection(alice);130 await collection.mint(alice, 200n, {Ethereum: owner});131132 const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);133 const contract = helper.ethNativeContract.collection(collectionAddress, 'ft', owner);134135 {136 const result = await contract.methods.approve(spender, 100).send({from: owner});137138 const event = result.events.Approval;139 expect(event.address).to.be.equal(collectionAddress);140 expect(event.returnValues.owner).to.be.equal(owner);141 expect(event.returnValues.spender).to.be.equal(spender);142 expect(event.returnValues.value).to.be.equal('100');143 }144145 {146 const allowance = await contract.methods.allowance(owner, spender).call();147 expect(+allowance).to.equal(100);148 }149 });150151 itEth('Can perform transferFrom()', async ({helper}) => {152 const owner = await helper.eth.createAccountWithBalance(donor);153 const spender = await helper.eth.createAccountWithBalance(donor);154 const receiver = helper.eth.createAccount();155 const collection = await helper.ft.mintCollection(alice);156 await collection.mint(alice, 200n, {Ethereum: owner});157158 const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);159 const contract = helper.ethNativeContract.collection(collectionAddress, 'ft', owner);160161 await contract.methods.approve(spender, 100).send();162163 {164 const result = await contract.methods.transferFrom(owner, receiver, 49).send({from: spender});165 166 let event = result.events.Transfer;167 expect(event.address).to.be.equal(collectionAddress);168 expect(event.returnValues.from).to.be.equal(owner);169 expect(event.returnValues.to).to.be.equal(receiver);170 expect(event.returnValues.value).to.be.equal('49');171172 event = result.events.Approval;173 expect(event.address).to.be.equal(collectionAddress);174 expect(event.returnValues.owner).to.be.equal(owner);175 expect(event.returnValues.spender).to.be.equal(spender);176 expect(event.returnValues.value).to.be.equal('51');177 }178179 {180 const balance = await contract.methods.balanceOf(receiver).call();181 expect(+balance).to.equal(49);182 }183184 {185 const balance = await contract.methods.balanceOf(owner).call();186 expect(+balance).to.equal(151);187 }188 });189190 itEth('Can perform transfer()', async ({helper}) => {191 const owner = await helper.eth.createAccountWithBalance(donor);192 const receiver = await helper.eth.createAccountWithBalance(donor);193 const collection = await helper.ft.mintCollection(alice);194 await collection.mint(alice, 200n, {Ethereum: owner});195196 const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);197 const contract = helper.ethNativeContract.collection(collectionAddress, 'ft', owner);198199 {200 const result = await contract.methods.transfer(receiver, 50).send({from: owner});201 202 const event = result.events.Transfer;203 expect(event.address).to.be.equal(collectionAddress);204 expect(event.returnValues.from).to.be.equal(owner);205 expect(event.returnValues.to).to.be.equal(receiver);206 expect(event.returnValues.value).to.be.equal('50');207 }208209 {210 const balance = await contract.methods.balanceOf(owner).call();211 expect(+balance).to.equal(150);212 }213214 {215 const balance = await contract.methods.balanceOf(receiver).call();216 expect(+balance).to.equal(50);217 }218 });219});220221describe('Fungible: Fees', () => {222 let donor: IKeyringPair;223 let alice: IKeyringPair;224225 before(async function() {226 await usingEthPlaygrounds(async (helper, privateKey) => {227 donor = await privateKey({filename: __filename});228 [alice] = await helper.arrange.createAccounts([20n], donor);229 });230 });231 232 itEth('approve() call fee is less than 0.2UNQ', async ({helper}) => {233 const owner = await helper.eth.createAccountWithBalance(donor);234 const spender = 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 const cost = await helper.eth.recordCallFee(owner, () => contract.methods.approve(spender, 100).send({from: owner}));242 expect(cost < BigInt(0.2 * Number(helper.balance.getOneTokenNominal())));243 });244245 itEth('transferFrom() call fee is less than 0.2UNQ', async ({helper}) => {246 const owner = await helper.eth.createAccountWithBalance(donor);247 const spender = await helper.eth.createAccountWithBalance(donor);248 const collection = await helper.ft.mintCollection(alice);249 await collection.mint(alice, 200n, {Ethereum: owner});250251 const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);252 const contract = helper.ethNativeContract.collection(collectionAddress, 'ft', owner);253254 await contract.methods.approve(spender, 100).send({from: owner});255256 const cost = await helper.eth.recordCallFee(spender, () => contract.methods.transferFrom(owner, spender, 100).send({from: spender}));257 expect(cost < BigInt(0.2 * Number(helper.balance.getOneTokenNominal())));258 });259260 itEth('transfer() call fee is less than 0.2UNQ', async ({helper}) => {261 const owner = await helper.eth.createAccountWithBalance(donor);262 const receiver = helper.eth.createAccount();263 const collection = await helper.ft.mintCollection(alice);264 await collection.mint(alice, 200n, {Ethereum: owner});265266 const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);267 const contract = helper.ethNativeContract.collection(collectionAddress, 'ft', owner);268269 const cost = await helper.eth.recordCallFee(owner, () => contract.methods.transfer(receiver, 100).send({from: owner}));270 expect(cost < BigInt(0.2 * Number(helper.balance.getOneTokenNominal())));271 });272});273274describe('Fungible: Substrate calls', () => {275 let donor: IKeyringPair;276 let alice: IKeyringPair;277278 before(async function() {279 await usingEthPlaygrounds(async (helper, privateKey) => {280 donor = await privateKey({filename: __filename});281 [alice] = await helper.arrange.createAccounts([20n], donor);282 });283 });284285 itEth('Events emitted for approve()', async ({helper}) => {286 const receiver = helper.eth.createAccount();287 const collection = await helper.ft.mintCollection(alice);288 await collection.mint(alice, 200n);289290 const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);291 const contract = helper.ethNativeContract.collection(collectionAddress, 'ft');292293 const events: any = [];294 contract.events.allEvents((_: any, event: any) => {295 events.push(event);296 });297 298 await collection.approveTokens(alice, {Ethereum: receiver}, 100n);299 if (events.length == 0) await helper.wait.newBlocks(1);300 const event = events[0];301302 expect(event.event).to.be.equal('Approval');303 expect(event.address).to.be.equal(collectionAddress);304 expect(event.returnValues.owner).to.be.equal(helper.address.substrateToEth(alice.address));305 expect(event.returnValues.spender).to.be.equal(receiver);306 expect(event.returnValues.value).to.be.equal('100');307 });308309 itEth('Events emitted for transferFrom()', async ({helper}) => {310 const [bob] = await helper.arrange.createAccounts([10n], donor);311 const receiver = helper.eth.createAccount();312 const collection = await helper.ft.mintCollection(alice);313 await collection.mint(alice, 200n);314 await collection.approveTokens(alice, {Substrate: bob.address}, 100n);315316 const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);317 const contract = helper.ethNativeContract.collection(collectionAddress, 'ft');318319 const events: any = [];320 contract.events.allEvents((_: any, event: any) => {321 events.push(event);322 });323324 await collection.transferFrom(bob, {Substrate: alice.address}, {Ethereum: receiver}, 51n);325 if (events.length == 0) await helper.wait.newBlocks(1);326 let event = events[0];327328 expect(event.event).to.be.equal('Transfer');329 expect(event.address).to.be.equal(collectionAddress);330 expect(event.returnValues.from).to.be.equal(helper.address.substrateToEth(alice.address));331 expect(event.returnValues.to).to.be.equal(receiver);332 expect(event.returnValues.value).to.be.equal('51');333334 event = events[1];335 expect(event.event).to.be.equal('Approval');336 expect(event.address).to.be.equal(collectionAddress);337 expect(event.returnValues.owner).to.be.equal(helper.address.substrateToEth(alice.address));338 expect(event.returnValues.spender).to.be.equal(helper.address.substrateToEth(bob.address));339 expect(event.returnValues.value).to.be.equal('49');340 });341342 itEth('Events emitted for transfer()', async ({helper}) => {343 const receiver = helper.eth.createAccount();344 const collection = await helper.ft.mintCollection(alice);345 await collection.mint(alice, 200n);346347 const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);348 const contract = helper.ethNativeContract.collection(collectionAddress, 'ft');349350 const events: any = [];351 contract.events.allEvents((_: any, event: any) => {352 events.push(event);353 });354 355 await collection.transfer(alice, {Ethereum:receiver}, 51n);356 if (events.length == 0) await helper.wait.newBlocks(1);357 const event = events[0];358359 expect(event.event).to.be.equal('Transfer');360 expect(event.address).to.be.equal(collectionAddress);361 expect(event.returnValues.from).to.be.equal(helper.address.substrateToEth(alice.address));362 expect(event.returnValues.to).to.be.equal(receiver);363 expect(event.returnValues.value).to.be.equal('51');364 });365});