difftreelog
refactor move token address mapping to trait
in: master
6 files changed
pallets/common/src/eth.rsdiffbeforeafterboth191920// 0x17c4e6453Cc49AAAaEACA894e6D9683e00000001 - collection 120// 0x17c4e6453Cc49AAAaEACA894e6D9683e00000001 - collection 121// TODO: Unhardcode prefix21// TODO: Unhardcode prefix22const ETH_ACCOUNT_PREFIX: [u8; 16] = [22const ETH_COLLECTION_PREFIX: [u8; 16] = [23 0x17, 0xc4, 0xe6, 0x45, 0x3c, 0xc4, 0x9a, 0xaa, 0xae, 0xac, 0xa8, 0x94, 0xe6, 0xd9, 0x68, 0x3e,23 0x17, 0xc4, 0xe6, 0x45, 0x3c, 0xc4, 0x9a, 0xaa, 0xae, 0xac, 0xa8, 0x94, 0xe6, 0xd9, 0x68, 0x3e,24];24];2526// 0xf8238ccfff8ed887463fd5e00000000100000002 - collection 1, token 227// TODO: Unhardcode prefix28const ETH_ACCOUNT_TOKEN_PREFIX: [u8; 12] = [29 0xf8, 0x23, 0x8c, 0xcf, 0xff, 0x8e, 0xd8, 0x87, 0x46, 0x3f, 0xd5, 0xe0,30];312532pub fn map_eth_to_id(eth: &H160) -> Option<CollectionId> {26pub fn map_eth_to_id(eth: &H160) -> Option<CollectionId> {33 if eth[0..16] != ETH_ACCOUNT_PREFIX {27 if eth[0..16] != ETH_COLLECTION_PREFIX {34 return None;28 return None;35 }29 }36 let mut id_bytes = [0; 4];30 let mut id_bytes = [0; 4];39}33}40pub fn collection_id_to_address(id: CollectionId) -> H160 {34pub fn collection_id_to_address(id: CollectionId) -> H160 {41 let mut out = [0; 20];35 let mut out = [0; 20];42 out[0..16].copy_from_slice(Ð_ACCOUNT_PREFIX);36 out[0..16].copy_from_slice(Ð_COLLECTION_PREFIX);43 out[16..20].copy_from_slice(&u32::to_be_bytes(id.0));37 out[16..20].copy_from_slice(&u32::to_be_bytes(id.0));44 H160(out)38 H160(out)45}39}4647pub fn map_eth_to_token_id(eth: &H160) -> Option<(CollectionId, TokenId)> {48 if eth[0..12] != ETH_ACCOUNT_TOKEN_PREFIX {49 return None;50 }51 let mut id_bytes = [0; 4];52 let mut token_id_bytes = [0; 4];53 id_bytes.copy_from_slice(ð[12..16]);54 token_id_bytes.copy_from_slice(ð[16..20]);55 Some((56 CollectionId(u32::from_be_bytes(id_bytes)),57 TokenId(u32::from_be_bytes(token_id_bytes)),58 ))59}60pub fn collection_token_id_to_address(id: CollectionId, token: TokenId) -> H160 {61 let mut out = [0; 20];62 out[0..12].copy_from_slice(Ð_ACCOUNT_TOKEN_PREFIX);63 out[12..16].copy_from_slice(&u32::to_be_bytes(id.0));64 out[16..20].copy_from_slice(&u32::to_be_bytes(token.0));65 H160(out)66}6740pallets/common/src/lib.rsdiffbeforeafterboth165 use frame_support::{Blake2_128Concat, pallet_prelude::*, storage::Key};165 use frame_support::{Blake2_128Concat, pallet_prelude::*, storage::Key};166 use pallet_evm::account;166 use pallet_evm::account;167 use dispatch::CollectionDispatch;167 use dispatch::CollectionDispatch;168 use frame_support::{Blake2_128Concat, pallet_prelude::*, storage::Key, traits::StorageVersion};169 use frame_system::pallet_prelude::*;168 use frame_support::traits::Currency;170 use frame_support::traits::Currency;169 use up_data_structs::TokenId;171 use up_data_structs::{TokenId, mapping::TokenAddressMapping};170 use scale_info::TypeInfo;172 use scale_info::TypeInfo;171 use up_evm_mapping::CrossAccountId;173 use up_evm_mapping::CrossAccountId;172174186188187 type TreasuryAccountId: Get<Self::AccountId>;189 type TreasuryAccountId: Get<Self::AccountId>;190191 type EvmTokenAddressMapping: TokenAddressMapping<H160>;192 type CrossTokenAddressMapping: TokenAddressMapping<Self::CrossAccountId>;188 }193 }189194190 #[pallet::pallet]195 #[pallet::pallet]primitives/data-structs/src/lib.rsdiffbeforeafterboth20 convert::{TryFrom, TryInto},20 convert::{TryFrom, TryInto},21 fmt,21 fmt,22};22};23use frame_support::storage::bounded_btree_map::BoundedBTreeMap;23use frame_support::{24 storage::{bounded_btree_map::BoundedBTreeMap, bounded_btree_set::BoundedBTreeSet},25 traits::ConstU16,26};24use sp_std::collections::btree_map::BTreeMap;27use sp_std::collections::{btree_map::BTreeMap, btree_set::BTreeSet};252826#[cfg(feature = "serde")]29#[cfg(feature = "serde")]27pub use serde::{Serialize, Deserialize};30use serde::{Serialize, Deserialize};283129use sp_core::U256;32use sp_core::U256;30use sp_runtime::{ArithmeticError, sp_std::prelude::Vec};33use sp_runtime::{ArithmeticError, sp_std::prelude::Vec};31use codec::{Decode, Encode, EncodeLike, MaxEncodedLen};34use codec::{Decode, Encode, EncodeLike, MaxEncodedLen};32pub use frame_support::{35use frame_support::{BoundedVec, traits::ConstU32};33 BoundedVec, construct_runtime, decl_event, decl_module, decl_storage, decl_error,34 dispatch::DispatchResult,35 ensure, fail, parameter_types,36 traits::{37 Currency, ExistenceRequirement, Get, Imbalance, KeyOwnerProofSystem, OnUnbalanced,38 Randomness, IsSubType, WithdrawReasons,39 },40 weights::{41 constants::{BlockExecutionWeight, ExtrinsicBaseWeight, RocksDbWeight, WEIGHT_PER_SECOND},42 DispatchInfo, GetDispatchInfo, IdentityFee, Pays, PostDispatchInfo, Weight,43 WeightToFeePolynomial, DispatchClass,44 },45 StorageValue, transactional,46 pallet_prelude::ConstU32,47};48use derivative::Derivative;36use derivative::Derivative;49use scale_info::TypeInfo;37use scale_info::TypeInfo;503839pub mod mapping;51mod migration;40mod migration;524153pub const MAX_DECIMAL_POINTS: DecimalPoints = 30;42pub const MAX_DECIMAL_POINTS: DecimalPoints = 30;primitives/data-structs/src/mapping.rsdiffbeforeafterbothno changes
runtime/opal/src/lib.rsdiffbeforeafterboth66 WeightToFeePolynomial, WeightToFeeCoefficient, WeightToFeeCoefficients, ConstantMultiplier,66 WeightToFeePolynomial, WeightToFeeCoefficient, WeightToFeeCoefficients, ConstantMultiplier,67 },67 },68};68};69use up_data_structs::mapping::{EvmTokenAddressMapping, CrossTokenAddressMapping};69use up_data_structs::{CollectionId, TokenId, CollectionStats, Collection};70use up_data_structs::{CollectionId, TokenId, CollectionStats, Collection};70// use pallet_contracts::weights::WeightInfo;71// use pallet_contracts::weights::WeightInfo;71// #[cfg(any(feature = "std", test))]72// #[cfg(any(feature = "std", test))]tests/src/eth/util/helpers.tsdiffbeforeafterboth56 }56 }57}57}585859export function collectionIdToAddress(address: number): string {59function encodeIntBE(v: number): number[] {60 if (address >= 0xffffffff || address < 0) throw new Error('id overflow');60 if (v >= 0xffffffff || v < 0) throw new Error('id overflow');61 const buf = Buffer.from([0x17, 0xc4, 0xe6, 0x45, 0x3c, 0xc4, 0x9a, 0xaa, 0xae, 0xac, 0xa8, 0x94, 0xe6, 0xd9, 0x68, 0x3e,61 return [62 address >> 24,62 v >> 24,63 (address >> 16) & 0xff,63 (v >> 16) & 0xff,64 (address >> 8) & 0xff,64 (v >> 8) & 0xff,65 address & 0xff,65 v & 0xff,66 ]);66 ];67 return Web3.utils.toChecksumAddress('0x' + buf.toString('hex'));68}67}6869export function collectionIdToAddress(collection: number): string {70 const buf = Buffer.from([0x17, 0xc4, 0xe6, 0x45, 0x3c, 0xc4, 0x9a, 0xaa, 0xae, 0xac, 0xa8, 0x94, 0xe6, 0xd9, 0x68, 0x3e,71 ...encodeIntBE(collection),72 ]);73 return Web3.utils.toChecksumAddress('0x' + buf.toString('hex'));74}7576export function tokenIdToAddress(collection: number, token: number): string {77 const buf = Buffer.from([0xf8, 0x23, 0x8c, 0xcf, 0xff, 0x8e, 0xd8, 0x87, 0x46, 0x3f, 0xd5, 0xe0,78 ...encodeIntBE(collection),79 ...encodeIntBE(token),80 ]);81 return Web3.utils.toChecksumAddress('0x' + buf.toString('hex'));82}698370export function createEthAccount(web3: Web3) {84export function createEthAccount(web3: Web3) {71 const account = web3.eth.accounts.create();85 const account = web3.eth.accounts.create();