1234567891011121314151617import {IKeyringPair} from '@polkadot/types/types';18import {evmToAddress} from '@polkadot/util-crypto';19import {Pallets, requirePalletsOrSkip} from '../util';20import {expect, itEth, usingEthPlaygrounds} from './util';21import {CollectionLimitField} from './util/playgrounds/types';2223const DECIMALS = 18;2425describe('Create FT collection from EVM', () => {26 let donor: IKeyringPair;2728 before(async function() {29 await usingEthPlaygrounds(async (helper, privateKey) => {30 requirePalletsOrSkip(this, helper, [Pallets.Fungible]);31 donor = await privateKey({filename: __filename});32 });33 });34 35 36 itEth('[eth] Set sponsorship', async ({helper}) => {37 const owner = await helper.eth.createAccountWithBalance(donor);38 const sponsor = await helper.eth.createAccountWithBalance(donor);39 const ss58Format = helper.chain.getChainProperties().ss58Format;40 const description = 'absolutely anything';41 42 const {collectionId, collectionAddress} = await helper.eth.createFungibleCollection(owner, 'Sponsor', DECIMALS, description, 'ENVY');4344 const collection = helper.ethNativeContract.collection(collectionAddress, 'ft', owner, true);45 await collection.methods.setCollectionSponsor(sponsor).send();4647 let data = (await helper.rft.getData(collectionId))!;48 expect(data.raw.sponsorship.Unconfirmed).to.be.equal(evmToAddress(sponsor, Number(ss58Format)));4950 await expect(collection.methods.confirmCollectionSponsorship().call()).to.be.rejectedWith('ConfirmSponsorshipFail');5152 const sponsorCollection = helper.ethNativeContract.collection(collectionAddress, 'rft', sponsor, true);53 await sponsorCollection.methods.confirmCollectionSponsorship().send();5455 data = (await helper.rft.getData(collectionId))!;56 expect(data.raw.sponsorship.Confirmed).to.be.equal(evmToAddress(sponsor, Number(ss58Format)));57 });5859 itEth('[cross] Set sponsorship', async ({helper}) => {60 const owner = await helper.eth.createAccountWithBalance(donor);61 const sponsor = await helper.eth.createAccountWithBalance(donor);62 const ss58Format = helper.chain.getChainProperties().ss58Format;63 const description = 'absolutely anything';64 const {collectionId, collectionAddress} = await helper.eth.createFungibleCollection(owner, 'Sponsor', DECIMALS, description, 'ENVY');65 66 const collection = helper.ethNativeContract.collection(collectionAddress, 'rft', owner);67 const sponsorCross = helper.ethCrossAccount.fromAddress(sponsor);68 await collection.methods.setCollectionSponsorCross(sponsorCross).send();6970 let data = (await helper.rft.getData(collectionId))!;71 expect(data.raw.sponsorship.Unconfirmed).to.be.equal(evmToAddress(sponsor, Number(ss58Format)));7273 await expect(collection.methods.confirmCollectionSponsorship().call()).to.be.rejectedWith('ConfirmSponsorshipFail');7475 const sponsorCollection = helper.ethNativeContract.collection(collectionAddress, 'rft', sponsor);76 await sponsorCollection.methods.confirmCollectionSponsorship().send();7778 data = (await helper.rft.getData(collectionId))!;79 expect(data.raw.sponsorship.Confirmed).to.be.equal(evmToAddress(sponsor, Number(ss58Format)));80 expect(await collection.methods.description().call()).to.deep.equal(description);81 });8283 itEth('Collection address exist', async ({helper}) => {84 const owner = await helper.eth.createAccountWithBalance(donor);85 const collectionAddressForNonexistentCollection = '0x17C4E6453CC49AAAAEACA894E6D9683E00112233';86 expect(await helper.ethNativeContract.collectionHelpers(collectionAddressForNonexistentCollection)87 .methods.isCollectionExist(collectionAddressForNonexistentCollection).call())88 .to.be.false;89 90 const {collectionAddress} = await helper.eth.createFungibleCollection(owner, 'Exister', DECIMALS, 'absolutely anything', 'WIWT');91 expect(await helper.ethNativeContract.collectionHelpers(collectionAddress)92 .methods.isCollectionExist(collectionAddress).call())93 .to.be.true;94 });95 96 itEth('destroyCollection', async ({helper}) => {97 const owner = await helper.eth.createAccountWithBalance(donor);98 const {collectionAddress, collectionId} = await helper.eth.createFungibleCollection(owner, 'Exister', DECIMALS, 'absolutely anything', 'WIWT');99 const collectionHelper = helper.ethNativeContract.collectionHelpers(owner);100101 const result = await collectionHelper.methods102 .destroyCollection(collectionAddress)103 .send({from: owner});104105 const events = helper.eth.normalizeEvents(result.events);106 107 expect(events).to.be.deep.equal([108 {109 address: collectionHelper.options.address,110 event: 'CollectionDestroyed',111 args: {112 collectionId: collectionAddress,113 },114 },115 ]);116117 expect(await collectionHelper.methods118 .isCollectionExist(collectionAddress)119 .call()).to.be.false;120 expect(await helper.collection.getData(collectionId)).to.be.null;121 });122});123124describe('(!negative tests!) Create FT collection from EVM', () => {125 let donor: IKeyringPair;126 let nominal: bigint;127128 before(async function() {129 await usingEthPlaygrounds(async (helper, privateKey) => {130 requirePalletsOrSkip(this, helper, [Pallets.Fungible]);131 donor = await privateKey({filename: __filename});132 nominal = helper.balance.getOneTokenNominal();133 });134 });135136 itEth('(!negative test!) Create collection (bad lengths)', async ({helper}) => {137 const owner = await helper.eth.createAccountWithBalance(donor);138 const collectionHelper = helper.ethNativeContract.collectionHelpers(owner);139 {140 const MAX_NAME_LENGTH = 64;141 const collectionName = 'A'.repeat(MAX_NAME_LENGTH + 1);142 const description = 'A';143 const tokenPrefix = 'A';144145 await expect(collectionHelper.methods146 .createFTCollection(collectionName, DECIMALS, description, tokenPrefix)147 .call({value: Number(2n * nominal)})).to.be.rejectedWith('name is too long. Max length is ' + MAX_NAME_LENGTH);148 }149 {150 const MAX_DESCRIPTION_LENGTH = 256;151 const collectionName = 'A';152 const description = 'A'.repeat(MAX_DESCRIPTION_LENGTH + 1);153 const tokenPrefix = 'A';154 await expect(collectionHelper.methods155 .createFTCollection(collectionName, DECIMALS, description, tokenPrefix)156 .call({value: Number(2n * nominal)})).to.be.rejectedWith('description is too long. Max length is ' + MAX_DESCRIPTION_LENGTH);157 }158 {159 const MAX_TOKEN_PREFIX_LENGTH = 16;160 const collectionName = 'A';161 const description = 'A';162 const tokenPrefix = 'A'.repeat(MAX_TOKEN_PREFIX_LENGTH + 1);163 await expect(collectionHelper.methods164 .createFTCollection(collectionName, DECIMALS, description, tokenPrefix)165 .call({value: Number(2n * nominal)})).to.be.rejectedWith('token_prefix is too long. Max length is ' + MAX_TOKEN_PREFIX_LENGTH);166 }167 });168 169 itEth('(!negative test!) cannot create collection if value !== 2', async ({helper}) => {170 const owner = await helper.eth.createAccountWithBalance(donor);171 const collectionHelper = helper.ethNativeContract.collectionHelpers(owner);172 const expects = [0n, 1n, 30n].map(async value => {173 await expect(collectionHelper.methods174 .createFTCollection('Peasantry', DECIMALS, 'absolutely anything', 'TWIW')175 .call({value: Number(value * nominal)})).to.be.rejectedWith('Sent amount not equals to collection creation price (2000000000000000000)');176 });177 await Promise.all(expects);178 });179180 181 itEth('(!negative test!) [eth] Check owner', async ({helper}) => {182 const owner = await helper.eth.createAccountWithBalance(donor);183 const peasant = helper.eth.createAccount();184 const {collectionAddress} = await helper.eth.createFungibleCollection(owner, 'Transgressed', DECIMALS, 'absolutely anything', 'YVNE');185 const peasantCollection = helper.ethNativeContract.collection(collectionAddress, 'ft', peasant, true);186 const EXPECTED_ERROR = 'NoPermission';187 {188 const sponsor = await helper.eth.createAccountWithBalance(donor);189 await expect(peasantCollection.methods190 .setCollectionSponsor(sponsor)191 .call()).to.be.rejectedWith(EXPECTED_ERROR);192 193 const sponsorCollection = helper.ethNativeContract.collection(collectionAddress, 'ft', sponsor, true);194 await expect(sponsorCollection.methods195 .confirmCollectionSponsorship()196 .call()).to.be.rejectedWith('ConfirmSponsorshipFail');197 }198 {199 await expect(peasantCollection.methods200 .setCollectionLimit({field: CollectionLimitField.AccountTokenOwnership, value: {status: true, value: 1000}})201 .call()).to.be.rejectedWith(EXPECTED_ERROR);202 }203 });204205 itEth('(!negative test!) [cross] Check owner', async ({helper}) => {206 const owner = await helper.eth.createAccountWithBalance(donor);207 const peasant = helper.eth.createAccount();208 const {collectionAddress} = await helper.eth.createFungibleCollection(owner, 'Transgressed', DECIMALS, 'absolutely anything', 'YVNE');209 const peasantCollection = helper.ethNativeContract.collection(collectionAddress, 'ft', peasant);210 const EXPECTED_ERROR = 'NoPermission';211 {212 const sponsor = await helper.eth.createAccountWithBalance(donor);213 const sponsorCross = helper.ethCrossAccount.fromAddress(sponsor);214 await expect(peasantCollection.methods215 .setCollectionSponsorCross(sponsorCross)216 .call()).to.be.rejectedWith(EXPECTED_ERROR);217 218 const sponsorCollection = helper.ethNativeContract.collection(collectionAddress, 'ft', sponsor);219 await expect(sponsorCollection.methods220 .confirmCollectionSponsorship()221 .call()).to.be.rejectedWith('ConfirmSponsorshipFail');222 }223 {224 await expect(peasantCollection.methods225 .setCollectionLimit({field: CollectionLimitField.AccountTokenOwnership, value: {status: true, value: 1000}})226 .call()).to.be.rejectedWith(EXPECTED_ERROR);227 }228 }); 229});