git.delta.rocks / unique-network / refs/commits / ec349b062948

difftreelog

feat impl transfer, transer_from, transfer_cross, transfer_from_cross,

Trubnikov Sergey2023-04-21parent: #1e5b247.patch.diff
in: master

6 files changed

modifiedpallets/balances-adapter/src/erc.rsdiffbeforeafterboth
1use crate::Config;1use crate::Config;
2use evm_coder::{abi::AbiType, AbiCoder, ToLog, generate_stubgen, solidity_interface, types::*};2use evm_coder::{abi::AbiType, AbiCoder, ToLog, generate_stubgen, solidity_interface, types::*};
3use frame_support::traits::Currency;3use frame_support::traits::{Currency, ExistenceRequirement};
4use pallet_common::erc::{CommonEvmHandler, CrossAccountId, PrecompileHandle, PrecompileResult};4use pallet_common::{
5 erc::{CommonEvmHandler, CrossAccountId, PrecompileHandle, PrecompileResult},
6 eth::CrossAddress,
7};
5use pallet_evm_coder_substrate::{8use pallet_evm_coder_substrate::{
6 call, dispatch_to_evm,9 call, dispatch_to_evm,
7 execution::{PreDispatch, Result},10 execution::{PreDispatch, Result},
8285
83 // #[weight(<SelfWeightOf<T>>::transfer())]86 // #[weight(<SelfWeightOf<T>>::transfer())]
84 fn transfer(&mut self, caller: Caller, to: Address, amount: U256) -> Result<bool> {87 fn transfer(&mut self, caller: Caller, to: Address, amount: U256) -> Result<bool> {
85 // let caller = T::CrossAccountId::from_eth(caller);88 let caller = T::CrossAccountId::from_eth(caller);
86 // let to = T::CrossAccountId::from_eth(to);89 let to = T::CrossAccountId::from_eth(to);
87 // let amount = amount.try_into().map_err(|_| "amount overflow")?;90 let amount = amount.try_into().map_err(|_| "amount overflow")?;
88 // let budget = self91 // let budget = self
89 // .recorder92 // .recorder
90 // .weight_calls_budget(<StructureWeight<T>>::find_parent());93 // .weight_calls_budget(<StructureWeight<T>>::find_parent());
9194
92 // <Pallet<T>>::transfer(self, &caller, &to, amount, &budget).map_err(|_| "transfer error")?;95 // <Pallet<T>>::transfer(self, &caller, &to, amount, &budget).map_err(|_| "transfer error")?;
93 // Ok(true)96 <T as Config>::Currency::transfer(
97 caller.as_sub(),
94 todo!()98 to.as_sub(),
99 amount,
100 ExistenceRequirement::KeepAlive,
101 )
102 .map_err(dispatch_to_evm::<T>);
103 Ok(true)
95 }104 }
96105
97 // #[weight(<SelfWeightOf<T>>::transfer_from())]106 // #[weight(<SelfWeightOf<T>>::transfer_from())]
102 to: Address,111 to: Address,
103 amount: U256,112 amount: U256,
104 ) -> Result<bool> {113 ) -> Result<bool> {
105 // let caller = T::CrossAccountId::from_eth(caller);114 let caller = T::CrossAccountId::from_eth(caller);
106 // let from = T::CrossAccountId::from_eth(from);115 let from = T::CrossAccountId::from_eth(from);
107 // let to = T::CrossAccountId::from_eth(to);116 let to = T::CrossAccountId::from_eth(to);
108 // let amount = amount.try_into().map_err(|_| "amount overflow")?;117 let amount = amount.try_into().map_err(|_| "amount overflow")?;
118
119 if (from != to) {
120 return Err("no permission".into());
121 }
109 // let budget = self122 // let budget = self
110 // .recorder123 // .recorder
111 // .weight_calls_budget(<StructureWeight<T>>::find_parent());124 // .weight_calls_budget(<StructureWeight<T>>::find_parent());
112125
113 // <Pallet<T>>::transfer_from(self, &caller, &from, &to, amount, &budget)126 // <Pallet<T>>::transfer_from(self, &caller, &from, &to, amount, &budget)
114 // .map_err(dispatch_to_evm::<T>)?;127 // .map_err(dispatch_to_evm::<T>)?;
115 // Ok(true)128 <T as Config>::Currency::transfer(
129 caller.as_sub(),
116 todo!()130 to.as_sub(),
131 amount,
132 ExistenceRequirement::KeepAlive,
133 )
134 .map_err(dispatch_to_evm::<T>);
135 Ok(true)
117 }136 }
118}137}
138
139#[solidity_interface(name = ERC20UniqueExtensions, enum(derive(PreDispatch)), enum_attr(weight))]
140impl<T: Config> NativeFungibleHandle<T>
141where
142 T::AccountId: From<[u8; 32]>,
143{
144 // #[weight(<SelfWeightOf<T>>::transfer())]
145 fn transfer_cross(&mut self, caller: Caller, to: CrossAddress, amount: U256) -> Result<bool> {
146 let caller = T::CrossAccountId::from_eth(caller);
147 let to = to.into_sub_cross_account::<T>()?;
148 let amount = amount.try_into().map_err(|_| "amount overflow")?;
149 // let budget = self
150 // .recorder
151 // .weight_calls_budget(<StructureWeight<T>>::find_parent());
152
153 // <Pallet<T>>::transfer(self, &caller, &to, amount, &budget).map_err(|_| "transfer error")?;
154 <T as Config>::Currency::transfer(
155 caller.as_sub(),
156 to.as_sub(),
157 amount,
158 ExistenceRequirement::KeepAlive,
159 )
160 .map_err(dispatch_to_evm::<T>);
161 Ok(true)
162 }
163
164 // #[weight(<SelfWeightOf<T>>::transfer_from())]
165 fn transfer_from_cross(
166 &mut self,
167 caller: Caller,
168 from: CrossAddress,
169 to: CrossAddress,
170 amount: U256,
171 ) -> Result<bool> {
172 let caller = T::CrossAccountId::from_eth(caller);
173 let from = from.into_sub_cross_account::<T>()?;
174 let to = to.into_sub_cross_account::<T>()?;
175 let amount = amount.try_into().map_err(|_| "amount overflow")?;
176
177 if (from != to) {
178 return Err("no permission".into());
179 }
180
181 // let budget = self
182 // .recorder
183 // .weight_calls_budget(<StructureWeight<T>>::find_parent());
184
185 // <Pallet<T>>::transfer_from(self, &caller, &from, &to, amount, &budget)
186 // .map_err(dispatch_to_evm::<T>)?;
187 <T as Config>::Currency::transfer(
188 caller.as_sub(),
189 to.as_sub(),
190 amount,
191 ExistenceRequirement::KeepAlive,
192 )
193 .map_err(dispatch_to_evm::<T>);
194 Ok(true)
195 }
196}
119197
120#[solidity_interface(198#[solidity_interface(
121 name = UniqueNativeFungible,199 name = UniqueNativeFungible,
122 is(ERC20),200 is(ERC20, ERC20UniqueExtensions),
123 enum(derive(PreDispatch))201 enum(derive(PreDispatch))
124)]202)]
125impl<T: Config> NativeFungibleHandle<T> where T::AccountId: From<[u8; 32]> + AsRef<[u8; 32]> {}203impl<T: Config> NativeFungibleHandle<T> where T::AccountId: From<[u8; 32]> + AsRef<[u8; 32]> {}
modifiedpallets/balances-adapter/src/lib.rsdiffbeforeafterboth
17 Self::AccountId,17 Self::AccountId,
18 Balance = Self::CurrencyBalance,18 Balance = Self::CurrencyBalance,
19 >;19 >;
20 type CurrencyBalance: Into<U256>;20 type CurrencyBalance: Into<U256> + TryFrom<U256>;
2121
22 type Decimals: Get<u8>;22 type Decimals: Get<u8>;
23 type Name: Get<String>;23 type Name: Get<String>;
modifiedpallets/balances-adapter/src/stubs/UniqueNativeFungible.rawdiffbeforeafterboth

