1234567891011121314151617import {evmToAddress} from '@polkadot/util-crypto';18import {IKeyringPair} from '@polkadot/types/types';19import {Pallets, requirePalletsOrSkip} from '../util';20import {expect, itEth, usingEthPlaygrounds} from './util';212223describe('Create RFT collection from EVM', () => {24 let donor: IKeyringPair;2526 before(async function() {27 await usingEthPlaygrounds(async (helper, privateKey) => {28 requirePalletsOrSkip(this, helper, [Pallets.ReFungible]);29 donor = await privateKey({filename: __filename});30 });31 });3233 itEth('Create collection', async ({helper}) => {34 const owner = await helper.eth.createAccountWithBalance(donor);35 36 const name = 'CollectionEVM';37 const description = 'Some description';38 const prefix = 'token prefix';39 40 const {collectionId} = await helper.eth.createRFTCollection(owner, name, description, prefix);41 const data = (await helper.rft.getData(collectionId))!;42 const collection = helper.rft.getCollectionObject(collectionId);4344 expect(data.name).to.be.eq(name);45 expect(data.description).to.be.eq(description);46 expect(data.raw.tokenPrefix).to.be.eq(prefix);47 expect(data.raw.mode).to.be.eq('ReFungible');4849 const options = await collection.getOptions();5051 expect(options.tokenPropertyPermissions).to.be.empty;52 });5354 5556 itEth('Create collection with properties', async ({helper}) => {57 const owner = await helper.eth.createAccountWithBalance(donor);5859 const name = 'CollectionEVM';60 const description = 'Some description';61 const prefix = 'token prefix';62 const baseUri = 'BaseURI';6364 const {collectionId} = await helper.eth.createERC721MetadataCompatibleRFTCollection(owner, name, description, prefix, baseUri);6566 const collection = helper.rft.getCollectionObject(collectionId);67 const data = (await collection.getData())!;68 69 expect(data.name).to.be.eq(name);70 expect(data.description).to.be.eq(description);71 expect(data.raw.tokenPrefix).to.be.eq(prefix);72 expect(data.raw.mode).to.be.eq('ReFungible');7374 const options = await collection.getOptions();75 expect(options.tokenPropertyPermissions).to.be.deep.equal([76 {77 key: 'URI',78 permission: {mutable: true, collectionAdmin: true, tokenOwner: false},79 },80 {81 key: 'URISuffix',82 permission: {mutable: true, collectionAdmin: true, tokenOwner: false},83 },84 ]);85 });86 87 88 itEth.skip('Check collection address exist', async ({helper}) => {89 const owner = await helper.eth.createAccountWithBalance(donor);9091 const expectedCollectionId = +(await helper.callRpc('api.rpc.unique.collectionStats')).created + 1;92 const expectedCollectionAddress = helper.ethAddress.fromCollectionId(expectedCollectionId);93 const collectionHelpers = helper.ethNativeContract.collectionHelpers(owner);9495 expect(await collectionHelpers.methods96 .isCollectionExist(expectedCollectionAddress)97 .call()).to.be.false;9899 await collectionHelpers.methods100 .createRFTCollection('A', 'A', 'A')101 .send({value: Number(2n * helper.balance.getOneTokenNominal())});102 103 expect(await collectionHelpers.methods104 .isCollectionExist(expectedCollectionAddress)105 .call()).to.be.true;106 });107 108 109 itEth('[eth] Set sponsorship', async ({helper}) => {110 const owner = await helper.eth.createAccountWithBalance(donor);111 const sponsor = await helper.eth.createAccountWithBalance(donor);112 const ss58Format = helper.chain.getChainProperties().ss58Format;113 const {collectionId, collectionAddress} = await helper.eth.createRFTCollection(owner, 'Sponsor', 'absolutely anything', 'ENVY');114115 const collection = helper.ethNativeContract.collection(collectionAddress, 'rft', owner, true);116 await collection.methods.setCollectionSponsor(sponsor).send();117118 let data = (await helper.rft.getData(collectionId))!;119 expect(data.raw.sponsorship.Unconfirmed).to.be.equal(evmToAddress(sponsor, Number(ss58Format)));120121 await expect(collection.methods.confirmCollectionSponsorship().call()).to.be.rejectedWith('caller is not set as sponsor');122123 const sponsorCollection = helper.ethNativeContract.collection(collectionAddress, 'rft', sponsor, true);124 await sponsorCollection.methods.confirmCollectionSponsorship().send();125126 data = (await helper.rft.getData(collectionId))!;127 expect(data.raw.sponsorship.Confirmed).to.be.equal(evmToAddress(sponsor, Number(ss58Format)));128 });129130 itEth('[cross] Set sponsorship', async ({helper}) => {131 const owner = await helper.eth.createAccountWithBalance(donor);132 const sponsor = await helper.eth.createAccountWithBalance(donor);133 const ss58Format = helper.chain.getChainProperties().ss58Format;134 const {collectionId, collectionAddress} = await helper.eth.createRFTCollection(owner, 'Sponsor', 'absolutely anything', 'ENVY');135136 const collection = helper.ethNativeContract.collection(collectionAddress, 'rft', owner);137 const sponsorCross = helper.ethCrossAccount.fromAddress(sponsor);138 await collection.methods.setCollectionSponsorCross(sponsorCross).send();139140 let data = (await helper.rft.getData(collectionId))!;141 expect(data.raw.sponsorship.Unconfirmed).to.be.equal(evmToAddress(sponsor, Number(ss58Format)));142143 await expect(collection.methods.confirmCollectionSponsorship().call()).to.be.rejectedWith('caller is not set as sponsor');144145 const sponsorCollection = helper.ethNativeContract.collection(collectionAddress, 'rft', sponsor);146 await sponsorCollection.methods.confirmCollectionSponsorship().send();147148 data = (await helper.rft.getData(collectionId))!;149 expect(data.raw.sponsorship.Confirmed).to.be.equal(evmToAddress(sponsor, Number(ss58Format)));150 });151152 itEth('Set limits', async ({helper}) => {153 const owner = await helper.eth.createAccountWithBalance(donor);154 const {collectionId, collectionAddress} = await helper.eth.createRFTCollection(owner, 'Limits', 'absolutely anything', 'INSI');155 const limits = {156 accountTokenOwnershipLimit: 1000,157 sponsoredDataSize: 1024,158 sponsoredDataRateLimit: 30,159 tokenLimit: 1000000,160 sponsorTransferTimeout: 6,161 sponsorApproveTimeout: 6,162 ownerCanTransfer: 0,163 ownerCanDestroy: 0,164 transfersEnabled: 0,165 };166 167 const expectedLimits = {168 accountTokenOwnershipLimit: 1000,169 sponsoredDataSize: 1024,170 sponsoredDataRateLimit: 30,171 tokenLimit: 1000000,172 sponsorTransferTimeout: 6,173 sponsorApproveTimeout: 6,174 ownerCanTransfer: false,175 ownerCanDestroy: false,176 transfersEnabled: false,177 };178 179 const collection = helper.ethNativeContract.collection(collectionAddress, 'rft', owner);180 await collection.methods.setCollectionLimit('accountTokenOwnershipLimit', limits.accountTokenOwnershipLimit).send();181 await collection.methods.setCollectionLimit('sponsoredDataSize', limits.sponsoredDataSize).send();182 await collection.methods.setCollectionLimit('sponsoredDataRateLimit', limits.sponsoredDataRateLimit).send();183 await collection.methods.setCollectionLimit('tokenLimit', limits.tokenLimit).send();184 await collection.methods.setCollectionLimit('sponsorTransferTimeout', limits.sponsorTransferTimeout).send();185 await collection.methods.setCollectionLimit('sponsorApproveTimeout', limits.sponsorApproveTimeout).send();186 await collection.methods.setCollectionLimit('ownerCanTransfer', limits.ownerCanTransfer).send();187 await collection.methods.setCollectionLimit('ownerCanDestroy', limits.ownerCanDestroy).send();188 await collection.methods.setCollectionLimit('transfersEnabled', limits.transfersEnabled).send();189 190 const data = (await helper.rft.getData(collectionId))!;191 expect(data.raw.limits.accountTokenOwnershipLimit).to.be.eq(expectedLimits.accountTokenOwnershipLimit);192 expect(data.raw.limits.sponsoredDataSize).to.be.eq(expectedLimits.sponsoredDataSize);193 expect(data.raw.limits.sponsoredDataRateLimit.blocks).to.be.eq(expectedLimits.sponsoredDataRateLimit);194 expect(data.raw.limits.tokenLimit).to.be.eq(expectedLimits.tokenLimit);195 expect(data.raw.limits.sponsorTransferTimeout).to.be.eq(expectedLimits.sponsorTransferTimeout);196 expect(data.raw.limits.sponsorApproveTimeout).to.be.eq(expectedLimits.sponsorApproveTimeout);197 expect(data.raw.limits.ownerCanTransfer).to.be.eq(expectedLimits.ownerCanTransfer);198 expect(data.raw.limits.ownerCanDestroy).to.be.eq(expectedLimits.ownerCanDestroy);199 expect(data.raw.limits.transfersEnabled).to.be.eq(expectedLimits.transfersEnabled);200 });201202 itEth('Collection address exist', async ({helper}) => {203 const owner = await helper.eth.createAccountWithBalance(donor);204 const collectionAddressForNonexistentCollection = '0x17C4E6453CC49AAAAEACA894E6D9683E00112233';205 expect(await helper.ethNativeContract.collectionHelpers(collectionAddressForNonexistentCollection)206 .methods.isCollectionExist(collectionAddressForNonexistentCollection).call())207 .to.be.false;208 209 const {collectionAddress} = await helper.eth.createRFTCollection(owner, 'Exister', 'absolutely anything', 'WIWT');210 expect(await helper.ethNativeContract.collectionHelpers(collectionAddress)211 .methods.isCollectionExist(collectionAddress).call())212 .to.be.true;213 });214});215216describe('(!negative tests!) Create RFT collection from EVM', () => {217 let donor: IKeyringPair;218 let nominal: bigint;219220 before(async function() {221 await usingEthPlaygrounds(async (helper, privateKey) => {222 requirePalletsOrSkip(this, helper, [Pallets.ReFungible]);223 donor = await privateKey({filename: __filename});224 nominal = helper.balance.getOneTokenNominal();225 });226 });227228 itEth('(!negative test!) Create collection (bad lengths)', async ({helper}) => {229 const owner = await helper.eth.createAccountWithBalance(donor);230 const collectionHelper = helper.ethNativeContract.collectionHelpers(owner);231 {232 const MAX_NAME_LENGTH = 64;233 const collectionName = 'A'.repeat(MAX_NAME_LENGTH + 1);234 const description = 'A';235 const tokenPrefix = 'A';236237 await expect(collectionHelper.methods238 .createRFTCollection(collectionName, description, tokenPrefix)239 .call({value: Number(2n * nominal)})).to.be.rejectedWith('name is too long. Max length is ' + MAX_NAME_LENGTH);240 }241 {242 const MAX_DESCRIPTION_LENGTH = 256;243 const collectionName = 'A';244 const description = 'A'.repeat(MAX_DESCRIPTION_LENGTH + 1);245 const tokenPrefix = 'A';246 await expect(collectionHelper.methods247 .createRFTCollection(collectionName, description, tokenPrefix)248 .call({value: Number(2n * nominal)})).to.be.rejectedWith('description is too long. Max length is ' + MAX_DESCRIPTION_LENGTH);249 }250 {251 const MAX_TOKEN_PREFIX_LENGTH = 16;252 const collectionName = 'A';253 const description = 'A';254 const tokenPrefix = 'A'.repeat(MAX_TOKEN_PREFIX_LENGTH + 1);255 await expect(collectionHelper.methods256 .createRFTCollection(collectionName, description, tokenPrefix)257 .call({value: Number(2n * nominal)})).to.be.rejectedWith('token_prefix is too long. Max length is ' + MAX_TOKEN_PREFIX_LENGTH);258 }259 });260 261 itEth('(!negative test!) Create collection (no funds)', async ({helper}) => {262 const owner = await helper.eth.createAccountWithBalance(donor);263 const collectionHelper = helper.ethNativeContract.collectionHelpers(owner);264 await expect(collectionHelper.methods265 .createRFTCollection('Peasantry', 'absolutely anything', 'TWIW')266 .call({value: Number(1n * nominal)})).to.be.rejectedWith('Sent amount not equals to collection creation price (2000000000000000000)');267 });268269 270 itEth('(!negative test!) [eth] Check owner', async ({helper}) => {271 const owner = await helper.eth.createAccountWithBalance(donor);272 const peasant = helper.eth.createAccount();273 const {collectionAddress} = await helper.eth.createRFTCollection(owner, 'Transgressed', 'absolutely anything', 'YVNE');274 const peasantCollection = helper.ethNativeContract.collection(collectionAddress, 'rft', peasant, true);275 const EXPECTED_ERROR = 'NoPermission';276 {277 const sponsor = await helper.eth.createAccountWithBalance(donor);278 await expect(peasantCollection.methods279 .setCollectionSponsor(sponsor)280 .call()).to.be.rejectedWith(EXPECTED_ERROR);281 282 const sponsorCollection = helper.ethNativeContract.collection(collectionAddress, 'rft', sponsor, true);283 await expect(sponsorCollection.methods284 .confirmCollectionSponsorship()285 .call()).to.be.rejectedWith('caller is not set as sponsor');286 }287 {288 await expect(peasantCollection.methods289 .setCollectionLimit('account_token_ownership_limit', '1000')290 .call()).to.be.rejectedWith(EXPECTED_ERROR);291 }292 });293294 itEth('(!negative test!) [cross] Check owner', async ({helper}) => {295 const owner = await helper.eth.createAccountWithBalance(donor);296 const peasant = helper.eth.createAccount();297 const {collectionAddress} = await helper.eth.createRFTCollection(owner, 'Transgressed', 'absolutely anything', 'YVNE');298 const peasantCollection = helper.ethNativeContract.collection(collectionAddress, 'rft', peasant);299 const EXPECTED_ERROR = 'NoPermission';300 {301 const sponsor = await helper.eth.createAccountWithBalance(donor);302 const sponsorCross = helper.ethCrossAccount.fromAddress(sponsor);303 await expect(peasantCollection.methods304 .setCollectionSponsorCross(sponsorCross)305 .call()).to.be.rejectedWith(EXPECTED_ERROR);306 307 const sponsorCollection = helper.ethNativeContract.collection(collectionAddress, 'rft', sponsor);308 await expect(sponsorCollection.methods309 .confirmCollectionSponsorship()310 .call()).to.be.rejectedWith('caller is not set as sponsor');311 }312 {313 await expect(peasantCollection.methods314 .setCollectionLimit('account_token_ownership_limit', '1000')315 .call()).to.be.rejectedWith(EXPECTED_ERROR);316 }317 });318319 itEth('(!negative test!) Set limits', async ({helper}) => {320 const invalidLimits = {321 accountTokenOwnershipLimit: BigInt(Number.MAX_SAFE_INTEGER),322 transfersEnabled: 3,323 };324325 const owner = await helper.eth.createAccountWithBalance(donor);326 const {collectionAddress} = await helper.eth.createRFTCollection(owner, 'Limits', 'absolutely anything', 'ISNI');327 const collectionEvm = helper.ethNativeContract.collection(collectionAddress, 'rft', owner);328 329 await expect(collectionEvm.methods330 .setCollectionLimit(Object.keys(invalidLimits)[0], invalidLimits.accountTokenOwnershipLimit)331 .call()).to.be.rejectedWith(`can't convert value to u32 "${invalidLimits.accountTokenOwnershipLimit}"`);332 333 await expect(collectionEvm.methods334 .setCollectionLimit(Object.keys(invalidLimits)[1], invalidLimits.transfersEnabled)335 .call()).to.be.rejectedWith(`can't convert value to boolean "${invalidLimits.transfersEnabled}"`);336 });337 338 itEth('destroyCollection', async ({helper}) => {339 const owner = await helper.eth.createAccountWithBalance(donor);340 const {collectionAddress} = await helper.eth.createRFTCollection(owner, 'Limits', 'absolutely anything', 'OLF');341 const collectionHelper = helper.ethNativeContract.collectionHelpers(owner);342 343 await expect(collectionHelper.methods344 .destroyCollection(collectionAddress)345 .send({from: owner})).to.be.fulfilled;346 347 expect(await collectionHelper.methods348 .isCollectionExist(collectionAddress)349 .call()).to.be.false; 350 });351});