difftreelog
feat erc nesting tests + some fixes
in: master
5 files changed
pallets/balances-adapter/src/lib.rsdiffbeforeafterboth--- a/pallets/balances-adapter/src/lib.rs
+++ b/pallets/balances-adapter/src/lib.rs
@@ -104,22 +104,20 @@
from: &T::CrossAccountId,
nesting_budget: &dyn Budget,
) -> Result<u128, DispatchError> {
- if spender.conv_eq(from) {
- return Ok(0);
- }
-
- if let Some(source) = T::CrossTokenAddressMapping::address_to_token(from) {
+ if let Some((collection_id, token_id)) =
+ T::CrossTokenAddressMapping::address_to_token(from)
+ {
ensure!(
<PalletStructure<T>>::check_indirectly_owned(
spender.clone(),
- source.0,
- source.1,
+ collection_id,
+ token_id,
None,
nesting_budget
)?,
<CommonError<T>>::ApprovedValueTooLow,
);
- } else if spender != from {
+ } else if !spender.conv_eq(from) {
return Ok(0);
}
@@ -183,7 +181,7 @@
amount: u128,
nesting_budget: &dyn Budget,
) -> DispatchResultWithPostInfo {
- let allowance = Self::check_allowed(collection, spender, from, amount, nesting_budget)?;
+ let allowance = Self::check_allowed(spender, from, nesting_budget)?;
if allowance < amount {
return Err(<CommonError<T>>::ApprovedValueTooLow.into());
}
pallets/fungible/src/erc.rsdiffbeforeafterboth--- a/pallets/fungible/src/erc.rs
+++ b/pallets/fungible/src/erc.rs
@@ -109,7 +109,8 @@
.recorder
.weight_calls_budget(<StructureWeight<T>>::find_parent());
- <Pallet<T>>::transfer(self, &caller, &to, amount, &budget).map_err(|_| "transfer error")?;
+ <Pallet<T>>::transfer(self, &caller, &to, amount, &budget)
+ .map_err(|e| dispatch_to_evm::<T>(e.error))?;
Ok(true)
}
tests/src/eth/nesting/nest.test.tsdiffbeforeafterboth--- a/tests/src/eth/nesting/nest.test.ts
+++ b/tests/src/eth/nesting/nest.test.ts
@@ -187,4 +187,80 @@
.call()).to.be.rejectedWith('SourceCollectionIsNotAllowedToNest');
});
});
+
+ describe('Fungible', () => {
+ async function createFungibleCollection(helper: EthUniqueHelper, owner: string, mode: 'ft' | 'native ft') {
+ if (mode === 'ft') {
+ const {collectionAddress} = await helper.eth.createFungibleCollection(owner, '', 18, '', '');
+ const contract = await helper.ethNativeContract.collection(collectionAddress, 'ft', owner);
+ await contract.methods.mint(owner, 100n).send({from: owner});
+ return {collectionAddress, contract};
+ }
+
+ // native ft
+ const collectionAddress = helper.ethAddress.fromCollectionId(0);
+ const contract = await helper.ethNativeContract.collection(collectionAddress, 'ft', owner);
+ return {collectionAddress, contract};
+ }
+
+ [
+ {mode: 'ft' as const},
+ {mode: 'native ft' as const},
+ ].map(testCase => {
+ itEth(`Allow nest [${testCase.mode}]`, async ({helper}) => {
+ const owner = await helper.eth.createAccountWithBalance(donor);
+ const {collectionId: targetCollectionId, contract: targetContract} = await createNestingCollection(helper, owner);
+ const {contract: ftContract} = await createFungibleCollection(helper, owner, testCase.mode);
+
+ const mintingTargetTokenIdResult = await targetContract.methods.mint(owner).send({from: owner});
+ const targetTokenId = mintingTargetTokenIdResult.events.Transfer.returnValues.tokenId;
+ const targetTokenAddress = helper.ethAddress.fromTokenId(targetCollectionId, targetTokenId);
+
+ await ftContract.methods.transfer(targetTokenAddress, 10n).send({from: owner});
+ expect(await ftContract.methods.balanceOf(targetTokenAddress).call({from: owner})).to.be.equal('10');
+ });
+ });
+
+ [
+ {mode: 'ft' as const},
+ {mode: 'native ft' as const},
+ ].map(testCase => {
+ itEth(`Allow partial/full unnest [${testCase.mode}]`, async ({helper}) => {
+ const owner = await helper.eth.createAccountWithBalance(donor);
+ const {collectionId: targetCollectionId, contract: targetContract} = await createNestingCollection(helper, owner);
+ const {contract: ftContract} = await createFungibleCollection(helper, owner, testCase.mode);
+
+ const mintingTargetTokenIdResult = await targetContract.methods.mint(owner).send({from: owner});
+ const targetTokenId = mintingTargetTokenIdResult.events.Transfer.returnValues.tokenId;
+ const targetTokenAddress = helper.ethAddress.fromTokenId(targetCollectionId, targetTokenId);
+
+ await ftContract.methods.transfer(targetTokenAddress, 10n).send({from: owner});
+
+ await ftContract.methods.transferFrom(targetTokenAddress, owner, 5n).send({from: owner});
+ expect(await ftContract.methods.balanceOf(targetTokenAddress).call({from: owner})).to.be.equal('5');
+
+ await ftContract.methods.transferFrom(targetTokenAddress, owner, 5n).send({from: owner});
+ expect(await ftContract.methods.balanceOf(targetTokenAddress).call({from: owner})).to.be.equal('0');
+ });
+ });
+
+ [
+ {mode: 'ft' as const},
+ {mode: 'native ft' as const},
+ ].map(testCase => {
+ itEth(`Disallow nest into collection without nesting permission [${testCase.mode}]`, async ({helper}) => {
+ const owner = await helper.eth.createAccountWithBalance(donor);
+ const {collectionId: targetCollectionId, contract: targetContract} = await createNestingCollection(helper, owner);
+ await targetContract.methods.setCollectionNesting(false).send({from: owner});
+
+ const {contract: ftContract} = await createFungibleCollection(helper, owner, testCase.mode);
+
+ const mintingTargetTokenIdResult = await targetContract.methods.mint(owner).send({from: owner});
+ const targetTokenId = mintingTargetTokenIdResult.events.Transfer.returnValues.tokenId;
+ const targetTokenAddress = helper.ethAddress.fromTokenId(targetCollectionId, targetTokenId);
+
+ await expect(ftContract.methods.transfer(targetTokenAddress, 10n).call({from: owner})).to.be.rejectedWith('UserIsNotAllowedToNest');
+ });
+ });
+ });
});
tests/src/nativeFungible.test.tsdiffbeforeafterboth--- a/tests/src/nativeFungible.test.ts
+++ b/tests/src/nativeFungible.test.ts
@@ -207,7 +207,7 @@
)).to.be.rejectedWith('BadOrigin');
});
- itSub.only('Nest into NFT token()', async ({helper}) => {
+ itSub('Nest into NFT token()', async ({helper}) => {
const nftCollection = await helper.nft.mintCollection(alice, {permissions: {nesting: {tokenOwner: true}}});
const targetToken = await nftCollection.mintToken(alice);
tests/src/sub/nesting/unnesting.negative.test.tsdiffbeforeafterboth1// Copyright 2019-2022 Unique Network (Gibraltar) Ltd.2// This file is part of Unique Network.34// Unique Network is free software: you can redistribute it and/or modify5// it under the terms of the GNU General Public License as published by6// the Free Software Foundation, either version 3 of the License, or7// (at your option) any later version.89// Unique Network is distributed in the hope that it will be useful,10// but WITHOUT ANY WARRANTY; without even the implied warranty of11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the12// GNU General Public License for more details.1314// You should have received a copy of the GNU General Public License15// along with Unique Network. If not, see <http://www.gnu.org/licenses/>.1617import {IKeyringPair} from '@polkadot/types/types';18import {expect, itSub, usingPlaygrounds} from '../../util';1920describe('Negative Test: Unnesting', () => {21 let alice: IKeyringPair;22 let bob: IKeyringPair;2324 before(async () => {25 await usingPlaygrounds(async (helper, privateKey) => {26 const donor = await privateKey({url: import.meta.url});27 [alice, bob] = await helper.arrange.createAccounts([100n, 50n], donor);28 });29 });3031 // TODO: make this test a bit more generic32 itSub('Admin (NFT): disallows an Admin to unnest someone else\'s token', async ({helper}) => {33 const collection = await helper.nft.mintCollection(alice, {limits: {ownerCanTransfer: true}, permissions: {access: 'AllowList', mintMode: true, nesting: {collectionAdmin: true}}});34 //await collection.addAdmin(alice, {Substrate: bob.address});35 const targetToken = await collection.mintToken(alice, {Substrate: bob.address});36 await collection.addToAllowList(alice, {Substrate: bob.address});37 await collection.addToAllowList(alice, targetToken.nestingAccount());3839 // Try to nest somebody else's token40 const newToken = await collection.mintToken(bob);41 await expect(newToken.nest(alice, targetToken))42 .to.be.rejectedWith(/common\.NoPermission/);4344 // Try to unnest a token belonging to someone else as collection admin45 const nestedToken = await collection.mintToken(alice, targetToken.nestingAccount());46 await expect(nestedToken.unnest(alice, targetToken, {Substrate: bob.address}))47 .to.be.rejectedWith(/common\.AddressNotInAllowlist/);4849 expect(await targetToken.getChildren()).to.be.length(1);50 expect(await nestedToken.getTopmostOwner()).to.be.deep.equal({Substrate: bob.address});51 expect(await nestedToken.getOwner()).to.be.deep.equal(targetToken.nestingAccount().toLowerCase());52 });5354 [55 {mode: 'ft' as const},56 {mode: 'native ft' as const},57 ].map(md => [58 {mode: md.mode, restrictedMode: true},59 {mode: md.mode, restrictedMode: false},60 ].map(testCase => {61 itSub.only(`Fungible: disallows a non-Owner to unnest someone else's token [${testCase.mode}${testCase.restrictedMode' (Restricted nesting)'''}]`, async ({helper}) => {62 const collectionNFT = await helper.nft.mintCollection(alice);63 const collectionFT = await (64 testCase.mode === 'ft'65 ? helper.ft.mintCollection(alice)66 : helper.ft.getCollectionObject(0)67 );68 const targetToken = await collectionNFT.mintToken(alice, {Substrate: bob.address});6970 await collectionNFT.setPermissions(alice, {nesting: {71 collectionAdmin: true, tokenOwner: true, restricted: testCase.restrictedMode ? [collectionFT.collectionId] : null,72 }});7374 // Nest some tokens as Alice into Bob's token75 await (76 testCase.mode === 'ft'77 ? collectionFT.mint(alice, 5n, targetToken.nestingAccount())78 : collectionFT.transfer(alice, targetToken.nestingAccount(), 5n)79 );8081 // Try to pull it out as Alice still82 await expect(collectionFT.transferFrom(alice, targetToken.nestingAccount(), {Substrate: bob.address}, 1n))83 .to.be.rejectedWith(/common\.ApprovedValueTooLow/);84 expect(await collectionFT.getBalance(targetToken.nestingAccount())).to.be.equal(5n);85 });86 }));87});1// Copyright 2019-2022 Unique Network (Gibraltar) Ltd.2// This file is part of Unique Network.34// Unique Network is free software: you can redistribute it and/or modify5// it under the terms of the GNU General Public License as published by6// the Free Software Foundation, either version 3 of the License, or7// (at your option) any later version.89// Unique Network is distributed in the hope that it will be useful,10// but WITHOUT ANY WARRANTY; without even the implied warranty of11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the12// GNU General Public License for more details.1314// You should have received a copy of the GNU General Public License15// along with Unique Network. If not, see <http://www.gnu.org/licenses/>.1617import {IKeyringPair} from '@polkadot/types/types';18import {expect, itSub, usingPlaygrounds} from '../../util';1920describe('Negative Test: Unnesting', () => {21 let alice: IKeyringPair;22 let bob: IKeyringPair;2324 before(async () => {25 await usingPlaygrounds(async (helper, privateKey) => {26 const donor = await privateKey({url: import.meta.url});27 [alice, bob] = await helper.arrange.createAccounts([100n, 50n], donor);28 });29 });3031 // TODO: make this test a bit more generic32 itSub('Admin (NFT): disallows an Admin to unnest someone else\'s token', async ({helper}) => {33 const collection = await helper.nft.mintCollection(alice, {limits: {ownerCanTransfer: true}, permissions: {access: 'AllowList', mintMode: true, nesting: {collectionAdmin: true}}});34 //await collection.addAdmin(alice, {Substrate: bob.address});35 const targetToken = await collection.mintToken(alice, {Substrate: bob.address});36 await collection.addToAllowList(alice, {Substrate: bob.address});37 await collection.addToAllowList(alice, targetToken.nestingAccount());3839 // Try to nest somebody else's token40 const newToken = await collection.mintToken(bob);41 await expect(newToken.nest(alice, targetToken))42 .to.be.rejectedWith(/common\.NoPermission/);4344 // Try to unnest a token belonging to someone else as collection admin45 const nestedToken = await collection.mintToken(alice, targetToken.nestingAccount());46 await expect(nestedToken.unnest(alice, targetToken, {Substrate: bob.address}))47 .to.be.rejectedWith(/common\.AddressNotInAllowlist/);4849 expect(await targetToken.getChildren()).to.be.length(1);50 expect(await nestedToken.getTopmostOwner()).to.be.deep.equal({Substrate: bob.address});51 expect(await nestedToken.getOwner()).to.be.deep.equal(targetToken.nestingAccount().toLowerCase());52 });5354 [55 {mode: 'ft' as const},56 {mode: 'native ft' as const},57 ].map(md => [58 {mode: md.mode, restrictedMode: true},59 {mode: md.mode, restrictedMode: false},60 ].map(testCase => {61 itSub(`Fungible: disallows a non-Owner to unnest someone else's token [${testCase.mode}${testCase.restrictedMode' (Restricted nesting)'''}]`, async ({helper}) => {62 const collectionNFT = await helper.nft.mintCollection(alice);63 const collectionFT = await (64 testCase.mode === 'ft'65 ? helper.ft.mintCollection(alice)66 : helper.ft.getCollectionObject(0)67 );68 const targetToken = await collectionNFT.mintToken(alice, {Substrate: bob.address});6970 await collectionNFT.setPermissions(alice, {nesting: {71 collectionAdmin: true, tokenOwner: true, restricted: testCase.restrictedMode ? [collectionFT.collectionId] : null,72 }});7374 // Nest some tokens as Alice into Bob's token75 await (76 testCase.mode === 'ft'77 ? collectionFT.mint(alice, 5n, targetToken.nestingAccount())78 : collectionFT.transfer(alice, targetToken.nestingAccount(), 5n)79 );8081 // Try to pull it out as Alice still82 await expect(collectionFT.transferFrom(alice, targetToken.nestingAccount(), {Substrate: bob.address}, 1n))83 .to.be.rejectedWith(/common\.ApprovedValueTooLow/);84 expect(await collectionFT.getBalance(targetToken.nestingAccount())).to.be.equal(5n);85 });86 }));87});