git.delta.rocks / unique-network / refs/commits / 008dfe41f9df

difftreelog

feat erc nesting tests + some fixes

Trubnikov Sergey2023-05-10parent: #2dd0a6a.patch.diff
in: master

5 files changed

modifiedpallets/balances-adapter/src/lib.rsdiffbeforeafterboth
104 from: &T::CrossAccountId,104 from: &T::CrossAccountId,
105 nesting_budget: &dyn Budget,105 nesting_budget: &dyn Budget,
106 ) -> Result<u128, DispatchError> {106 ) -> Result<u128, DispatchError> {
107 if spender.conv_eq(from) {
108 return Ok(0);
109 }
110
111 if let Some(source) = T::CrossTokenAddressMapping::address_to_token(from) {107 if let Some((collection_id, token_id)) =
108 T::CrossTokenAddressMapping::address_to_token(from)
109 {
112 ensure!(110 ensure!(
113 <PalletStructure<T>>::check_indirectly_owned(111 <PalletStructure<T>>::check_indirectly_owned(
114 spender.clone(),112 spender.clone(),
115 source.0,113 collection_id,
116 source.1,114 token_id,
117 None,115 None,
118 nesting_budget116 nesting_budget
119 )?,117 )?,
120 <CommonError<T>>::ApprovedValueTooLow,118 <CommonError<T>>::ApprovedValueTooLow,
121 );119 );
122 } else if spender != from {120 } else if !spender.conv_eq(from) {
123 return Ok(0);121 return Ok(0);
124 }122 }
125123
183 amount: u128,181 amount: u128,
184 nesting_budget: &dyn Budget,182 nesting_budget: &dyn Budget,
185 ) -> DispatchResultWithPostInfo {183 ) -> DispatchResultWithPostInfo {
186 let allowance = Self::check_allowed(collection, spender, from, amount, nesting_budget)?;184 let allowance = Self::check_allowed(spender, from, nesting_budget)?;
187 if allowance < amount {185 if allowance < amount {
188 return Err(<CommonError<T>>::ApprovedValueTooLow.into());186 return Err(<CommonError<T>>::ApprovedValueTooLow.into());
189 }187 }
modifiedpallets/fungible/src/erc.rsdiffbeforeafterboth
109 .recorder109 .recorder
110 .weight_calls_budget(<StructureWeight<T>>::find_parent());110 .weight_calls_budget(<StructureWeight<T>>::find_parent());
111111
112 <Pallet<T>>::transfer(self, &caller, &to, amount, &budget).map_err(|_| "transfer error")?;112 <Pallet<T>>::transfer(self, &caller, &to, amount, &budget)
113 .map_err(|e| dispatch_to_evm::<T>(e.error))?;
113 Ok(true)114 Ok(true)
114 }115 }
115116
modifiedtests/src/eth/nesting/nest.test.tsdiffbeforeafterboth
188 });188 });
189 });189 });
190
191 describe('Fungible', () => {
192 async function createFungibleCollection(helper: EthUniqueHelper, owner: string, mode: 'ft' | 'native ft') {
193 if (mode === 'ft') {
194 const {collectionAddress} = await helper.eth.createFungibleCollection(owner, '', 18, '', '');
195 const contract = await helper.ethNativeContract.collection(collectionAddress, 'ft', owner);
196 await contract.methods.mint(owner, 100n).send({from: owner});
197 return {collectionAddress, contract};
198 }
199
200 // native ft
201 const collectionAddress = helper.ethAddress.fromCollectionId(0);
202 const contract = await helper.ethNativeContract.collection(collectionAddress, 'ft', owner);
203 return {collectionAddress, contract};
204 }
205
206 [
207 {mode: 'ft' as const},
208 {mode: 'native ft' as const},
209 ].map(testCase => {
210 itEth(`Allow nest [${testCase.mode}]`, async ({helper}) => {
211 const owner = await helper.eth.createAccountWithBalance(donor);
212 const {collectionId: targetCollectionId, contract: targetContract} = await createNestingCollection(helper, owner);
213 const {contract: ftContract} = await createFungibleCollection(helper, owner, testCase.mode);
214
215 const mintingTargetTokenIdResult = await targetContract.methods.mint(owner).send({from: owner});
216 const targetTokenId = mintingTargetTokenIdResult.events.Transfer.returnValues.tokenId;
217 const targetTokenAddress = helper.ethAddress.fromTokenId(targetCollectionId, targetTokenId);
218
219 await ftContract.methods.transfer(targetTokenAddress, 10n).send({from: owner});
220 expect(await ftContract.methods.balanceOf(targetTokenAddress).call({from: owner})).to.be.equal('10');
221 });
222 });
223
224 [
225 {mode: 'ft' as const},
226 {mode: 'native ft' as const},
227 ].map(testCase => {
228 itEth(`Allow partial/full unnest [${testCase.mode}]`, async ({helper}) => {
229 const owner = await helper.eth.createAccountWithBalance(donor);
230 const {collectionId: targetCollectionId, contract: targetContract} = await createNestingCollection(helper, owner);
231 const {contract: ftContract} = await createFungibleCollection(helper, owner, testCase.mode);
232
233 const mintingTargetTokenIdResult = await targetContract.methods.mint(owner).send({from: owner});
234 const targetTokenId = mintingTargetTokenIdResult.events.Transfer.returnValues.tokenId;
235 const targetTokenAddress = helper.ethAddress.fromTokenId(targetCollectionId, targetTokenId);
236
237 await ftContract.methods.transfer(targetTokenAddress, 10n).send({from: owner});
238
239 await ftContract.methods.transferFrom(targetTokenAddress, owner, 5n).send({from: owner});
240 expect(await ftContract.methods.balanceOf(targetTokenAddress).call({from: owner})).to.be.equal('5');
241
242 await ftContract.methods.transferFrom(targetTokenAddress, owner, 5n).send({from: owner});
243 expect(await ftContract.methods.balanceOf(targetTokenAddress).call({from: owner})).to.be.equal('0');
244 });
245 });
246
247 [
248 {mode: 'ft' as const},
249 {mode: 'native ft' as const},
250 ].map(testCase => {
251 itEth(`Disallow nest into collection without nesting permission [${testCase.mode}]`, async ({helper}) => {
252 const owner = await helper.eth.createAccountWithBalance(donor);
253 const {collectionId: targetCollectionId, contract: targetContract} = await createNestingCollection(helper, owner);
254 await targetContract.methods.setCollectionNesting(false).send({from: owner});
255
256 const {contract: ftContract} = await createFungibleCollection(helper, owner, testCase.mode);
257
258 const mintingTargetTokenIdResult = await targetContract.methods.mint(owner).send({from: owner});
259 const targetTokenId = mintingTargetTokenIdResult.events.Transfer.returnValues.tokenId;
260 const targetTokenAddress = helper.ethAddress.fromTokenId(targetCollectionId, targetTokenId);
261
262 await expect(ftContract.methods.transfer(targetTokenAddress, 10n).call({from: owner})).to.be.rejectedWith('UserIsNotAllowedToNest');
263 });
264 });
265 });
190});266});
191267
modifiedtests/src/nativeFungible.test.tsdiffbeforeafterboth
207 )).to.be.rejectedWith('BadOrigin');207 )).to.be.rejectedWith('BadOrigin');
208 });208 });
209209
210 itSub.only('Nest into NFT token()', async ({helper}) => {210 itSub('Nest into NFT token()', async ({helper}) => {
211 const nftCollection = await helper.nft.mintCollection(alice, {permissions: {nesting: {tokenOwner: true}}});211 const nftCollection = await helper.nft.mintCollection(alice, {permissions: {nesting: {tokenOwner: true}}});
212 const targetToken = await nftCollection.mintToken(alice);212 const targetToken = await nftCollection.mintToken(alice);
213213
modifiedtests/src/sub/nesting/unnesting.negative.test.tsdiffbeforeafterboth
58 {mode: md.mode, restrictedMode: true},58 {mode: md.mode, restrictedMode: true},
59 {mode: md.mode, restrictedMode: false},59 {mode: md.mode, restrictedMode: false},
60 ].map(testCase => {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}) => {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);62 const collectionNFT = await helper.nft.mintCollection(alice);
63 const collectionFT = await (63 const collectionFT = await (
64 testCase.mode === 'ft'64 testCase.mode === 'ft'