difftreelog
feat impl transfer, transer_from, transfer_cross, transfer_from_cross,
in: master
6 files changed
pallets/balances-adapter/src/erc.rsdiffbeforeafterboth1use crate::Config;2use evm_coder::{abi::AbiType, AbiCoder, ToLog, generate_stubgen, solidity_interface, types::*};3use frame_support::traits::{Currency, ExistenceRequirement};4use pallet_common::{5 erc::{CommonEvmHandler, CrossAccountId, PrecompileHandle, PrecompileResult},6 eth::CrossAddress,7};8use pallet_evm_coder_substrate::{9 call, dispatch_to_evm,10 execution::{PreDispatch, Result},11 frontier_contract, WithRecorder, SubstrateRecorder,12};13use sp_core::{U256, Get};14use sp_std::vec::Vec;1516frontier_contract! {17 macro_rules! NativeFungibleHandle_result {...}18 impl<T: Config> Contract for NativeFungibleHandle<T> {...}19}2021#[derive(ToLog)]22pub enum ERC20Events {23 Transfer {24 #[indexed]25 from: Address,26 #[indexed]27 to: Address,28 value: U256,29 },30 Approval {31 #[indexed]32 owner: Address,33 #[indexed]34 spender: Address,35 value: U256,36 },37}3839pub struct NativeFungibleHandle<T: Config>(SubstrateRecorder<T>);4041impl<T: Config> WithRecorder<T> for NativeFungibleHandle<T> {42 fn recorder(&self) -> &pallet_evm_coder_substrate::SubstrateRecorder<T> {43 &self.044 }45 fn into_recorder(self) -> pallet_evm_coder_substrate::SubstrateRecorder<T> {46 self.047 }48}4950#[solidity_interface(name = ERC20, events(ERC20Events), enum(derive(PreDispatch)), enum_attr(weight), expect_selector = 0x942e8b22)]51impl<T: Config> NativeFungibleHandle<T> {52 fn allowance(&self, owner: Address, spender: Address) -> Result<U256> {53 Ok(U256::zero())54 }5556 // #[weight(<SelfWeightOf<T>>::approve())]57 fn approve(&mut self, caller: Caller, spender: Address, amount: U256) -> Result<bool> {58 // self.consume_store_reads(1)?;59 Err("Approve not supported now".into())60 }61 fn balance_of(&self, owner: Address) -> Result<U256> {62 // self.consume_store_reads(1)?;63 let owner = T::CrossAccountId::from_eth(owner);64 let balance = <T as Config>::Currency::free_balance(owner.as_sub());65 Ok(balance.into())66 }6768 fn decimals(&self) -> Result<u8> {69 Ok(T::Decimals::get())70 }7172 fn name(&self) -> Result<String> {73 Ok(T::Name::get())74 }7576 fn symbol(&self) -> Result<String> {77 Ok(T::Symbol::get())78 }7980 fn total_supply(&self) -> Result<U256> {81 // self.consume_store_reads(1)?;82 let total = <T as Config>::Currency::total_issuance();83 Ok(total.into())84 }8586 // #[weight(<SelfWeightOf<T>>::transfer())]87 fn transfer(&mut self, caller: Caller, to: Address, amount: U256) -> Result<bool> {88 let caller = T::CrossAccountId::from_eth(caller);89 let to = T::CrossAccountId::from_eth(to);90 let amount = amount.try_into().map_err(|_| "amount overflow")?;91 // let budget = self92 // .recorder93 // .weight_calls_budget(<StructureWeight<T>>::find_parent());9495 // <Pallet<T>>::transfer(self, &caller, &to, amount, &budget).map_err(|_| "transfer error")?;96 <T as Config>::Currency::transfer(97 caller.as_sub(),98 to.as_sub(),99 amount,100 ExistenceRequirement::KeepAlive,101 )102 .map_err(dispatch_to_evm::<T>);103 Ok(true)104 }105106 // #[weight(<SelfWeightOf<T>>::transfer_from())]107 fn transfer_from(108 &mut self,109 caller: Caller,110 from: Address,111 to: Address,112 amount: U256,113 ) -> Result<bool> {114 let caller = T::CrossAccountId::from_eth(caller);115 let from = T::CrossAccountId::from_eth(from);116 let to = T::CrossAccountId::from_eth(to);117 let amount = amount.try_into().map_err(|_| "amount overflow")?;118119 if (from != to) {120 return Err("no permission".into());121 }122 // let budget = self123 // .recorder124 // .weight_calls_budget(<StructureWeight<T>>::find_parent());125126 // <Pallet<T>>::transfer_from(self, &caller, &from, &to, amount, &budget)127 // .map_err(dispatch_to_evm::<T>)?;128 <T as Config>::Currency::transfer(129 caller.as_sub(),130 to.as_sub(),131 amount,132 ExistenceRequirement::KeepAlive,133 )134 .map_err(dispatch_to_evm::<T>);135 Ok(true)136 }137}138139#[solidity_interface(name = ERC20UniqueExtensions, enum(derive(PreDispatch)), enum_attr(weight))]140impl<T: Config> NativeFungibleHandle<T>141where142 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 = self150 // .recorder151 // .weight_calls_budget(<StructureWeight<T>>::find_parent());152153 // <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 }163164 // #[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")?;176177 if (from != to) {178 return Err("no permission".into());179 }180181 // let budget = self182 // .recorder183 // .weight_calls_budget(<StructureWeight<T>>::find_parent());184185 // <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}197198#[solidity_interface(199 name = UniqueNativeFungible,200 is(ERC20, ERC20UniqueExtensions),201 enum(derive(PreDispatch))202)]203impl<T: Config> NativeFungibleHandle<T> where T::AccountId: From<[u8; 32]> + AsRef<[u8; 32]> {}204205generate_stubgen!(gen_impl, UniqueNativeFungibleCall<()>, true);206generate_stubgen!(gen_iface, UniqueNativeFungibleCall<()>, false);207208impl<T: Config> CommonEvmHandler for NativeFungibleHandle<T>209where210 T::AccountId: From<[u8; 32]> + AsRef<[u8; 32]>,211{212 const CODE: &'static [u8] = include_bytes!("./stubs/UniqueNativeFungible.raw");213214 fn call(self, handle: &mut impl PrecompileHandle) -> Option<PrecompileResult> {215 call::<T, UniqueNativeFungibleCall<T>, _, _>(handle, self)216 }217}pallets/balances-adapter/src/lib.rsdiffbeforeafterboth--- a/pallets/balances-adapter/src/lib.rs
+++ b/pallets/balances-adapter/src/lib.rs
@@ -17,7 +17,7 @@
Self::AccountId,
Balance = Self::CurrencyBalance,
>;
- type CurrencyBalance: Into<U256>;
+ type CurrencyBalance: Into<U256> + TryFrom<U256>;
type Decimals: Get<u8>;
type Name: Get<String>;
pallets/balances-adapter/src/stubs/UniqueNativeFungible.rawdiffbeforeafterbothbinary blob — no preview
pallets/balances-adapter/src/stubs/UniqueNativeFungible.soldiffbeforeafterboth--- a/pallets/balances-adapter/src/stubs/UniqueNativeFungible.sol
+++ b/pallets/balances-adapter/src/stubs/UniqueNativeFungible.sol
@@ -17,6 +17,40 @@
}
}
+/// @dev the ERC-165 identifier for this interface is 0xff15c6f4
+contract ERC20UniqueExtensions is Dummy, ERC165 {
+ /// @dev EVM selector for this function is: 0x2ada85ff,
+ /// or in textual repr: transferCross((address,uint256),uint256)
+ function transferCross(CrossAddress memory to, uint256 amount) public returns (bool) {
+ require(false, stub_error);
+ to;
+ amount;
+ dummy = 0;
+ return false;
+ }
+
+ /// @dev EVM selector for this function is: 0xd5cf430b,
+ /// or in textual repr: transferFromCross((address,uint256),(address,uint256),uint256)
+ function transferFromCross(
+ CrossAddress memory from,
+ CrossAddress memory to,
+ uint256 amount
+ ) public returns (bool) {
+ require(false, stub_error);
+ from;
+ to;
+ amount;
+ dummy = 0;
+ return false;
+ }
+}
+
+/// Cross account struct
+struct CrossAddress {
+ address eth;
+ uint256 sub;
+}
+
/// @dev inlined interface
contract ERC20Events {
event Transfer(address indexed from, address indexed to, uint256 value);
@@ -25,26 +59,31 @@
/// @dev the ERC-165 identifier for this interface is 0x942e8b22
contract ERC20 is Dummy, ERC165, ERC20Events {
- /// @dev EVM selector for this function is: 0x06fdde03,
- /// or in textual repr: name()
- function name() public view returns (string memory) {
+ /// @dev EVM selector for this function is: 0xdd62ed3e,
+ /// or in textual repr: allowance(address,address)
+ function allowance(address owner, address spender) public view returns (uint256) {
require(false, stub_error);
+ owner;
+ spender;
dummy;
- return "";
+ return 0;
}
- /// @dev EVM selector for this function is: 0x95d89b41,
- /// or in textual repr: symbol()
- function symbol() public view returns (string memory) {
+ /// @dev EVM selector for this function is: 0x095ea7b3,
+ /// or in textual repr: approve(address,uint256)
+ function approve(address spender, uint256 amount) public returns (bool) {
require(false, stub_error);
- dummy;
- return "";
+ spender;
+ amount;
+ dummy = 0;
+ return false;
}
- /// @dev EVM selector for this function is: 0x18160ddd,
- /// or in textual repr: totalSupply()
- function totalSupply() public view returns (uint256) {
+ /// @dev EVM selector for this function is: 0x70a08231,
+ /// or in textual repr: balanceOf(address)
+ function balanceOf(address owner) public view returns (uint256) {
require(false, stub_error);
+ owner;
dummy;
return 0;
}
@@ -57,11 +96,26 @@
return 0;
}
- /// @dev EVM selector for this function is: 0x70a08231,
- /// or in textual repr: balanceOf(address)
- function balanceOf(address owner) public view returns (uint256) {
+ /// @dev EVM selector for this function is: 0x06fdde03,
+ /// or in textual repr: name()
+ function name() public view returns (string memory) {
+ require(false, stub_error);
+ dummy;
+ return "";
+ }
+
+ /// @dev EVM selector for this function is: 0x95d89b41,
+ /// or in textual repr: symbol()
+ function symbol() public view returns (string memory) {
+ require(false, stub_error);
+ dummy;
+ return "";
+ }
+
+ /// @dev EVM selector for this function is: 0x18160ddd,
+ /// or in textual repr: totalSupply()
+ function totalSupply() public view returns (uint256) {
require(false, stub_error);
- owner;
dummy;
return 0;
}
@@ -90,26 +144,6 @@
dummy = 0;
return false;
}
-
- /// @dev EVM selector for this function is: 0x095ea7b3,
- /// or in textual repr: approve(address,uint256)
- function approve(address spender, uint256 amount) public returns (bool) {
- require(false, stub_error);
- spender;
- amount;
- dummy = 0;
- return false;
- }
-
- /// @dev EVM selector for this function is: 0xdd62ed3e,
- /// or in textual repr: allowance(address,address)
- function allowance(address owner, address spender) public view returns (uint256) {
- require(false, stub_error);
- owner;
- spender;
- dummy;
- return 0;
- }
}
-contract UniqueNativeFungible is Dummy, ERC165, ERC20 {}
+contract UniqueNativeFungible is Dummy, ERC165, ERC20, ERC20UniqueExtensions {}
tests/src/eth/abi/nativeFungible.jsondiffbeforeafterboth--- a/tests/src/eth/abi/nativeFungible.json
+++ b/tests/src/eth/abi/nativeFungible.json
@@ -127,6 +127,24 @@
},
{
"inputs": [
+ {
+ "components": [
+ { "internalType": "address", "name": "eth", "type": "address" },
+ { "internalType": "uint256", "name": "sub", "type": "uint256" }
+ ],
+ "internalType": "struct CrossAddress",
+ "name": "to",
+ "type": "tuple"
+ },
+ { "internalType": "uint256", "name": "amount", "type": "uint256" }
+ ],
+ "name": "transferCross",
+ "outputs": [{ "internalType": "bool", "name": "", "type": "bool" }],
+ "stateMutability": "nonpayable",
+ "type": "function"
+ },
+ {
+ "inputs": [
{ "internalType": "address", "name": "from", "type": "address" },
{ "internalType": "address", "name": "to", "type": "address" },
{ "internalType": "uint256", "name": "amount", "type": "uint256" }
@@ -135,5 +153,32 @@
"outputs": [{ "internalType": "bool", "name": "", "type": "bool" }],
"stateMutability": "nonpayable",
"type": "function"
+ },
+ {
+ "inputs": [
+ {
+ "components": [
+ { "internalType": "address", "name": "eth", "type": "address" },
+ { "internalType": "uint256", "name": "sub", "type": "uint256" }
+ ],
+ "internalType": "struct CrossAddress",
+ "name": "from",
+ "type": "tuple"
+ },
+ {
+ "components": [
+ { "internalType": "address", "name": "eth", "type": "address" },
+ { "internalType": "uint256", "name": "sub", "type": "uint256" }
+ ],
+ "internalType": "struct CrossAddress",
+ "name": "to",
+ "type": "tuple"
+ },
+ { "internalType": "uint256", "name": "amount", "type": "uint256" }
+ ],
+ "name": "transferFromCross",
+ "outputs": [{ "internalType": "bool", "name": "", "type": "bool" }],
+ "stateMutability": "nonpayable",
+ "type": "function"
}
]
tests/src/eth/api/UniqueNativeFungible.soldiffbeforeafterboth--- a/tests/src/eth/api/UniqueNativeFungible.sol
+++ b/tests/src/eth/api/UniqueNativeFungible.sol
@@ -12,6 +12,27 @@
function supportsInterface(bytes4 interfaceID) external view returns (bool);
}
+/// @dev the ERC-165 identifier for this interface is 0xff15c6f4
+interface ERC20UniqueExtensions is Dummy, ERC165 {
+ /// @dev EVM selector for this function is: 0x2ada85ff,
+ /// or in textual repr: transferCross((address,uint256),uint256)
+ function transferCross(CrossAddress memory to, uint256 amount) external returns (bool);
+
+ /// @dev EVM selector for this function is: 0xd5cf430b,
+ /// or in textual repr: transferFromCross((address,uint256),(address,uint256),uint256)
+ function transferFromCross(
+ CrossAddress memory from,
+ CrossAddress memory to,
+ uint256 amount
+ ) external returns (bool);
+}
+
+/// Cross account struct
+struct CrossAddress {
+ address eth;
+ uint256 sub;
+}
+
/// @dev inlined interface
interface ERC20Events {
event Transfer(address indexed from, address indexed to, uint256 value);
@@ -20,6 +41,22 @@
/// @dev the ERC-165 identifier for this interface is 0x942e8b22
interface ERC20 is Dummy, ERC165, ERC20Events {
+ /// @dev EVM selector for this function is: 0xdd62ed3e,
+ /// or in textual repr: allowance(address,address)
+ function allowance(address owner, address spender) external view returns (uint256);
+
+ /// @dev EVM selector for this function is: 0x095ea7b3,
+ /// or in textual repr: approve(address,uint256)
+ function approve(address spender, uint256 amount) external returns (bool);
+
+ /// @dev EVM selector for this function is: 0x70a08231,
+ /// or in textual repr: balanceOf(address)
+ function balanceOf(address owner) external view returns (uint256);
+
+ /// @dev EVM selector for this function is: 0x313ce567,
+ /// or in textual repr: decimals()
+ function decimals() external view returns (uint8);
+
/// @dev EVM selector for this function is: 0x06fdde03,
/// or in textual repr: name()
function name() external view returns (string memory);
@@ -32,14 +69,6 @@
/// or in textual repr: totalSupply()
function totalSupply() external view returns (uint256);
- /// @dev EVM selector for this function is: 0x313ce567,
- /// or in textual repr: decimals()
- function decimals() external view returns (uint8);
-
- /// @dev EVM selector for this function is: 0x70a08231,
- /// or in textual repr: balanceOf(address)
- function balanceOf(address owner) external view returns (uint256);
-
/// @dev EVM selector for this function is: 0xa9059cbb,
/// or in textual repr: transfer(address,uint256)
function transfer(address to, uint256 amount) external returns (bool);
@@ -51,14 +80,6 @@
address to,
uint256 amount
) external returns (bool);
-
- /// @dev EVM selector for this function is: 0x095ea7b3,
- /// or in textual repr: approve(address,uint256)
- function approve(address spender, uint256 amount) external returns (bool);
-
- /// @dev EVM selector for this function is: 0xdd62ed3e,
- /// or in textual repr: allowance(address,address)
- function allowance(address owner, address spender) external view returns (uint256);
}
-interface UniqueNativeFungible is Dummy, ERC165, ERC20 {}
+interface UniqueNativeFungible is Dummy, ERC165, ERC20, ERC20UniqueExtensions {}