difftreelog
feat burn_from
in: master
12 files changed
pallets/common/src/lib.rsdiffbeforeafterboth--- a/pallets/common/src/lib.rs
+++ b/pallets/common/src/lib.rs
@@ -459,6 +459,7 @@
fn transfer() -> Weight;
fn approve() -> Weight;
fn transfer_from() -> Weight;
+ fn burn_from() -> Weight;
fn set_variable_metadata(bytes: u32) -> Weight;
}
@@ -504,6 +505,13 @@
token: TokenId,
amount: u128,
) -> DispatchResultWithPostInfo;
+ fn burn_from(
+ &self,
+ sender: T::CrossAccountId,
+ from: T::CrossAccountId,
+ token: TokenId,
+ amount: u128,
+ ) -> DispatchResultWithPostInfo;
fn set_variable_metadata(
&self,
pallets/fungible/src/common.rsdiffbeforeafterboth--- a/pallets/fungible/src/common.rs
+++ b/pallets/fungible/src/common.rs
@@ -38,6 +38,10 @@
<SelfWeightOf<T>>::transfer_from()
}
+ fn burn_from() -> Weight {
+ 0
+ }
+
fn set_variable_metadata(_bytes: u32) -> Weight {
// Error
0
@@ -156,6 +160,24 @@
)
}
+ fn burn_from(
+ &self,
+ sender: T::CrossAccountId,
+ from: T::CrossAccountId,
+ token: TokenId,
+ amount: u128,
+ ) -> DispatchResultWithPostInfo {
+ ensure!(
+ token == TokenId::default(),
+ <Error<T>>::FungibleItemsHaveNoId
+ );
+
+ with_weight(
+ <Pallet<T>>::burn_from(&self, &sender, &from, amount),
+ <CommonWeights<T>>::burn_from(),
+ )
+ }
+
fn set_variable_metadata(
&self,
_sender: T::CrossAccountId,
pallets/fungible/src/erc.rsdiffbeforeafterboth1use core::char::{REPLACEMENT_CHARACTER, decode_utf16};2use core::convert::TryInto;3use evm_coder::{ToLog, execution::*, generate_stubgen, solidity_interface, types::*};4use nft_data_structs::CollectionMode;5use pallet_common::erc::CommonEvmHandler;6use sp_core::{H160, U256};7use sp_std::vec::Vec;8use pallet_common::account::CrossAccountId;9use pallet_common::erc::PrecompileOutput;10use pallet_evm_coder_substrate::{call_internal, dispatch_to_evm};1112use crate::{Allowance, Balance, Config, FungibleHandle, Pallet, TotalSupply};1314#[derive(ToLog)]15pub enum ERC20Events {16 Transfer {17 #[indexed]18 from: address,19 #[indexed]20 to: address,21 value: uint256,22 },23 Approval {24 #[indexed]25 owner: address,26 #[indexed]27 spender: address,28 value: uint256,29 },30}3132#[solidity_interface(name = "ERC20", events(ERC20Events))]33impl<T: Config> FungibleHandle<T> {34 fn name(&self) -> Result<string> {35 Ok(decode_utf16(self.name.iter().copied())36 .map(|r| r.unwrap_or(REPLACEMENT_CHARACTER))37 .collect::<string>())38 }39 fn symbol(&self) -> Result<string> {40 Ok(string::from_utf8_lossy(&self.token_prefix).into())41 }42 fn total_supply(&self) -> Result<uint256> {43 Ok(<TotalSupply<T>>::get(self.id).into())44 }4546 fn decimals(&self) -> Result<uint8> {47 Ok(if let CollectionMode::Fungible(decimals) = &self.mode {48 *decimals49 } else {50 unreachable!()51 })52 }53 fn balance_of(&self, owner: address) -> Result<uint256> {54 let owner = T::CrossAccountId::from_eth(owner);55 let balance = <Balance<T>>::get((self.id, owner.as_sub()));56 Ok(balance.into())57 }58 fn transfer(&mut self, caller: caller, to: address, amount: uint256) -> Result<bool> {59 let caller = T::CrossAccountId::from_eth(caller);60 let to = T::CrossAccountId::from_eth(to);61 let amount = amount.try_into().map_err(|_| "amount overflow")?;6263 <Pallet<T>>::transfer(self, &caller, &to, amount).map_err(|_| "transfer error")?;64 Ok(true)65 }66 fn transfer_from(67 &mut self,68 caller: caller,69 from: address,70 to: address,71 amount: uint256,72 ) -> Result<bool> {73 let caller = T::CrossAccountId::from_eth(caller);74 let from = T::CrossAccountId::from_eth(from);75 let to = T::CrossAccountId::from_eth(to);76 let amount = amount.try_into().map_err(|_| "amount overflow")?;7778 <Pallet<T>>::transfer_from(self, &caller, &from, &to, amount)79 .map_err(dispatch_to_evm::<T>)?;80 Ok(true)81 }82 fn approve(&mut self, caller: caller, spender: address, amount: uint256) -> Result<bool> {83 let caller = T::CrossAccountId::from_eth(caller);84 let spender = T::CrossAccountId::from_eth(spender);85 let amount = amount.try_into().map_err(|_| "amount overflow")?;8687 <Pallet<T>>::set_allowance(self, &caller, &spender, amount)88 .map_err(dispatch_to_evm::<T>)?;89 Ok(true)90 }91 fn allowance(&self, owner: address, spender: address) -> Result<uint256> {92 let owner = T::CrossAccountId::from_eth(owner);93 let spender = T::CrossAccountId::from_eth(spender);9495 Ok(<Allowance<T>>::get((self.id, owner.as_sub(), spender.as_sub())).into())96 }97}9899#[solidity_interface(name = "UniqueFungible", is(ERC20))]100impl<T: Config> FungibleHandle<T> {}101102generate_stubgen!(get_impl, UniqueFungibleCall, true);103generate_stubgen!(gen_iface, UniqueFungibleCall, false);104105impl<T: Config> CommonEvmHandler for FungibleHandle<T> {106 const CODE: &'static [u8] = include_bytes!("./stubs/UniqueFungible.raw");107108 fn call(mut self, source: &H160, input: &[u8], value: U256) -> Option<PrecompileOutput> {109 let result = call_internal::<UniqueFungibleCall, _>(*source, &mut self, value, input);110 self.0.recorder.evm_to_precompile_output(result)111 }112}1use core::char::{REPLACEMENT_CHARACTER, decode_utf16};2use core::convert::TryInto;3use evm_coder::{ToLog, execution::*, generate_stubgen, solidity_interface, types::*};4use nft_data_structs::CollectionMode;5use pallet_common::erc::CommonEvmHandler;6use sp_core::{H160, U256};7use sp_std::vec::Vec;8use pallet_common::account::CrossAccountId;9use pallet_common::erc::PrecompileOutput;10use pallet_evm_coder_substrate::{call_internal, dispatch_to_evm};1112use crate::{Allowance, Balance, Config, FungibleHandle, Pallet, TotalSupply};1314#[derive(ToLog)]15pub enum ERC20Events {16 Transfer {17 #[indexed]18 from: address,19 #[indexed]20 to: address,21 value: uint256,22 },23 Approval {24 #[indexed]25 owner: address,26 #[indexed]27 spender: address,28 value: uint256,29 },30}3132#[solidity_interface(name = "ERC20", events(ERC20Events))]33impl<T: Config> FungibleHandle<T> {34 fn name(&self) -> Result<string> {35 Ok(decode_utf16(self.name.iter().copied())36 .map(|r| r.unwrap_or(REPLACEMENT_CHARACTER))37 .collect::<string>())38 }39 fn symbol(&self) -> Result<string> {40 Ok(string::from_utf8_lossy(&self.token_prefix).into())41 }42 fn total_supply(&self) -> Result<uint256> {43 Ok(<TotalSupply<T>>::get(self.id).into())44 }4546 fn decimals(&self) -> Result<uint8> {47 Ok(if let CollectionMode::Fungible(decimals) = &self.mode {48 *decimals49 } else {50 unreachable!()51 })52 }53 fn balance_of(&self, owner: address) -> Result<uint256> {54 let owner = T::CrossAccountId::from_eth(owner);55 let balance = <Balance<T>>::get((self.id, owner.as_sub()));56 Ok(balance.into())57 }58 fn transfer(&mut self, caller: caller, to: address, amount: uint256) -> Result<bool> {59 let caller = T::CrossAccountId::from_eth(caller);60 let to = T::CrossAccountId::from_eth(to);61 let amount = amount.try_into().map_err(|_| "amount overflow")?;6263 <Pallet<T>>::transfer(self, &caller, &to, amount).map_err(|_| "transfer error")?;64 Ok(true)65 }66 fn transfer_from(67 &mut self,68 caller: caller,69 from: address,70 to: address,71 amount: uint256,72 ) -> Result<bool> {73 let caller = T::CrossAccountId::from_eth(caller);74 let from = T::CrossAccountId::from_eth(from);75 let to = T::CrossAccountId::from_eth(to);76 let amount = amount.try_into().map_err(|_| "amount overflow")?;7778 <Pallet<T>>::transfer_from(self, &caller, &from, &to, amount)79 .map_err(dispatch_to_evm::<T>)?;80 Ok(true)81 }82 fn approve(&mut self, caller: caller, spender: address, amount: uint256) -> Result<bool> {83 let caller = T::CrossAccountId::from_eth(caller);84 let spender = T::CrossAccountId::from_eth(spender);85 let amount = amount.try_into().map_err(|_| "amount overflow")?;8687 <Pallet<T>>::set_allowance(self, &caller, &spender, amount)88 .map_err(dispatch_to_evm::<T>)?;89 Ok(true)90 }91 fn allowance(&self, owner: address, spender: address) -> Result<uint256> {92 let owner = T::CrossAccountId::from_eth(owner);93 let spender = T::CrossAccountId::from_eth(spender);9495 Ok(<Allowance<T>>::get((self.id, owner.as_sub(), spender.as_sub())).into())96 }97}9899#[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")?;105106 <Pallet<T>>::burn_from(self, &caller, &from, amount).map_err(dispatch_to_evm::<T>)?;107 Ok(true)108 }109}110111#[solidity_interface(name = "UniqueFungible", is(ERC20))]112impl<T: Config> FungibleHandle<T> {}113114generate_stubgen!(get_impl, UniqueFungibleCall, true);115generate_stubgen!(gen_iface, UniqueFungibleCall, false);116117impl<T: Config> CommonEvmHandler for FungibleHandle<T> {118 const CODE: &'static [u8] = include_bytes!("./stubs/UniqueFungible.raw");119120 fn call(mut self, source: &H160, input: &[u8], value: U256) -> Option<PrecompileOutput> {121 let result = call_internal::<UniqueFungibleCall, _>(*source, &mut self, value, input);122 self.0.recorder.evm_to_precompile_output(result)123 }124}pallets/fungible/src/lib.rsdiffbeforeafterboth--- a/pallets/fungible/src/lib.rs
+++ b/pallets/fungible/src/lib.rs
@@ -356,6 +356,38 @@
Ok(())
}
+ pub fn burn_from(
+ collection: &FungibleHandle<T>,
+ spender: &T::CrossAccountId,
+ from: &T::CrossAccountId,
+ amount: u128,
+ ) -> DispatchResult {
+ if spender == from {
+ return Self::burn(collection, from, amount);
+ }
+ if collection.access == AccessMode::WhiteList {
+ // `from` checked in [`burn`]
+ collection.check_allowlist(spender)?;
+ }
+
+ let allowance = <Allowance<T>>::get((collection.id, from.as_sub(), spender.as_sub()))
+ .checked_sub(amount);
+ if allowance.is_none() {
+ ensure!(
+ collection.ignores_allowance(spender)?,
+ <CommonError<T>>::TokenValueNotEnough
+ );
+ }
+
+ // =========
+
+ Self::burn(collection, from, amount)?;
+ if let Some(allowance) = allowance {
+ Self::set_allowance_unchecked(collection, from, spender, allowance);
+ }
+ Ok(())
+ }
+
/// Delegated to `create_multiple_items`
pub fn create_item(
collection: &FungibleHandle<T>,
pallets/nft/src/common.rsdiffbeforeafterboth--- a/pallets/nft/src/common.rs
+++ b/pallets/nft/src/common.rs
@@ -45,4 +45,8 @@
fn set_variable_metadata(bytes: u32) -> nft_data_structs::Weight {
dispatch_weight::<T>() + max_weight_of!(set_variable_metadata(bytes))
}
+
+ fn burn_from() -> Weight {
+ dispatch_weight::<T>() + max_weight_of!(burn_from())
+ }
}
pallets/nft/src/eth/sponsoring.rsdiffbeforeafterboth--- a/pallets/nft/src/eth/sponsoring.rs
+++ b/pallets/nft/src/eth/sponsoring.rs
@@ -35,9 +35,10 @@
.map_err(|_| AnyError)?
.ok_or(AnyError)?;
match call {
- UniqueNFTCall::ERC721UniqueExtensions(
- ERC721UniqueExtensionsCall::TransferNft { token_id, .. },
- )
+ UniqueNFTCall::ERC721UniqueExtensions(ERC721UniqueExtensionsCall::Transfer {
+ token_id,
+ ..
+ })
| UniqueNFTCall::ERC721(ERC721Call::TransferFrom { token_id, .. }) => {
let token_id: u32 = token_id.try_into().map_err(|_| AnyError)?;
let block_number = <frame_system::Pallet<T>>::block_number() as T::BlockNumber;
pallets/nft/src/lib.rsdiffbeforeafterboth--- a/pallets/nft/src/lib.rs
+++ b/pallets/nft/src/lib.rs
@@ -623,14 +623,13 @@
/// * item_id: ID of NFT to burn.
///
/// * from: owner of item
- // #[weight = 0]
- // #[transactional]
- // pub fn burn_from(origin, collection_id: CollectionId, from: T::CrossAccountId, item_id: TokenId, value: u128) -> PostDispatchInfo {
- // let sender = T::CrossAccountId::from_sub(ensure_signed(origin)?);
+ #[weight = <CommonWeights<T>>::burn_from()]
+ #[transactional]
+ pub fn burn_from(origin, collection_id: CollectionId, from: T::CrossAccountId, item_id: TokenId, value: u128) -> DispatchResultWithPostInfo {
+ let sender = T::CrossAccountId::from_sub(ensure_signed(origin)?);
- // // dispatch_call::<T, _>(collection_id, |d| d.burn_from(sender, from, item_id, value))
- // todo!()
- // }
+ dispatch_call::<T, _>(collection_id, |d| d.burn_from(sender, from, item_id, value))
+ }
/// Change ownership of the token.
///
pallets/nonfungible/src/common.rsdiffbeforeafterboth--- a/pallets/nonfungible/src/common.rs
+++ b/pallets/nonfungible/src/common.rs
@@ -39,6 +39,10 @@
<SelfWeightOf<T>>::transfer_from()
}
+ fn burn_from() -> Weight {
+ 0
+ }
+
fn set_variable_metadata(bytes: u32) -> Weight {
<SelfWeightOf<T>>::set_variable_metadata(bytes)
}
@@ -163,6 +167,25 @@
}
}
+ fn burn_from(
+ &self,
+ sender: T::CrossAccountId,
+ from: T::CrossAccountId,
+ token: TokenId,
+ amount: u128,
+ ) -> DispatchResultWithPostInfo {
+ ensure!(amount <= 1, <Error<T>>::NonfungibleItemsHaveNoAmount);
+
+ if amount == 1 {
+ with_weight(
+ <Pallet<T>>::burn_from(&self, &sender, &from, token),
+ <CommonWeights<T>>::burn_from(),
+ )
+ } else {
+ Ok(().into())
+ }
+ }
+
fn set_variable_metadata(
&self,
sender: T::CrossAccountId,
pallets/nonfungible/src/erc.rsdiffbeforeafterboth--- a/pallets/nonfungible/src/erc.rs
+++ b/pallets/nonfungible/src/erc.rs
@@ -264,8 +264,7 @@
#[solidity_interface(name = "ERC721UniqueExtensions")]
impl<T: Config> NonfungibleHandle<T> {
- #[solidity(rename_selector = "transfer")]
- fn transfer_nft(
+ fn transfer(
&mut self,
caller: caller,
to: address,
@@ -280,6 +279,21 @@
Ok(())
}
+ fn burn_from(
+ &mut self,
+ caller: caller,
+ from: address,
+ token_id: uint256,
+ _value: value,
+ ) -> Result<void> {
+ let caller = T::CrossAccountId::from_eth(caller);
+ let from = T::CrossAccountId::from_eth(from);
+ let token = token_id.try_into()?;
+
+ <Pallet<T>>::burn_from(self, &caller, &from, token).map_err(dispatch_to_evm::<T>)?;
+ Ok(())
+ }
+
fn next_token_id(&self) -> Result<uint256> {
Ok(<TokensMinted<T>>::get(self.id)
.checked_add(1)
pallets/nonfungible/src/lib.rsdiffbeforeafterboth--- a/pallets/nonfungible/src/lib.rs
+++ b/pallets/nonfungible/src/lib.rs
@@ -499,6 +499,32 @@
Ok(())
}
+ pub fn burn_from(
+ collection: &NonfungibleHandle<T>,
+ spender: &T::CrossAccountId,
+ from: &T::CrossAccountId,
+ token: TokenId,
+ ) -> DispatchResult {
+ if spender == from {
+ return Self::burn(collection, from, token);
+ }
+ if collection.access == AccessMode::WhiteList {
+ // `from` checked in [`burn`]
+ collection.check_allowlist(spender)?;
+ }
+
+ if <Allowance<T>>::get((collection.id, token)).as_ref() != Some(spender) {
+ ensure!(
+ collection.ignores_allowance(spender)?,
+ <CommonError<T>>::TokenValueNotEnough
+ );
+ }
+
+ // =========
+
+ Self::burn(collection, &from, token)
+ }
+
pub fn set_variable_metadata(
collection: &NonfungibleHandle<T>,
sender: &T::CrossAccountId,
pallets/refungible/src/common.rsdiffbeforeafterboth--- a/pallets/refungible/src/common.rs
+++ b/pallets/refungible/src/common.rs
@@ -59,6 +59,10 @@
)
}
+ fn burn_from() -> Weight {
+ 0
+ }
+
fn set_variable_metadata(bytes: u32) -> Weight {
<SelfWeightOf<T>>::set_variable_metadata(bytes)
}
@@ -161,7 +165,20 @@
) -> DispatchResultWithPostInfo {
with_weight(
<Pallet<T>>::transfer_from(&self, &sender, &from, &to, token, amount),
- <CommonWeights<T>>::approve(),
+ <CommonWeights<T>>::transfer_from(),
+ )
+ }
+
+ fn burn_from(
+ &self,
+ sender: T::CrossAccountId,
+ from: T::CrossAccountId,
+ token: TokenId,
+ amount: u128,
+ ) -> DispatchResultWithPostInfo {
+ with_weight(
+ <Pallet<T>>::burn_from(&self, &sender, &from, token, amount),
+ <CommonWeights<T>>::burn_from(),
)
}
pallets/refungible/src/lib.rsdiffbeforeafterboth--- a/pallets/refungible/src/lib.rs
+++ b/pallets/refungible/src/lib.rs
@@ -537,6 +537,39 @@
Ok(())
}
+ pub fn burn_from(
+ collection: &RefungibleHandle<T>,
+ spender: &T::CrossAccountId,
+ from: &T::CrossAccountId,
+ token: TokenId,
+ amount: u128,
+ ) -> DispatchResult {
+ if spender == from {
+ return Self::burn(collection, from, token, amount);
+ }
+ if collection.access == AccessMode::WhiteList {
+ // `from` checked in [`burn`]
+ collection.check_allowlist(spender)?;
+ }
+
+ let allowance = <Allowance<T>>::get((collection.id, token, from.as_sub(), &spender))
+ .checked_sub(amount);
+ if allowance.is_none() {
+ ensure!(
+ collection.ignores_allowance(spender)?,
+ <CommonError<T>>::TokenValueNotEnough
+ );
+ }
+
+ // =========
+
+ Self::burn(collection, from, token, amount)?;
+ if let Some(allowance) = allowance {
+ Self::set_allowance_unchecked(collection, from, spender, token, allowance);
+ }
+ Ok(())
+ }
+
pub fn set_variable_metadata(
collection: &RefungibleHandle<T>,
sender: &T::CrossAccountId,