git.delta.rocks / unique-network / refs/commits / 31c3cec78d0b

difftreelog

Contract sponsoring in progress as is

Greg Zaitsev2020-11-05parent: #3f1f974.patch.diff
in: master

4 files changed

modifiedCargo.lockdiffbeforeafterboth
3737 "frame-support",3737 "frame-support",
3738 "frame-system",3738 "frame-system",
3739 "log",3739 "log",
3740 "pallet-contracts",
3740 "pallet-transaction-payment",3741 "pallet-transaction-payment",
3741 "parity-scale-codec",3742 "parity-scale-codec",
3742 "serde",3743 "serde",
modifiedpallets/nft/Cargo.tomldiffbeforeafterboth
73branch = 'v2.0.0_release'73branch = 'v2.0.0_release'
74optional = true74optional = true
75
76[dependencies.pallet-contracts]
77default-features = false
78git = 'https://github.com/usetech-llc/substrate.git'
79package = 'pallet-contracts'
80branch = 'v2.0.0_release'
81version = '2.0.0'
7582
76[features]83[features]
77default = ['std']84default = ['std']
modifiedpallets/nft/src/lib.rsdiffbeforeafterboth
11 construct_runtime, decl_event, decl_module, decl_storage,11 construct_runtime, decl_event, decl_module, decl_storage,
12 dispatch::DispatchResult,12 dispatch::DispatchResult,
13 ensure, parameter_types,13 ensure, parameter_types,
14 debug,
14 traits::{15 traits::{
15 Currency, ExistenceRequirement, Get, Imbalance, KeyOwnerProofSystem, OnUnbalanced,16 Currency, ExistenceRequirement, Get, Imbalance, KeyOwnerProofSystem, OnUnbalanced,
16 Randomness, WithdrawReason,17 Randomness, WithdrawReason,
22 },23 },
23 IsSubType, StorageValue,24 IsSubType, StorageValue,
24};25};
26use sp_runtime::print;
25// use frame_support::weights::{Weight, constants::RocksDbWeight as DbWeight};27// use frame_support::weights::{Weight, constants::RocksDbWeight as DbWeight};
2628
27use frame_system::{self as system, ensure_signed, ensure_root};29use frame_system::{self as system, ensure_signed, ensure_root};
205 fn set_offchain_schema() -> Weight;207 fn set_offchain_schema() -> Weight;
206}208}
207209
208pub trait Trait: system::Trait + Sized {210pub trait Trait: system::Trait + Sized + transaction_payment::Trait + pallet_contracts::Trait {
209 type Event: From<Event<Self>> + Into<<Self as system::Trait>::Event>;211 type Event: From<Event<Self>> + Into<<Self as system::Trait>::Event>;
210212
211 /// Weight information for extrinsics in this pallet.213 /// Weight information for extrinsics in this pallet.
1737/// Require the transactor pay for themselves and maybe include a tip to gain additional priority1739/// Require the transactor pay for themselves and maybe include a tip to gain additional priority
1738/// in the queue.1740/// in the queue.
1739#[derive(Encode, Decode, Clone, Eq, PartialEq)]1741#[derive(Encode, Decode, Clone, Eq, PartialEq)]
1740pub struct ChargeTransactionPayment<T: transaction_payment::Trait + Send + Sync>(1742pub struct ChargeTransactionPayment<T: Trait + Send + Sync>(
1741 #[codec(compact)] BalanceOf<T>,1743 #[codec(compact)] BalanceOf<T>
1742);1744);
17431745
1744impl<T: Trait + transaction_payment::Trait + Send + Sync> sp_std::fmt::Debug1746impl<T: Trait + Send + Sync> sp_std::fmt::Debug
1745 for ChargeTransactionPayment<T>1747 for ChargeTransactionPayment<T>
1746{1748{
1747 #[cfg(feature = "std")]1749 #[cfg(feature = "std")]
1754 }1756 }
1755}1757}
17561758
1757impl<T: Trait + transaction_payment::Trait + Send + Sync> ChargeTransactionPayment<T>1759impl<T: Trait + Send + Sync> ChargeTransactionPayment<T>
1758where1760where
1759 T::Call:1761 T::Call: Dispatchable<Info = DispatchInfo, PostInfo = PostDispatchInfo> + IsSubType<Call<T>>,
1760 Dispatchable<Info = DispatchInfo, PostInfo = PostDispatchInfo> + IsSubType<Call<T>>,
1761 BalanceOf<T>: Send + Sync + FixedPointOperand,1762 BalanceOf<T>: Send + Sync + FixedPointOperand,
1762{1763{
1763 /// utility constructor. Used only in client/factory code.1764 /// utility constructor. Used only in client/factory code.
17871788
1788 // Set fee based on call type. Creating collection costs 1 Unique.1789 // Set fee based on call type. Creating collection costs 1 Unique.
1789 // All other transactions have traditional fees so far1790 // All other transactions have traditional fees so far
1790 let fee = match call.is_sub_type() {1791 // let fee = match call.is_sub_type() {
1791 Some(Call::create_collection(..)) => <BalanceOf<T>>::from(1_000_000_000),1792 // Some(Call::create_collection(..)) => <BalanceOf<T>>::from(1_000_000_000),
1792 _ => Self::traditional_fee(len, info, tip), // Flat fee model, use only for testing purposes1793 // _ => Self::traditional_fee(len, info, tip), // Flat fee model, use only for testing purposes
1793 // _ => <BalanceOf<T>>::from(100)1794 // // _ => <BalanceOf<T>>::from(100)
1794 };1795 // };
1796 let fee = Self::traditional_fee(len, info, tip);
17951797
1796 // Determine who is paying transaction fee based on ecnomic model1798 // Determine who is paying transaction fee based on ecnomic model
1797 // Parse call to extract collection ID and access collection sponsor1799 // Parse call to extract collection ID and access collection sponsor
1861 }1863 }
1862 }1864 }
18631865
1866 // Some(pallet_contracts::Call::call(_dest, _value, _gas_limit, _data)) => {
1867 // Some(pallet_contracts::Call::call(..)) => {
1868 // T::AccountId::default()
1869 // }
1870
1864 _ => T::AccountId::default(),1871 _ => T::AccountId::default(),
1865 };1872 };
18661873
1890 }1897 }
1891}1898}
18921899
1900
1893impl<T: Trait + transaction_payment::Trait + Send + Sync> SignedExtension1901impl<T: Trait + Send + Sync> SignedExtension
1894 for ChargeTransactionPayment<T>1902 for ChargeTransactionPayment<T>
1895where1903where
1896 BalanceOf<T>: Send + Sync + From<u64> + FixedPointOperand,1904 BalanceOf<T>: Send + Sync + From<u64> + FixedPointOperand,
1900 type AccountId = T::AccountId;1908 type AccountId = T::AccountId;
1901 type Call = T::Call;1909 type Call = T::Call;
1902 type AdditionalSigned = ();1910 type AdditionalSigned = ();
1903 type Pre = (1911 type Pre = ();
1904 BalanceOf<T>,1912 // type Pre = (
1913 // BalanceOf<T>,
1905 Self::AccountId,1914 // Self::AccountId,
1906 Option<NegativeImbalanceOf<T>>,1915 // Option<NegativeImbalanceOf<T>>,
1907 BalanceOf<T>,1916 // BalanceOf<T>,
1908 );1917 // );
1909 fn additional_signed(&self) -> sp_std::result::Result<(), TransactionValidityError> {1918 fn additional_signed(&self) -> sp_std::result::Result<(), TransactionValidityError> {
1910 Ok(())1919 Ok(())
1911 }1920 }
1919 ) -> TransactionValidity {1928 ) -> TransactionValidity {
1920 let (fee, _) = self.withdraw_fee(who, call, info, len)?;1929 let (fee, _) = self.withdraw_fee(who, call, info, len)?;
19211930
1922 let mut r = ValidTransaction::default();1931 print("====== validate");
1923 // NOTE: we probably want to maximize the _fee (of any type) per weight unit_ here, which1932
1924 // will be a bit more than setting the priority to tip. For now, this is enough.1933 Ok(ValidTransaction::default())
1925 r.priority = fee.saturated_into::<TransactionPriority>();
1926 Ok(r)
1927 }1934 }
19281935
1929 fn pre_dispatch(1936 fn pre_dispatch(
1933 info: &DispatchInfoOf<Self::Call>,1940 info: &DispatchInfoOf<Self::Call>,
1934 len: usize,1941 len: usize,
1935 ) -> Result<Self::Pre, TransactionValidityError> {1942 ) -> Result<Self::Pre, TransactionValidityError> {
1943
1936 let (fee, imbalance) = self.withdraw_fee(who, call, info, len)?;1944 print("========= PreDispatch");
1945
1946 // let (fee, imbalance) = self.withdraw_fee(who, call, info, len)?;
1947
1937 Ok((self.0, who.clone(), imbalance, fee))1948 // debug::info!("Fee: {:?}", fee);
1949
1950 // Ok((self.0, who.clone(), imbalance, fee))
1951 Ok(())
1938 }1952 }
19391953
1940 fn post_dispatch(1954 fn post_dispatch(
1944 len: usize,1958 len: usize,
1945 _result: &DispatchResult,1959 _result: &DispatchResult,
1946 ) -> Result<(), TransactionValidityError> {1960 ) -> Result<(), TransactionValidityError> {
1947 let (tip, who, imbalance, fee) = pre;1961 // let (tip, who, imbalance, fee) = pre;
1948 if let Some(payed) = imbalance {1962 // if let Some(payed) = imbalance {
1949 let actual_fee = <transaction_payment::Module<T>>::compute_actual_fee(1963 // let actual_fee = <transaction_payment::Module<T>>::compute_actual_fee(
1950 len as u32, info, post_info, tip,1964 // len as u32, info, post_info, tip,
1951 );1965 // );
1952 let refund = fee.saturating_sub(actual_fee);1966 // let refund = fee.saturating_sub(actual_fee);
1953 let actual_payment =1967 // let actual_payment =
1954 match <T as transaction_payment::Trait>::Currency::deposit_into_existing(1968 // match <T as transaction_payment::Trait>::Currency::deposit_into_existing(
1955 &who, refund,1969 // &who, refund,
1956 ) {1970 // ) {
1957 Ok(refund_imbalance) => {1971 // Ok(refund_imbalance) => {
1958 // The refund cannot be larger than the up front payed max weight.1972 // // The refund cannot be larger than the up front payed max weight.
1959 // `PostDispatchInfo::calc_unspent` guards against such a case.1973 // // `PostDispatchInfo::calc_unspent` guards against such a case.
1960 match payed.offset(refund_imbalance) {1974 // match payed.offset(refund_imbalance) {
1961 Ok(actual_payment) => actual_payment,1975 // Ok(actual_payment) => actual_payment,
1962 Err(_) => return Err(InvalidTransaction::Payment.into()),1976 // Err(_) => return Err(InvalidTransaction::Payment.into()),
1963 }1977 // }
1964 }1978 // }
1965 // We do not recreate the account using the refund. The up front payment1979 // // We do not recreate the account using the refund. The up front payment
1966 // is gone in that case.1980 // // is gone in that case.
1967 Err(_) => payed,1981 // Err(_) => payed,
1968 };1982 // };
1969 let imbalances = actual_payment.split(tip);1983 // let imbalances = actual_payment.split(tip);
1970 <T as transaction_payment::Trait>::OnTransactionPayment::on_unbalanceds(1984 // <T as transaction_payment::Trait>::OnTransactionPayment::on_unbalanceds(
1971 Some(imbalances.0).into_iter().chain(Some(imbalances.1)),1985 // Some(imbalances.0).into_iter().chain(Some(imbalances.1)),
1972 );1986 // );
1973 }1987 // }
1974 Ok(())1988 Ok(())
1975 }1989 }
1976}1990}
1991
1992/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
1993
1994
1995#[derive(Encode, Decode, Clone, Eq, PartialEq)]
1996pub struct ChargeContractTransactionPayment<T: Trait + Send + Sync>(
1997 #[codec(compact)] BalanceOf<T>
1998);
1999
2000impl<T: Trait + Send + Sync> sp_std::fmt::Debug
2001 for ChargeContractTransactionPayment<T>
2002{
2003 #[cfg(feature = "std")]
2004 fn fmt(&self, f: &mut sp_std::fmt::Formatter) -> sp_std::fmt::Result {
2005 write!(f, "ChargeContractTransactionPayment<{:?}>", self.0)
2006 }
2007 #[cfg(not(feature = "std"))]
2008 fn fmt(&self, _: &mut sp_std::fmt::Formatter) -> sp_std::fmt::Result {
2009 Ok(())
2010 }
2011}
2012
2013// impl<T: Trait + Send + Sync> ChargeContractTransactionPayment<T>
2014// where
2015// // T::Call: Dispatchable<Info = DispatchInfo, PostInfo = PostDispatchInfo>,
2016// T::Call: Dispatchable<Info = DispatchInfo, PostInfo = PostDispatchInfo> + IsSubType<pallet_contracts::Call<T>>,
2017// BalanceOf<T>: Send + Sync + FixedPointOperand,
2018// {
2019// // /// utility constructor. Used only in client/factory code.
2020// // pub fn from(fee: BalanceOf<T>) -> Self {
2021// // Self(fee)
2022// // }
2023
2024// pub fn traditional_fee(
2025// len: usize,
2026// info: &DispatchInfoOf<T::Call>,
2027// tip: BalanceOf<T>,
2028// ) -> BalanceOf<T>
2029// where
2030// T::Call: Dispatchable<Info = DispatchInfo>,
2031// {
2032// <transaction_payment::Module<T>>::compute_fee(len as u32, info, tip)
2033// }
2034
2035// fn withdraw_fee(
2036// &self,
2037// who: &T::AccountId,
2038// call: &T::Call,
2039// info: &DispatchInfoOf<T::Call>,
2040// len: usize,
2041// ) -> Result<(BalanceOf<T>, Option<NegativeImbalanceOf<T>>), TransactionValidityError> {
2042// let tip = self.0;
2043
2044// let mut fee = Self::traditional_fee(len, info, tip);
2045
2046// // Determine who is paying transaction fee based on ecnomic model
2047// // Parse call to extract collection ID and access collection sponsor
2048// // let sponsor: T::AccountId = match call.is_sub_type() {
2049// // // Some(pallet_contracts::Call::call(_dest, _value, _gas_limit, _data)) => {
2050// // Some(pallet_contracts::Call::call(..)) => {
2051// // // fee = <BalanceOf<T>>::from(0);
2052// // T::AccountId::default()
2053// // }
2054// // _ => T::AccountId::default(),
2055// // };
2056// let sponsor = T::AccountId::default();
2057
2058// let mut who_pays_fee: T::AccountId = sponsor.clone();
2059// if sponsor == T::AccountId::default() {
2060// who_pays_fee = who.clone();
2061// }
2062
2063// // Only mess with balances if fee is not zero.
2064// if fee.is_zero() {
2065// return Ok((fee, None));
2066// }
2067
2068// match <T as transaction_payment::Trait>::Currency::withdraw(
2069// &who_pays_fee,
2070// fee,
2071// if tip.is_zero() {
2072// WithdrawReason::TransactionPayment.into()
2073// } else {
2074// WithdrawReason::TransactionPayment | WithdrawReason::Tip
2075// },
2076// ExistenceRequirement::KeepAlive,
2077// ) {
2078// Ok(imbalance) => Ok((fee, Some(imbalance))),
2079// Err(_) => Err(InvalidTransaction::Payment.into()),
2080// }
2081// }
2082// }
2083
2084
2085
2086impl<T: Trait + Send + Sync> SignedExtension
2087 for ChargeContractTransactionPayment<T>
2088where
2089 BalanceOf<T>: Send + Sync + From<u64> + FixedPointOperand,
2090 // T::Call: Dispatchable<Info = DispatchInfo, PostInfo = PostDispatchInfo>,
2091 T::Call: Dispatchable<Info = DispatchInfo, PostInfo = PostDispatchInfo> + IsSubType<pallet_contracts::Call<T>>,
2092{
2093 const IDENTIFIER: &'static str = "ChargeContractTransactionPayment";
2094 type AccountId = T::AccountId;
2095 type Call = T::Call;
2096 type AdditionalSigned = ();
2097 type Pre = ();
2098
2099 // type Pre = (
2100 // BalanceOf<T>,
2101 // Self::AccountId,
2102 // Option<NegativeImbalanceOf<T>>,
2103 // BalanceOf<T>,
2104 // );
2105 fn additional_signed(&self) -> sp_std::result::Result<(), TransactionValidityError> {
2106 Ok(())
2107 }
2108
2109 fn validate(
2110 &self,
2111 who: &Self::AccountId,
2112 call: &Self::Call,
2113 info: &DispatchInfoOf<Self::Call>,
2114 len: usize,
2115 ) -> TransactionValidity {
2116 // let (fee, _) = self.withdraw_fee(who, call, info, len)?;
2117
2118 print("====== Contracts validate");
2119
2120 Ok(ValidTransaction::default())
2121 }
2122
2123 fn pre_dispatch(
2124 self,
2125 who: &Self::AccountId,
2126 call: &Self::Call,
2127 info: &DispatchInfoOf<Self::Call>,
2128 len: usize,
2129 ) -> Result<Self::Pre, TransactionValidityError> {
2130 // let (fee, imbalance) = self.withdraw_fee(who, call, info, len)?;
2131 // Ok((self.0, who.clone(), imbalance, fee))
2132
2133 print("====== Contracts pre-dispatch");
2134 // debug::info!("Fee: {:?}", fee);
2135
2136 Ok(())
2137 }
2138
2139 fn post_dispatch(
2140 pre: Self::Pre,
2141 info: &DispatchInfoOf<Self::Call>,
2142 post_info: &PostDispatchInfoOf<Self::Call>,
2143 len: usize,
2144 _result: &DispatchResult,
2145 ) -> Result<(), TransactionValidityError> {
2146 // let (tip, who, imbalance, fee) = pre;
2147 // if let Some(payed) = imbalance {
2148 // let actual_fee = <transaction_payment::Module<T>>::compute_actual_fee(
2149 // len as u32, info, post_info, tip,
2150 // );
2151 // let refund = fee.saturating_sub(actual_fee);
2152 // let actual_payment =
2153 // match <T as transaction_payment::Trait>::Currency::deposit_into_existing(
2154 // &who, refund,
2155 // ) {
2156 // Ok(refund_imbalance) => {
2157 // // The refund cannot be larger than the up front payed max weight.
2158 // // `PostDispatchInfo::calc_unspent` guards against such a case.
2159 // match payed.offset(refund_imbalance) {
2160 // Ok(actual_payment) => actual_payment,
2161 // Err(_) => return Err(InvalidTransaction::Payment.into()),
2162 // }
2163 // }
2164 // // We do not recreate the account using the refund. The up front payment
2165 // // is gone in that case.
2166 // Err(_) => payed,
2167 // };
2168 // let imbalances = actual_payment.split(tip);
2169 // <T as transaction_payment::Trait>::OnTransactionPayment::on_unbalanceds(
2170 // Some(imbalances.0).into_iter().chain(Some(imbalances.1)),
2171 // );
2172 // }
2173 Ok(())
2174 }
2175}
2176
2177
1977// #endregion2178// #endregion
modifiedruntime/src/lib.rsdiffbeforeafterboth
352 system::CheckNonce<Runtime>,352 system::CheckNonce<Runtime>,
353 system::CheckWeight<Runtime>,353 system::CheckWeight<Runtime>,
354 pallet_nft::ChargeTransactionPayment<Runtime>,354 pallet_nft::ChargeTransactionPayment<Runtime>,
355 pallet_nft::ChargeContractTransactionPayment<Runtime>,
355);356);
356/// Unchecked extrinsic type as expected by this runtime.357/// Unchecked extrinsic type as expected by this runtime.
357pub type UncheckedExtrinsic = generic::UncheckedExtrinsic<Address, Call, Signature, SignedExtra>;358pub type UncheckedExtrinsic = generic::UncheckedExtrinsic<Address, Call, Signature, SignedExtra>;