1234567891011121314151617import {evmToAddress} from '@polkadot/util-crypto';18import {IKeyringPair} from '@polkadot/types/types';19import {Pallets, requirePalletsOrSkip} from '../util/playgrounds';20import {expect, itEth, usingEthPlaygrounds} from './util/playgrounds';2122describe('Create RFT collection from EVM', () => {23 let donor: IKeyringPair;2425 before(async function() {26 await usingEthPlaygrounds(async (helper, privateKey) => {27 requirePalletsOrSkip(this, helper, [Pallets.ReFungible]);28 donor = privateKey('//Alice');29 });30 });3132 itEth('Create collection', async ({helper}) => {33 const owner = await helper.eth.createAccountWithBalance(donor);34 35 const name = 'CollectionEVM';36 const description = 'Some description';37 const prefix = 'token prefix';38 39 40 const collectionCountBefore = +(await helper.callRpc('api.rpc.unique.collectionStats')).created;41 const {collectionId} = await helper.eth.createRefungibleCollection(owner, name, description, prefix);42 const collectionCountAfter = +(await helper.callRpc('api.rpc.unique.collectionStats')).created;43 44 const data = (await helper.rft.getData(collectionId))!;4546 expect(collectionCountAfter - collectionCountBefore).to.be.eq(1);47 expect(collectionId).to.be.eq(collectionCountAfter);48 expect(data.name).to.be.eq(name);49 expect(data.description).to.be.eq(description);50 expect(data.raw.tokenPrefix).to.be.eq(prefix);51 expect(data.raw.mode).to.be.eq('ReFungible');52 });5354 55 itEth('Check collection address exist', async ({helper}) => {56 const owner = await helper.eth.createAccountWithBalance(donor);5758 const expectedCollectionId = +(await helper.callRpc('api.rpc.unique.collectionStats')).created + 1;59 const expectedCollectionAddress = helper.ethAddress.fromCollectionId(expectedCollectionId);60 const collectionHelpers = helper.ethNativeContract.collectionHelpers(owner);6162 expect(await collectionHelpers.methods63 .isCollectionExist(expectedCollectionAddress)64 .call()).to.be.false;6566 await collectionHelpers.methods67 .createRFTCollection('A', 'A', 'A')68 .send();69 70 expect(await collectionHelpers.methods71 .isCollectionExist(expectedCollectionAddress)72 .call()).to.be.true;73 });74 75 itEth('Set sponsorship', async ({helper}) => {76 const owner = await helper.eth.createAccountWithBalance(donor);77 const sponsor = await helper.eth.createAccountWithBalance(donor);78 const ss58Format = helper.chain.getChainProperties().ss58Format;79 const {collectionId, collectionAddress} = await helper.eth.createRefungibleCollection(owner, 'Sponsor', 'absolutely anything', 'ENVY');8081 const collection = helper.ethNativeContract.collection(collectionAddress, 'rft', owner);82 await collection.methods.setCollectionSponsor(sponsor).send();8384 let data = (await helper.rft.getData(collectionId))!;85 expect(data.raw.sponsorship.Unconfirmed).to.be.equal(evmToAddress(sponsor, Number(ss58Format)));8687 await expect(collection.methods.confirmCollectionSponsorship().call()).to.be.rejectedWith('caller is not set as sponsor');8889 const sponsorCollection = helper.ethNativeContract.collection(collectionAddress, 'rft', sponsor);90 await sponsorCollection.methods.confirmCollectionSponsorship().send();9192 data = (await helper.rft.getData(collectionId))!;93 expect(data.raw.sponsorship.Confirmed).to.be.equal(evmToAddress(sponsor, Number(ss58Format)));94 });9596 itEth('Set limits', async ({helper}) => {97 const owner = await helper.eth.createAccountWithBalance(donor);98 const {collectionId, collectionAddress} = await helper.eth.createRefungibleCollection(owner, 'Limits', 'absolutely anything', 'INSI');99 const limits = {100 accountTokenOwnershipLimit: 1000,101 sponsoredDataSize: 1024,102 sponsoredDataRateLimit: 30,103 tokenLimit: 1000000,104 sponsorTransferTimeout: 6,105 sponsorApproveTimeout: 6,106 ownerCanTransfer: false,107 ownerCanDestroy: false,108 transfersEnabled: false,109 };110111 const collection = helper.ethNativeContract.collection(collectionAddress, 'rft', owner);112 await collection.methods['setCollectionLimit(string,uint32)']('accountTokenOwnershipLimit', limits.accountTokenOwnershipLimit).send();113 await collection.methods['setCollectionLimit(string,uint32)']('sponsoredDataSize', limits.sponsoredDataSize).send();114 await collection.methods['setCollectionLimit(string,uint32)']('sponsoredDataRateLimit', limits.sponsoredDataRateLimit).send();115 await collection.methods['setCollectionLimit(string,uint32)']('tokenLimit', limits.tokenLimit).send();116 await collection.methods['setCollectionLimit(string,uint32)']('sponsorTransferTimeout', limits.sponsorTransferTimeout).send();117 await collection.methods['setCollectionLimit(string,uint32)']('sponsorApproveTimeout', limits.sponsorApproveTimeout).send();118 await collection.methods['setCollectionLimit(string,bool)']('ownerCanTransfer', limits.ownerCanTransfer).send();119 await collection.methods['setCollectionLimit(string,bool)']('ownerCanDestroy', limits.ownerCanDestroy).send();120 await collection.methods['setCollectionLimit(string,bool)']('transfersEnabled', limits.transfersEnabled).send();121 122 const data = (await helper.rft.getData(collectionId))!;123 expect(data.raw.limits.accountTokenOwnershipLimit).to.be.eq(limits.accountTokenOwnershipLimit);124 expect(data.raw.limits.sponsoredDataSize).to.be.eq(limits.sponsoredDataSize);125 expect(data.raw.limits.sponsoredDataRateLimit.blocks).to.be.eq(limits.sponsoredDataRateLimit);126 expect(data.raw.limits.tokenLimit).to.be.eq(limits.tokenLimit);127 expect(data.raw.limits.sponsorTransferTimeout).to.be.eq(limits.sponsorTransferTimeout);128 expect(data.raw.limits.sponsorApproveTimeout).to.be.eq(limits.sponsorApproveTimeout);129 expect(data.raw.limits.ownerCanTransfer).to.be.eq(limits.ownerCanTransfer);130 expect(data.raw.limits.ownerCanDestroy).to.be.eq(limits.ownerCanDestroy);131 expect(data.raw.limits.transfersEnabled).to.be.eq(limits.transfersEnabled);132 });133134 itEth('Collection address exist', async ({helper}) => {135 const owner = await helper.eth.createAccountWithBalance(donor);136 const collectionAddressForNonexistentCollection = '0x17C4E6453CC49AAAAEACA894E6D9683E00112233';137 expect(await helper.ethNativeContract.collectionHelpers(collectionAddressForNonexistentCollection)138 .methods.isCollectionExist(collectionAddressForNonexistentCollection).call())139 .to.be.false;140 141 const {collectionAddress} = await helper.eth.createRefungibleCollection(owner, 'Exister', 'absolutely anything', 'WIWT');142 expect(await helper.ethNativeContract.collectionHelpers(collectionAddress)143 .methods.isCollectionExist(collectionAddress).call())144 .to.be.true;145 });146});147148describe('(!negative tests!) Create RFT collection from EVM', () => {149 let donor: IKeyringPair;150151 before(async function() {152 await usingEthPlaygrounds(async (helper, privateKey) => {153 requirePalletsOrSkip(this, helper, [Pallets.ReFungible]);154 donor = privateKey('//Alice');155 });156 });157158 itEth('(!negative test!) Create collection (bad lengths)', async ({helper}) => {159 const owner = await helper.eth.createAccountWithBalance(donor);160 const collectionHelper = helper.ethNativeContract.collectionHelpers(owner);161 {162 const MAX_NAME_LENGTH = 64;163 const collectionName = 'A'.repeat(MAX_NAME_LENGTH + 1);164 const description = 'A';165 const tokenPrefix = 'A';166167 await expect(collectionHelper.methods168 .createRFTCollection(collectionName, description, tokenPrefix)169 .call()).to.be.rejectedWith('name is too long. Max length is ' + MAX_NAME_LENGTH);170 }171 {172 const MAX_DESCRIPTION_LENGTH = 256;173 const collectionName = 'A';174 const description = 'A'.repeat(MAX_DESCRIPTION_LENGTH + 1);175 const tokenPrefix = 'A';176 await expect(collectionHelper.methods177 .createRFTCollection(collectionName, description, tokenPrefix)178 .call()).to.be.rejectedWith('description is too long. Max length is ' + MAX_DESCRIPTION_LENGTH);179 }180 {181 const MAX_TOKEN_PREFIX_LENGTH = 16;182 const collectionName = 'A';183 const description = 'A';184 const tokenPrefix = 'A'.repeat(MAX_TOKEN_PREFIX_LENGTH + 1);185 await expect(collectionHelper.methods186 .createRFTCollection(collectionName, description, tokenPrefix)187 .call()).to.be.rejectedWith('token_prefix is too long. Max length is ' + MAX_TOKEN_PREFIX_LENGTH);188 }189 });190 191 itEth('(!negative test!) Create collection (no funds)', async ({helper}) => {192 const owner = helper.eth.createAccount();193 const collectionHelper = helper.ethNativeContract.collectionHelpers(owner);194 await expect(collectionHelper.methods195 .createRFTCollection('Peasantry', 'absolutely anything', 'TWIW')196 .call()).to.be.rejectedWith('NotSufficientFounds');197 });198199 itEth('(!negative test!) Check owner', async ({helper}) => {200 const owner = await helper.eth.createAccountWithBalance(donor);201 const peasant = helper.eth.createAccount();202 const {collectionAddress} = await helper.eth.createRefungibleCollection(owner, 'Transgressed', 'absolutely anything', 'YVNE');203 const peasantCollection = helper.ethNativeContract.collection(collectionAddress, 'rft', peasant);204 const EXPECTED_ERROR = 'NoPermission';205 {206 const sponsor = await helper.eth.createAccountWithBalance(donor);207 await expect(peasantCollection.methods208 .setCollectionSponsor(sponsor)209 .call()).to.be.rejectedWith(EXPECTED_ERROR);210 211 const sponsorCollection = helper.ethNativeContract.collection(collectionAddress, 'rft', sponsor);212 await expect(sponsorCollection.methods213 .confirmCollectionSponsorship()214 .call()).to.be.rejectedWith('caller is not set as sponsor');215 }216 {217 await expect(peasantCollection.methods218 .setCollectionLimit('account_token_ownership_limit', '1000')219 .call()).to.be.rejectedWith(EXPECTED_ERROR);220 }221 });222223 itEth('(!negative test!) Set limits', async ({helper}) => {224 const owner = await helper.eth.createAccountWithBalance(donor);225 const {collectionAddress} = await helper.eth.createRefungibleCollection(owner, 'Limits', 'absolutely anything', 'ISNI');226 const collectionEvm = helper.ethNativeContract.collection(collectionAddress, 'rft', owner);227 await expect(collectionEvm.methods228 .setCollectionLimit('badLimit', 'true')229 .call()).to.be.rejectedWith('unknown boolean limit "badLimit"');230 });231});