--- a/pallets/common/src/erc.rs +++ b/pallets/common/src/erc.rs @@ -284,6 +284,11 @@ fn collection_limits(&self) -> Result> { let limits = &self.collection.limits; + let convert_value_from_bool = |ob: Option| match ob { + Some(b) => Some(b as u32), + None => None, + }; + Ok(vec![ eth::CollectionLimit::new( eth::CollectionLimitField::AccountTokenOwnership, @@ -297,15 +302,15 @@ .sponsored_data_rate_limit .and_then(|limit| { if let SponsoringRateLimit::Blocks(blocks) = limit { - Some(eth::CollectionLimit::new::( + Some(eth::CollectionLimit::new( eth::CollectionLimitField::SponsoredDataRateLimit, - blocks, + Some(blocks), )) } else { None } }) - .unwrap_or(eth::CollectionLimit::new::( + .unwrap_or(eth::CollectionLimit::new( eth::CollectionLimitField::SponsoredDataRateLimit, Default::default(), )), @@ -320,15 +325,15 @@ ), eth::CollectionLimit::new( eth::CollectionLimitField::OwnerCanTransfer, - limits.owner_can_transfer, + convert_value_from_bool(limits.owner_can_transfer), ), eth::CollectionLimit::new( eth::CollectionLimitField::OwnerCanDestroy, - limits.owner_can_destroy, + convert_value_from_bool(limits.owner_can_destroy), ), eth::CollectionLimit::new( eth::CollectionLimitField::TransferEnabled, - limits.transfers_enabled, + convert_value_from_bool(limits.transfers_enabled), ), ]) } --- a/pallets/common/src/eth.rs +++ b/pallets/common/src/eth.rs @@ -66,62 +66,6 @@ T::CrossAccountId::from_sub(account_id) } -/// Ethereum representation of Optional value with uint256. -#[derive(Debug, Default, AbiCoder)] -pub struct OptionUint { - status: bool, - value: uint256, -} - -impl From for OptionUint { - fn from(value: u32) -> Self { - Self { - status: true, - value: uint256::from(value), - } - } -} - -impl From> for OptionUint { - fn from(value: Option) -> Self { - match value { - Some(value) => Self { - status: true, - value: value.into(), - }, - None => Self { - status: false, - value: Default::default(), - }, - } - } -} - -impl From for OptionUint { - fn from(value: bool) -> Self { - Self { - status: true, - value: if value { - uint256::from(1) - } else { - Default::default() - }, - } - } -} - -impl From> for OptionUint { - fn from(value: Option) -> Self { - match value { - Some(value) => Self::from(value), - None => Self { - status: false, - value: Default::default(), - }, - } - } -} - /// Ethereum representation of Optional value with CrossAddress. #[derive(Debug, Default, AbiCoder)] pub struct OptionCrossAddress { @@ -252,23 +196,23 @@ #[derive(Debug, Default, AbiCoder)] pub struct CollectionLimit { field: CollectionLimitField, - value: OptionUint, + value: Option, } impl CollectionLimit { /// Create [`CollectionLimit`] from field and value. - pub fn new(field: CollectionLimitField, value: T) -> Self - where - OptionUint: From, - { + pub fn new(field: CollectionLimitField, value: Option) -> Self { Self { field, - value: value.into(), + value: match value { + Some(value) => Some(value.into()), + None => None, + }, } } /// Whether the field contains a value. pub fn has_value(&self) -> bool { - self.value.status + self.value.is_some() } } @@ -276,52 +220,60 @@ type Error = evm_coder::execution::Error; fn try_into(self) -> Result { - let value = self.value.value.try_into().map_err(|error| { + let value = self + .value + .ok_or::("can't convert `None` value to boolean".into())?; + let value = Some(value.try_into().map_err(|error| { Self::Error::Revert(format!( "can't convert value to u32 \"{}\" because: \"{error}\"", - self.value.value + value )) - })?; + })?); let convert_value_to_bool = || match value { - 0 => Ok(false), - 1 => Ok(true), - _ => { - return Err(Self::Error::Revert(format!( - "can't convert value to boolean \"{value}\"" - ))) - } + Some(value) => match value { + 0 => Ok(Some(false)), + 1 => Ok(Some(true)), + _ => { + return Err(Self::Error::Revert(format!( + "can't convert value to boolean \"{value}\"" + ))) + } + }, + None => Ok(None), }; let mut limits = up_data_structs::CollectionLimits::default(); match self.field { CollectionLimitField::AccountTokenOwnership => { - limits.account_token_ownership_limit = Some(value); + limits.account_token_ownership_limit = value; } CollectionLimitField::SponsoredDataSize => { - limits.sponsored_data_size = Some(value); + limits.sponsored_data_size = value; } CollectionLimitField::SponsoredDataRateLimit => { - limits.sponsored_data_rate_limit = - Some(up_data_structs::SponsoringRateLimit::Blocks(value)); + limits.sponsored_data_rate_limit = match value { + Some(value) => Some(up_data_structs::SponsoringRateLimit::Blocks(value)), + None => None, + }; } CollectionLimitField::TokenLimit => { - limits.token_limit = Some(value); + limits.token_limit = value; } CollectionLimitField::SponsorTransferTimeout => { - limits.sponsor_transfer_timeout = Some(value); + limits.sponsor_transfer_timeout = value; } CollectionLimitField::SponsorApproveTimeout => { - limits.sponsor_approve_timeout = Some(value); + limits.sponsor_approve_timeout = value; } CollectionLimitField::OwnerCanTransfer => { - limits.owner_can_transfer = Some(convert_value_to_bool()?); + limits.owner_can_transfer = convert_value_to_bool()?; } CollectionLimitField::OwnerCanDestroy => { - limits.owner_can_destroy = Some(convert_value_to_bool()?); + limits.owner_can_destroy = convert_value_to_bool()?; } CollectionLimitField::TransferEnabled => { - limits.transfers_enabled = Some(convert_value_to_bool()?); + limits.transfers_enabled = convert_value_to_bool()?; } }; Ok(limits) --- a/pallets/fungible/src/stubs/UniqueFungible.sol +++ b/pallets/fungible/src/stubs/UniqueFungible.sol @@ -466,12 +466,14 @@ /// [`CollectionLimits`](up_data_structs::CollectionLimits) field representation for EVM. struct CollectionLimit { CollectionLimitField field; - OptionUint value; + Option_uint256 value; } -/// Ethereum representation of Optional value with uint256. -struct OptionUint { +/// Optional value +struct Option_uint256 { + /// Shows the status of accessibility of value bool status; + /// Actual value if `status` is true uint256 value; } --- a/pallets/nonfungible/src/stubs/UniqueNFT.sol +++ b/pallets/nonfungible/src/stubs/UniqueNFT.sol @@ -608,12 +608,14 @@ /// [`CollectionLimits`](up_data_structs::CollectionLimits) field representation for EVM. struct CollectionLimit { CollectionLimitField field; - OptionUint value; + Option_uint256 value; } -/// Ethereum representation of Optional value with uint256. -struct OptionUint { +/// Optional value +struct Option_uint256 { + /// Shows the status of accessibility of value bool status; + /// Actual value if `status` is true uint256 value; } --- a/pallets/refungible/src/stubs/UniqueRefungible.sol +++ b/pallets/refungible/src/stubs/UniqueRefungible.sol @@ -608,12 +608,14 @@ /// [`CollectionLimits`](up_data_structs::CollectionLimits) field representation for EVM. struct CollectionLimit { CollectionLimitField field; - OptionUint value; + Option_uint256 value; } -/// Ethereum representation of Optional value with uint256. -struct OptionUint { +/// Optional value +struct Option_uint256 { + /// Shows the status of accessibility of value bool status; + /// Actual value if `status` is true uint256 value; } --- a/tests/src/eth/abi/fungible.json +++ b/tests/src/eth/abi/fungible.json @@ -222,7 +222,7 @@ { "internalType": "bool", "name": "status", "type": "bool" }, { "internalType": "uint256", "name": "value", "type": "uint256" } ], - "internalType": "struct OptionUint", + "internalType": "struct Option_uint256", "name": "value", "type": "tuple" } @@ -508,7 +508,7 @@ { "internalType": "bool", "name": "status", "type": "bool" }, { "internalType": "uint256", "name": "value", "type": "uint256" } ], - "internalType": "struct OptionUint", + "internalType": "struct Option_uint256", "name": "value", "type": "tuple" } --- a/tests/src/eth/abi/nonFungible.json +++ b/tests/src/eth/abi/nonFungible.json @@ -252,7 +252,7 @@ { "internalType": "bool", "name": "status", "type": "bool" }, { "internalType": "uint256", "name": "value", "type": "uint256" } ], - "internalType": "struct OptionUint", + "internalType": "struct Option_uint256", "name": "value", "type": "tuple" } @@ -670,7 +670,7 @@ { "internalType": "bool", "name": "status", "type": "bool" }, { "internalType": "uint256", "name": "value", "type": "uint256" } ], - "internalType": "struct OptionUint", + "internalType": "struct Option_uint256", "name": "value", "type": "tuple" } --- a/tests/src/eth/abi/reFungible.json +++ b/tests/src/eth/abi/reFungible.json @@ -234,7 +234,7 @@ { "internalType": "bool", "name": "status", "type": "bool" }, { "internalType": "uint256", "name": "value", "type": "uint256" } ], - "internalType": "struct OptionUint", + "internalType": "struct Option_uint256", "name": "value", "type": "tuple" } @@ -652,7 +652,7 @@ { "internalType": "bool", "name": "status", "type": "bool" }, { "internalType": "uint256", "name": "value", "type": "uint256" } ], - "internalType": "struct OptionUint", + "internalType": "struct Option_uint256", "name": "value", "type": "tuple" } --- a/tests/src/eth/api/UniqueFungible.sol +++ b/tests/src/eth/api/UniqueFungible.sol @@ -308,12 +308,14 @@ /// [`CollectionLimits`](up_data_structs::CollectionLimits) field representation for EVM. struct CollectionLimit { CollectionLimitField field; - OptionUint value; + Option_uint256 value; } -/// Ethereum representation of Optional value with uint256. -struct OptionUint { +/// Optional value +struct Option_uint256 { + /// Shows the status of accessibility of value bool status; + /// Actual value if `status` is true uint256 value; } --- a/tests/src/eth/api/UniqueNFT.sol +++ b/tests/src/eth/api/UniqueNFT.sol @@ -408,12 +408,14 @@ /// [`CollectionLimits`](up_data_structs::CollectionLimits) field representation for EVM. struct CollectionLimit { CollectionLimitField field; - OptionUint value; + Option_uint256 value; } -/// Ethereum representation of Optional value with uint256. -struct OptionUint { +/// Optional value +struct Option_uint256 { + /// Shows the status of accessibility of value bool status; + /// Actual value if `status` is true uint256 value; } --- a/tests/src/eth/api/UniqueRefungible.sol +++ b/tests/src/eth/api/UniqueRefungible.sol @@ -408,12 +408,14 @@ /// [`CollectionLimits`](up_data_structs::CollectionLimits) field representation for EVM. struct CollectionLimit { CollectionLimitField field; - OptionUint value; + Option_uint256 value; } -/// Ethereum representation of Optional value with uint256. -struct OptionUint { +/// Optional value +struct Option_uint256 { + /// Shows the status of accessibility of value bool status; + /// Actual value if `status` is true uint256 value; }