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: false,89 ownerCanDestroy: false,90 transfersEnabled: false,91 };9293 const collection = helper.ethNativeContract.collection(collectionAddress, 'ft', owner);94 await collection.methods['setCollectionLimit(string,uint32)']('accountTokenOwnershipLimit', limits.accountTokenOwnershipLimit).send();95 await collection.methods['setCollectionLimit(string,uint32)']('sponsoredDataSize', limits.sponsoredDataSize).send();96 await collection.methods['setCollectionLimit(string,uint32)']('sponsoredDataRateLimit', limits.sponsoredDataRateLimit).send();97 await collection.methods['setCollectionLimit(string,uint32)']('tokenLimit', limits.tokenLimit).send();98 await collection.methods['setCollectionLimit(string,uint32)']('sponsorTransferTimeout', limits.sponsorTransferTimeout).send();99 await collection.methods['setCollectionLimit(string,uint32)']('sponsorApproveTimeout', limits.sponsorApproveTimeout).send();100 await collection.methods['setCollectionLimit(string,bool)']('ownerCanTransfer', limits.ownerCanTransfer).send();101 await collection.methods['setCollectionLimit(string,bool)']('ownerCanDestroy', limits.ownerCanDestroy).send();102 await collection.methods['setCollectionLimit(string,bool)']('transfersEnabled', limits.transfersEnabled).send();103 104 const data = (await helper.rft.getData(collectionId))!;105 expect(data.raw.limits.accountTokenOwnershipLimit).to.be.eq(limits.accountTokenOwnershipLimit);106 expect(data.raw.limits.sponsoredDataSize).to.be.eq(limits.sponsoredDataSize);107 expect(data.raw.limits.sponsoredDataRateLimit.blocks).to.be.eq(limits.sponsoredDataRateLimit);108 expect(data.raw.limits.tokenLimit).to.be.eq(limits.tokenLimit);109 expect(data.raw.limits.sponsorTransferTimeout).to.be.eq(limits.sponsorTransferTimeout);110 expect(data.raw.limits.sponsorApproveTimeout).to.be.eq(limits.sponsorApproveTimeout);111 expect(data.raw.limits.ownerCanTransfer).to.be.eq(limits.ownerCanTransfer);112 expect(data.raw.limits.ownerCanDestroy).to.be.eq(limits.ownerCanDestroy);113 expect(data.raw.limits.transfersEnabled).to.be.eq(limits.transfersEnabled);114 });115116 itEth('Collection address exist', async ({helper}) => {117 const owner = await helper.eth.createAccountWithBalance(donor);118 const collectionAddressForNonexistentCollection = '0x17C4E6453CC49AAAAEACA894E6D9683E00112233';119 expect(await helper.ethNativeContract.collectionHelpers(collectionAddressForNonexistentCollection)120 .methods.isCollectionExist(collectionAddressForNonexistentCollection).call())121 .to.be.false;122 123 const {collectionAddress} = await helper.eth.createFungibleCollection(owner, 'Exister', DECIMALS, 'absolutely anything', 'WIWT');124 expect(await helper.ethNativeContract.collectionHelpers(collectionAddress)125 .methods.isCollectionExist(collectionAddress).call())126 .to.be.true;127 });128 129 itEth('destroyCollection', async ({helper}) => {130 const owner = await helper.eth.createAccountWithBalance(donor);131 const {collectionAddress} = await helper.eth.createFungibleCollection(owner, 'Exister', DECIMALS, 'absolutely anything', 'WIWT');132 const collectionHelper = helper.ethNativeContract.collectionHelpers(owner);133134 const result = await collectionHelper.methods135 .destroyCollection(collectionAddress)136 .send({from: owner});137138 const events = helper.eth.normalizeEvents(result.events);139 140 expect(events).to.be.deep.equal([141 {142 address: collectionHelper.options.address,143 event: 'CollectionDestroyed',144 args: {145 collectionId: collectionAddress,146 },147 },148 ]);149150 expect(await collectionHelper.methods151 .isCollectionExist(collectionAddress)152 .call()).to.be.false;153 });154});155156describe('(!negative tests!) Create FT collection from EVM', () => {157 let donor: IKeyringPair;158 let nominal: bigint;159160 before(async function() {161 await usingEthPlaygrounds(async (helper, privateKey) => {162 requirePalletsOrSkip(this, helper, [Pallets.Fungible]);163 donor = await privateKey({filename: __filename});164 nominal = helper.balance.getOneTokenNominal();165 });166 });167168 itEth('(!negative test!) Create collection (bad lengths)', async ({helper}) => {169 const owner = await helper.eth.createAccountWithBalance(donor);170 const collectionHelper = helper.ethNativeContract.collectionHelpers(owner);171 {172 const MAX_NAME_LENGTH = 64;173 const collectionName = 'A'.repeat(MAX_NAME_LENGTH + 1);174 const description = 'A';175 const tokenPrefix = 'A';176177 await expect(collectionHelper.methods178 .createFTCollection(collectionName, DECIMALS, description, tokenPrefix)179 .call({value: Number(2n * nominal)})).to.be.rejectedWith('name is too long. Max length is ' + MAX_NAME_LENGTH);180 }181 {182 const MAX_DESCRIPTION_LENGTH = 256;183 const collectionName = 'A';184 const description = 'A'.repeat(MAX_DESCRIPTION_LENGTH + 1);185 const tokenPrefix = 'A';186 await expect(collectionHelper.methods187 .createFTCollection(collectionName, DECIMALS, description, tokenPrefix)188 .call({value: Number(2n * nominal)})).to.be.rejectedWith('description is too long. Max length is ' + MAX_DESCRIPTION_LENGTH);189 }190 {191 const MAX_TOKEN_PREFIX_LENGTH = 16;192 const collectionName = 'A';193 const description = 'A';194 const tokenPrefix = 'A'.repeat(MAX_TOKEN_PREFIX_LENGTH + 1);195 await expect(collectionHelper.methods196 .createFTCollection(collectionName, DECIMALS, description, tokenPrefix)197 .call({value: Number(2n * nominal)})).to.be.rejectedWith('token_prefix is too long. Max length is ' + MAX_TOKEN_PREFIX_LENGTH);198 }199 });200 201 itEth('(!negative test!) Create collection (no funds)', async ({helper}) => {202 const owner = await helper.eth.createAccountWithBalance(donor);203 const collectionHelper = helper.ethNativeContract.collectionHelpers(owner);204 await expect(collectionHelper.methods205 .createFTCollection('Peasantry', DECIMALS, 'absolutely anything', 'TWIW')206 .call({value: Number(1n * nominal)})).to.be.rejectedWith('Sent amount not equals to collection creation price (2000000000000000000)');207 });208209 210 itEth('(!negative test!) [eth] Check owner', async ({helper}) => {211 const owner = await helper.eth.createAccountWithBalance(donor);212 const peasant = helper.eth.createAccount();213 const {collectionAddress} = await helper.eth.createFungibleCollection(owner, 'Transgressed', DECIMALS, 'absolutely anything', 'YVNE');214 const peasantCollection = helper.ethNativeContract.collection(collectionAddress, 'ft', peasant, true);215 const EXPECTED_ERROR = 'NoPermission';216 {217 const sponsor = await helper.eth.createAccountWithBalance(donor);218 await expect(peasantCollection.methods219 .setCollectionSponsor(sponsor)220 .call()).to.be.rejectedWith(EXPECTED_ERROR);221 222 const sponsorCollection = helper.ethNativeContract.collection(collectionAddress, 'ft', sponsor, true);223 await expect(sponsorCollection.methods224 .confirmCollectionSponsorship()225 .call()).to.be.rejectedWith('caller is not set as sponsor');226 }227 {228 await expect(peasantCollection.methods229 .setCollectionLimit('account_token_ownership_limit', '1000')230 .call()).to.be.rejectedWith(EXPECTED_ERROR);231 }232 });233234 itEth('(!negative test!) [cross] Check owner', async ({helper}) => {235 const owner = await helper.eth.createAccountWithBalance(donor);236 const peasant = helper.eth.createAccount();237 const {collectionAddress} = await helper.eth.createFungibleCollection(owner, 'Transgressed', DECIMALS, 'absolutely anything', 'YVNE');238 const peasantCollection = helper.ethNativeContract.collection(collectionAddress, 'ft', peasant);239 const EXPECTED_ERROR = 'NoPermission';240 {241 const sponsor = await helper.eth.createAccountWithBalance(donor);242 const sponsorCross = helper.ethCrossAccount.fromAddress(sponsor);243 await expect(peasantCollection.methods244 .setCollectionSponsorCross(sponsorCross)245 .call()).to.be.rejectedWith(EXPECTED_ERROR);246 247 const sponsorCollection = helper.ethNativeContract.collection(collectionAddress, 'ft', sponsor);248 await expect(sponsorCollection.methods249 .confirmCollectionSponsorship()250 .call()).to.be.rejectedWith('caller is not set as sponsor');251 }252 {253 await expect(peasantCollection.methods254 .setCollectionLimit('account_token_ownership_limit', '1000')255 .call()).to.be.rejectedWith(EXPECTED_ERROR);256 }257 });258259 itEth('(!negative test!) Set limits', async ({helper}) => {260 const owner = await helper.eth.createAccountWithBalance(donor);261 const {collectionAddress} = await helper.eth.createFungibleCollection(owner, 'Limits', DECIMALS, 'absolutely anything', 'ISNI');262 const collectionEvm = helper.ethNativeContract.collection(collectionAddress, 'ft', owner);263 await expect(collectionEvm.methods264 .setCollectionLimit('badLimit', 'true')265 .call()).to.be.rejectedWith('unknown boolean limit "badLimit"');266 });267});