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 35 itEth('[eth] Set sponsorship', async ({helper}) => {36 const owner = await helper.eth.createAccountWithBalance(donor);37 const sponsor = await helper.eth.createAccountWithBalance(donor);38 const ss58Format = helper.chain.getChainProperties().ss58Format;39 const {collectionId, collectionAddress} = await helper.eth.createFungibleCollection(owner, 'Sponsor', DECIMALS, 'absolutely anything', 'ENVY');4041 const collection = helper.ethNativeContract.collection(collectionAddress, 'rft', owner, true);42 await collection.methods.setCollectionSponsor(sponsor).send();4344 let data = (await helper.rft.getData(collectionId))!;45 expect(data.raw.sponsorship.Unconfirmed).to.be.equal(evmToAddress(sponsor, Number(ss58Format)));4647 await expect(collection.methods.confirmCollectionSponsorship().call()).to.be.rejectedWith('caller is not set as sponsor');4849 const sponsorCollection = helper.ethNativeContract.collection(collectionAddress, 'rft', sponsor, true);50 await sponsorCollection.methods.confirmCollectionSponsorship().send();5152 data = (await helper.rft.getData(collectionId))!;53 expect(data.raw.sponsorship.Confirmed).to.be.equal(evmToAddress(sponsor, Number(ss58Format)));54 });5556 itEth('[cross] Set sponsorship', async ({helper}) => {57 const owner = await helper.eth.createAccountWithBalance(donor);58 const sponsor = await helper.eth.createAccountWithBalance(donor);59 const ss58Format = helper.chain.getChainProperties().ss58Format;60 const {collectionId, collectionAddress} = await helper.eth.createFungibleCollection(owner, 'Sponsor', DECIMALS, 'absolutely anything', 'ENVY');6162 const collection = helper.ethNativeContract.collection(collectionAddress, 'rft', owner);63 const sponsorCross = helper.ethCrossAccount.fromAddress(sponsor);64 await collection.methods.setCollectionSponsorCross(sponsorCross).send();6566 let data = (await helper.rft.getData(collectionId))!;67 expect(data.raw.sponsorship.Unconfirmed).to.be.equal(evmToAddress(sponsor, Number(ss58Format)));6869 await expect(collection.methods.confirmCollectionSponsorship().call()).to.be.rejectedWith('caller is not set as sponsor');7071 const sponsorCollection = helper.ethNativeContract.collection(collectionAddress, 'rft', sponsor);72 await sponsorCollection.methods.confirmCollectionSponsorship().send();7374 data = (await helper.rft.getData(collectionId))!;75 expect(data.raw.sponsorship.Confirmed).to.be.equal(evmToAddress(sponsor, Number(ss58Format)));76 });7778 itEth('Set limits', async ({helper}) => {79 const owner = await helper.eth.createAccountWithBalance(donor);80 const {collectionId, collectionAddress} = await helper.eth.createFungibleCollection(owner, 'Limits', DECIMALS, 'absolutely anything', 'INSI');81 const limits = {82 accountTokenOwnershipLimit: 1000,83 sponsoredDataSize: 1024,84 sponsoredDataRateLimit: 30,85 tokenLimit: 1000000,86 sponsorTransferTimeout: 6,87 sponsorApproveTimeout: 6,88 ownerCanTransfer: 0,89 ownerCanDestroy: 0,90 transfersEnabled: 0,91 };92 93 const expectedLimits = {94 accountTokenOwnershipLimit: 1000,95 sponsoredDataSize: 1024,96 sponsoredDataRateLimit: 30,97 tokenLimit: 1000000,98 sponsorTransferTimeout: 6,99 sponsorApproveTimeout: 6,100 ownerCanTransfer: false,101 ownerCanDestroy: false,102 transfersEnabled: false,103 };104 105 const collection = helper.ethNativeContract.collection(collectionAddress, 'ft', owner);106 await collection.methods.setCollectionLimit('accountTokenOwnershipLimit', limits.accountTokenOwnershipLimit).send();107 await collection.methods.setCollectionLimit('sponsoredDataSize', limits.sponsoredDataSize).send();108 await collection.methods.setCollectionLimit('sponsoredDataRateLimit', limits.sponsoredDataRateLimit).send();109 await collection.methods.setCollectionLimit('tokenLimit', limits.tokenLimit).send();110 await collection.methods.setCollectionLimit('sponsorTransferTimeout', limits.sponsorTransferTimeout).send();111 await collection.methods.setCollectionLimit('sponsorApproveTimeout', limits.sponsorApproveTimeout).send();112 await collection.methods.setCollectionLimit('ownerCanTransfer', limits.ownerCanTransfer).send();113 await collection.methods.setCollectionLimit('ownerCanDestroy', limits.ownerCanDestroy).send();114 await collection.methods.setCollectionLimit('transfersEnabled', limits.transfersEnabled).send();115 116 const data = (await helper.rft.getData(collectionId))!;117 expect(data.raw.limits.accountTokenOwnershipLimit).to.be.eq(expectedLimits.accountTokenOwnershipLimit);118 expect(data.raw.limits.sponsoredDataSize).to.be.eq(expectedLimits.sponsoredDataSize);119 expect(data.raw.limits.sponsoredDataRateLimit.blocks).to.be.eq(expectedLimits.sponsoredDataRateLimit);120 expect(data.raw.limits.tokenLimit).to.be.eq(expectedLimits.tokenLimit);121 expect(data.raw.limits.sponsorTransferTimeout).to.be.eq(expectedLimits.sponsorTransferTimeout);122 expect(data.raw.limits.sponsorApproveTimeout).to.be.eq(expectedLimits.sponsorApproveTimeout);123 expect(data.raw.limits.ownerCanTransfer).to.be.eq(expectedLimits.ownerCanTransfer);124 expect(data.raw.limits.ownerCanDestroy).to.be.eq(expectedLimits.ownerCanDestroy);125 expect(data.raw.limits.transfersEnabled).to.be.eq(expectedLimits.transfersEnabled);126 });127128 itEth('Collection address exist', async ({helper}) => {129 const owner = await helper.eth.createAccountWithBalance(donor);130 const collectionAddressForNonexistentCollection = '0x17C4E6453CC49AAAAEACA894E6D9683E00112233';131 expect(await helper.ethNativeContract.collectionHelpers(collectionAddressForNonexistentCollection)132 .methods.isCollectionExist(collectionAddressForNonexistentCollection).call())133 .to.be.false;134 135 const {collectionAddress} = await helper.eth.createFungibleCollection(owner, 'Exister', DECIMALS, 'absolutely anything', 'WIWT');136 expect(await helper.ethNativeContract.collectionHelpers(collectionAddress)137 .methods.isCollectionExist(collectionAddress).call())138 .to.be.true;139 });140 141 itEth('destroyCollection', async ({helper}) => {142 const owner = await helper.eth.createAccountWithBalance(donor);143 const {collectionAddress} = await helper.eth.createFungibleCollection(owner, 'Exister', DECIMALS, 'absolutely anything', 'WIWT');144 const collectionHelper = helper.ethNativeContract.collectionHelpers(owner);145146 const result = await collectionHelper.methods147 .destroyCollection(collectionAddress)148 .send({from: owner});149150 const events = helper.eth.normalizeEvents(result.events);151 152 expect(events).to.be.deep.equal([153 {154 address: collectionHelper.options.address,155 event: 'CollectionDestroyed',156 args: {157 collectionId: collectionAddress,158 },159 },160 ]);161162 expect(await collectionHelper.methods163 .isCollectionExist(collectionAddress)164 .call()).to.be.false;165 });166});167168describe('(!negative tests!) Create FT collection from EVM', () => {169 let donor: IKeyringPair;170 let nominal: bigint;171172 before(async function() {173 await usingEthPlaygrounds(async (helper, privateKey) => {174 requirePalletsOrSkip(this, helper, [Pallets.Fungible]);175 donor = await privateKey({filename: __filename});176 nominal = helper.balance.getOneTokenNominal();177 });178 });179180 itEth('(!negative test!) Create collection (bad lengths)', async ({helper}) => {181 const owner = await helper.eth.createAccountWithBalance(donor);182 const collectionHelper = helper.ethNativeContract.collectionHelpers(owner);183 {184 const MAX_NAME_LENGTH = 64;185 const collectionName = 'A'.repeat(MAX_NAME_LENGTH + 1);186 const description = 'A';187 const tokenPrefix = 'A';188189 await expect(collectionHelper.methods190 .createFTCollection(collectionName, DECIMALS, description, tokenPrefix)191 .call({value: Number(2n * nominal)})).to.be.rejectedWith('name is too long. Max length is ' + MAX_NAME_LENGTH);192 }193 {194 const MAX_DESCRIPTION_LENGTH = 256;195 const collectionName = 'A';196 const description = 'A'.repeat(MAX_DESCRIPTION_LENGTH + 1);197 const tokenPrefix = 'A';198 await expect(collectionHelper.methods199 .createFTCollection(collectionName, DECIMALS, description, tokenPrefix)200 .call({value: Number(2n * nominal)})).to.be.rejectedWith('description is too long. Max length is ' + MAX_DESCRIPTION_LENGTH);201 }202 {203 const MAX_TOKEN_PREFIX_LENGTH = 16;204 const collectionName = 'A';205 const description = 'A';206 const tokenPrefix = 'A'.repeat(MAX_TOKEN_PREFIX_LENGTH + 1);207 await expect(collectionHelper.methods208 .createFTCollection(collectionName, DECIMALS, description, tokenPrefix)209 .call({value: Number(2n * nominal)})).to.be.rejectedWith('token_prefix is too long. Max length is ' + MAX_TOKEN_PREFIX_LENGTH);210 }211 });212 213 itEth('(!negative test!) Create collection (no funds)', async ({helper}) => {214 const owner = await helper.eth.createAccountWithBalance(donor);215 const collectionHelper = helper.ethNativeContract.collectionHelpers(owner);216 await expect(collectionHelper.methods217 .createFTCollection('Peasantry', DECIMALS, 'absolutely anything', 'TWIW')218 .call({value: Number(1n * nominal)})).to.be.rejectedWith('Sent amount not equals to collection creation price (2000000000000000000)');219 });220221 222 itEth('(!negative test!) [eth] Check owner', async ({helper}) => {223 const owner = await helper.eth.createAccountWithBalance(donor);224 const peasant = helper.eth.createAccount();225 const {collectionAddress} = await helper.eth.createFungibleCollection(owner, 'Transgressed', DECIMALS, 'absolutely anything', 'YVNE');226 const peasantCollection = helper.ethNativeContract.collection(collectionAddress, 'ft', peasant, true);227 const EXPECTED_ERROR = 'NoPermission';228 {229 const sponsor = await helper.eth.createAccountWithBalance(donor);230 await expect(peasantCollection.methods231 .setCollectionSponsor(sponsor)232 .call()).to.be.rejectedWith(EXPECTED_ERROR);233 234 const sponsorCollection = helper.ethNativeContract.collection(collectionAddress, 'ft', sponsor, true);235 await expect(sponsorCollection.methods236 .confirmCollectionSponsorship()237 .call()).to.be.rejectedWith('caller is not set as sponsor');238 }239 {240 await expect(peasantCollection.methods241 .setCollectionLimit('account_token_ownership_limit', '1000')242 .call()).to.be.rejectedWith(EXPECTED_ERROR);243 }244 });245246 itEth('(!negative test!) [cross] Check owner', async ({helper}) => {247 const owner = await helper.eth.createAccountWithBalance(donor);248 const peasant = helper.eth.createAccount();249 const {collectionAddress} = await helper.eth.createFungibleCollection(owner, 'Transgressed', DECIMALS, 'absolutely anything', 'YVNE');250 const peasantCollection = helper.ethNativeContract.collection(collectionAddress, 'ft', peasant);251 const EXPECTED_ERROR = 'NoPermission';252 {253 const sponsor = await helper.eth.createAccountWithBalance(donor);254 const sponsorCross = helper.ethCrossAccount.fromAddress(sponsor);255 await expect(peasantCollection.methods256 .setCollectionSponsorCross(sponsorCross)257 .call()).to.be.rejectedWith(EXPECTED_ERROR);258 259 const sponsorCollection = helper.ethNativeContract.collection(collectionAddress, 'ft', sponsor);260 await expect(sponsorCollection.methods261 .confirmCollectionSponsorship()262 .call()).to.be.rejectedWith('caller is not set as sponsor');263 }264 {265 await expect(peasantCollection.methods266 .setCollectionLimit('account_token_ownership_limit', '1000')267 .call()).to.be.rejectedWith(EXPECTED_ERROR);268 }269 });270271 itEth('(!negative test!) Set limits', async ({helper}) => {272273 const invalidLimits = {274 accountTokenOwnershipLimit: BigInt(Number.MAX_SAFE_INTEGER),275 transfersEnabled: 3,276 };277278 const owner = await helper.eth.createAccountWithBalance(donor);279 const {collectionAddress} = await helper.eth.createFungibleCollection(owner, 'Limits', DECIMALS, 'absolutely anything', 'ISNI');280 const collectionEvm = helper.ethNativeContract.collection(collectionAddress, 'ft', owner);281 await expect(collectionEvm.methods282 .setCollectionLimit('badLimit', '1')283 .call()).to.be.rejectedWith('unknown limit "badLimit"');284 285 await expect(collectionEvm.methods286 .setCollectionLimit(Object.keys(invalidLimits)[0], invalidLimits.accountTokenOwnershipLimit)287 .call()).to.be.rejectedWith(`can't convert value to u32 "${invalidLimits.accountTokenOwnershipLimit}"`);288 289 await expect(collectionEvm.methods290 .setCollectionLimit(Object.keys(invalidLimits)[1], invalidLimits.transfersEnabled)291 .call()).to.be.rejectedWith(`can't convert value to boolean "${invalidLimits.transfersEnabled}"`);292 });293294 295 296});