git.delta.rocks / unique-network / refs/commits / 44992fca8f8c

difftreelog

feat burn_from

Yaroslav Bolyukin2021-11-02parent: #283713e.patch.diff
in: master

12 files changed

modifiedpallets/common/src/lib.rsdiffbeforeafterboth
459 fn transfer() -> Weight;459 fn transfer() -> Weight;
460 fn approve() -> Weight;460 fn approve() -> Weight;
461 fn transfer_from() -> Weight;461 fn transfer_from() -> Weight;
462 fn burn_from() -> Weight;
462 fn set_variable_metadata(bytes: u32) -> Weight;463 fn set_variable_metadata(bytes: u32) -> Weight;
463}464}
464465
504 token: TokenId,505 token: TokenId,
505 amount: u128,506 amount: u128,
506 ) -> DispatchResultWithPostInfo;507 ) -> DispatchResultWithPostInfo;
508 fn burn_from(
509 &self,
510 sender: T::CrossAccountId,
511 from: T::CrossAccountId,
512 token: TokenId,
513 amount: u128,
514 ) -> DispatchResultWithPostInfo;
507515
508 fn set_variable_metadata(516 fn set_variable_metadata(
509 &self,517 &self,
modifiedpallets/fungible/src/common.rsdiffbeforeafterboth
38 <SelfWeightOf<T>>::transfer_from()38 <SelfWeightOf<T>>::transfer_from()
39 }39 }
40
41 fn burn_from() -> Weight {
42 0
43 }
4044
41 fn set_variable_metadata(_bytes: u32) -> Weight {45 fn set_variable_metadata(_bytes: u32) -> Weight {
42 // Error46 // Error
156 )160 )
157 }161 }
162
163 fn burn_from(
164 &self,
165 sender: T::CrossAccountId,
166 from: T::CrossAccountId,
167 token: TokenId,
168 amount: u128,
169 ) -> DispatchResultWithPostInfo {
170 ensure!(
171 token == TokenId::default(),
172 <Error<T>>::FungibleItemsHaveNoId
173 );
174
175 with_weight(
176 <Pallet<T>>::burn_from(&self, &sender, &from, amount),
177 <CommonWeights<T>>::burn_from(),
178 )
179 }
158180
159 fn set_variable_metadata(181 fn set_variable_metadata(
160 &self,182 &self,
modifiedpallets/fungible/src/erc.rsdiffbeforeafterboth
96 }96 }
97}97}
98
99#[solidity_interface(name = "ERC20UniqueExtensions")]
100impl<T: Config> FungibleHandle<T> {
101 fn burn_from(&mut self, caller: caller, from: address, amount: uint256) -> Result<bool> {
102 let caller = T::CrossAccountId::from_eth(caller);
103 let from = T::CrossAccountId::from_eth(from);
104 let amount = amount.try_into().map_err(|_| "amount overflow")?;
105
106 <Pallet<T>>::burn_from(self, &caller, &from, amount).map_err(dispatch_to_evm::<T>)?;
107 Ok(true)
108 }
109}
98110
99#[solidity_interface(name = "UniqueFungible", is(ERC20))]111#[solidity_interface(name = "UniqueFungible", is(ERC20))]
100impl<T: Config> FungibleHandle<T> {}112impl<T: Config> FungibleHandle<T> {}
modifiedpallets/fungible/src/lib.rsdiffbeforeafterboth
356 Ok(())356 Ok(())
357 }357 }
358
359 pub fn burn_from(
360 collection: &FungibleHandle<T>,
361 spender: &T::CrossAccountId,
362 from: &T::CrossAccountId,
363 amount: u128,
364 ) -> DispatchResult {
365 if spender == from {
366 return Self::burn(collection, from, amount);
367 }
368 if collection.access == AccessMode::WhiteList {
369 // `from` checked in [`burn`]
370 collection.check_allowlist(spender)?;
371 }
372
373 let allowance = <Allowance<T>>::get((collection.id, from.as_sub(), spender.as_sub()))
374 .checked_sub(amount);
375 if allowance.is_none() {
376 ensure!(
377 collection.ignores_allowance(spender)?,
378 <CommonError<T>>::TokenValueNotEnough
379 );
380 }
381
382 // =========
383
384 Self::burn(collection, from, amount)?;
385 if let Some(allowance) = allowance {
386 Self::set_allowance_unchecked(collection, from, spender, allowance);
387 }
388 Ok(())
389 }
358390
359 /// Delegated to `create_multiple_items`391 /// Delegated to `create_multiple_items`
360 pub fn create_item(392 pub fn create_item(
modifiedpallets/nft/src/common.rsdiffbeforeafterboth
46 dispatch_weight::<T>() + max_weight_of!(set_variable_metadata(bytes))46 dispatch_weight::<T>() + max_weight_of!(set_variable_metadata(bytes))
47 }47 }
48
49 fn burn_from() -> Weight {
50 dispatch_weight::<T>() + max_weight_of!(burn_from())
51 }
48}52}
4953
modifiedpallets/nft/src/eth/sponsoring.rsdiffbeforeafterboth
35 .map_err(|_| AnyError)?35 .map_err(|_| AnyError)?
36 .ok_or(AnyError)?;36 .ok_or(AnyError)?;
37 match call {37 match call {
38 UniqueNFTCall::ERC721UniqueExtensions(38 UniqueNFTCall::ERC721UniqueExtensions(ERC721UniqueExtensionsCall::Transfer {
39 ERC721UniqueExtensionsCall::TransferNft { token_id, .. },39 token_id,
40 ..
40 )41 })
41 | UniqueNFTCall::ERC721(ERC721Call::TransferFrom { token_id, .. }) => {42 | UniqueNFTCall::ERC721(ERC721Call::TransferFrom { token_id, .. }) => {
modifiedpallets/nft/src/lib.rsdiffbeforeafterboth
623 /// * item_id: ID of NFT to burn.623 /// * item_id: ID of NFT to burn.
624 ///624 ///
625 /// * from: owner of item625 /// * from: owner of item
626 // #[weight = 0]626 #[weight = <CommonWeights<T>>::burn_from()]
627 // #[transactional]627 #[transactional]
628 // pub fn burn_from(origin, collection_id: CollectionId, from: T::CrossAccountId, item_id: TokenId, value: u128) -> PostDispatchInfo {628 pub fn burn_from(origin, collection_id: CollectionId, from: T::CrossAccountId, item_id: TokenId, value: u128) -> DispatchResultWithPostInfo {
629 // let sender = T::CrossAccountId::from_sub(ensure_signed(origin)?);629 let sender = T::CrossAccountId::from_sub(ensure_signed(origin)?);
630630
631 // // dispatch_call::<T, _>(collection_id, |d| d.burn_from(sender, from, item_id, value))631 dispatch_call::<T, _>(collection_id, |d| d.burn_from(sender, from, item_id, value))
632 // todo!()632 }
633 // }
634633
635 /// Change ownership of the token.634 /// Change ownership of the token.
636 ///635 ///
modifiedpallets/nonfungible/src/common.rsdiffbeforeafterboth
39 <SelfWeightOf<T>>::transfer_from()39 <SelfWeightOf<T>>::transfer_from()
40 }40 }
41
42 fn burn_from() -> Weight {
43 0
44 }
4145
42 fn set_variable_metadata(bytes: u32) -> Weight {46 fn set_variable_metadata(bytes: u32) -> Weight {
43 <SelfWeightOf<T>>::set_variable_metadata(bytes)47 <SelfWeightOf<T>>::set_variable_metadata(bytes)
163 }167 }
164 }168 }
169
170 fn burn_from(
171 &self,
172 sender: T::CrossAccountId,
173 from: T::CrossAccountId,
174 token: TokenId,
175 amount: u128,
176 ) -> DispatchResultWithPostInfo {
177 ensure!(amount <= 1, <Error<T>>::NonfungibleItemsHaveNoAmount);
178
179 if amount == 1 {
180 with_weight(
181 <Pallet<T>>::burn_from(&self, &sender, &from, token),
182 <CommonWeights<T>>::burn_from(),
183 )
184 } else {
185 Ok(().into())
186 }
187 }
165188
166 fn set_variable_metadata(189 fn set_variable_metadata(
167 &self,190 &self,
modifiedpallets/nonfungible/src/erc.rsdiffbeforeafterboth
264264
265#[solidity_interface(name = "ERC721UniqueExtensions")]265#[solidity_interface(name = "ERC721UniqueExtensions")]
266impl<T: Config> NonfungibleHandle<T> {266impl<T: Config> NonfungibleHandle<T> {
267 #[solidity(rename_selector = "transfer")]
268 fn transfer_nft(267 fn transfer(
269 &mut self,268 &mut self,
270 caller: caller,269 caller: caller,
271 to: address,270 to: address,
280 Ok(())279 Ok(())
281 }280 }
281
282 fn burn_from(
283 &mut self,
284 caller: caller,
285 from: address,
286 token_id: uint256,
287 _value: value,
288 ) -> Result<void> {
289 let caller = T::CrossAccountId::from_eth(caller);
290 let from = T::CrossAccountId::from_eth(from);
291 let token = token_id.try_into()?;
292
293 <Pallet<T>>::burn_from(self, &caller, &from, token).map_err(dispatch_to_evm::<T>)?;
294 Ok(())
295 }
282296
283 fn next_token_id(&self) -> Result<uint256> {297 fn next_token_id(&self) -> Result<uint256> {
284 Ok(<TokensMinted<T>>::get(self.id)298 Ok(<TokensMinted<T>>::get(self.id)
modifiedpallets/nonfungible/src/lib.rsdiffbeforeafterboth
499 Ok(())499 Ok(())
500 }500 }
501
502 pub fn burn_from(
503 collection: &NonfungibleHandle<T>,
504 spender: &T::CrossAccountId,
505 from: &T::CrossAccountId,
506 token: TokenId,
507 ) -> DispatchResult {
508 if spender == from {
509 return Self::burn(collection, from, token);
510 }
511 if collection.access == AccessMode::WhiteList {
512 // `from` checked in [`burn`]
513 collection.check_allowlist(spender)?;
514 }
515
516 if <Allowance<T>>::get((collection.id, token)).as_ref() != Some(spender) {
517 ensure!(
518 collection.ignores_allowance(spender)?,
519 <CommonError<T>>::TokenValueNotEnough
520 );
521 }
522
523 // =========
524
525 Self::burn(collection, &from, token)
526 }
501527
502 pub fn set_variable_metadata(528 pub fn set_variable_metadata(
503 collection: &NonfungibleHandle<T>,529 collection: &NonfungibleHandle<T>,
modifiedpallets/refungible/src/common.rsdiffbeforeafterboth
59 )59 )
60 }60 }
61
62 fn burn_from() -> Weight {
63 0
64 }
6165
62 fn set_variable_metadata(bytes: u32) -> Weight {66 fn set_variable_metadata(bytes: u32) -> Weight {
63 <SelfWeightOf<T>>::set_variable_metadata(bytes)67 <SelfWeightOf<T>>::set_variable_metadata(bytes)
161 ) -> DispatchResultWithPostInfo {165 ) -> DispatchResultWithPostInfo {
162 with_weight(166 with_weight(
163 <Pallet<T>>::transfer_from(&self, &sender, &from, &to, token, amount),167 <Pallet<T>>::transfer_from(&self, &sender, &from, &to, token, amount),
164 <CommonWeights<T>>::approve(),168 <CommonWeights<T>>::transfer_from(),
165 )169 )
166 }170 }
171
172 fn burn_from(
173 &self,
174 sender: T::CrossAccountId,
175 from: T::CrossAccountId,
176 token: TokenId,
177 amount: u128,
178 ) -> DispatchResultWithPostInfo {
179 with_weight(
180 <Pallet<T>>::burn_from(&self, &sender, &from, token, amount),
181 <CommonWeights<T>>::burn_from(),
182 )
183 }
167184
168 fn set_variable_metadata(185 fn set_variable_metadata(
169 &self,186 &self,
modifiedpallets/refungible/src/lib.rsdiffbeforeafterboth
537 Ok(())537 Ok(())
538 }538 }
539
540 pub fn burn_from(
541 collection: &RefungibleHandle<T>,
542 spender: &T::CrossAccountId,
543 from: &T::CrossAccountId,
544 token: TokenId,
545 amount: u128,
546 ) -> DispatchResult {
547 if spender == from {
548 return Self::burn(collection, from, token, amount);
549 }
550 if collection.access == AccessMode::WhiteList {
551 // `from` checked in [`burn`]
552 collection.check_allowlist(spender)?;
553 }
554
555 let allowance = <Allowance<T>>::get((collection.id, token, from.as_sub(), &spender))
556 .checked_sub(amount);
557 if allowance.is_none() {
558 ensure!(
559 collection.ignores_allowance(spender)?,
560 <CommonError<T>>::TokenValueNotEnough
561 );
562 }
563
564 // =========
565
566 Self::burn(collection, from, token, amount)?;
567 if let Some(allowance) = allowance {
568 Self::set_allowance_unchecked(collection, from, spender, token, allowance);
569 }
570 Ok(())
571 }
539572
540 pub fn set_variable_metadata(573 pub fn set_variable_metadata(
541 collection: &RefungibleHandle<T>,574 collection: &RefungibleHandle<T>,