1234567891011121314151617import {evmToAddress} from '@polkadot/util-crypto';18import type {IKeyringPair} from '@polkadot/types/types';19import {Pallets, requirePalletsOrSkip} from '@unique/test-utils/util.js';20import {expect, itEth, usingEthPlaygrounds} from '@unique/test-utils/eth/util.js';21import {CollectionLimitField} from '@unique/test-utils/eth/types.js';222324describe('Create RFT collection from EVM', () => {25 let donor: IKeyringPair;2627 before(async function() {28 await usingEthPlaygrounds(async (helper, privateKey) => {29 requirePalletsOrSkip(this, helper, [Pallets.ReFungible]);30 donor = await privateKey({url: import.meta.url});31 });32 });3334 itEth('Create collection', async ({helper}) => {35 const owner = await helper.eth.createAccountWithBalance(donor);3637 const name = 'CollectionEVM';38 const description = 'Some description';39 const prefix = 'token prefix';4041 const {collectionId} = await helper.eth.createRFTCollection(owner, name, description, prefix);42 const data = (await helper.rft.getData(collectionId))!;43 const collection = helper.rft.getCollectionObject(collectionId);4445 expect(data.name).to.be.eq(name);46 expect(data.description).to.be.eq(description);47 expect(data.raw.tokenPrefix).to.be.eq(prefix);48 expect(data.raw.mode).to.be.eq('ReFungible');4950 const options = await collection.getOptions();5152 expect(options.tokenPropertyPermissions).to.be.empty;53 });54555657 itEth('Create collection with properties & get description', async ({helper}) => {58 const owner = await helper.eth.createAccountWithBalance(donor);5960 const name = 'CollectionEVM';61 const description = 'Some description';62 const prefix = 'token prefix';63 const baseUri = 'BaseURI';6465 const {collectionId, collectionAddress} = await helper.eth.createERC721MetadataCompatibleRFTCollection(owner, name, description, prefix, baseUri);66 const contract = helper.ethNativeContract.collection(collectionAddress, 'nft');6768 const collection = helper.rft.getCollectionObject(collectionId);69 const data = (await collection.getData())!;7071 expect(data.name).to.be.eq(name);72 expect(data.description).to.be.eq(description);73 expect(data.raw.tokenPrefix).to.be.eq(prefix);74 expect(data.raw.mode).to.be.eq('ReFungible');7576 expect(await contract.methods.description().call()).to.deep.equal(description);7778 const options = await collection.getOptions();79 expect(options.tokenPropertyPermissions).to.be.deep.equal([80 {81 key: 'URI',82 permission: {mutable: true, collectionAdmin: true, tokenOwner: false},83 },84 {85 key: 'URISuffix',86 permission: {mutable: true, collectionAdmin: true, tokenOwner: false},87 },88 ]);89 });9091 92 itEth('[eth] Set sponsorship', async ({helper}) => {93 const owner = await helper.eth.createAccountWithBalance(donor);94 const sponsor = await helper.eth.createAccountWithBalance(donor);95 const ss58Format = helper.chain.getChainProperties().ss58Format;96 const {collectionId, collectionAddress} = await helper.eth.createRFTCollection(owner, 'Sponsor', 'absolutely anything', 'ENVY');9798 const collection = helper.ethNativeContract.collection(collectionAddress, 'rft', owner, true);99 await collection.methods.setCollectionSponsor(sponsor).send();100101 let data = (await helper.rft.getData(collectionId))!;102 expect(data.raw.sponsorship.Unconfirmed).to.be.equal(evmToAddress(sponsor, Number(ss58Format)));103104 await expect(collection.methods.confirmCollectionSponsorship().call()).to.be.rejectedWith('ConfirmSponsorshipFail');105106 const sponsorCollection = helper.ethNativeContract.collection(collectionAddress, 'rft', sponsor, true);107 await sponsorCollection.methods.confirmCollectionSponsorship().send();108109 data = (await helper.rft.getData(collectionId))!;110 expect(data.raw.sponsorship.Confirmed).to.be.equal(evmToAddress(sponsor, Number(ss58Format)));111 });112113 itEth('[cross] Set sponsorship', async ({helper}) => {114 const owner = await helper.eth.createAccountWithBalance(donor);115 const sponsor = await helper.eth.createAccountWithBalance(donor);116 const ss58Format = helper.chain.getChainProperties().ss58Format;117 const {collectionId, collectionAddress} = await helper.eth.createRFTCollection(owner, 'Sponsor', 'absolutely anything', 'ENVY');118119 const collection = helper.ethNativeContract.collection(collectionAddress, 'rft', owner);120 const sponsorCross = helper.ethCrossAccount.fromAddress(sponsor);121 await collection.methods.setCollectionSponsorCross(sponsorCross).send();122123 let data = (await helper.rft.getData(collectionId))!;124 expect(data.raw.sponsorship.Unconfirmed).to.be.equal(evmToAddress(sponsor, Number(ss58Format)));125126 await expect(collection.methods.confirmCollectionSponsorship().call()).to.be.rejectedWith('ConfirmSponsorshipFail');127128 const sponsorCollection = helper.ethNativeContract.collection(collectionAddress, 'rft', sponsor);129 await sponsorCollection.methods.confirmCollectionSponsorship().send();130131 data = (await helper.rft.getData(collectionId))!;132 expect(data.raw.sponsorship.Confirmed).to.be.equal(evmToAddress(sponsor, Number(ss58Format)));133 });134135 itEth('Collection address exist', async ({helper}) => {136 const owner = await helper.eth.createAccountWithBalance(donor);137 const collectionAddressForNonexistentCollection = '0x17C4E6453CC49AAAAEACA894E6D9683E00112233';138 const collectionHelpers = helper.ethNativeContract.collectionHelpers(owner);139140 expect(await collectionHelpers141 .methods.isCollectionExist(collectionAddressForNonexistentCollection).call())142 .to.be.false;143144 const {collectionAddress} = await helper.eth.createRFTCollection(owner, 'Exister', 'absolutely anything', 'WIWT');145 expect(await collectionHelpers146 .methods.isCollectionExist(collectionAddress).call())147 .to.be.true;148149 150 const collectionEvm = helper.ethNativeContract.collection(collectionAddress, 'ft', owner, true);151 const collectionOwner = await collectionEvm.methods.collectionOwner().call();152 expect(helper.address.restoreCrossAccountFromBigInt(BigInt(collectionOwner.sub))).to.eq(helper.address.ethToSubstrate(owner, true));153 });154});155156describe('(!negative tests!) Create RFT 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.ReFungible]);163 donor = await privateKey({url: import.meta.url});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 .createRFTCollection(collectionName, 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 .createRFTCollection(collectionName, 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 .createRFTCollection(collectionName, 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 });200201 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 .createRFTCollection('Peasantry', '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.createRFTCollection(owner, 'Transgressed', 'absolutely anything', 'YVNE');214 const peasantCollection = helper.ethNativeContract.collection(collectionAddress, 'rft', 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);221222 const sponsorCollection = helper.ethNativeContract.collection(collectionAddress, 'rft', sponsor, true);223 await expect(sponsorCollection.methods224 .confirmCollectionSponsorship()225 .call()).to.be.rejectedWith('ConfirmSponsorshipFail');226 }227 {228 await expect(peasantCollection.methods229 .setCollectionLimit({field: CollectionLimitField.AccountTokenOwnership, value: {status: true, value: 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.createRFTCollection(owner, 'Transgressed', 'absolutely anything', 'YVNE');238 const peasantCollection = helper.ethNativeContract.collection(collectionAddress, 'rft', 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);246247 const sponsorCollection = helper.ethNativeContract.collection(collectionAddress, 'rft', sponsor);248 await expect(sponsorCollection.methods249 .confirmCollectionSponsorship()250 .call()).to.be.rejectedWith('ConfirmSponsorshipFail');251 }252 {253 await expect(peasantCollection.methods254 .setCollectionLimit({field: CollectionLimitField.AccountTokenOwnership, value: {status: true, value: 1000}})255 .call()).to.be.rejectedWith(EXPECTED_ERROR);256 }257 });258259 itEth('destroyCollection', async ({helper}) => {260 const owner = await helper.eth.createAccountWithBalance(donor);261 const {collectionAddress, collectionId} = await helper.eth.createRFTCollection(owner, 'Limits', 'absolutely anything', 'OLF');262 const collectionHelper = helper.ethNativeContract.collectionHelpers(owner);263264 await expect(collectionHelper.methods265 .destroyCollection(collectionAddress)266 .send({from: owner})).to.be.fulfilled;267268 expect(await collectionHelper.methods269 .isCollectionExist(collectionAddress)270 .call()).to.be.false;271 expect(await helper.collection.getData(collectionId)).to.be.null;272 });273});