difftreelog
fix change sponsor to OptioCrossAddress
in: master
9 files changed
crates/evm-coder/src/abi/impls.rsdiffbeforeafterboth1use crate::{2 custom_signature::SignatureUnit,3 execution::{Result, ResultWithPostInfo, WithPostDispatchInfo},4 make_signature, sealed,5 types::*,6};7use super::{traits::*, ABI_ALIGNMENT, AbiReader, AbiWriter};8use primitive_types::{U256, H160};910#[cfg(not(feature = "std"))]11use alloc::vec::Vec;1213macro_rules! impl_abi_type {14 ($ty:ty, $name:ident, $dynamic:literal) => {15 impl sealed::CanBePlacedInVec for $ty {}1617 impl AbiType for $ty {18 const SIGNATURE: SignatureUnit = make_signature!(new fixed(stringify!($name)));1920 fn is_dynamic() -> bool {21 $dynamic22 }2324 fn size() -> usize {25 ABI_ALIGNMENT26 }27 }28 };29}3031macro_rules! impl_abi_readable {32 ($ty:ty, $method:ident) => {33 impl AbiRead for $ty {34 fn abi_read(reader: &mut AbiReader) -> Result<$ty> {35 reader.$method()36 }37 }38 };39}4041macro_rules! impl_abi_writeable {42 ($ty:ty, $method:ident) => {43 impl AbiWrite for $ty {44 fn abi_write(&self, writer: &mut AbiWriter) {45 writer.$method(&self)46 }47 }48 };49}5051macro_rules! impl_abi {52 ($ty:ty, $method:ident, $dynamic:literal) => {53 impl_abi_type!($ty, $method, $dynamic);54 impl_abi_readable!($ty, $method);55 impl_abi_writeable!($ty, $method);56 };57}5859impl_abi!(bool, bool, false);60impl_abi!(u8, uint8, false);61impl_abi!(u32, uint32, false);62impl_abi!(u64, uint64, false);63impl_abi!(u128, uint128, false);64impl_abi!(U256, uint256, false);65impl_abi!(H160, address, false);66impl_abi!(string, string, true);6768impl_abi_writeable!(&str, string);6970impl_abi_type!(bytes, bytes, true);7172impl AbiRead for bytes {73 fn abi_read(reader: &mut AbiReader) -> Result<bytes> {74 Ok(bytes(reader.bytes()?))75 }76}7778impl AbiWrite for bytes {79 fn abi_write(&self, writer: &mut AbiWriter) {80 writer.bytes(self.0.as_slice())81 }82}8384impl_abi_type!(bytes4, bytes4, false);85impl AbiRead for bytes4 {86 fn abi_read(reader: &mut AbiReader) -> Result<bytes4> {87 reader.bytes4()88 }89}9091impl<T: AbiWrite> AbiWrite for &T {92 fn abi_write(&self, writer: &mut AbiWriter) {93 T::abi_write(self, writer);94 }95}9697impl<T: AbiType> AbiType for &T {98 const SIGNATURE: SignatureUnit = T::SIGNATURE;99100 fn is_dynamic() -> bool {101 T::is_dynamic()102 }103104 fn size() -> usize {105 T::size()106 }107}108109impl<T: AbiType + AbiRead + sealed::CanBePlacedInVec> AbiRead for Vec<T> {110 fn abi_read(reader: &mut AbiReader) -> Result<Vec<T>> {111 let mut sub = reader.subresult(None)?;112 let size = sub.uint32()? as usize;113 sub.subresult_offset = sub.offset;114 let is_dynamic = <T as AbiType>::is_dynamic();115 let mut out = Vec::with_capacity(size);116 for _ in 0..size {117 out.push(<T as AbiRead>::abi_read(&mut sub)?);118 if !is_dynamic {119 sub.bytes_read(<T as AbiType>::size());120 };121 }122 Ok(out)123 }124}125126impl<T: AbiType> AbiType for Vec<T> {127 const SIGNATURE: SignatureUnit = make_signature!(new nameof(T::SIGNATURE) fixed("[]"));128129 fn is_dynamic() -> bool {130 true131 }132133 fn size() -> usize {134 ABI_ALIGNMENT135 }136}137138impl<T: AbiWrite + AbiType> AbiWrite for Vec<T> {139 fn abi_write(&self, writer: &mut AbiWriter) {140 let is_dynamic = T::is_dynamic();141 let mut sub = if is_dynamic {142 AbiWriter::new_dynamic(is_dynamic)143 } else {144 AbiWriter::new()145 };146147 // Write items count148 (self.len() as u32).abi_write(&mut sub);149150 for item in self {151 item.abi_write(&mut sub);152 }153 writer.write_subresult(sub);154 }155}156157impl AbiWrite for () {158 fn abi_write(&self, _writer: &mut AbiWriter) {}159}160161/// This particular AbiWrite implementation should be split to another trait,162/// which only implements `to_result`, but due to lack of specialization feature163/// in stable Rust, we can't have blanket impl of this trait `for T where T: AbiWrite`,164/// so here we abusing default trait methods for it165impl<T: AbiWrite> AbiWrite for ResultWithPostInfo<T> {166 fn abi_write(&self, _writer: &mut AbiWriter) {167 debug_assert!(false, "shouldn't be called, see comment")168 }169 fn to_result(&self) -> ResultWithPostInfo<AbiWriter> {170 match self {171 Ok(v) => Ok(WithPostDispatchInfo {172 post_info: v.post_info.clone(),173 data: {174 let mut out = AbiWriter::new();175 v.data.abi_write(&mut out);176 out177 },178 }),179 Err(e) => Err(e.clone()),180 }181 }182}183184macro_rules! count {185 () => (0usize);186 ( $x:tt $($xs:tt)* ) => (1usize + count!($($xs)*));187}188189macro_rules! impl_tuples {190 ($($ident:ident)+) => {191 impl<$($ident: AbiType,)+> AbiType for ($($ident,)+)192 where193 $(194 $ident: AbiType,195 )+196 {197 const SIGNATURE: SignatureUnit = make_signature!(198 new fixed("(")199 $(nameof(<$ident>::SIGNATURE) fixed(","))+200 shift_left(1)201 fixed(")")202 );203204 fn is_dynamic() -> bool {205 false206 $(207 || <$ident>::is_dynamic()208 )*209 }210211 fn size() -> usize {212 0 $(+ <$ident>::size())+213 }214 }215216 impl<$($ident),+> sealed::CanBePlacedInVec for ($($ident,)+) {}217218 impl<$($ident),+> AbiRead for ($($ident,)+)219 where220 Self: AbiType,221 $($ident: AbiRead + AbiType,)+222 {223 fn abi_read(reader: &mut AbiReader) -> Result<($($ident,)+)> {224 let is_dynamic = <Self>::is_dynamic();225 let size = if !is_dynamic { Some(<Self>::size()) } else { None };226 let mut subresult = reader.subresult(size)?;227 Ok((228 $({229 let value = <$ident>::abi_read(&mut subresult)?;230 if !is_dynamic {subresult.bytes_read(<$ident as AbiType>::size())};231 value232 },)+233 ))234 }235 }236237 #[allow(non_snake_case)]238 impl<$($ident),+> AbiWrite for ($($ident,)+)239 where240 $($ident: AbiWrite + AbiType,)+241 {242 fn abi_write(&self, writer: &mut AbiWriter) {243 let ($($ident,)+) = self;244 if <Self as AbiType>::is_dynamic() {245 let mut sub = AbiWriter::new();246 $($ident.abi_write(&mut sub);)+247 writer.write_subresult(sub);248 } else {249 $($ident.abi_write(writer);)+250 }251 }252 }253 };254}255256impl_tuples! {A}257impl_tuples! {A B}258impl_tuples! {A B C}259impl_tuples! {A B C D}260impl_tuples! {A B C D E}261impl_tuples! {A B C D E F}262impl_tuples! {A B C D E F G}263impl_tuples! {A B C D E F G H}264impl_tuples! {A B C D E F G H I}265impl_tuples! {A B C D E F G H I J}1use crate::{2 custom_signature::SignatureUnit,3 execution::{Result, ResultWithPostInfo, WithPostDispatchInfo},4 make_signature, sealed,5 types::*,6};7use super::{traits::*, ABI_ALIGNMENT, AbiReader, AbiWriter};8use primitive_types::{U256, H160};910#[cfg(not(feature = "std"))]11use alloc::vec::Vec;1213macro_rules! impl_abi_type {14 ($ty:ty, $name:ident, $dynamic:literal) => {15 impl sealed::CanBePlacedInVec for $ty {}1617 impl AbiType for $ty {18 const SIGNATURE: SignatureUnit = make_signature!(new fixed(stringify!($name)));1920 fn is_dynamic() -> bool {21 $dynamic22 }2324 fn size() -> usize {25 ABI_ALIGNMENT26 }27 }28 };29}3031macro_rules! impl_abi_readable {32 ($ty:ty, $method:ident) => {33 impl AbiRead for $ty {34 fn abi_read(reader: &mut AbiReader) -> Result<$ty> {35 reader.$method()36 }37 }38 };39}4041macro_rules! impl_abi_writeable {42 ($ty:ty, $method:ident) => {43 impl AbiWrite for $ty {44 fn abi_write(&self, writer: &mut AbiWriter) {45 writer.$method(&self)46 }47 }48 };49}5051macro_rules! impl_abi {52 ($ty:ty, $method:ident, $dynamic:literal) => {53 impl_abi_type!($ty, $method, $dynamic);54 impl_abi_readable!($ty, $method);55 impl_abi_writeable!($ty, $method);56 };57}5859impl_abi!(bool, bool, false);60impl_abi!(u8, uint8, false);61impl_abi!(u32, uint32, false);62impl_abi!(u64, uint64, false);63impl_abi!(u128, uint128, false);64impl_abi!(U256, uint256, false);65impl_abi!(H160, address, false);66impl_abi!(string, string, true);6768impl_abi_writeable!(&str, string);6970impl_abi_type!(bytes, bytes, true);7172impl AbiRead for bytes {73 fn abi_read(reader: &mut AbiReader) -> Result<bytes> {74 Ok(bytes(reader.bytes()?))75 }76}7778impl AbiWrite for bytes {79 fn abi_write(&self, writer: &mut AbiWriter) {80 writer.bytes(self.0.as_slice())81 }82}8384impl_abi_type!(bytes4, bytes4, false);85impl AbiRead for bytes4 {86 fn abi_read(reader: &mut AbiReader) -> Result<bytes4> {87 reader.bytes4()88 }89}9091impl<T: AbiWrite> AbiWrite for &T {92 fn abi_write(&self, writer: &mut AbiWriter) {93 T::abi_write(self, writer);94 }95}9697impl<T: AbiType> AbiType for &T {98 const SIGNATURE: SignatureUnit = T::SIGNATURE;99100 fn is_dynamic() -> bool {101 T::is_dynamic()102 }103104 fn size() -> usize {105 T::size()106 }107}108109impl<T: AbiType + AbiRead + sealed::CanBePlacedInVec> AbiRead for Vec<T> {110 fn abi_read(reader: &mut AbiReader) -> Result<Vec<T>> {111 let mut sub = reader.subresult(None)?;112 let size = sub.uint32()? as usize;113 sub.subresult_offset = sub.offset;114 let is_dynamic = <T as AbiType>::is_dynamic();115 let mut out = Vec::with_capacity(size);116 for _ in 0..size {117 out.push(<T as AbiRead>::abi_read(&mut sub)?);118 if !is_dynamic {119 sub.bytes_read(<T as AbiType>::size());120 };121 }122 Ok(out)123 }124}125126impl<T: AbiType> AbiType for Vec<T> {127 const SIGNATURE: SignatureUnit = make_signature!(new nameof(T::SIGNATURE) fixed("[]"));128129 fn is_dynamic() -> bool {130 true131 }132133 fn size() -> usize {134 ABI_ALIGNMENT135 }136}137138impl<T: AbiWrite + AbiType> AbiWrite for Vec<T> {139 fn abi_write(&self, writer: &mut AbiWriter) {140 let is_dynamic = T::is_dynamic();141 let mut sub = if is_dynamic {142 AbiWriter::new_dynamic(is_dynamic)143 } else {144 AbiWriter::new()145 };146147 // Write items count148 (self.len() as u32).abi_write(&mut sub);149150 for item in self {151 item.abi_write(&mut sub);152 }153 writer.write_subresult(sub);154 }155}156157impl AbiWrite for () {158 fn abi_write(&self, _writer: &mut AbiWriter) {}159}160161/// This particular AbiWrite implementation should be split to another trait,162/// which only implements `to_result`, but due to lack of specialization feature163/// in stable Rust, we can't have blanket impl of this trait `for T where T: AbiWrite`,164/// so here we abusing default trait methods for it165impl<T: AbiWrite> AbiWrite for ResultWithPostInfo<T> {166 fn abi_write(&self, _writer: &mut AbiWriter) {167 debug_assert!(false, "shouldn't be called, see comment")168 }169 fn to_result(&self) -> ResultWithPostInfo<AbiWriter> {170 match self {171 Ok(v) => Ok(WithPostDispatchInfo {172 post_info: v.post_info.clone(),173 data: {174 let mut out = AbiWriter::new();175 v.data.abi_write(&mut out);176 out177 },178 }),179 Err(e) => Err(e.clone()),180 }181 }182}183184macro_rules! impl_tuples {185 ($($ident:ident)+) => {186 impl<$($ident: AbiType,)+> AbiType for ($($ident,)+)187 where188 $(189 $ident: AbiType,190 )+191 {192 const SIGNATURE: SignatureUnit = make_signature!(193 new fixed("(")194 $(nameof(<$ident>::SIGNATURE) fixed(","))+195 shift_left(1)196 fixed(")")197 );198199 fn is_dynamic() -> bool {200 false201 $(202 || <$ident>::is_dynamic()203 )*204 }205206 fn size() -> usize {207 0 $(+ <$ident>::size())+208 }209 }210211 impl<$($ident),+> sealed::CanBePlacedInVec for ($($ident,)+) {}212213 impl<$($ident),+> AbiRead for ($($ident,)+)214 where215 Self: AbiType,216 $($ident: AbiRead + AbiType,)+217 {218 fn abi_read(reader: &mut AbiReader) -> Result<($($ident,)+)> {219 let is_dynamic = <Self>::is_dynamic();220 let size = if !is_dynamic { Some(<Self>::size()) } else { None };221 let mut subresult = reader.subresult(size)?;222 Ok((223 $({224 let value = <$ident>::abi_read(&mut subresult)?;225 if !is_dynamic {subresult.bytes_read(<$ident as AbiType>::size())};226 value227 },)+228 ))229 }230 }231232 #[allow(non_snake_case)]233 impl<$($ident),+> AbiWrite for ($($ident,)+)234 where235 $($ident: AbiWrite + AbiType,)+236 {237 fn abi_write(&self, writer: &mut AbiWriter) {238 let ($($ident,)+) = self;239 if <Self as AbiType>::is_dynamic() {240 let mut sub = AbiWriter::new();241 $($ident.abi_write(&mut sub);)+242 writer.write_subresult(sub);243 } else {244 $($ident.abi_write(writer);)+245 }246 }247 }248 };249}250251impl_tuples! {A}252impl_tuples! {A B}253impl_tuples! {A B C}254impl_tuples! {A B C D}255impl_tuples! {A B C D E}256impl_tuples! {A B C D E F}257impl_tuples! {A B C D E F G}258impl_tuples! {A B C D E F G H}259impl_tuples! {A B C D E F G H I}260impl_tuples! {A B C D E F G H I J}pallets/common/src/eth.rsdiffbeforeafterboth--- a/pallets/common/src/eth.rs
+++ b/pallets/common/src/eth.rs
@@ -122,6 +122,13 @@
}
}
+/// Ethereum representation of Optional value with CrossAddress.
+#[derive(Debug, Default, AbiCoder)]
+pub struct OptionCrossAddress {
+ pub status: bool,
+ pub value: CrossAddress,
+}
+
/// Cross account struct
#[derive(Debug, Default, AbiCoder)]
pub struct CrossAddress {
pallets/evm-contract-helpers/src/eth.rsdiffbeforeafterboth--- a/pallets/evm-contract-helpers/src/eth.rs
+++ b/pallets/evm-contract-helpers/src/eth.rs
@@ -25,6 +25,7 @@
types::*,
ToLog,
};
+use pallet_common::eth;
use pallet_evm::{
ExitRevert, OnCreate, OnMethodCall, PrecompileResult, PrecompileFailure, PrecompileHandle,
account::CrossAccountId,
@@ -174,12 +175,17 @@
///
/// @param contractAddress The contract for which a sponsor is requested.
/// @return Tuble with sponsor address and his substrate mirror. If there is no confirmed sponsor error "Contract has no sponsor" throw.
- fn sponsor(&self, contract_address: address) -> Result<pallet_common::eth::CrossAddress> {
- Ok(
- pallet_common::eth::CrossAddress::from_sub_cross_account::<T>(
- &Pallet::<T>::get_sponsor(contract_address).ok_or("Contract has no sponsor")?,
- ),
- )
+ fn sponsor(&self, contract_address: address) -> Result<eth::OptionCrossAddress> {
+ Ok(match Pallet::<T>::get_sponsor(contract_address) {
+ Some(ref value) => eth::OptionCrossAddress {
+ status: true,
+ value: eth::CrossAddress::from_sub_cross_account::<T>(value),
+ },
+ None => eth::OptionCrossAddress {
+ status: false,
+ value: Default::default(),
+ },
+ })
}
/// Check tat contract has confirmed sponsor.
pallets/evm-contract-helpers/src/stubs/ContractHelpers.rawdiffbeforeafterbothbinary blob — no preview
pallets/evm-contract-helpers/src/stubs/ContractHelpers.soldiffbeforeafterboth--- a/pallets/evm-contract-helpers/src/stubs/ContractHelpers.sol
+++ b/pallets/evm-contract-helpers/src/stubs/ContractHelpers.sol
@@ -96,11 +96,11 @@
/// @return Tuble with sponsor address and his substrate mirror. If there is no confirmed sponsor error "Contract has no sponsor" throw.
/// @dev EVM selector for this function is: 0x766c4f37,
/// or in textual repr: sponsor(address)
- function sponsor(address contractAddress) public view returns (CrossAddress memory) {
+ function sponsor(address contractAddress) public view returns (OptionCrossAddress memory) {
require(false, stub_error);
contractAddress;
dummy;
- return CrossAddress(0x0000000000000000000000000000000000000000, 0);
+ return OptionCrossAddress(false, CrossAddress(0x0000000000000000000000000000000000000000, 0));
}
/// Check tat contract has confirmed sponsor.
@@ -270,3 +270,9 @@
address eth;
uint256 sub;
}
+
+/// @dev Ethereum representation of Optional value with CrossAddress.
+struct OptionCrossAddress {
+ bool status;
+ CrossAddress value;
+}
pallets/unique/src/lib.rsdiffbeforeafterboth--- a/pallets/unique/src/lib.rs
+++ b/pallets/unique/src/lib.rs
@@ -74,7 +74,7 @@
extern crate alloc;
use frame_support::{
- decl_module, decl_storage, decl_error, decl_event,
+ decl_module, decl_storage, decl_error,
dispatch::DispatchResult,
ensure, fail,
weights::{Weight},
tests/src/eth/abi/contractHelpers.jsondiffbeforeafterboth--- a/tests/src/eth/abi/contractHelpers.json
+++ b/tests/src/eth/abi/contractHelpers.json
@@ -223,10 +223,18 @@
"outputs": [
{
"components": [
- { "internalType": "address", "name": "eth", "type": "address" },
- { "internalType": "uint256", "name": "sub", "type": "uint256" }
+ { "internalType": "bool", "name": "status", "type": "bool" },
+ {
+ "components": [
+ { "internalType": "address", "name": "eth", "type": "address" },
+ { "internalType": "uint256", "name": "sub", "type": "uint256" }
+ ],
+ "internalType": "struct CrossAddress",
+ "name": "value",
+ "type": "tuple"
+ }
],
- "internalType": "struct CrossAddress",
+ "internalType": "struct OptionCrossAddress",
"name": "",
"type": "tuple"
}
tests/src/eth/api/ContractHelpers.soldiffbeforeafterboth--- a/tests/src/eth/api/ContractHelpers.sol
+++ b/tests/src/eth/api/ContractHelpers.sol
@@ -69,7 +69,7 @@
/// @return Tuble with sponsor address and his substrate mirror. If there is no confirmed sponsor error "Contract has no sponsor" throw.
/// @dev EVM selector for this function is: 0x766c4f37,
/// or in textual repr: sponsor(address)
- function sponsor(address contractAddress) external view returns (CrossAddress memory);
+ function sponsor(address contractAddress) external view returns (OptionCrossAddress memory);
/// Check tat contract has confirmed sponsor.
///
@@ -171,6 +171,12 @@
function toggleAllowlist(address contractAddress, bool enabled) external;
}
+/// @dev Ethereum representation of Optional value with CrossAddress.
+struct OptionCrossAddress {
+ bool status;
+ CrossAddress value;
+}
+
/// @dev Cross account struct
struct CrossAddress {
address eth;
tests/src/eth/contractSponsoring.test.tsdiffbeforeafterboth--- a/tests/src/eth/contractSponsoring.test.ts
+++ b/tests/src/eth/contractSponsoring.test.ts
@@ -42,7 +42,9 @@
expect(await helpers.methods.hasSponsor(flipper.options.address).call()).to.be.true;
// 1.1 Can get sponsor using methods.sponsor:
- const actualSponsor = await helpers.methods.sponsor(flipper.options.address).call();
+ const actualSponsorOpt = await helpers.methods.sponsor(flipper.options.address).call();
+ expect(actualSponsorOpt.status).to.be.true;
+ const actualSponsor = actualSponsorOpt.value;
expect(actualSponsor.eth).to.eq(flipper.options.address);
expect(actualSponsor.sub).to.eq('0');
@@ -151,7 +153,9 @@
expect(await helpers.methods.hasSponsor(flipper.options.address).call()).to.be.true;
// 1.1 Can get sponsor using methods.sponsor:
- const actualSponsor = await helpers.methods.sponsor(flipper.options.address).call();
+ const actualSponsorOpt = await helpers.methods.sponsor(flipper.options.address).call();
+ expect(actualSponsorOpt.status).to.be.true;
+ const actualSponsor = actualSponsorOpt.value;
expect(actualSponsor.eth).to.eq(sponsor);
expect(actualSponsor.sub).to.eq('0');