difftreelog
refactor `collection_limits`, add docs for `CollectionLimits`
in: master
9 files changed
pallets/common/src/erc.rsdiffbeforeafterboth--- a/pallets/common/src/erc.rs
+++ b/pallets/common/src/erc.rs
@@ -358,18 +358,16 @@
),
limits
.sponsored_data_rate_limit
- .map(|limit| {
- (
- EvmCollectionLimits::SponsoredDataRateLimit,
- match limit {
- SponsoringRateLimit::Blocks(_) => true,
- _ => false,
- },
- match limit {
- SponsoringRateLimit::Blocks(blocks) => blocks.into(),
- _ => Default::default(),
- },
- )
+ .and_then(|limit| {
+ if let SponsoringRateLimit::Blocks(blocks) = limit {
+ Some((
+ EvmCollectionLimits::SponsoredDataRateLimit,
+ true,
+ blocks.into(),
+ ))
+ } else {
+ None
+ }
})
.unwrap_or((
EvmCollectionLimits::SponsoredDataRateLimit,
pallets/common/src/eth.rsdiffbeforeafterboth--- a/pallets/common/src/eth.rs
+++ b/pallets/common/src/eth.rs
@@ -155,6 +155,8 @@
}
}
}
+
+/// [`CollectionLimits`](up_data_structs::CollectionLimits) representation for EVM.
#[derive(Debug, Default, Clone, Copy, AbiCoder)]
#[repr(u8)]
pub enum CollectionLimits {
tests/src/eth/collectionLimits.test.tsdiffbeforeafterboth1import {IKeyringPair} from '@polkadot/types/types';2import {Pallets} from '../util';3import {CollectionLimits, expect, itEth, usingEthPlaygrounds} from './util';456describe('Can set collection limits', () => {7 let donor: IKeyringPair;89 before(async () => {10 await usingEthPlaygrounds(async (_helper, privateKey) => {11 donor = await privateKey({filename: __filename});12 });13 });1415 [16 {case: 'nft' as const},17 {case: 'rft' as const, requiredPallets: [Pallets.ReFungible]},18 {case: 'ft' as const},19 ].map(testCase =>20 itEth.ifWithPallets(`for ${testCase.case}`, testCase.requiredPallets || [], async ({helper}) => {21 const owner = await helper.eth.createAccountWithBalance(donor);22 const {collectionId, collectionAddress} = await helper.eth.createCollection(testCase.case, owner, 'Limits', 'absolutely anything', 'FLO', 18);23 const limits = {24 accountTokenOwnershipLimit: 1000,25 sponsoredDataSize: 1024,26 sponsoredDataRateLimit: 30,27 tokenLimit: 1000000,28 sponsorTransferTimeout: 6,29 sponsorApproveTimeout: 6,30 ownerCanTransfer: 1,31 ownerCanDestroy: 0,32 transfersEnabled: 0,33 };34 35 const expectedLimits = {36 accountTokenOwnershipLimit: 1000,37 sponsoredDataSize: 1024,38 sponsoredDataRateLimit: {blocks: 30},39 tokenLimit: 1000000,40 sponsorTransferTimeout: 6,41 sponsorApproveTimeout: 6,42 ownerCanTransfer: true,43 ownerCanDestroy: false,44 transfersEnabled: false,45 };46 47 const collectionEvm = helper.ethNativeContract.collection(collectionAddress, testCase.case, owner);48 await collectionEvm.methods.setCollectionLimit(CollectionLimits.AccountTokenOwnership, true, limits.accountTokenOwnershipLimit).send();49 await collectionEvm.methods.setCollectionLimit(CollectionLimits.SponsoredDataSize, true, limits.sponsoredDataSize).send();50 await collectionEvm.methods.setCollectionLimit(CollectionLimits.SponsoredDataRateLimit, true, limits.sponsoredDataRateLimit).send();51 await collectionEvm.methods.setCollectionLimit(CollectionLimits.TokenLimit, true, limits.tokenLimit).send();52 await collectionEvm.methods.setCollectionLimit(CollectionLimits.SponsorTransferTimeout, true, limits.sponsorTransferTimeout).send();53 await collectionEvm.methods.setCollectionLimit(CollectionLimits.SponsorApproveTimeout, true, limits.sponsorApproveTimeout).send();54 await collectionEvm.methods.setCollectionLimit(CollectionLimits.OwnerCanTransfer, true, limits.ownerCanTransfer).send();55 await collectionEvm.methods.setCollectionLimit(CollectionLimits.OwnerCanDestroy, true, limits.ownerCanDestroy).send();56 await collectionEvm.methods.setCollectionLimit(CollectionLimits.TransferEnabled, true, limits.transfersEnabled).send();57 58 // Check limits from sub:59 const data = (await helper.rft.getData(collectionId))!;60 expect(data.raw.limits).to.deep.eq(expectedLimits);61 expect(await helper.collection.getEffectiveLimits(collectionId)).to.deep.eq(expectedLimits);62 // Check limits from eth:63 const limitsEvm = await collectionEvm.methods.collectionLimits().call({from: owner});64 expect(limitsEvm).to.have.length(9);65 expect(limitsEvm[0]).to.deep.eq(['0', true, limits.accountTokenOwnershipLimit.toString()]);66 expect(limitsEvm[1]).to.deep.eq(['1', true, limits.sponsoredDataSize.toString()]);67 expect(limitsEvm[2]).to.deep.eq(['2', true, limits.sponsoredDataRateLimit.toString()]);68 expect(limitsEvm[3]).to.deep.eq(['3', true, limits.tokenLimit.toString()]);69 expect(limitsEvm[4]).to.deep.eq(['4', true, limits.sponsorTransferTimeout.toString()]);70 expect(limitsEvm[5]).to.deep.eq(['5', true, limits.sponsorApproveTimeout.toString()]);71 expect(limitsEvm[6]).to.deep.eq(['6', true, limits.ownerCanTransfer.toString()]);72 expect(limitsEvm[7]).to.deep.eq(['7', true, limits.ownerCanDestroy.toString()]);73 expect(limitsEvm[8]).to.deep.eq(['8', true, limits.transfersEnabled.toString()]);74 }));75});7677describe('Cannot set invalid collection limits', () => {78 let donor: IKeyringPair;7980 before(async () => {81 await usingEthPlaygrounds(async (_helper, privateKey) => {82 donor = await privateKey({filename: __filename});83 });84 });8586 [87 {case: 'nft' as const},88 {case: 'rft' as const, requiredPallets: [Pallets.ReFungible]},89 {case: 'ft' as const},90 ].map(testCase =>91 itEth.ifWithPallets(`for ${testCase.case}`, testCase.requiredPallets || [], async ({helper}) => {92 const invalidLimits = {93 accountTokenOwnershipLimit: BigInt(Number.MAX_SAFE_INTEGER),94 transfersEnabled: 3,95 };9697 const owner = await helper.eth.createAccountWithBalance(donor);98 const {collectionAddress} = await helper.eth.createCollection(testCase.case, owner, 'Limits', 'absolutely anything', 'ISNI', 18);99 const collectionEvm = helper.ethNativeContract.collection(collectionAddress, testCase.case, owner);100101 // Cannot set non-existing limit102 await expect(collectionEvm.methods103 .setCollectionLimit(9, true, 1)104 .call()).to.be.rejectedWith('Returned error: VM Exception while processing transaction: revert Value not convertible into enum "CollectionLimits"'); 105 106 // Cannot disable limits107 await expect(collectionEvm.methods108 .setCollectionLimit(CollectionLimits.AccountTokenOwnership, false, 200)109 .call()).to.be.rejectedWith('Returned error: VM Exception while processing transaction: revert user can\'t disable limits');110111 await expect(collectionEvm.methods112 .setCollectionLimit(CollectionLimits.AccountTokenOwnership, true, invalidLimits.accountTokenOwnershipLimit)113 .call()).to.be.rejectedWith(`can't convert value to u32 "${invalidLimits.accountTokenOwnershipLimit}"`);114 115 await expect(collectionEvm.methods116 .setCollectionLimit(CollectionLimits.TransferEnabled, true, 3)117 .call()).to.be.rejectedWith(`can't convert value to boolean "${invalidLimits.transfersEnabled}"`);118119 expect(() => collectionEvm.methods120 .setCollectionLimit(CollectionLimits.SponsoredDataSize, true, -1).send()).to.throw('value out-of-bounds');121 }));122123 [124 {case: 'nft' as const, requiredPallets: []},125 {case: 'rft' as const, requiredPallets: [Pallets.ReFungible]},126 {case: 'ft' as const, requiredPallets: []},127 ].map(testCase =>128 itEth.ifWithPallets(`Non-owner and non-admin cannot set collection limits for ${testCase.case}`, testCase.requiredPallets || [], async ({helper}) => {129 const owner = await helper.eth.createAccountWithBalance(donor);130 const nonOwner = await helper.eth.createAccountWithBalance(donor);131 const {collectionAddress} = await helper.eth.createCollection(testCase.case, owner, 'Limits', 'absolutely anything', 'FLO', 18);132133 const collectionEvm = helper.ethNativeContract.collection(collectionAddress, testCase.case, owner);134 await expect(collectionEvm.methods135 .setCollectionLimit(CollectionLimits.AccountTokenOwnership, true, 1000)136 .call({from: nonOwner}))137 .to.be.rejectedWith('NoPermission');138139 await expect(collectionEvm.methods140 .setCollectionLimit(CollectionLimits.AccountTokenOwnership, true, 1000)141 .send({from: nonOwner}))142 .to.be.rejected;143 }));144});1import {IKeyringPair} from '@polkadot/types/types';2import {Pallets} from '../util';3import {expect, itEth, usingEthPlaygrounds} from './util';4import {CollectionLimits} from './util/playgrounds/types';567describe('Can set collection limits', () => {8 let donor: IKeyringPair;910 before(async () => {11 await usingEthPlaygrounds(async (_helper, privateKey) => {12 donor = await privateKey({filename: __filename});13 });14 });1516 [17 {case: 'nft' as const},18 {case: 'rft' as const, requiredPallets: [Pallets.ReFungible]},19 {case: 'ft' as const},20 ].map(testCase =>21 itEth.ifWithPallets(`for ${testCase.case}`, testCase.requiredPallets || [], async ({helper}) => {22 const owner = await helper.eth.createAccountWithBalance(donor);23 const {collectionId, collectionAddress} = await helper.eth.createCollection(testCase.case, owner, 'Limits', 'absolutely anything', 'FLO', 18);24 const limits = {25 accountTokenOwnershipLimit: 1000,26 sponsoredDataSize: 1024,27 sponsoredDataRateLimit: 30,28 tokenLimit: 1000000,29 sponsorTransferTimeout: 6,30 sponsorApproveTimeout: 6,31 ownerCanTransfer: 1,32 ownerCanDestroy: 0,33 transfersEnabled: 0,34 };35 36 const expectedLimits = {37 accountTokenOwnershipLimit: 1000,38 sponsoredDataSize: 1024,39 sponsoredDataRateLimit: {blocks: 30},40 tokenLimit: 1000000,41 sponsorTransferTimeout: 6,42 sponsorApproveTimeout: 6,43 ownerCanTransfer: true,44 ownerCanDestroy: false,45 transfersEnabled: false,46 };47 48 const collectionEvm = helper.ethNativeContract.collection(collectionAddress, testCase.case, owner);49 await collectionEvm.methods.setCollectionLimit(CollectionLimits.AccountTokenOwnership, true, limits.accountTokenOwnershipLimit).send();50 await collectionEvm.methods.setCollectionLimit(CollectionLimits.SponsoredDataSize, true, limits.sponsoredDataSize).send();51 await collectionEvm.methods.setCollectionLimit(CollectionLimits.SponsoredDataRateLimit, true, limits.sponsoredDataRateLimit).send();52 await collectionEvm.methods.setCollectionLimit(CollectionLimits.TokenLimit, true, limits.tokenLimit).send();53 await collectionEvm.methods.setCollectionLimit(CollectionLimits.SponsorTransferTimeout, true, limits.sponsorTransferTimeout).send();54 await collectionEvm.methods.setCollectionLimit(CollectionLimits.SponsorApproveTimeout, true, limits.sponsorApproveTimeout).send();55 await collectionEvm.methods.setCollectionLimit(CollectionLimits.OwnerCanTransfer, true, limits.ownerCanTransfer).send();56 await collectionEvm.methods.setCollectionLimit(CollectionLimits.OwnerCanDestroy, true, limits.ownerCanDestroy).send();57 await collectionEvm.methods.setCollectionLimit(CollectionLimits.TransferEnabled, true, limits.transfersEnabled).send();58 59 // Check limits from sub:60 const data = (await helper.rft.getData(collectionId))!;61 expect(data.raw.limits).to.deep.eq(expectedLimits);62 expect(await helper.collection.getEffectiveLimits(collectionId)).to.deep.eq(expectedLimits);63 // Check limits from eth:64 const limitsEvm = await collectionEvm.methods.collectionLimits().call({from: owner});65 expect(limitsEvm).to.have.length(9);66 expect(limitsEvm[0]).to.deep.eq([CollectionLimits.AccountTokenOwnership.toString(), true, limits.accountTokenOwnershipLimit.toString()]);67 expect(limitsEvm[1]).to.deep.eq([CollectionLimits.SponsoredDataSize.toString(), true, limits.sponsoredDataSize.toString()]);68 expect(limitsEvm[2]).to.deep.eq([CollectionLimits.SponsoredDataRateLimit.toString(), true, limits.sponsoredDataRateLimit.toString()]);69 expect(limitsEvm[3]).to.deep.eq([CollectionLimits.TokenLimit.toString(), true, limits.tokenLimit.toString()]);70 expect(limitsEvm[4]).to.deep.eq([CollectionLimits.SponsorTransferTimeout.toString(), true, limits.sponsorTransferTimeout.toString()]);71 expect(limitsEvm[5]).to.deep.eq([CollectionLimits.SponsorApproveTimeout.toString(), true, limits.sponsorApproveTimeout.toString()]);72 expect(limitsEvm[6]).to.deep.eq([CollectionLimits.OwnerCanTransfer.toString(), true, limits.ownerCanTransfer.toString()]);73 expect(limitsEvm[7]).to.deep.eq([CollectionLimits.OwnerCanDestroy.toString(), true, limits.ownerCanDestroy.toString()]);74 expect(limitsEvm[8]).to.deep.eq([CollectionLimits.TransferEnabled.toString(), true, limits.transfersEnabled.toString()]);75 }));76});7778describe('Cannot set invalid collection limits', () => {79 let donor: IKeyringPair;8081 before(async () => {82 await usingEthPlaygrounds(async (_helper, privateKey) => {83 donor = await privateKey({filename: __filename});84 });85 });8687 [88 {case: 'nft' as const},89 {case: 'rft' as const, requiredPallets: [Pallets.ReFungible]},90 {case: 'ft' as const},91 ].map(testCase =>92 itEth.ifWithPallets(`for ${testCase.case}`, testCase.requiredPallets || [], async ({helper}) => {93 const invalidLimits = {94 accountTokenOwnershipLimit: BigInt(Number.MAX_SAFE_INTEGER),95 transfersEnabled: 3,96 };9798 const owner = await helper.eth.createAccountWithBalance(donor);99 const {collectionAddress} = await helper.eth.createCollection(testCase.case, owner, 'Limits', 'absolutely anything', 'ISNI', 18);100 const collectionEvm = helper.ethNativeContract.collection(collectionAddress, testCase.case, owner);101102 // Cannot set non-existing limit103 await expect(collectionEvm.methods104 .setCollectionLimit(9, true, 1)105 .call()).to.be.rejectedWith('Returned error: VM Exception while processing transaction: revert Value not convertible into enum "CollectionLimits"'); 106 107 // Cannot disable limits108 await expect(collectionEvm.methods109 .setCollectionLimit(CollectionLimits.AccountTokenOwnership, false, 200)110 .call()).to.be.rejectedWith('Returned error: VM Exception while processing transaction: revert user can\'t disable limits');111112 await expect(collectionEvm.methods113 .setCollectionLimit(CollectionLimits.AccountTokenOwnership, true, invalidLimits.accountTokenOwnershipLimit)114 .call()).to.be.rejectedWith(`can't convert value to u32 "${invalidLimits.accountTokenOwnershipLimit}"`);115 116 await expect(collectionEvm.methods117 .setCollectionLimit(CollectionLimits.TransferEnabled, true, 3)118 .call()).to.be.rejectedWith(`can't convert value to boolean "${invalidLimits.transfersEnabled}"`);119120 expect(() => collectionEvm.methods121 .setCollectionLimit(CollectionLimits.SponsoredDataSize, true, -1).send()).to.throw('value out-of-bounds');122 }));123124 [125 {case: 'nft' as const, requiredPallets: []},126 {case: 'rft' as const, requiredPallets: [Pallets.ReFungible]},127 {case: 'ft' as const, requiredPallets: []},128 ].map(testCase =>129 itEth.ifWithPallets(`Non-owner and non-admin cannot set collection limits for ${testCase.case}`, testCase.requiredPallets || [], async ({helper}) => {130 const owner = await helper.eth.createAccountWithBalance(donor);131 const nonOwner = await helper.eth.createAccountWithBalance(donor);132 const {collectionAddress} = await helper.eth.createCollection(testCase.case, owner, 'Limits', 'absolutely anything', 'FLO', 18);133134 const collectionEvm = helper.ethNativeContract.collection(collectionAddress, testCase.case, owner);135 await expect(collectionEvm.methods136 .setCollectionLimit(CollectionLimits.AccountTokenOwnership, true, 1000)137 .call({from: nonOwner}))138 .to.be.rejectedWith('NoPermission');139140 await expect(collectionEvm.methods141 .setCollectionLimit(CollectionLimits.AccountTokenOwnership, true, 1000)142 .send({from: nonOwner}))143 .to.be.rejected;144 }));145});tests/src/eth/createFTCollection.test.tsdiffbeforeafterboth--- a/tests/src/eth/createFTCollection.test.ts
+++ b/tests/src/eth/createFTCollection.test.ts
@@ -17,7 +17,8 @@
import {IKeyringPair} from '@polkadot/types/types';
import {evmToAddress} from '@polkadot/util-crypto';
import {Pallets, requirePalletsOrSkip} from '../util';
-import {CollectionLimits, expect, itEth, usingEthPlaygrounds} from './util';
+import {expect, itEth, usingEthPlaygrounds} from './util';
+import { CollectionLimits } from './util/playgrounds/types';
const DECIMALS = 18;
tests/src/eth/createNFTCollection.test.tsdiffbeforeafterboth--- a/tests/src/eth/createNFTCollection.test.ts
+++ b/tests/src/eth/createNFTCollection.test.ts
@@ -16,7 +16,8 @@
import {evmToAddress} from '@polkadot/util-crypto';
import {IKeyringPair} from '@polkadot/types/types';
-import {CollectionLimits, expect, itEth, usingEthPlaygrounds} from './util';
+import {expect, itEth, usingEthPlaygrounds} from './util';
+import { CollectionLimits } from './util/playgrounds/types';
describe('Create NFT collection from EVM', () => {
tests/src/eth/createRFTCollection.test.tsdiffbeforeafterboth--- a/tests/src/eth/createRFTCollection.test.ts
+++ b/tests/src/eth/createRFTCollection.test.ts
@@ -17,7 +17,8 @@
import {evmToAddress} from '@polkadot/util-crypto';
import {IKeyringPair} from '@polkadot/types/types';
import {Pallets, requirePalletsOrSkip} from '../util';
-import {CollectionLimits, expect, itEth, usingEthPlaygrounds} from './util';
+import {expect, itEth, usingEthPlaygrounds} from './util';
+import { CollectionLimits } from './util/playgrounds/types';
describe('Create RFT collection from EVM', () => {
tests/src/eth/events.test.tsdiffbeforeafterboth--- a/tests/src/eth/events.test.ts
+++ b/tests/src/eth/events.test.ts
@@ -16,10 +16,10 @@
import {expect} from 'chai';
import {IKeyringPair} from '@polkadot/types/types';
-import {CollectionLimits, EthUniqueHelper, itEth, usingEthPlaygrounds} from './util';
+import {EthUniqueHelper, itEth, usingEthPlaygrounds} from './util';
import {IEvent, TCollectionMode} from '../util/playgrounds/types';
import {Pallets, requirePalletsOrSkip} from '../util';
-import {EthTokenPermissions, NormalizedEvent} from './util/playgrounds/types';
+import {CollectionLimits, EthTokenPermissions, NormalizedEvent} from './util/playgrounds/types';
let donor: IKeyringPair;
tests/src/eth/util/index.tsdiffbeforeafterboth--- a/tests/src/eth/util/index.ts
+++ b/tests/src/eth/util/index.ts
@@ -26,18 +26,8 @@
Allowlisted = 1,
Generous = 2,
}
-export enum CollectionLimits {
- AccountTokenOwnership,
- SponsoredDataSize,
- SponsoredDataRateLimit,
- TokenLimit,
- SponsorTransferTimeout,
- SponsorApproveTimeout,
- OwnerCanTransfer,
- OwnerCanDestroy,
- TransferEnabled
-}
+
export const usingEthPlaygrounds = async (code: (helper: EthUniqueHelper, privateKey: (seed: string | {filename: string}) => Promise<IKeyringPair>) => Promise<void>) => {
const silentConsole = new SilentConsole();
silentConsole.enable();
tests/src/eth/util/playgrounds/types.tsdiffbeforeafterboth--- a/tests/src/eth/util/playgrounds/types.ts
+++ b/tests/src/eth/util/playgrounds/types.ts
@@ -24,4 +24,15 @@
Mutable,
TokenOwner,
CollectionAdmin
-}
\ No newline at end of file
+}
+export enum CollectionLimits {
+ AccountTokenOwnership,
+ SponsoredDataSize,
+ SponsoredDataRateLimit,
+ TokenLimit,
+ SponsorTransferTimeout,
+ SponsorApproveTimeout,
+ OwnerCanTransfer,
+ OwnerCanDestroy,
+ TransferEnabled
+}