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 });3334 itEth('Create collection', async ({helper}) => {35 const owner = await helper.eth.createAccountWithBalance(donor);36 37 const name = 'CollectionEVM';38 const description = 'Some description';39 const prefix = 'token prefix';40 41 42 const collectionCountBefore = +(await helper.callRpc('api.rpc.unique.collectionStats')).created;4344 const {collectionId} = await helper.eth.createFungibleCollection(owner, name, DECIMALS, description, prefix);45 46 const collectionCountAfter = +(await helper.callRpc('api.rpc.unique.collectionStats')).created;47 const data = (await helper.ft.getData(collectionId))!;4849 expect(collectionCountAfter - collectionCountBefore).to.be.eq(1);50 expect(collectionId).to.be.eq(collectionCountAfter);51 expect(data.name).to.be.eq(name);52 expect(data.description).to.be.eq(description);53 expect(data.raw.tokenPrefix).to.be.eq(prefix);54 expect(data.raw.mode).to.be.deep.eq({Fungible: DECIMALS.toString()});55 });5657 58 itEth('Check collection address exist', async ({helper}) => {59 const owner = await helper.eth.createAccountWithBalance(donor);6061 const expectedCollectionId = +(await helper.callRpc('api.rpc.unique.collectionStats')).created + 1;62 const expectedCollectionAddress = helper.ethAddress.fromCollectionId(expectedCollectionId);63 const collectionHelpers = helper.ethNativeContract.collectionHelpers(owner);6465 expect(await collectionHelpers.methods66 .isCollectionExist(expectedCollectionAddress)67 .call()).to.be.false;6869 70 await helper.eth.createFungibleCollection(owner, 'A', DECIMALS, 'A', 'A');7172 73 expect(await collectionHelpers.methods74 .isCollectionExist(expectedCollectionAddress)75 .call()).to.be.true;76 });77 78 itEth('Set sponsorship', async ({helper}) => {79 const owner = await helper.eth.createAccountWithBalance(donor);80 const sponsor = await helper.eth.createAccountWithBalance(donor);81 const ss58Format = helper.chain.getChainProperties().ss58Format;82 const {collectionId, collectionAddress} = await helper.eth.createFungibleCollection(owner, 'Sponsor', DECIMALS, 'absolutely anything', 'ENVY');8384 const collection = helper.ethNativeContract.collection(collectionAddress, 'rft', owner);85 await collection.methods.setCollectionSponsor(sponsor).send();8687 let data = (await helper.rft.getData(collectionId))!;88 expect(data.raw.sponsorship.Unconfirmed).to.be.equal(evmToAddress(sponsor, Number(ss58Format)));8990 await expect(collection.methods.confirmCollectionSponsorship().call()).to.be.rejectedWith('caller is not set as sponsor');9192 const sponsorCollection = helper.ethNativeContract.collection(collectionAddress, 'rft', sponsor);93 await sponsorCollection.methods.confirmCollectionSponsorship().send();9495 data = (await helper.rft.getData(collectionId))!;96 expect(data.raw.sponsorship.Confirmed).to.be.equal(evmToAddress(sponsor, Number(ss58Format)));97 });9899 itEth('Set limits', async ({helper}) => {100 const owner = await helper.eth.createAccountWithBalance(donor);101 const {collectionId, collectionAddress} = await helper.eth.createFungibleCollection(owner, 'Limits', DECIMALS, 'absolutely anything', 'INSI');102 const limits = {103 accountTokenOwnershipLimit: 1000,104 sponsoredDataSize: 1024,105 sponsoredDataRateLimit: 30,106 tokenLimit: 1000000,107 sponsorTransferTimeout: 6,108 sponsorApproveTimeout: 6,109 ownerCanTransfer: false,110 ownerCanDestroy: false,111 transfersEnabled: false,112 };113114 const collection = helper.ethNativeContract.collection(collectionAddress, 'ft', owner);115 await collection.methods['setCollectionLimit(string,uint32)']('accountTokenOwnershipLimit', limits.accountTokenOwnershipLimit).send();116 await collection.methods['setCollectionLimit(string,uint32)']('sponsoredDataSize', limits.sponsoredDataSize).send();117 await collection.methods['setCollectionLimit(string,uint32)']('sponsoredDataRateLimit', limits.sponsoredDataRateLimit).send();118 await collection.methods['setCollectionLimit(string,uint32)']('tokenLimit', limits.tokenLimit).send();119 await collection.methods['setCollectionLimit(string,uint32)']('sponsorTransferTimeout', limits.sponsorTransferTimeout).send();120 await collection.methods['setCollectionLimit(string,uint32)']('sponsorApproveTimeout', limits.sponsorApproveTimeout).send();121 await collection.methods['setCollectionLimit(string,bool)']('ownerCanTransfer', limits.ownerCanTransfer).send();122 await collection.methods['setCollectionLimit(string,bool)']('ownerCanDestroy', limits.ownerCanDestroy).send();123 await collection.methods['setCollectionLimit(string,bool)']('transfersEnabled', limits.transfersEnabled).send();124 125 const data = (await helper.rft.getData(collectionId))!;126 expect(data.raw.limits.accountTokenOwnershipLimit).to.be.eq(limits.accountTokenOwnershipLimit);127 expect(data.raw.limits.sponsoredDataSize).to.be.eq(limits.sponsoredDataSize);128 expect(data.raw.limits.sponsoredDataRateLimit.blocks).to.be.eq(limits.sponsoredDataRateLimit);129 expect(data.raw.limits.tokenLimit).to.be.eq(limits.tokenLimit);130 expect(data.raw.limits.sponsorTransferTimeout).to.be.eq(limits.sponsorTransferTimeout);131 expect(data.raw.limits.sponsorApproveTimeout).to.be.eq(limits.sponsorApproveTimeout);132 expect(data.raw.limits.ownerCanTransfer).to.be.eq(limits.ownerCanTransfer);133 expect(data.raw.limits.ownerCanDestroy).to.be.eq(limits.ownerCanDestroy);134 expect(data.raw.limits.transfersEnabled).to.be.eq(limits.transfersEnabled);135 });136137 itEth('Collection address exist', async ({helper}) => {138 const owner = await helper.eth.createAccountWithBalance(donor);139 const collectionAddressForNonexistentCollection = '0x17C4E6453CC49AAAAEACA894E6D9683E00112233';140 expect(await helper.ethNativeContract.collectionHelpers(collectionAddressForNonexistentCollection)141 .methods.isCollectionExist(collectionAddressForNonexistentCollection).call())142 .to.be.false;143 144 const {collectionAddress} = await helper.eth.createFungibleCollection(owner, 'Exister', DECIMALS, 'absolutely anything', 'WIWT');145 expect(await helper.ethNativeContract.collectionHelpers(collectionAddress)146 .methods.isCollectionExist(collectionAddress).call())147 .to.be.true;148 });149 150 itEth('destroyCollection', async ({helper}) => {151 const owner = await helper.eth.createAccountWithBalance(donor);152 const {collectionAddress} = await helper.eth.createFungibleCollection(owner, 'Exister', DECIMALS, 'absolutely anything', 'WIWT');153 const collectionHelper = helper.ethNativeContract.collectionHelpers(owner);154155 const result = await collectionHelper.methods156 .destroyCollection(collectionAddress)157 .send({from: owner});158159 const events = helper.eth.normalizeEvents(result.events);160 161 expect(events).to.be.deep.equal([162 {163 address: collectionHelper.options.address,164 event: 'CollectionDestroyed',165 args: {166 collectionId: collectionAddress,167 },168 },169 ]);170171 expect(await collectionHelper.methods172 .isCollectionExist(collectionAddress)173 .call()).to.be.false;174 });175});176177describe('(!negative tests!) Create FT collection from EVM', () => {178 let donor: IKeyringPair;179 let nominal: bigint;180181 before(async function() {182 await usingEthPlaygrounds(async (helper, privateKey) => {183 requirePalletsOrSkip(this, helper, [Pallets.Fungible]);184 donor = await privateKey({filename: __filename});185 nominal = helper.balance.getOneTokenNominal();186 });187 });188189 itEth('(!negative test!) Create collection (bad lengths)', async ({helper}) => {190 const owner = await helper.eth.createAccountWithBalance(donor);191 const collectionHelper = helper.ethNativeContract.collectionHelpers(owner);192 {193 const MAX_NAME_LENGTH = 64;194 const collectionName = 'A'.repeat(MAX_NAME_LENGTH + 1);195 const description = 'A';196 const tokenPrefix = 'A';197198 await expect(collectionHelper.methods199 .createFTCollection(collectionName, DECIMALS, description, tokenPrefix)200 .call({value: Number(2n * nominal)})).to.be.rejectedWith('name is too long. Max length is ' + MAX_NAME_LENGTH);201 }202 {203 const MAX_DESCRIPTION_LENGTH = 256;204 const collectionName = 'A';205 const description = 'A'.repeat(MAX_DESCRIPTION_LENGTH + 1);206 const tokenPrefix = 'A';207 await expect(collectionHelper.methods208 .createFTCollection(collectionName, DECIMALS, description, tokenPrefix)209 .call({value: Number(2n * nominal)})).to.be.rejectedWith('description is too long. Max length is ' + MAX_DESCRIPTION_LENGTH);210 }211 {212 const MAX_TOKEN_PREFIX_LENGTH = 16;213 const collectionName = 'A';214 const description = 'A';215 const tokenPrefix = 'A'.repeat(MAX_TOKEN_PREFIX_LENGTH + 1);216 await expect(collectionHelper.methods217 .createFTCollection(collectionName, DECIMALS, description, tokenPrefix)218 .call({value: Number(2n * nominal)})).to.be.rejectedWith('token_prefix is too long. Max length is ' + MAX_TOKEN_PREFIX_LENGTH);219 }220 });221 222 itEth('(!negative test!) Create collection (no funds)', async ({helper}) => {223 const owner = await helper.eth.createAccountWithBalance(donor);224 const collectionHelper = helper.ethNativeContract.collectionHelpers(owner);225 await expect(collectionHelper.methods226 .createFTCollection('Peasantry', DECIMALS, 'absolutely anything', 'TWIW')227 .call({value: Number(1n * nominal)})).to.be.rejectedWith('Sent amount not equals to collection creation price (2000000000000000000)');228 });229230 itEth('(!negative test!) Check owner', async ({helper}) => {231 const owner = await helper.eth.createAccountWithBalance(donor);232 const peasant = helper.eth.createAccount();233 const {collectionAddress} = await helper.eth.createFungibleCollection(owner, 'Transgressed', DECIMALS, 'absolutely anything', 'YVNE');234 const peasantCollection = helper.ethNativeContract.collection(collectionAddress, 'ft', peasant);235 const EXPECTED_ERROR = 'NoPermission';236 {237 const sponsor = await helper.eth.createAccountWithBalance(donor);238 await expect(peasantCollection.methods239 .setCollectionSponsor(sponsor)240 .call()).to.be.rejectedWith(EXPECTED_ERROR);241 242 const sponsorCollection = helper.ethNativeContract.collection(collectionAddress, 'ft', sponsor);243 await expect(sponsorCollection.methods244 .confirmCollectionSponsorship()245 .call()).to.be.rejectedWith('caller is not set as sponsor');246 }247 {248 await expect(peasantCollection.methods249 .setCollectionLimit('account_token_ownership_limit', '1000')250 .call()).to.be.rejectedWith(EXPECTED_ERROR);251 }252 });253254 itEth('(!negative test!) Set limits', async ({helper}) => {255 const owner = await helper.eth.createAccountWithBalance(donor);256 const {collectionAddress} = await helper.eth.createFungibleCollection(owner, 'Limits', DECIMALS, 'absolutely anything', 'ISNI');257 const collectionEvm = helper.ethNativeContract.collection(collectionAddress, 'ft', owner);258 await expect(collectionEvm.methods259 .setCollectionLimit('badLimit', 'true')260 .call()).to.be.rejectedWith('unknown boolean limit "badLimit"');261 });262});