difftreelog
feat Add AbiWrite support for vec with dynamic type
in: master
6 files changed
crates/evm-coder/Cargo.tomldiffbeforeafterboth--- a/crates/evm-coder/Cargo.toml
+++ b/crates/evm-coder/Cargo.toml
@@ -22,6 +22,7 @@
# We want to assert some large binary blobs equality in tests
hex = "0.4.3"
hex-literal = "0.3.4"
+similar-asserts = "1.4.2"
[features]
default = ["std"]
crates/evm-coder/src/abi.rsdiffbeforeafterboth--- a/crates/evm-coder/src/abi.rs
+++ b/crates/evm-coder/src/abi.rs
@@ -25,7 +25,7 @@
use crate::{
execution::{Error, ResultWithPostInfo, WithPostDispatchInfo},
- types::{string, self},
+ types::*,
};
use crate::execution::Result;
@@ -56,7 +56,7 @@
}
}
/// Start reading RLP buffer, parsing first 4 bytes as selector
- pub fn new_call(buf: &'i [u8]) -> Result<(types::bytes4, Self)> {
+ pub fn new_call(buf: &'i [u8]) -> Result<(bytes4, Self)> {
if buf.len() < 4 {
return Err(Error::Error(ExitError::OutOfOffset));
}
@@ -252,11 +252,11 @@
self.static_part.extend(block);
}
- fn write_padright(&mut self, bytes: &[u8]) {
- assert!(bytes.len() <= ABI_ALIGNMENT);
- self.static_part.extend(bytes);
+ fn write_padright(&mut self, block: &[u8]) {
+ assert!(block.len() <= ABI_ALIGNMENT);
+ self.static_part.extend(block);
self.static_part
- .extend(&[0; ABI_ALIGNMENT][0..ABI_ALIGNMENT - bytes.len()]);
+ .extend(&[0; ABI_ALIGNMENT][0..ABI_ALIGNMENT - block.len()]);
}
/// Write [`H160`] to end of buffer
@@ -369,16 +369,30 @@
};
}
-impl_abi_readable!(u8, uint8, false);
-impl_abi_readable!(u32, uint32, false);
-impl_abi_readable!(u64, uint64, false);
-impl_abi_readable!(u128, uint128, false);
-impl_abi_readable!(U256, uint256, false);
-impl_abi_readable!([u8; 4], bytes4, false);
-impl_abi_readable!(H160, address, false);
-impl_abi_readable!(Vec<u8>, bytes, true);
-impl_abi_readable!(bool, bool, true);
+impl_abi_readable!(bool, bool, false);
+impl_abi_readable!(uint8, uint8, false);
+impl_abi_readable!(uint32, uint32, false);
+impl_abi_readable!(uint64, uint64, false);
+impl_abi_readable!(uint128, uint128, false);
+impl_abi_readable!(uint256, uint256, false);
+impl_abi_readable!(bytes4, bytes4, false);
+impl_abi_readable!(address, address, false);
impl_abi_readable!(string, string, true);
+// impl_abi_readable!(bytes, bytes, true);
+
+impl TypeHelper for bytes {
+ fn is_dynamic() -> bool {
+ true
+ }
+ fn size() -> usize {
+ ABI_ALIGNMENT
+ }
+}
+impl AbiRead<bytes> for AbiReader<'_> {
+ fn abi_read(&mut self) -> Result<bytes> {
+ Ok(bytes(self.bytes()?))
+ }
+}
mod sealed {
/// Not all types can be placed in vec, i.e `Vec<u8>` is restricted, `bytes` should be used instead
@@ -524,17 +538,19 @@
impl_abi_writeable!(H160, address);
impl_abi_writeable!(bool, bool);
impl_abi_writeable!(&str, string);
+
impl AbiWrite for string {
fn abi_write(&self, writer: &mut AbiWriter) {
writer.string(self)
}
}
-// impl AbiWrite for Vec<u8> {
-// fn abi_write(&self, writer: &mut AbiWriter) {
-// writer.bytes(self)
-// }
-// }
+impl AbiWrite for bytes {
+ fn abi_write(&self, writer: &mut AbiWriter) {
+ writer.bytes(self.0.as_slice())
+ }
+}
+
impl<T: AbiWrite + TypeHelper> AbiWrite for Vec<T> {
fn abi_write(&self, writer: &mut AbiWriter) {
let is_dynamic = T::is_dynamic();
crates/evm-coder/src/lib.rsdiffbeforeafterboth--- a/crates/evm-coder/src/lib.rs
+++ b/crates/evm-coder/src/lib.rs
@@ -133,8 +133,10 @@
pub type string = ::alloc::string::String;
#[cfg(feature = "std")]
pub type string = ::std::string::String;
- pub type bytes = Vec<u8>;
+ #[derive(Default, Debug)]
+ pub struct bytes(pub Vec<u8>);
+
/// Solidity doesn't have `void` type, however we have special implementation
/// for empty tuple return type
pub type void = ();
@@ -157,6 +159,30 @@
/// and there is no `receiver()` function defined.
pub value: U256,
}
+
+ impl From<Vec<u8>> for bytes {
+ fn from(src: Vec<u8>) -> Self {
+ Self(src)
+ }
+ }
+
+ impl Into<Vec<u8>> for bytes {
+ fn into(self) -> Vec<u8> {
+ self.0
+ }
+ }
+
+ impl bytes {
+ #[must_use]
+ pub fn len(&self) -> usize {
+ self.0.len()
+ }
+
+ #[must_use]
+ pub fn is_empty(&self) -> bool {
+ self.len() == 0
+ }
+ }
}
/// Parseable EVM call, this trait should be implemented with [`solidity_interface`] macro
pallets/common/src/erc.rsdiffbeforeafterboth1// Copyright 2019-2022 Unique Network (Gibraltar) Ltd.2// This file is part of Unique Network.34// Unique Network is free software: you can redistribute it and/or modify5// it under the terms of the GNU General Public License as published by6// the Free Software Foundation, either version 3 of the License, or7// (at your option) any later version.89// Unique Network is distributed in the hope that it will be useful,10// but WITHOUT ANY WARRANTY; without even the implied warranty of11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the12// GNU General Public License for more details.1314// You should have received a copy of the GNU General Public License15// along with Unique Network. If not, see <http://www.gnu.org/licenses/>.1617//! This module contains the implementation of pallet methods for evm.1819use evm_coder::{20 solidity_interface, solidity, ToLog,21 types::*,22 execution::{Result, Error},23 weight,24};25pub use pallet_evm::{PrecompileOutput, PrecompileResult, PrecompileHandle, account::CrossAccountId};26use pallet_evm_coder_substrate::dispatch_to_evm;27use sp_std::vec::Vec;28use up_data_structs::{29 AccessMode, CollectionMode, CollectionPermissions, OwnerRestrictedSet, Property,30 SponsoringRateLimit, SponsorshipState,31};32use alloc::format;3334use crate::{35 Pallet, CollectionHandle, Config, CollectionProperties, SelfWeightOf,36 eth::{37 convert_cross_account_to_uint256, convert_uint256_to_cross_account,38 convert_cross_account_to_tuple,39 },40 weights::WeightInfo,41};4243/// Events for ethereum collection helper.44#[derive(ToLog)]45pub enum CollectionHelpersEvents {46 /// The collection has been created.47 CollectionCreated {48 /// Collection owner.49 #[indexed]50 owner: address,5152 /// Collection ID.53 #[indexed]54 collection_id: address,55 },56}5758/// Does not always represent a full collection, for RFT it is either59/// collection (Implementing ERC721), or specific collection token (Implementing ERC20).60pub trait CommonEvmHandler {61 const CODE: &'static [u8];6263 /// Call precompiled handle.64 fn call(self, handle: &mut impl PrecompileHandle) -> Option<PrecompileResult>;65}6667/// @title A contract that allows you to work with collections.68#[solidity_interface(name = Collection)]69impl<T: Config> CollectionHandle<T>70where71 T::AccountId: From<[u8; 32]> + AsRef<[u8; 32]>,72{73 /// Set collection property.74 ///75 /// @param key Property key.76 /// @param value Propery value.77 #[weight(<SelfWeightOf<T>>::set_collection_properties(1))]78 fn set_collection_property(79 &mut self,80 caller: caller,81 key: string,82 value: bytes,83 ) -> Result<void> {84 let caller = T::CrossAccountId::from_eth(caller);85 let key = <Vec<u8>>::from(key)86 .try_into()87 .map_err(|_| "key too large")?;88 let value = value.try_into().map_err(|_| "value too large")?;8990 <Pallet<T>>::set_collection_property(self, &caller, Property { key, value })91 .map_err(dispatch_to_evm::<T>)92 }9394 /// Delete collection property.95 ///96 /// @param key Property key.97 #[weight(<SelfWeightOf<T>>::delete_collection_properties(1))]98 fn delete_collection_property(&mut self, caller: caller, key: string) -> Result<()> {99 self.consume_store_reads_and_writes(1, 1)?;100101 let caller = T::CrossAccountId::from_eth(caller);102 let key = <Vec<u8>>::from(key)103 .try_into()104 .map_err(|_| "key too large")?;105106 <Pallet<T>>::delete_collection_property(self, &caller, key).map_err(dispatch_to_evm::<T>)107 }108109 /// Get collection property.110 ///111 /// @dev Throws error if key not found.112 ///113 /// @param key Property key.114 /// @return bytes The property corresponding to the key.115 fn collection_property(&self, key: string) -> Result<bytes> {116 let key = <Vec<u8>>::from(key)117 .try_into()118 .map_err(|_| "key too large")?;119120 let props = <CollectionProperties<T>>::get(self.id);121 let prop = props.get(&key).ok_or("key not found")?;122123 Ok(prop.to_vec())124 }125126 /// Set the sponsor of the collection.127 ///128 /// @dev In order for sponsorship to work, it must be confirmed on behalf of the sponsor.129 ///130 /// @param sponsor Address of the sponsor from whose account funds will be debited for operations with the contract.131 fn set_collection_sponsor(&mut self, caller: caller, sponsor: address) -> Result<void> {132 self.consume_store_reads_and_writes(1, 1)?;133134 check_is_owner_or_admin(caller, self)?;135136 let sponsor = T::CrossAccountId::from_eth(sponsor);137 self.set_sponsor(sponsor.as_sub().clone())138 .map_err(dispatch_to_evm::<T>)?;139 save(self)140 }141142 // TODO: Temprorary off. Need refactor143 // /// Set the substrate sponsor of the collection.144 // ///145 // /// @dev In order for sponsorship to work, it must be confirmed on behalf of the sponsor.146 // ///147 // /// @param sponsor Substrate address of the sponsor from whose account funds will be debited for operations with the contract.148 // fn set_collection_sponsor_substrate(149 // &mut self,150 // caller: caller,151 // sponsor: uint256,152 // ) -> Result<void> {153 // self.consume_store_reads_and_writes(1, 1)?;154155 // check_is_owner_or_admin(caller, self)?;156157 // let sponsor = convert_uint256_to_cross_account::<T>(sponsor);158 // self.set_sponsor(sponsor.as_sub().clone())159 // .map_err(dispatch_to_evm::<T>)?;160 // save(self)161 // }162163 /// Whether there is a pending sponsor.164 fn has_collection_pending_sponsor(&self) -> Result<bool> {165 Ok(matches!(166 self.collection.sponsorship,167 SponsorshipState::Unconfirmed(_)168 ))169 }170171 /// Collection sponsorship confirmation.172 ///173 /// @dev After setting the sponsor for the collection, it must be confirmed with this function.174 fn confirm_collection_sponsorship(&mut self, caller: caller) -> Result<void> {175 self.consume_store_writes(1)?;176177 let caller = T::CrossAccountId::from_eth(caller);178 if !self179 .confirm_sponsorship(caller.as_sub())180 .map_err(dispatch_to_evm::<T>)?181 {182 return Err("caller is not set as sponsor".into());183 }184 save(self)185 }186187 /// Remove collection sponsor.188 fn remove_collection_sponsor(&mut self, caller: caller) -> Result<void> {189 self.consume_store_reads_and_writes(1, 1)?;190 check_is_owner_or_admin(caller, self)?;191 self.remove_sponsor().map_err(dispatch_to_evm::<T>)?;192 save(self)193 }194195 /// Get current sponsor.196 ///197 /// @return Tuble with sponsor address and his substrate mirror. If there is no confirmed sponsor error "Contract has no sponsor" throw.198 fn collection_sponsor(&self) -> Result<(address, uint256)> {199 let sponsor = match self.collection.sponsorship.sponsor() {200 Some(sponsor) => sponsor,201 None => return Ok(Default::default()),202 };203 let sponsor = T::CrossAccountId::from_sub(sponsor.clone());204 let result: (address, uint256) = if sponsor.is_canonical_substrate() {205 let sponsor = convert_cross_account_to_uint256::<T>(&sponsor);206 (Default::default(), sponsor)207 } else {208 let sponsor = *sponsor.as_eth();209 (sponsor, Default::default())210 };211 Ok(result)212 }213214 /// Set limits for the collection.215 /// @dev Throws error if limit not found.216 /// @param limit Name of the limit. Valid names:217 /// "accountTokenOwnershipLimit",218 /// "sponsoredDataSize",219 /// "sponsoredDataRateLimit",220 /// "tokenLimit",221 /// "sponsorTransferTimeout",222 /// "sponsorApproveTimeout"223 /// @param value Value of the limit.224 #[solidity(rename_selector = "setCollectionLimit")]225 fn set_int_limit(&mut self, caller: caller, limit: string, value: uint32) -> Result<void> {226 self.consume_store_reads_and_writes(1, 1)?;227228 check_is_owner_or_admin(caller, self)?;229 let mut limits = self.limits.clone();230231 match limit.as_str() {232 "accountTokenOwnershipLimit" => {233 limits.account_token_ownership_limit = Some(value);234 }235 "sponsoredDataSize" => {236 limits.sponsored_data_size = Some(value);237 }238 "sponsoredDataRateLimit" => {239 limits.sponsored_data_rate_limit = Some(SponsoringRateLimit::Blocks(value));240 }241 "tokenLimit" => {242 limits.token_limit = Some(value);243 }244 "sponsorTransferTimeout" => {245 limits.sponsor_transfer_timeout = Some(value);246 }247 "sponsorApproveTimeout" => {248 limits.sponsor_approve_timeout = Some(value);249 }250 _ => {251 return Err(Error::Revert(format!(252 "unknown integer limit \"{}\"",253 limit254 )))255 }256 }257 self.limits = <Pallet<T>>::clamp_limits(self.mode.clone(), &self.limits, limits)258 .map_err(dispatch_to_evm::<T>)?;259 save(self)260 }261262 /// Set limits for the collection.263 /// @dev Throws error if limit not found.264 /// @param limit Name of the limit. Valid names:265 /// "ownerCanTransfer",266 /// "ownerCanDestroy",267 /// "transfersEnabled"268 /// @param value Value of the limit.269 #[solidity(rename_selector = "setCollectionLimit")]270 fn set_bool_limit(&mut self, caller: caller, limit: string, value: bool) -> Result<void> {271 self.consume_store_reads_and_writes(1, 1)?;272273 check_is_owner_or_admin(caller, self)?;274 let mut limits = self.limits.clone();275276 match limit.as_str() {277 "ownerCanTransfer" => {278 limits.owner_can_transfer = Some(value);279 }280 "ownerCanDestroy" => {281 limits.owner_can_destroy = Some(value);282 }283 "transfersEnabled" => {284 limits.transfers_enabled = Some(value);285 }286 _ => {287 return Err(Error::Revert(format!(288 "unknown boolean limit \"{}\"",289 limit290 )))291 }292 }293 self.limits = <Pallet<T>>::clamp_limits(self.mode.clone(), &self.limits, limits)294 .map_err(dispatch_to_evm::<T>)?;295 save(self)296 }297298 /// Get contract address.299 fn contract_address(&self) -> Result<address> {300 Ok(crate::eth::collection_id_to_address(self.id))301 }302303 // TODO: Temprorary off. Need refactor304 // /// Add collection admin by substrate address.305 // /// @param newAdmin Substrate administrator address.306 // fn add_collection_admin_substrate(307 // &mut self,308 // caller: caller,309 // new_admin: uint256,310 // ) -> Result<void> {311 // self.consume_store_writes(2)?;312313 // let caller = T::CrossAccountId::from_eth(caller);314 // let new_admin = convert_uint256_to_cross_account::<T>(new_admin);315 // <Pallet<T>>::toggle_admin(self, &caller, &new_admin, true).map_err(dispatch_to_evm::<T>)?;316 // Ok(())317 // }318319 // TODO: Temprorary off. Need refactor320 // /// Remove collection admin by substrate address.321 // /// @param admin Substrate administrator address.322 // fn remove_collection_admin_substrate(323 // &mut self,324 // caller: caller,325 // admin: uint256,326 // ) -> Result<void> {327 // self.consume_store_writes(2)?;328329 // let caller = T::CrossAccountId::from_eth(caller);330 // let admin = convert_uint256_to_cross_account::<T>(admin);331 // <Pallet<T>>::toggle_admin(self, &caller, &admin, false).map_err(dispatch_to_evm::<T>)?;332 // Ok(())333 // }334335 /// Add collection admin.336 /// @param newAdmin Address of the added administrator.337 fn add_collection_admin(&mut self, caller: caller, new_admin: address) -> Result<void> {338 self.consume_store_writes(2)?;339340 let caller = T::CrossAccountId::from_eth(caller);341 let new_admin = T::CrossAccountId::from_eth(new_admin);342 <Pallet<T>>::toggle_admin(self, &caller, &new_admin, true).map_err(dispatch_to_evm::<T>)?;343 Ok(())344 }345346 /// Remove collection admin.347 ///348 /// @param admin Address of the removed administrator.349 fn remove_collection_admin(&mut self, caller: caller, admin: address) -> Result<void> {350 self.consume_store_writes(2)?;351352 let caller = T::CrossAccountId::from_eth(caller);353 let admin = T::CrossAccountId::from_eth(admin);354 <Pallet<T>>::toggle_admin(self, &caller, &admin, false).map_err(dispatch_to_evm::<T>)?;355 Ok(())356 }357358 /// Toggle accessibility of collection nesting.359 ///360 /// @param enable If "true" degenerates to nesting: 'Owner' else to nesting: 'Disabled'361 #[solidity(rename_selector = "setCollectionNesting")]362 fn set_nesting_bool(&mut self, caller: caller, enable: bool) -> Result<void> {363 self.consume_store_reads_and_writes(1, 1)?;364365 check_is_owner_or_admin(caller, self)?;366367 let mut permissions = self.collection.permissions.clone();368 let mut nesting = permissions.nesting().clone();369 nesting.token_owner = enable;370 nesting.restricted = None;371 permissions.nesting = Some(nesting);372373 self.collection.permissions = <Pallet<T>>::clamp_permissions(374 self.collection.mode.clone(),375 &self.collection.permissions,376 permissions,377 )378 .map_err(dispatch_to_evm::<T>)?;379380 save(self)381 }382383 /// Toggle accessibility of collection nesting.384 ///385 /// @param enable If "true" degenerates to nesting: {OwnerRestricted: [1, 2, 3]} else to nesting: 'Disabled'386 /// @param collections Addresses of collections that will be available for nesting.387 #[solidity(rename_selector = "setCollectionNesting")]388 fn set_nesting(389 &mut self,390 caller: caller,391 enable: bool,392 collections: Vec<address>,393 ) -> Result<void> {394 self.consume_store_reads_and_writes(1, 1)?;395396 if collections.is_empty() {397 return Err("no addresses provided".into());398 }399 check_is_owner_or_admin(caller, self)?;400401 let mut permissions = self.collection.permissions.clone();402 match enable {403 false => {404 let mut nesting = permissions.nesting().clone();405 nesting.token_owner = false;406 nesting.restricted = None;407 permissions.nesting = Some(nesting);408 }409 true => {410 let mut bv = OwnerRestrictedSet::new();411 for i in collections {412 bv.try_insert(crate::eth::map_eth_to_id(&i).ok_or_else(|| {413 Error::Revert("Can't convert address into collection id".into())414 })?)415 .map_err(|_| "too many collections")?;416 }417 let mut nesting = permissions.nesting().clone();418 nesting.token_owner = true;419 nesting.restricted = Some(bv);420 permissions.nesting = Some(nesting);421 }422 };423424 self.collection.permissions = <Pallet<T>>::clamp_permissions(425 self.collection.mode.clone(),426 &self.collection.permissions,427 permissions,428 )429 .map_err(dispatch_to_evm::<T>)?;430431 save(self)432 }433434 /// Set the collection access method.435 /// @param mode Access mode436 /// 0 for Normal437 /// 1 for AllowList438 fn set_collection_access(&mut self, caller: caller, mode: uint8) -> Result<void> {439 self.consume_store_reads_and_writes(1, 1)?;440441 check_is_owner_or_admin(caller, self)?;442 let permissions = CollectionPermissions {443 access: Some(match mode {444 0 => AccessMode::Normal,445 1 => AccessMode::AllowList,446 _ => return Err("not supported access mode".into()),447 }),448 ..Default::default()449 };450 self.collection.permissions = <Pallet<T>>::clamp_permissions(451 self.collection.mode.clone(),452 &self.collection.permissions,453 permissions,454 )455 .map_err(dispatch_to_evm::<T>)?;456457 save(self)458 }459460 /// Checks that user allowed to operate with collection.461 ///462 /// @param user User address to check.463 fn allowed(&self, user: address) -> Result<bool> {464 Ok(Pallet::<T>::allowed(465 self.id,466 T::CrossAccountId::from_eth(user),467 ))468 }469470 /// Add the user to the allowed list.471 ///472 /// @param user Address of a trusted user.473 fn add_to_collection_allow_list(&mut self, caller: caller, user: address) -> Result<void> {474 self.consume_store_writes(1)?;475476 let caller = T::CrossAccountId::from_eth(caller);477 let user = T::CrossAccountId::from_eth(user);478 <Pallet<T>>::toggle_allowlist(self, &caller, &user, true).map_err(dispatch_to_evm::<T>)?;479 Ok(())480 }481482 // TODO: Temprorary off. Need refactor483 // /// Add substrate user to allowed list.484 // ///485 // /// @param user User substrate address.486 // fn add_to_collection_allow_list_substrate(487 // &mut self,488 // caller: caller,489 // user: uint256,490 // ) -> Result<void> {491 // self.consume_store_writes(1)?;492493 // let caller = T::CrossAccountId::from_eth(caller);494 // let user = convert_uint256_to_cross_account::<T>(user);495 // Pallet::<T>::toggle_allowlist(self, &caller, &user, true).map_err(dispatch_to_evm::<T>)?;496 // Ok(())497 // }498499 /// Remove the user from the allowed list.500 ///501 /// @param user Address of a removed user.502 fn remove_from_collection_allow_list(&mut self, caller: caller, user: address) -> Result<void> {503 self.consume_store_writes(1)?;504505 let caller = T::CrossAccountId::from_eth(caller);506 let user = T::CrossAccountId::from_eth(user);507 <Pallet<T>>::toggle_allowlist(self, &caller, &user, false).map_err(dispatch_to_evm::<T>)?;508 Ok(())509 }510511 // TODO: Temprorary off. Need refactor512 // /// Remove substrate user from allowed list.513 // ///514 // /// @param user User substrate address.515 // fn remove_from_collection_allow_list_substrate(516 // &mut self,517 // caller: caller,518 // user: uint256,519 // ) -> Result<void> {520 // self.consume_store_writes(1)?;521522 // let caller = T::CrossAccountId::from_eth(caller);523 // let user = convert_uint256_to_cross_account::<T>(user);524 // Pallet::<T>::toggle_allowlist(self, &caller, &user, false).map_err(dispatch_to_evm::<T>)?;525 // Ok(())526 // }527528 /// Switch permission for minting.529 ///530 /// @param mode Enable if "true".531 fn set_collection_mint_mode(&mut self, caller: caller, mode: bool) -> Result<void> {532 self.consume_store_reads_and_writes(1, 1)?;533534 check_is_owner_or_admin(caller, self)?;535 let permissions = CollectionPermissions {536 mint_mode: Some(mode),537 ..Default::default()538 };539 self.collection.permissions = <Pallet<T>>::clamp_permissions(540 self.collection.mode.clone(),541 &self.collection.permissions,542 permissions,543 )544 .map_err(dispatch_to_evm::<T>)?;545546 save(self)547 }548549 /// Check that account is the owner or admin of the collection550 ///551 /// @param user account to verify552 /// @return "true" if account is the owner or admin553 #[solidity(rename_selector = "isOwnerOrAdmin")]554 fn is_owner_or_admin_eth(&self, user: address) -> Result<bool> {555 let user = T::CrossAccountId::from_eth(user);556 Ok(self.is_owner_or_admin(&user))557 }558559 // TODO: Temprorary off. Need refactor560 // /// Check that substrate account is the owner or admin of the collection561 // ///562 // /// @param user account to verify563 // /// @return "true" if account is the owner or admin564 // fn is_owner_or_admin_substrate(&self, user: uint256) -> Result<bool> {565 // let user = convert_uint256_to_cross_account::<T>(user);566 // Ok(self.is_owner_or_admin(&user))567 // }568569 /// Returns collection type570 ///571 /// @return `Fungible` or `NFT` or `ReFungible`572 fn unique_collection_type(&self) -> Result<string> {573 let mode = match self.collection.mode {574 CollectionMode::Fungible(_) => "Fungible",575 CollectionMode::NFT => "NFT",576 CollectionMode::ReFungible => "ReFungible",577 };578 Ok(mode.into())579 }580581 /// Get collection owner.582 ///583 /// @return Tuble with sponsor address and his substrate mirror.584 /// If address is canonical then substrate mirror is zero and vice versa.585 fn collection_owner(&self) -> Result<(address, uint256)> {586 Ok(convert_cross_account_to_tuple::<T>(587 &T::CrossAccountId::from_sub(self.owner.clone()),588 ))589 }590591 /// Changes collection owner to another account592 ///593 /// @dev Owner can be changed only by current owner594 /// @param newOwner new owner account595 #[solidity(rename_selector = "changeCollectionOwner")]596 fn set_owner(&mut self, caller: caller, new_owner: address) -> Result<void> {597 self.consume_store_writes(1)?;598599 let caller = T::CrossAccountId::from_eth(caller);600 let new_owner = T::CrossAccountId::from_eth(new_owner);601 self.set_owner_internal(caller, new_owner)602 .map_err(dispatch_to_evm::<T>)603 }604605 // TODO: Temprorary off. Need refactor606 // /// Changes collection owner to another substrate account607 // ///608 // /// @dev Owner can be changed only by current owner609 // /// @param newOwner new owner substrate account610 // fn set_owner_substrate(&mut self, caller: caller, new_owner: uint256) -> Result<void> {611 // self.consume_store_writes(1)?;612613 // let caller = T::CrossAccountId::from_eth(caller);614 // let new_owner = convert_uint256_to_cross_account::<T>(new_owner);615 // self.set_owner_internal(caller, new_owner)616 // .map_err(dispatch_to_evm::<T>)617 // }618619 // TODO: need implement AbiWriter for &Vec<T>620 // fn collection_admins(&self) -> Result<Vec<(address, uint256)>> {621 // let result = pallet_common::IsAdmin::<T>::iter_prefix((self.id,))622 // .map(|(admin, _)| pallet_common::eth::convert_cross_account_to_tuple::<T>(&admin))623 // .collect();624 // Ok(result)625 // }626}627628/// ### Note629/// Do not forget to add: `self.consume_store_reads(1)?;`630fn check_is_owner_or_admin<T: Config>(631 caller: caller,632 collection: &CollectionHandle<T>,633) -> Result<T::CrossAccountId> {634 let caller = T::CrossAccountId::from_eth(caller);635 collection636 .check_is_owner_or_admin(&caller)637 .map_err(dispatch_to_evm::<T>)?;638 Ok(caller)639}640641/// ### Note642/// Do not forget to add: `self.consume_store_writes(1)?;`643fn save<T: Config>(collection: &CollectionHandle<T>) -> Result<void> {644 collection645 .check_is_internal()646 .map_err(dispatch_to_evm::<T>)?;647 collection.save().map_err(dispatch_to_evm::<T>)?;648 Ok(())649}650651/// Contains static property keys and values.652pub mod static_property {653 use evm_coder::{654 execution::{Result, Error},655 };656 use alloc::format;657658 const EXPECT_CONVERT_ERROR: &str = "length < limit";659660 /// Keys.661 pub mod key {662 use super::*;663664 /// Key "baseURI".665 pub fn base_uri() -> up_data_structs::PropertyKey {666 property_key_from_bytes(b"baseURI").expect(EXPECT_CONVERT_ERROR)667 }668669 /// Key "url".670 pub fn url() -> up_data_structs::PropertyKey {671 property_key_from_bytes(b"URI").expect(EXPECT_CONVERT_ERROR)672 }673674 /// Key "suffix".675 pub fn suffix() -> up_data_structs::PropertyKey {676 property_key_from_bytes(b"URISuffix").expect(EXPECT_CONVERT_ERROR)677 }678679 /// Key "parentNft".680 pub fn parent_nft() -> up_data_structs::PropertyKey {681 property_key_from_bytes(b"parentNft").expect(EXPECT_CONVERT_ERROR)682 }683 }684685 /// Convert `byte` to [`PropertyKey`].686 pub fn property_key_from_bytes(bytes: &[u8]) -> Result<up_data_structs::PropertyKey> {687 bytes.to_vec().try_into().map_err(|_| {688 Error::Revert(format!(689 "Property key is too long. Max length is {}.",690 up_data_structs::PropertyKey::bound()691 ))692 })693 }694695 /// Convert `bytes` to [`PropertyValue`].696 pub fn property_value_from_bytes(bytes: &[u8]) -> Result<up_data_structs::PropertyValue> {697 bytes.to_vec().try_into().map_err(|_| {698 Error::Revert(format!(699 "Property key is too long. Max length is {}.",700 up_data_structs::PropertyKey::bound()701 ))702 })703 }704}pallets/nonfungible/src/erc.rsdiffbeforeafterboth--- a/pallets/nonfungible/src/erc.rs
+++ b/pallets/nonfungible/src/erc.rs
@@ -97,7 +97,7 @@
let key = <Vec<u8>>::from(key)
.try_into()
.map_err(|_| "key too long")?;
- let value = value.try_into().map_err(|_| "value too long")?;
+ let value = value.0.try_into().map_err(|_| "value too long")?;
let nesting_budget = self
.recorder
@@ -146,7 +146,7 @@
let props = <TokenProperties<T>>::get((self.id, token_id));
let prop = props.get(&key).ok_or("key not found")?;
- Ok(prop.to_vec())
+ Ok(prop.to_vec().into())
}
}
pallets/refungible/src/erc.rsdiffbeforeafterboth--- a/pallets/refungible/src/erc.rs
+++ b/pallets/refungible/src/erc.rs
@@ -100,7 +100,7 @@
let key = <Vec<u8>>::from(key)
.try_into()
.map_err(|_| "key too long")?;
- let value = value.try_into().map_err(|_| "value too long")?;
+ let value = value.0.try_into().map_err(|_| "value too long")?;
let nesting_budget = self
.recorder
@@ -149,7 +149,7 @@
let props = <TokenProperties<T>>::get((self.id, token_id));
let prop = props.get(&key).ok_or("key not found")?;
- Ok(prop.to_vec())
+ Ok(prop.to_vec().into())
}
}