Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: account token ownership limit when transfer #1080

Merged
merged 8 commits into from
Jun 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 48 additions & 2 deletions js-packages/tests/limits.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,12 @@ import {expect, itSub, Pallets, requirePalletsOrSkip, usingPlaygrounds} from '@u

describe('Number of tokens per address (NFT)', () => {
let alice: IKeyringPair;
let bob: IKeyringPair;

before(async () => {
await usingPlaygrounds(async (helper, privateKey) => {
const donor = await privateKey({url: import.meta.url});
[alice] = await helper.arrange.createAccounts([10n], donor);
[alice, bob] = await helper.arrange.createAccounts([10n, 0n], donor);
});
});

Expand Down Expand Up @@ -51,17 +52,40 @@ describe('Number of tokens per address (NFT)', () => {
await collection.burnToken(alice, 1);
await expect(collection.burn(alice)).to.be.not.rejected;
});

itSub('Can transfer tokens to address equal to accountTokenOwnershipLimit', async ({helper}) => {
const collection = await helper.nft.mintCollection(alice, {});
await collection.setLimits(alice, {accountTokenOwnershipLimit: 1});

// Limit 1 - can transfer #1 token
const tkn1 = await collection.mintToken(alice);
await collection.transferToken(alice, tkn1.tokenId, {Substrate: bob.address});

// Limit 1 - cannot transfer #2 token
const tkn2 = await collection.mintToken(alice);
await expect(collection.transferToken(alice, tkn2.tokenId, {Substrate: bob.address})).to.be.rejectedWith(/common\.AccountTokenLimitExceeded/);

// Increase limit to 2
// Now can transfer token #2
await collection.setLimits(alice, {accountTokenOwnershipLimit: 2});
await collection.transferToken(alice, tkn2.tokenId, {Substrate: bob.address});

// But cannot transfer token #3
const tkn3 = await collection.mintToken(alice);
await expect(collection.transferToken(alice, tkn3.tokenId, {Substrate: bob.address})).to.be.rejectedWith(/common\.AccountTokenLimitExceeded/);
});
});

describe('Number of tokens per address (ReFungible)', () => {
let alice: IKeyringPair;
let bob: IKeyringPair;

before(async function() {
await usingPlaygrounds(async (helper, privateKey) => {
requirePalletsOrSkip(this, helper, [Pallets.ReFungible]);

const donor = await privateKey({url: import.meta.url});
[alice] = await helper.arrange.createAccounts([10n], donor);
[alice, bob] = await helper.arrange.createAccounts([10n, 0n], donor);
});
});

Expand Down Expand Up @@ -89,6 +113,28 @@ describe('Number of tokens per address (ReFungible)', () => {
await collection.burnToken(alice, 1);
await expect(collection.burn(alice)).to.be.not.rejected;
});

itSub('Can transfer tokens to address equal to accountTokenOwnershipLimit', async ({helper}) => {
const collection = await helper.rft.mintCollection(alice, {});
await collection.setLimits(alice, {accountTokenOwnershipLimit: 1});

// Limit 1 - can transfer #1 token
const tkn1 = await collection.mintToken(alice);
await collection.transferToken(alice, tkn1.tokenId, {Substrate: bob.address});

// Limit 1 - cannot transfer #2 token
const tkn2 = await collection.mintToken(alice);
await expect(collection.transferToken(alice, tkn2.tokenId, {Substrate: bob.address})).to.be.rejectedWith(/common\.AccountTokenLimitExceeded/);

// Increase limit to 2
// Now can transfer token #2
await collection.setLimits(alice, {accountTokenOwnershipLimit: 2});
await collection.transferToken(alice, tkn2.tokenId, {Substrate: bob.address});

// But cannot transfer token #3
const tkn3 = await collection.mintToken(alice);
await expect(collection.transferToken(alice, tkn3.tokenId, {Substrate: bob.address})).to.be.rejectedWith(/common\.AccountTokenLimitExceeded/);
});
});

// todo:playgrounds skipped ~ postponed
Expand Down
2 changes: 1 addition & 1 deletion pallets/nonfungible/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -746,7 +746,7 @@ impl<T: Config> Pallet<T> {
.ok_or(ArithmeticError::Overflow)?;

ensure!(
balance_to < collection.limits.account_token_ownership_limit(),
balance_to <= collection.limits.account_token_ownership_limit(),
<CommonError<T>>::AccountTokenLimitExceeded,
);

Expand Down
2 changes: 1 addition & 1 deletion pallets/refungible/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -679,7 +679,7 @@ impl<T: Config> Pallet<T> {
.checked_add(1)
.ok_or(ArithmeticError::Overflow)?;
ensure!(
account_balance_to < collection.limits.account_token_ownership_limit(),
account_balance_to <= collection.limits.account_token_ownership_limit(),
<CommonError<T>>::AccountTokenLimitExceeded,
);

Expand Down
Loading