1234567891011121314151617import {IKeyringPair} from '@polkadot/types/types';18import {evmToAddress} from '@polkadot/util-crypto';19import {Pallets, requirePalletsOrSkip} from '../util';20import {expect, itEth, usingEthPlaygrounds} from './util';2122const DECIMALS = 18;2324describe('Create FT collection from EVM', () => {25 let donor: IKeyringPair;2627 before(async function() {28 await usingEthPlaygrounds(async (helper, privateKey) => {29 requirePalletsOrSkip(this, helper, [Pallets.Fungible]);30 donor = await privateKey({filename: __filename});31 });32 });33 34 itEth('Set sponsorship', async ({helper}) => {35 const owner = await helper.eth.createAccountWithBalance(donor);36 const sponsor = await helper.eth.createAccountWithBalance(donor);37 const ss58Format = helper.chain.getChainProperties().ss58Format;38 const {collectionId, collectionAddress} = await helper.eth.createFungibleCollection(owner, 'Sponsor', DECIMALS, 'absolutely anything', 'ENVY');3940 const collection = helper.ethNativeContract.collection(collectionAddress, 'rft', owner);41 await collection.methods.setCollectionSponsor(sponsor).send();4243 let data = (await helper.rft.getData(collectionId))!;44 expect(data.raw.sponsorship.Unconfirmed).to.be.equal(evmToAddress(sponsor, Number(ss58Format)));4546 await expect(collection.methods.confirmCollectionSponsorship().call()).to.be.rejectedWith('caller is not set as sponsor');4748 const sponsorCollection = helper.ethNativeContract.collection(collectionAddress, 'rft', sponsor);49 await sponsorCollection.methods.confirmCollectionSponsorship().send();5051 data = (await helper.rft.getData(collectionId))!;52 expect(data.raw.sponsorship.Confirmed).to.be.equal(evmToAddress(sponsor, Number(ss58Format)));53 });5455 itEth('Set limits', async ({helper}) => {56 const owner = await helper.eth.createAccountWithBalance(donor);57 const {collectionId, collectionAddress} = await helper.eth.createFungibleCollection(owner, 'Limits', DECIMALS, 'absolutely anything', 'INSI');58 const limits = {59 accountTokenOwnershipLimit: 1000,60 sponsoredDataSize: 1024,61 sponsoredDataRateLimit: 30,62 tokenLimit: 1000000,63 sponsorTransferTimeout: 6,64 sponsorApproveTimeout: 6,65 ownerCanTransfer: false,66 ownerCanDestroy: false,67 transfersEnabled: false,68 };6970 const collection = helper.ethNativeContract.collection(collectionAddress, 'ft', owner);71 await collection.methods['setCollectionLimit(string,uint32)']('accountTokenOwnershipLimit', limits.accountTokenOwnershipLimit).send();72 await collection.methods['setCollectionLimit(string,uint32)']('sponsoredDataSize', limits.sponsoredDataSize).send();73 await collection.methods['setCollectionLimit(string,uint32)']('sponsoredDataRateLimit', limits.sponsoredDataRateLimit).send();74 await collection.methods['setCollectionLimit(string,uint32)']('tokenLimit', limits.tokenLimit).send();75 await collection.methods['setCollectionLimit(string,uint32)']('sponsorTransferTimeout', limits.sponsorTransferTimeout).send();76 await collection.methods['setCollectionLimit(string,uint32)']('sponsorApproveTimeout', limits.sponsorApproveTimeout).send();77 await collection.methods['setCollectionLimit(string,bool)']('ownerCanTransfer', limits.ownerCanTransfer).send();78 await collection.methods['setCollectionLimit(string,bool)']('ownerCanDestroy', limits.ownerCanDestroy).send();79 await collection.methods['setCollectionLimit(string,bool)']('transfersEnabled', limits.transfersEnabled).send();80 81 const data = (await helper.rft.getData(collectionId))!;82 expect(data.raw.limits.accountTokenOwnershipLimit).to.be.eq(limits.accountTokenOwnershipLimit);83 expect(data.raw.limits.sponsoredDataSize).to.be.eq(limits.sponsoredDataSize);84 expect(data.raw.limits.sponsoredDataRateLimit.blocks).to.be.eq(limits.sponsoredDataRateLimit);85 expect(data.raw.limits.tokenLimit).to.be.eq(limits.tokenLimit);86 expect(data.raw.limits.sponsorTransferTimeout).to.be.eq(limits.sponsorTransferTimeout);87 expect(data.raw.limits.sponsorApproveTimeout).to.be.eq(limits.sponsorApproveTimeout);88 expect(data.raw.limits.ownerCanTransfer).to.be.eq(limits.ownerCanTransfer);89 expect(data.raw.limits.ownerCanDestroy).to.be.eq(limits.ownerCanDestroy);90 expect(data.raw.limits.transfersEnabled).to.be.eq(limits.transfersEnabled);91 });9293 itEth('Collection address exist', async ({helper}) => {94 const owner = await helper.eth.createAccountWithBalance(donor);95 const collectionAddressForNonexistentCollection = '0x17C4E6453CC49AAAAEACA894E6D9683E00112233';96 expect(await helper.ethNativeContract.collectionHelpers(collectionAddressForNonexistentCollection)97 .methods.isCollectionExist(collectionAddressForNonexistentCollection).call())98 .to.be.false;99 100 const {collectionAddress} = await helper.eth.createFungibleCollection(owner, 'Exister', DECIMALS, 'absolutely anything', 'WIWT');101 expect(await helper.ethNativeContract.collectionHelpers(collectionAddress)102 .methods.isCollectionExist(collectionAddress).call())103 .to.be.true;104 });105 106 itEth('destroyCollection', async ({helper}) => {107 const owner = await helper.eth.createAccountWithBalance(donor);108 const {collectionAddress} = await helper.eth.createFungibleCollection(owner, 'Exister', DECIMALS, 'absolutely anything', 'WIWT');109 const collectionHelper = helper.ethNativeContract.collectionHelpers(owner);110111 const result = await collectionHelper.methods112 .destroyCollection(collectionAddress)113 .send({from: owner});114115 const events = helper.eth.normalizeEvents(result.events);116 117 expect(events).to.be.deep.equal([118 {119 address: collectionHelper.options.address,120 event: 'CollectionDestroyed',121 args: {122 collectionId: collectionAddress,123 },124 },125 ]);126127 expect(await collectionHelper.methods128 .isCollectionExist(collectionAddress)129 .call()).to.be.false;130 });131});132133describe('(!negative tests!) Create FT collection from EVM', () => {134 let donor: IKeyringPair;135 let nominal: bigint;136137 before(async function() {138 await usingEthPlaygrounds(async (helper, privateKey) => {139 requirePalletsOrSkip(this, helper, [Pallets.Fungible]);140 donor = await privateKey({filename: __filename});141 nominal = helper.balance.getOneTokenNominal();142 });143 });144145 itEth('(!negative test!) Create collection (bad lengths)', async ({helper}) => {146 const owner = await helper.eth.createAccountWithBalance(donor);147 const collectionHelper = helper.ethNativeContract.collectionHelpers(owner);148 {149 const MAX_NAME_LENGTH = 64;150 const collectionName = 'A'.repeat(MAX_NAME_LENGTH + 1);151 const description = 'A';152 const tokenPrefix = 'A';153154 await expect(collectionHelper.methods155 .createFTCollection(collectionName, DECIMALS, description, tokenPrefix)156 .call({value: Number(2n * nominal)})).to.be.rejectedWith('name is too long. Max length is ' + MAX_NAME_LENGTH);157 }158 {159 const MAX_DESCRIPTION_LENGTH = 256;160 const collectionName = 'A';161 const description = 'A'.repeat(MAX_DESCRIPTION_LENGTH + 1);162 const tokenPrefix = 'A';163 await expect(collectionHelper.methods164 .createFTCollection(collectionName, DECIMALS, description, tokenPrefix)165 .call({value: Number(2n * nominal)})).to.be.rejectedWith('description is too long. Max length is ' + MAX_DESCRIPTION_LENGTH);166 }167 {168 const MAX_TOKEN_PREFIX_LENGTH = 16;169 const collectionName = 'A';170 const description = 'A';171 const tokenPrefix = 'A'.repeat(MAX_TOKEN_PREFIX_LENGTH + 1);172 await expect(collectionHelper.methods173 .createFTCollection(collectionName, DECIMALS, description, tokenPrefix)174 .call({value: Number(2n * nominal)})).to.be.rejectedWith('token_prefix is too long. Max length is ' + MAX_TOKEN_PREFIX_LENGTH);175 }176 });177 178 itEth('(!negative test!) Create collection (no funds)', async ({helper}) => {179 const owner = await helper.eth.createAccountWithBalance(donor);180 const collectionHelper = helper.ethNativeContract.collectionHelpers(owner);181 await expect(collectionHelper.methods182 .createFTCollection('Peasantry', DECIMALS, 'absolutely anything', 'TWIW')183 .call({value: Number(1n * nominal)})).to.be.rejectedWith('Sent amount not equals to collection creation price (2000000000000000000)');184 });185186 itEth('(!negative test!) Check owner', async ({helper}) => {187 const owner = await helper.eth.createAccountWithBalance(donor);188 const peasant = helper.eth.createAccount();189 const {collectionAddress} = await helper.eth.createFungibleCollection(owner, 'Transgressed', DECIMALS, 'absolutely anything', 'YVNE');190 const peasantCollection = helper.ethNativeContract.collection(collectionAddress, 'ft', peasant);191 const EXPECTED_ERROR = 'NoPermission';192 {193 const sponsor = await helper.eth.createAccountWithBalance(donor);194 await expect(peasantCollection.methods195 .setCollectionSponsor(sponsor)196 .call()).to.be.rejectedWith(EXPECTED_ERROR);197 198 const sponsorCollection = helper.ethNativeContract.collection(collectionAddress, 'ft', sponsor);199 await expect(sponsorCollection.methods200 .confirmCollectionSponsorship()201 .call()).to.be.rejectedWith('caller is not set as sponsor');202 }203 {204 await expect(peasantCollection.methods205 .setCollectionLimit('account_token_ownership_limit', '1000')206 .call()).to.be.rejectedWith(EXPECTED_ERROR);207 }208 });209210 itEth('(!negative test!) Set limits', async ({helper}) => {211 const owner = await helper.eth.createAccountWithBalance(donor);212 const {collectionAddress} = await helper.eth.createFungibleCollection(owner, 'Limits', DECIMALS, 'absolutely anything', 'ISNI');213 const collectionEvm = helper.ethNativeContract.collection(collectionAddress, 'ft', owner);214 await expect(collectionEvm.methods215 .setCollectionLimit('badLimit', 'true')216 .call()).to.be.rejectedWith('unknown boolean limit "badLimit"');217 });218});