1234567891011121314151617import {Pallets, requirePalletsOrSkip} from '@unique/test-utils/util.js';18import {expect, itEth, usingEthPlaygrounds} from '@unique/test-utils/eth/util.js';19import {EthUniqueHelper} from '@unique/test-utils/eth/index.js';20import type {IKeyringPair} from '@polkadot/types/types';21import {Contract} from 'web3-eth-contract';222324describe('Check ERC721 token URI for ReFungible', () => {25 let donor: IKeyringPair;2627 before(async function() {28 await usingEthPlaygrounds(async (helper, privateKey) => {29 requirePalletsOrSkip(this, helper, [Pallets.ReFungible]);3031 donor = await privateKey({url: import.meta.url});32 });33 });3435 async function setup(helper: EthUniqueHelper, baseUri: string, propertyKey?: string, propertyValue?: string): Promise<{contract: Contract, nextTokenId: string}> {36 const owner = await helper.eth.createAccountWithBalance(donor);37 const receiver = helper.eth.createAccount();3839 const {collectionAddress} = await helper.eth.createERC721MetadataCompatibleRFTCollection(owner, 'Mint collection', 'a', 'b', baseUri);40 const contract = await helper.ethNativeContract.collection(collectionAddress, 'rft', owner);4142 const result = await contract.methods.mint(receiver).send();4344 const event = result.events.Transfer;45 const tokenId = event.returnValues.tokenId;46 expect(tokenId).to.be.equal('1');47 expect(event.address).to.be.equal(collectionAddress);48 expect(event.returnValues.from).to.be.equal('0x0000000000000000000000000000000000000000');49 expect(event.returnValues.to).to.be.equal(receiver);5051 if(propertyKey && propertyValue) {52 5354 await contract.methods.setProperties(tokenId, [{key: propertyKey, value: Buffer.from(propertyValue)}]).send();55 }5657 return {contract, nextTokenId: tokenId};58 }5960 itEth('Empty tokenURI', async ({helper}) => {61 const {contract, nextTokenId} = await setup(helper, '');62 expect(await contract.methods.tokenURI(nextTokenId).call()).to.be.equal('');63 });6465 itEth('TokenURI from url', async ({helper}) => {66 const {contract, nextTokenId} = await setup(helper, 'BaseURI_', 'URI', 'Token URI');67 expect(await contract.methods.tokenURI(nextTokenId).call()).to.be.equal('Token URI');68 });6970 itEth('TokenURI from baseURI', async ({helper}) => {71 const {contract, nextTokenId} = await setup(helper, 'BaseURI_');72 expect(await contract.methods.tokenURI(nextTokenId).call()).to.be.equal('BaseURI_');73 });7475 itEth('TokenURI from baseURI + suffix', async ({helper}) => {76 const suffix = '/some/suffix';77 const {contract, nextTokenId} = await setup(helper, 'BaseURI_', 'URISuffix', suffix);78 expect(await contract.methods.tokenURI(nextTokenId).call()).to.be.equal('BaseURI_' + suffix);79 });80});8182describe('Refungible: Plain calls', () => {83 let donor: IKeyringPair;84 let alice: IKeyringPair;8586 before(async function() {87 await usingEthPlaygrounds(async (helper, privateKey) => {88 requirePalletsOrSkip(this, helper, [Pallets.ReFungible]);8990 donor = await privateKey({url: import.meta.url});91 [alice] = await helper.arrange.createAccounts([50n], donor);92 });93 });9495 itEth('Can perform approve()', async ({helper}) => {96 const owner = await helper.eth.createAccountWithBalance(donor);97 const spender = helper.eth.createAccount();98 const collection = await helper.rft.mintCollection(alice);99 const {tokenId} = await collection.mintToken(alice, 200n, {Ethereum: owner});100101 const tokenAddress = helper.ethAddress.fromTokenId(collection.collectionId, tokenId);102 const contract = await helper.ethNativeContract.rftToken(tokenAddress, owner);103104 {105 const result = await contract.methods.approve(spender, 100).send({from: owner});106 const event = result.events.Approval;107 expect(event.address).to.be.equal(tokenAddress);108 expect(event.returnValues.owner).to.be.equal(owner);109 expect(event.returnValues.spender).to.be.equal(spender);110 expect(event.returnValues.value).to.be.equal('100');111 }112113 {114 const allowance = await contract.methods.allowance(owner, spender).call();115 expect(+allowance).to.equal(100);116 }117 });118119 itEth('Can perform approveCross()', async ({helper}) => {120 const owner = await helper.eth.createAccountWithBalance(donor);121 const spender = helper.eth.createAccount();122 const spenderSub = (await helper.arrange.createAccounts([1n], donor))[0];123 const spenderCrossEth = helper.ethCrossAccount.fromAddress(spender);124 const spenderCrossSub = helper.ethCrossAccount.fromKeyringPair(spenderSub);125126127 const collection = await helper.rft.mintCollection(alice);128 const {tokenId} = await collection.mintToken(alice, 200n, {Ethereum: owner});129130 const tokenAddress = helper.ethAddress.fromTokenId(collection.collectionId, tokenId);131 const contract = await helper.ethNativeContract.rftToken(tokenAddress, owner);132133 {134 const result = await contract.methods.approveCross(spenderCrossEth, 100).send({from: owner});135 const event = result.events.Approval;136 expect(event.address).to.be.equal(tokenAddress);137 expect(event.returnValues.owner).to.be.equal(owner);138 expect(event.returnValues.spender).to.be.equal(spender);139 expect(event.returnValues.value).to.be.equal('100');140 }141142 {143 const allowance = await contract.methods.allowance(owner, spender).call();144 expect(+allowance).to.equal(100);145 }146147148 {149 const result = await contract.methods.approveCross(spenderCrossSub, 100).send({from: owner});150 const event = result.events.Approval;151 expect(event.address).to.be.equal(tokenAddress);152 expect(event.returnValues.owner).to.be.equal(owner);153 expect(event.returnValues.spender).to.be.equal(helper.address.substrateToEth(spenderSub.address));154 expect(event.returnValues.value).to.be.equal('100');155 }156157 {158 const allowance = await collection.getTokenApprovedPieces(tokenId, {Ethereum: owner}, {Substrate: spenderSub.address});159 expect(allowance).to.equal(100n);160 }161162 {163 164 }165 });166167 itEth('Non-owner and non admin cannot approveCross', async ({helper}) => {168 const nonOwner = await helper.eth.createAccountWithBalance(donor);169 const nonOwnerCross = helper.ethCrossAccount.fromAddress(nonOwner);170 const owner = await helper.eth.createAccountWithBalance(donor);171 const collection = await helper.rft.mintCollection(alice, {name: 'A', description: 'B', tokenPrefix: 'C'});172 const token = await collection.mintToken(alice, 100n, {Ethereum: owner});173174 const tokenAddress = helper.ethAddress.fromTokenId(collection.collectionId, token.tokenId);175 const tokenEvm = await helper.ethNativeContract.rftToken(tokenAddress, owner);176177 await expect(tokenEvm.methods.approveCross(nonOwnerCross, 20).call({from: nonOwner})).to.be.rejectedWith('CantApproveMoreThanOwned');178 });179180 [181 'transferFrom',182 'transferFromCross',183 ].map(testCase =>184 itEth(`Can perform ${testCase}`, async ({helper}) => {185 const isCross = testCase === 'transferFromCross';186 const owner = await helper.eth.createAccountWithBalance(donor);187 const ownerCross = helper.ethCrossAccount.fromAddress(owner);188 const spender = await helper.eth.createAccountWithBalance(donor);189 const receiverEth = helper.eth.createAccount();190 const receiverCrossEth = helper.ethCrossAccount.fromAddress(receiverEth);191 const [receiverSub] = await helper.arrange.createAccounts([1n], donor);192 const receiverCrossSub = helper.ethCrossAccount.fromKeyringPair(receiverSub);193194 const collection = await helper.rft.mintCollection(alice);195 const {tokenId} = await collection.mintToken(alice, 200n, {Ethereum: owner});196197 const tokenAddress = helper.ethAddress.fromTokenId(collection.collectionId, tokenId);198 const contract = await helper.ethNativeContract.rftToken(tokenAddress, owner);199200 await contract.methods.approve(spender, 100).send({from: owner});201202 203 204 {205 const result = await contract.methods[testCase](206 isCross ? ownerCross : owner,207 isCross ? receiverCrossEth : receiverEth,208 49,209 ).send({from: spender});210211 212 const transferEvent = result.events.Transfer;213 expect(transferEvent.address).to.be.equal(tokenAddress);214 expect(transferEvent.returnValues.from).to.be.equal(owner);215 expect(transferEvent.returnValues.to).to.be.equal(receiverEth);216 expect(transferEvent.returnValues.value).to.be.equal('49');217218 const approvalEvent = result.events.Approval;219 expect(approvalEvent.address).to.be.equal(tokenAddress);220 expect(approvalEvent.returnValues.owner).to.be.equal(owner);221 expect(approvalEvent.returnValues.spender).to.be.equal(spender);222 expect(approvalEvent.returnValues.value).to.be.equal('51');223224 225 const receiverBalance = await contract.methods.balanceOf(receiverEth).call();226 const ownerBalance = await contract.methods.balanceOf(owner).call();227228 expect(+receiverBalance).to.equal(49);229 expect(+ownerBalance).to.equal(151);230 }231232 233 if(testCase === 'transferFromCross') {234 const result = await contract.methods.transferFromCross(ownerCross, receiverCrossSub, 51).send({from: spender});235 236 const transferEvent = result.events.Transfer;237 expect(transferEvent.address).to.be.equal(tokenAddress);238 expect(transferEvent.returnValues.from).to.be.equal(owner);239 expect(transferEvent.returnValues.to).to.be.equal(helper.address.substrateToEth(receiverSub.address));240 expect(transferEvent.returnValues.value).to.be.equal('51');241242 const approvalEvent = result.events.Approval;243 expect(approvalEvent.address).to.be.equal(tokenAddress);244 expect(approvalEvent.returnValues.owner).to.be.equal(owner);245 expect(approvalEvent.returnValues.spender).to.be.equal(spender);246 expect(approvalEvent.returnValues.value).to.be.equal('0');247248 249 const receiverBalance = await collection.getTokenBalance(tokenId, {Substrate: receiverSub.address});250 const ownerBalance = await contract.methods.balanceOf(owner).call();251 expect(receiverBalance).to.equal(51n);252 expect(+ownerBalance).to.equal(100);253 }254 }));255256 [257 'transfer',258 'transferCross',259 ].map(testCase =>260 itEth(`Can perform ${testCase}()`, async ({helper}) => {261 const isCross = testCase === 'transferCross';262 const owner = await helper.eth.createAccountWithBalance(donor);263 const receiverEth = helper.eth.createAccount();264 const receiverCrossEth = helper.ethCrossAccount.fromAddress(receiverEth);265 const [receiverSub] = await helper.arrange.createAccounts([1n], donor);266 const receiverCrossSub = helper.ethCrossAccount.fromKeyringPair(receiverSub);267 const collection = await helper.rft.mintCollection(alice);268 const {tokenId} = await collection.mintToken(alice, 200n, {Ethereum: owner});269270 const tokenAddress = helper.ethAddress.fromTokenId(collection.collectionId, tokenId);271 const contract = await helper.ethNativeContract.rftToken(tokenAddress, owner);272273 274 {275 const result = await contract.methods[testCase](isCross ? receiverCrossEth : receiverEth, 50).send({from: owner});276 277 const transferEvent = result.events.Transfer;278 expect(transferEvent.address).to.be.equal(tokenAddress);279 expect(transferEvent.returnValues.from).to.be.equal(owner);280 expect(transferEvent.returnValues.to).to.be.equal(receiverEth);281 expect(transferEvent.returnValues.value).to.be.equal('50');282 283 const ownerBalance = await contract.methods.balanceOf(owner).call();284 const receiverBalance = await contract.methods.balanceOf(receiverEth).call();285 expect(+ownerBalance).to.equal(150);286 expect(+receiverBalance).to.equal(50);287 }288289 290 if(isCross) {291 const result = await contract.methods.transferCross(receiverCrossSub, 50).send({from: owner});292 293 const event = result.events.Transfer;294 expect(event.address).to.be.equal(tokenAddress);295 expect(event.returnValues.from).to.be.equal(owner);296 expect(event.returnValues.to).to.be.equal(helper.address.substrateToEth(receiverSub.address));297 expect(event.returnValues.value).to.be.equal('50');298 299 const ownerBalance = await contract.methods.balanceOf(owner).call();300 const receiverBalance = await collection.getTokenBalance(tokenId, {Substrate: receiverSub.address});301 expect(+ownerBalance).to.equal(100);302 expect(receiverBalance).to.equal(50n);303 }304 }));305306 [307 'transfer',308 'transferCross',309 ].map(testCase =>310 itEth(`Cannot ${testCase}() non-owned token`, async ({helper}) => {311 const isCross = testCase === 'transferCross';312 const owner = await helper.eth.createAccountWithBalance(donor);313 const ownerCross = helper.ethCrossAccount.fromAddress(owner);314 const receiverEth = await helper.eth.createAccountWithBalance(donor);315 const receiverCrossEth = helper.ethCrossAccount.fromAddress(receiverEth);316 const collection = await helper.rft.mintCollection(alice);317 const rftOwner = await collection.mintToken(alice, 10n, {Ethereum: owner});318 const rftReceiver = await collection.mintToken(alice, 10n, {Ethereum: receiverEth});319 const tokenIdNonExist = 9999999;320321 const tokenAddress1 = helper.ethAddress.fromTokenId(collection.collectionId, rftOwner.tokenId);322 const tokenAddress2 = helper.ethAddress.fromTokenId(collection.collectionId, rftReceiver.tokenId);323 const tokenAddressNonExist = helper.ethAddress.fromTokenId(collection.collectionId, tokenIdNonExist);324 const tokenEvmOwner = await helper.ethNativeContract.rftToken(tokenAddress1, owner);325 const tokenEvmReceiver = await helper.ethNativeContract.rftToken(tokenAddress2, owner);326 const tokenEvmNonExist = await helper.ethNativeContract.rftToken(tokenAddressNonExist, owner);327328 329 await tokenEvmOwner.methods[testCase](isCross ? receiverCrossEth : receiverEth, 0).send({from: owner});330 331 await expect(tokenEvmReceiver.methods[testCase](isCross ? ownerCross : owner, 0).send({from: owner})).to.be.rejected;332 await expect(tokenEvmReceiver.methods[testCase](isCross ? ownerCross : owner, 5).send({from: owner})).to.be.rejected;333 334 await expect(tokenEvmNonExist.methods[testCase](isCross ? ownerCross : owner, 0).send({from: owner})).to.be.rejected;335 await expect(tokenEvmNonExist.methods[testCase](isCross ? ownerCross : owner, 5).send({from: owner})).to.be.rejected;336337 338 expect(await rftOwner.getTop10Owners()).to.deep.eq([{Ethereum: owner.toLowerCase()}]);339 expect(await rftReceiver.getTop10Owners()).to.deep.eq([{Ethereum: receiverEth.toLowerCase()}]);340 expect(await helper.rft.getTokenTop10Owners(collection.collectionId, tokenIdNonExist)).to.deep.eq([]);341342 343 await tokenEvmOwner.methods[testCase](isCross ? receiverCrossEth : receiverEth, 10).send({from: owner});344 await tokenEvmReceiver.methods[testCase](isCross ? ownerCross : owner, 10).send({from: receiverEth});345 expect(await rftOwner.getTop10Owners()).to.deep.eq([{Ethereum: receiverEth.toLowerCase()}]);346 expect(await rftReceiver.getTop10Owners()).to.deep.eq([{Ethereum: owner.toLowerCase()}]);347 }));348349 itEth('Can perform repartition()', async ({helper}) => {350 const owner = await helper.eth.createAccountWithBalance(donor);351 const receiver = await helper.eth.createAccountWithBalance(donor);352 const collection = await helper.rft.mintCollection(alice);353 const {tokenId} = await collection.mintToken(alice, 100n, {Ethereum: owner});354355 const tokenAddress = helper.ethAddress.fromTokenId(collection.collectionId, tokenId);356 const contract = await helper.ethNativeContract.rftToken(tokenAddress, owner);357358 await contract.methods.repartition(200).send({from: owner});359 expect(+await contract.methods.balanceOf(owner).call()).to.be.equal(200);360 await contract.methods.transfer(receiver, 110).send({from: owner});361 expect(+await contract.methods.balanceOf(owner).call()).to.be.equal(90);362 expect(+await contract.methods.balanceOf(receiver).call()).to.be.equal(110);363364 await expect(contract.methods.repartition(80).send({from: owner})).to.eventually.be.rejected; 365366 await contract.methods.transfer(receiver, 90).send({from: owner});367 expect(+await contract.methods.balanceOf(owner).call()).to.be.equal(0);368 expect(+await contract.methods.balanceOf(receiver).call()).to.be.equal(200);369370 await contract.methods.repartition(150).send({from: receiver});371 await expect(contract.methods.transfer(owner, 160).send({from: receiver})).to.eventually.be.rejected; 372 expect(+await contract.methods.balanceOf(receiver).call()).to.be.equal(150);373 });374375 itEth('Can repartition with increased amount', async ({helper}) => {376 const owner = await helper.eth.createAccountWithBalance(donor);377 const collection = await helper.rft.mintCollection(alice);378 const {tokenId} = await collection.mintToken(alice, 100n, {Ethereum: owner});379380 const tokenAddress = helper.ethAddress.fromTokenId(collection.collectionId, tokenId);381 const contract = await helper.ethNativeContract.rftToken(tokenAddress, owner);382383 const result = await contract.methods.repartition(200).send();384385 const event = result.events.Transfer;386 expect(event.address).to.be.equal(tokenAddress);387 expect(event.returnValues.from).to.be.equal('0x0000000000000000000000000000000000000000');388 expect(event.returnValues.to).to.be.equal(owner);389 expect(event.returnValues.value).to.be.equal('100');390 });391392 itEth('Can repartition with decreased amount', async ({helper}) => {393 const owner = await helper.eth.createAccountWithBalance(donor);394 const collection = await helper.rft.mintCollection(alice);395 const {tokenId} = await collection.mintToken(alice, 100n, {Ethereum: owner});396397 const tokenAddress = helper.ethAddress.fromTokenId(collection.collectionId, tokenId);398 const contract = await helper.ethNativeContract.rftToken(tokenAddress, owner);399400 const result = await contract.methods.repartition(50).send();401 const event = result.events.Transfer;402 expect(event.address).to.be.equal(tokenAddress);403 expect(event.returnValues.from).to.be.equal(owner);404 expect(event.returnValues.to).to.be.equal('0x0000000000000000000000000000000000000000');405 expect(event.returnValues.value).to.be.equal('50');406 });407408 itEth('Receiving Transfer event on burning into full ownership', async ({helper}) => {409 const caller = await helper.eth.createAccountWithBalance(donor);410 const receiver = await helper.eth.createAccountWithBalance(donor);411 const {collectionId, collectionAddress} = await helper.eth.createRFTCollection(caller, 'Devastation', '6', '6');412 const contract = await helper.ethNativeContract.collection(collectionAddress, 'rft', caller);413414 const result = await contract.methods.mint(caller).send();415 const tokenId = result.events.Transfer.returnValues.tokenId;416 const tokenAddress = helper.ethAddress.fromTokenId(collectionId, tokenId);417 const tokenContract = await helper.ethNativeContract.rftToken(tokenAddress, caller, true);418419 await tokenContract.methods.repartition(2).send();420 await tokenContract.methods.transfer(receiver, 1).send();421422 const events: any = [];423 contract.events.allEvents((_: any, event: any) => {424 events.push(event);425 });426 await tokenContract.methods.burnFrom(caller, 1).send();427428 if(events.length == 0) await helper.wait.newBlocks(1);429 const event = events[0];430 expect(event.address).to.be.equal(collectionAddress);431 expect(event.returnValues.from).to.be.equal('0xFFfFfFffFFfffFFfFFfFFFFFffFFFffffFfFFFfF');432 expect(event.returnValues.to).to.be.equal(receiver);433 expect(event.returnValues.tokenId).to.be.equal(tokenId);434 });435436 itEth('Can perform burnFromCross()', async ({helper}) => {437 const owner = await helper.eth.createAccountWithBalance(donor);438 const ownerSub = (await helper.arrange.createAccounts([10n], donor))[0];439 const ownerCross = helper.ethCrossAccount.fromAddress(owner);440 const spender = await helper.eth.createAccountWithBalance(donor);441442 const spenderCrossEth = helper.ethCrossAccount.fromAddress(spender);443 const ownerSubCross = helper.ethCrossAccount.fromKeyringPair(ownerSub);444445 const collection = await helper.rft.mintCollection(alice);446 const {tokenId} = await collection.mintToken(alice, 200n, {Ethereum: owner});447448449 const tokenAddress = helper.ethAddress.fromTokenId(collection.collectionId, tokenId);450 const contract = await helper.ethNativeContract.rftToken(tokenAddress, owner);451452 {453 await contract.methods.approveCross(spenderCrossEth, 100).send({from: owner});454455 await expect(contract.methods.burnFromCross(ownerCross, 50).send({from: spender})).to.be.fulfilled;456 await expect(contract.methods.burnFromCross(ownerCross, 100).send({from: spender})).to.be.rejected;457 expect(await contract.methods.balanceOf(owner).call({from: owner})).to.be.equal('150');458 }459 {460 const {tokenId} = await collection.mintToken(alice, 200n, {Substrate: ownerSub.address});461 await collection.approveToken(ownerSub, tokenId, {Ethereum: spender}, 100n);462 const tokenAddress = helper.ethAddress.fromTokenId(collection.collectionId, tokenId);463 const contract = await helper.ethNativeContract.rftToken(tokenAddress, owner);464465 await expect(contract.methods.burnFromCross(ownerSubCross, 50).send({from: spender})).to.be.fulfilled;466 await expect(contract.methods.burnFromCross(ownerSubCross, 100).send({from: spender})).to.be.rejected;467 expect(await collection.getTokenBalance(tokenId, {Substrate: ownerSub.address})).to.be.equal(150n);468 }469 });470471 itEth('Check balanceOfCross()', async ({helper}) => {472 const collection = await helper.rft.mintCollection(alice, {});473 const owner = await helper.ethCrossAccount.createAccountWithBalance(donor);474 const other = await helper.ethCrossAccount.createAccountWithBalance(donor);475 const {tokenId} = await collection.mintToken(alice, 200n, {Ethereum: owner.eth});476 const tokenAddress = helper.ethAddress.fromTokenId(collection.collectionId, tokenId);477 const tokenContract = await helper.ethNativeContract.rftToken(tokenAddress, owner.eth);478479 expect(await tokenContract.methods.balanceOfCross(owner).call({from: owner.eth})).to.be.eq('200');480 expect(await tokenContract.methods.balanceOfCross(other).call({from: owner.eth})).to.be.eq('0');481482 await tokenContract.methods.repartition(100n).send({from: owner.eth});483 expect(await tokenContract.methods.balanceOfCross(owner).call({from: owner.eth})).to.be.eq('100');484 expect(await tokenContract.methods.balanceOfCross(other).call({from: owner.eth})).to.be.eq('0');485486 await tokenContract.methods.transferCross(other, 50n).send({from: owner.eth});487 expect(await tokenContract.methods.balanceOfCross(owner).call({from: owner.eth})).to.be.eq('50');488 expect(await tokenContract.methods.balanceOfCross(other).call({from: owner.eth})).to.be.eq('50');489490 await tokenContract.methods.transferCross(other, 50n).send({from: owner.eth});491 expect(await tokenContract.methods.balanceOfCross(owner).call({from: owner.eth})).to.be.eq('0');492 expect(await tokenContract.methods.balanceOfCross(other).call({from: owner.eth})).to.be.eq('100');493494 await tokenContract.methods.repartition(1000n).send({from: other.eth});495 await tokenContract.methods.transferCross(owner, 500n).send({from: other.eth});496 expect(await tokenContract.methods.balanceOfCross(owner).call({from: owner.eth})).to.be.eq('500');497 expect(await tokenContract.methods.balanceOfCross(other).call({from: owner.eth})).to.be.eq('500');498 });499});500501describe('Refungible: Fees', () => {502 let donor: IKeyringPair;503 let alice: IKeyringPair;504505 before(async function() {506 await usingEthPlaygrounds(async (helper, privateKey) => {507 requirePalletsOrSkip(this, helper, [Pallets.ReFungible]);508509 donor = await privateKey({url: import.meta.url});510 [alice] = await helper.arrange.createAccounts([50n], donor);511 });512 });513514 itEth('approve() call fee is less than 0.2UNQ', async ({helper}) => {515 const owner = await helper.eth.createAccountWithBalance(donor);516 const spender = helper.eth.createAccount();517 const collection = await helper.rft.mintCollection(alice);518 const {tokenId} = await collection.mintToken(alice, 100n, {Ethereum: owner});519520 const tokenAddress = helper.ethAddress.fromTokenId(collection.collectionId, tokenId);521 const contract = await helper.ethNativeContract.rftToken(tokenAddress, owner);522523 const cost = await helper.eth.recordCallFee(owner, () => contract.methods.approve(spender, 100).send({from: owner}));524 expect(cost < BigInt(0.2 * Number(helper.balance.getOneTokenNominal())));525 });526527 itEth('transferFrom() call fee is less than 0.2UNQ', async ({helper}) => {528 const owner = await helper.eth.createAccountWithBalance(donor);529 const spender = await helper.eth.createAccountWithBalance(donor);530 const collection = await helper.rft.mintCollection(alice);531 const {tokenId} = await collection.mintToken(alice, 200n, {Ethereum: owner});532533 const tokenAddress = helper.ethAddress.fromTokenId(collection.collectionId, tokenId);534 const contract = await helper.ethNativeContract.rftToken(tokenAddress, owner);535536 await contract.methods.approve(spender, 100).send({from: owner});537538 const cost = await helper.eth.recordCallFee(spender, () => contract.methods.transferFrom(owner, spender, 100).send({from: spender}));539 expect(cost < BigInt(0.2 * Number(helper.balance.getOneTokenNominal())));540 });541542 itEth('transfer() call fee is less than 0.2UNQ', async ({helper}) => {543 const owner = await helper.eth.createAccountWithBalance(donor);544 const receiver = helper.eth.createAccount();545 const collection = await helper.rft.mintCollection(alice);546 const {tokenId} = await collection.mintToken(alice, 200n, {Ethereum: owner});547548 const tokenAddress = helper.ethAddress.fromTokenId(collection.collectionId, tokenId);549 const contract = await helper.ethNativeContract.rftToken(tokenAddress, owner);550551 const cost = await helper.eth.recordCallFee(owner, () => contract.methods.transfer(receiver, 100).send({from: owner}));552 expect(cost < BigInt(0.2 * Number(helper.balance.getOneTokenNominal())));553 });554});555556describe('Refungible: Substrate calls', () => {557 let donor: IKeyringPair;558 let alice: IKeyringPair;559560 before(async function() {561 await usingEthPlaygrounds(async (helper, privateKey) => {562 requirePalletsOrSkip(this, helper, [Pallets.ReFungible]);563564 donor = await privateKey({url: import.meta.url});565 [alice] = await helper.arrange.createAccounts([50n], donor);566 });567 });568569 itEth('Events emitted for approve()', async ({helper}) => {570 const receiver = helper.eth.createAccount();571 const collection = await helper.rft.mintCollection(alice);572 const token = await collection.mintToken(alice, 200n);573574 const tokenAddress = helper.ethAddress.fromTokenId(collection.collectionId, token.tokenId);575 const contract = await helper.ethNativeContract.rftToken(tokenAddress);576577 const events: any = [];578 contract.events.allEvents((_: any, event: any) => {579 events.push(event);580 });581582 expect(await token.approve(alice, {Ethereum: receiver}, 100n)).to.be.true;583 if(events.length == 0) await helper.wait.newBlocks(1);584 const event = events[0];585586 expect(event.event).to.be.equal('Approval');587 expect(event.address).to.be.equal(tokenAddress);588 expect(event.returnValues.owner).to.be.equal(helper.address.substrateToEth(alice.address));589 expect(event.returnValues.spender).to.be.equal(receiver);590 expect(event.returnValues.value).to.be.equal('100');591 });592593 itEth('Events emitted for transferFrom()', async ({helper}) => {594 const [bob] = await helper.arrange.createAccounts([10n], donor);595 const receiver = helper.eth.createAccount();596 const collection = await helper.rft.mintCollection(alice);597 const token = await collection.mintToken(alice, 200n);598 await token.approve(alice, {Substrate: bob.address}, 100n);599600 const tokenAddress = helper.ethAddress.fromTokenId(collection.collectionId, token.tokenId);601 const contract = await helper.ethNativeContract.rftToken(tokenAddress);602603 const events: any = [];604 contract.events.allEvents((_: any, event: any) => {605 events.push(event);606 });607608 expect(await token.transferFrom(bob, {Substrate: alice.address}, {Ethereum: receiver}, 51n)).to.be.true;609 if(events.length == 0) await helper.wait.newBlocks(1);610611 let event = events[0];612 expect(event.event).to.be.equal('Transfer');613 expect(event.address).to.be.equal(tokenAddress);614 expect(event.returnValues.from).to.be.equal(helper.address.substrateToEth(alice.address));615 expect(event.returnValues.to).to.be.equal(receiver);616 expect(event.returnValues.value).to.be.equal('51');617618 event = events[1];619 expect(event.event).to.be.equal('Approval');620 expect(event.address).to.be.equal(tokenAddress);621 expect(event.returnValues.owner).to.be.equal(helper.address.substrateToEth(alice.address));622 expect(event.returnValues.spender).to.be.equal(helper.address.substrateToEth(bob.address));623 expect(event.returnValues.value).to.be.equal('49');624 });625626 itEth('Events emitted for transfer()', async ({helper}) => {627 const receiver = helper.eth.createAccount();628 const collection = await helper.rft.mintCollection(alice);629 const token = await collection.mintToken(alice, 200n);630631 const tokenAddress = helper.ethAddress.fromTokenId(collection.collectionId, token.tokenId);632 const contract = await helper.ethNativeContract.rftToken(tokenAddress);633634 const events: any = [];635 contract.events.allEvents((_: any, event: any) => {636 events.push(event);637 });638639 expect(await token.transfer(alice, {Ethereum: receiver}, 51n)).to.be.true;640 if(events.length == 0) await helper.wait.newBlocks(1);641 const event = events[0];642643 expect(event.event).to.be.equal('Transfer');644 expect(event.address).to.be.equal(tokenAddress);645 expect(event.returnValues.from).to.be.equal(helper.address.substrateToEth(alice.address));646 expect(event.returnValues.to).to.be.equal(receiver);647 expect(event.returnValues.value).to.be.equal('51');648 });649});650651describe('ERC 1633 implementation', () => {652 let donor: IKeyringPair;653654 before(async function() {655 await usingEthPlaygrounds(async (helper, privateKey) => {656 requirePalletsOrSkip(this, helper, [Pallets.ReFungible]);657658 donor = await privateKey({url: import.meta.url});659 });660 });661662 itEth('Default parent token address and id', async ({helper}) => {663 const owner = await helper.eth.createAccountWithBalance(donor);664665 const {collectionId, collectionAddress} = await helper.eth.createRFTCollection(owner, 'Sands', '', 'GRAIN');666 const collectionContract = await helper.ethNativeContract.collection(collectionAddress, 'rft', owner);667668 const result = await collectionContract.methods.mint(owner).send();669 const tokenId = result.events.Transfer.returnValues.tokenId;670671 const tokenAddress = helper.ethAddress.fromTokenId(collectionId, tokenId);672 const tokenContract = await helper.ethNativeContract.rftToken(tokenAddress, owner);673674 expect(await tokenContract.methods.parentToken().call()).to.be.equal(collectionAddress);675 expect(await tokenContract.methods.parentTokenId().call()).to.be.equal(tokenId);676 });677});