binary blob — no preview

modifiedpallets/balances-adapter/src/stubs/UniqueNativeFungible.soldiffbeforeafterboth
17 }17 }
18}18}
19
20/// @dev the ERC-165 identifier for this interface is 0xff15c6f4
21contract ERC20UniqueExtensions is Dummy, ERC165 {
22 /// @dev EVM selector for this function is: 0x2ada85ff,
23 /// or in textual repr: transferCross((address,uint256),uint256)
24 function transferCross(CrossAddress memory to, uint256 amount) public returns (bool) {
25 require(false, stub_error);
26 to;
27 amount;
28 dummy = 0;
29 return false;
30 }
31
32 /// @dev EVM selector for this function is: 0xd5cf430b,
33 /// or in textual repr: transferFromCross((address,uint256),(address,uint256),uint256)
34 function transferFromCross(
35 CrossAddress memory from,
36 CrossAddress memory to,
37 uint256 amount
38 ) public returns (bool) {
39 require(false, stub_error);
40 from;
41 to;
42 amount;
43 dummy = 0;
44 return false;
45 }
46}
47
48/// Cross account struct
49struct CrossAddress {
50 address eth;
51 uint256 sub;
52}
1953
20/// @dev inlined interface54/// @dev inlined interface
21contract ERC20Events {55contract ERC20Events {
2559
26/// @dev the ERC-165 identifier for this interface is 0x942e8b2260/// @dev the ERC-165 identifier for this interface is 0x942e8b22
27contract ERC20 is Dummy, ERC165, ERC20Events {61contract ERC20 is Dummy, ERC165, ERC20Events {
28 /// @dev EVM selector for this function is: 0x06fdde03,62 /// @dev EVM selector for this function is: 0xdd62ed3e,
29 /// or in textual repr: name()63 /// or in textual repr: allowance(address,address)
30 function name() public view returns (string memory) {64 function allowance(address owner, address spender) public view returns (uint256) {
31 require(false, stub_error);65 require(false, stub_error);
66 owner;
67 spender;
32 dummy;68 dummy;
33 return "";69 return 0;
34 }70 }
3571
36 /// @dev EVM selector for this function is: 0x95d89b41,72 /// @dev EVM selector for this function is: 0x095ea7b3,
37 /// or in textual repr: symbol()73 /// or in textual repr: approve(address,uint256)
38 function symbol() public view returns (string memory) {74 function approve(address spender, uint256 amount) public returns (bool) {
39 require(false, stub_error);75 require(false, stub_error);
76 spender;
77 amount;
40 dummy;78 dummy = 0;
41 return "";79 return false;
42 }80 }
4381
44 /// @dev EVM selector for this function is: 0x18160ddd,82 /// @dev EVM selector for this function is: 0x70a08231,
45 /// or in textual repr: totalSupply()83 /// or in textual repr: balanceOf(address)
46 function totalSupply() public view returns (uint256) {84 function balanceOf(address owner) public view returns (uint256) {
47 require(false, stub_error);85 require(false, stub_error);
86 owner;
48 dummy;87 dummy;
49 return 0;88 return 0;
50 }89 }
57 return 0;96 return 0;
58 }97 }
5998
60 /// @dev EVM selector for this function is: 0x70a08231,99 /// @dev EVM selector for this function is: 0x06fdde03,
61 /// or in textual repr: balanceOf(address)100 /// or in textual repr: name()
62 function balanceOf(address owner) public view returns (uint256) {101 function name() public view returns (string memory) {
63 require(false, stub_error);102 require(false, stub_error);
64 owner;
65 dummy;103 dummy;
66 return 0;104 return "";
67 }105 }
68106
69 /// @dev EVM selector for this function is: 0xa9059cbb,107 /// @dev EVM selector for this function is: 0x95d89b41,
70 /// or in textual repr: transfer(address,uint256)108 /// or in textual repr: symbol()
71 function transfer(address to, uint256 amount) public returns (bool) {109 function symbol() public view returns (string memory) {
72 require(false, stub_error);110 require(false, stub_error);
73 to;
74 amount;
75 dummy = 0;111 dummy;
76 return false;112 return "";
77 }113 }
78114
79 /// @dev EVM selector for this function is: 0x23b872dd,115 /// @dev EVM selector for this function is: 0x18160ddd,
80 /// or in textual repr: transferFrom(address,address,uint256)116 /// or in textual repr: totalSupply()
81 function transferFrom(117 function totalSupply() public view returns (uint256) {
82 address from,
83 address to,
84 uint256 amount
85 ) public returns (bool) {
86 require(false, stub_error);118 require(false, stub_error);
87 from;
88 to;
89 amount;
90 dummy = 0;119 dummy;
91 return false;120 return 0;
92 }121 }
93122
94 /// @dev EVM selector for this function is: 0x095ea7b3,123 /// @dev EVM selector for this function is: 0xa9059cbb,
95 /// or in textual repr: approve(address,uint256)124 /// or in textual repr: transfer(address,uint256)
96 function approve(address spender, uint256 amount) public returns (bool) {125 function transfer(address to, uint256 amount) public returns (bool) {
97 require(false, stub_error);126 require(false, stub_error);
98 spender;127 to;
99 amount;128 amount;
100 dummy = 0;129 dummy = 0;
101 return false;130 return false;
102 }131 }
103132
104 /// @dev EVM selector for this function is: 0xdd62ed3e,133 /// @dev EVM selector for this function is: 0x23b872dd,
105 /// or in textual repr: allowance(address,address)134 /// or in textual repr: transferFrom(address,address,uint256)
106 function allowance(address owner, address spender) public view returns (uint256) {135 function transferFrom(
136 address from,
137 address to,
138 uint256 amount
139 ) public returns (bool) {
107 require(false, stub_error);140 require(false, stub_error);
108 owner;141 from;
109 spender;142 to;
143 amount;
110 dummy;144 dummy = 0;
111 return 0;145 return false;
112 }146 }
113}147}
114148
115contract UniqueNativeFungible is Dummy, ERC165, ERC20 {}149contract UniqueNativeFungible is Dummy, ERC165, ERC20, ERC20UniqueExtensions {}
116150
modifiedtests/src/eth/abi/nativeFungible.jsondiffbeforeafterboth
125 "stateMutability": "nonpayable",125 "stateMutability": "nonpayable",
126 "type": "function"126 "type": "function"
127 },127 },
128 {
129 "inputs": [
130 {
131 "components": [
132 { "internalType": "address", "name": "eth", "type": "address" },
133 { "internalType": "uint256", "name": "sub", "type": "uint256" }
134 ],
135 "internalType": "struct CrossAddress",
136 "name": "to",
137 "type": "tuple"
138 },
139 { "internalType": "uint256", "name": "amount", "type": "uint256" }
140 ],
141 "name": "transferCross",
142 "outputs": [{ "internalType": "bool", "name": "", "type": "bool" }],
143 "stateMutability": "nonpayable",
144 "type": "function"
145 },
128 {146 {
129 "inputs": [147 "inputs": [
130 { "internalType": "address", "name": "from", "type": "address" },148 { "internalType": "address", "name": "from", "type": "address" },
135 "outputs": [{ "internalType": "bool", "name": "", "type": "bool" }],153 "outputs": [{ "internalType": "bool", "name": "", "type": "bool" }],
136 "stateMutability": "nonpayable",154 "stateMutability": "nonpayable",
137 "type": "function"155 "type": "function"
138 }156 },
157 {
158 "inputs": [
159 {
160 "components": [
161 { "internalType": "address", "name": "eth", "type": "address" },
162 { "internalType": "uint256", "name": "sub", "type": "uint256" }
163 ],
164 "internalType": "struct CrossAddress",
165 "name": "from",
166 "type": "tuple"
167 },
168 {
169 "components": [
170 { "internalType": "address", "name": "eth", "type": "address" },
171 { "internalType": "uint256", "name": "sub", "type": "uint256" }
172 ],
173 "internalType": "struct CrossAddress",
174 "name": "to",
175 "type": "tuple"
176 },
177 { "internalType": "uint256", "name": "amount", "type": "uint256" }
178 ],
179 "name": "transferFromCross",
180 "outputs": [{ "internalType": "bool", "name": "", "type": "bool" }],
181 "stateMutability": "nonpayable",
182 "type": "function"
183 }
139]184]
140185
modifiedtests/src/eth/api/UniqueNativeFungible.soldiffbeforeafterboth
12 function supportsInterface(bytes4 interfaceID) external view returns (bool);12 function supportsInterface(bytes4 interfaceID) external view returns (bool);
13}13}
14
15/// @dev the ERC-165 identifier for this interface is 0xff15c6f4
16interface ERC20UniqueExtensions is Dummy, ERC165 {
17 /// @dev EVM selector for this function is: 0x2ada85ff,
18 /// or in textual repr: transferCross((address,uint256),uint256)
19 function transferCross(CrossAddress memory to, uint256 amount) external returns (bool);
20
21 /// @dev EVM selector for this function is: 0xd5cf430b,
22 /// or in textual repr: transferFromCross((address,uint256),(address,uint256),uint256)
23 function transferFromCross(
24 CrossAddress memory from,
25 CrossAddress memory to,
26 uint256 amount
27 ) external returns (bool);
28}
29
30/// Cross account struct
31struct CrossAddress {
32 address eth;
33 uint256 sub;
34}
1435
15/// @dev inlined interface36/// @dev inlined interface
16interface ERC20Events {37interface ERC20Events {
2041
21/// @dev the ERC-165 identifier for this interface is 0x942e8b2242/// @dev the ERC-165 identifier for this interface is 0x942e8b22
22interface ERC20 is Dummy, ERC165, ERC20Events {43interface ERC20 is Dummy, ERC165, ERC20Events {
23 /// @dev EVM selector for this function is: 0x06fdde03,44 /// @dev EVM selector for this function is: 0xdd62ed3e,
24 /// or in textual repr: name()45 /// or in textual repr: allowance(address,address)
25 function name() external view returns (string memory);46 function allowance(address owner, address spender) external view returns (uint256);
2647
27 /// @dev EVM selector for this function is: 0x95d89b41,48 /// @dev EVM selector for this function is: 0x095ea7b3,
28 /// or in textual repr: symbol()49 /// or in textual repr: approve(address,uint256)
29 function symbol() external view returns (string memory);50 function approve(address spender, uint256 amount) external returns (bool);
3051
31 /// @dev EVM selector for this function is: 0x18160ddd,52 /// @dev EVM selector for this function is: 0x70a08231,
32 /// or in textual repr: totalSupply()53 /// or in textual repr: balanceOf(address)
33 function totalSupply() external view returns (uint256);54 function balanceOf(address owner) external view returns (uint256);
3455
35 /// @dev EVM selector for this function is: 0x313ce567,56 /// @dev EVM selector for this function is: 0x313ce567,
36 /// or in textual repr: decimals()57 /// or in textual repr: decimals()
37 function decimals() external view returns (uint8);58 function decimals() external view returns (uint8);
3859
39 /// @dev EVM selector for this function is: 0x70a08231,60 /// @dev EVM selector for this function is: 0x06fdde03,
40 /// or in textual repr: balanceOf(address)61 /// or in textual repr: name()
62 function name() external view returns (string memory);
63
64 /// @dev EVM selector for this function is: 0x95d89b41,
65 /// or in textual repr: symbol()
66 function symbol() external view returns (string memory);
67
68 /// @dev EVM selector for this function is: 0x18160ddd,
69 /// or in textual repr: totalSupply()
41 function balanceOf(address owner) external view returns (uint256);70 function totalSupply() external view returns (uint256);
4271
43 /// @dev EVM selector for this function is: 0xa9059cbb,72 /// @dev EVM selector for this function is: 0xa9059cbb,
44 /// or in textual repr: transfer(address,uint256)73 /// or in textual repr: transfer(address,uint256)
52 uint256 amount81 uint256 amount
53 ) external returns (bool);82 ) external returns (bool);
54
55 /// @dev EVM selector for this function is: 0x095ea7b3,
56 /// or in textual repr: approve(address,uint256)
57 function approve(address spender, uint256 amount) external returns (bool);
58
59 /// @dev EVM selector for this function is: 0xdd62ed3e,
60 /// or in textual repr: allowance(address,address)
61 function allowance(address owner, address spender) external view returns (uint256);
62}83}
6384
64interface UniqueNativeFungible is Dummy, ERC165, ERC20 {}85interface UniqueNativeFungible is Dummy, ERC165, ERC20, ERC20UniqueExtensions {}
6586