Skip to content
This repository has been archived by the owner on Oct 2, 2023. It is now read-only.

Refactor use generic AccountId type #137

Merged
merged 150 commits into from
Aug 31, 2023
Merged
Show file tree
Hide file tree
Changes from 142 commits
Commits
Show all changes
150 commits
Select commit Hold shift + click to select a range
8a0bc02
create mock and testing
asiniscalchi Aug 1, 2023
0fe9a63
first test
asiniscalchi Aug 1, 2023
7ebabef
testing ethereum reserved addresses
asiniscalchi Aug 1, 2023
e964668
refactoring
asiniscalchi Aug 1, 2023
aba874e
refactoring
asiniscalchi Aug 1, 2023
80dd172
refactoring tests
asiniscalchi Aug 1, 2023
9604f6c
fmt
asiniscalchi Aug 1, 2023
390359b
erc721 starting point
asiniscalchi Aug 3, 2023
64c449a
Merge branch 'dev' into feature/precompile_erc_721
asiniscalchi Aug 3, 2023
5dafd9f
fmt
asiniscalchi Aug 3, 2023
e92faae
Merge branch 'dev' into feature/precompile_erc_721
asiniscalchi Aug 3, 2023
21d6842
Merge branch 'feature/solidity_create_collection_return_an_address' i…
asiniscalchi Aug 4, 2023
e06adce
compiling
asiniscalchi Aug 4, 2023
3869690
check selectors
asiniscalchi Aug 4, 2023
d514f88
create trait Erc721
asiniscalchi Aug 4, 2023
2efff92
Erc721Precompile
asiniscalchi Aug 4, 2023
0357938
set mocks
asiniscalchi Aug 4, 2023
cec3cbf
first implermentation
asiniscalchi Aug 4, 2023
b0c10ce
first integration of erc721 in the runtime
asiniscalchi Aug 4, 2023
1420c21
fmt
asiniscalchi Aug 4, 2023
d0c4ce7
check on the collection id
asiniscalchi Aug 4, 2023
aa84dde
test on extract owner form asset_id
asiniscalchi Aug 4, 2023
7e49dae
Merge branch 'dev' into feature/precompile_erc_721
asiniscalchi Aug 5, 2023
0ec72a7
fix tests
asiniscalchi Aug 5, 2023
9be1a5b
test for erc721 trait
asiniscalchi Aug 5, 2023
2427e0d
Merge branch 'feature/precompile_erc_721' into feature/testing_precom…
asiniscalchi Aug 5, 2023
4625aad
fix compilation
asiniscalchi Aug 5, 2023
d858bf0
refactoring
asiniscalchi Aug 5, 2023
6c65ddd
returning address
asiniscalchi Aug 6, 2023
1c9e12a
return value is correctly encoded as a n Address
asiniscalchi Aug 7, 2023
c7ec2ee
Merge branch 'bug/create_collection_logs' into feature/precompile_erc…
asiniscalchi Aug 7, 2023
c39efbd
Merge branch 'feature/testing_precompiles' into feature/precompile_er…
asiniscalchi Aug 7, 2023
9dc0c14
is_erc721_contract is not member of PrecompoileSet
asiniscalchi Aug 7, 2023
b9bba2d
test on precompiled contracts
asiniscalchi Aug 7, 2023
a39432c
erc721 returns hardcoded address
asiniscalchi Aug 7, 2023
c4cd42f
refactoring
asiniscalchi Aug 7, 2023
89af0a0
testing return of asset ownership
asiniscalchi Aug 7, 2023
c6c7810
fix errors
asiniscalchi Aug 7, 2023
5cfb2b5
remove unused use
asiniscalchi Aug 7, 2023
874d2f6
create collection got an uri
asiniscalchi Aug 7, 2023
fa612ab
Merge branch 'dev' into feature/precompile_erc_721
asiniscalchi Aug 7, 2023
d3add5c
Merge branch 'dev' into feature/precompile_erc_721_token_uri
asiniscalchi Aug 7, 2023
0568c47
refactoring
asiniscalchi Aug 7, 2023
b31f1db
rewriting testing mod
asiniscalchi Aug 7, 2023
9a4f100
refactoring
asiniscalchi Aug 7, 2023
09738af
moving check for cllection address in pallet
asiniscalchi Aug 8, 2023
daf2910
collection address prefix changed
asiniscalchi Aug 8, 2023
bd47de2
test passing
asiniscalchi Aug 8, 2023
907ca7e
test passing
asiniscalchi Aug 8, 2023
10557aa
refactoring tests
asiniscalchi Aug 8, 2023
6d38abe
do not panic the runtime
asiniscalchi Aug 8, 2023
daef051
fmt
asiniscalchi Aug 8, 2023
f71eef7
solidity tokenId -> _tokenId
asiniscalchi Aug 8, 2023
15123db
erc721 functions are views
asiniscalchi Aug 8, 2023
4b0ec80
update docs
asiniscalchi Aug 8, 2023
df59324
sp-core in std , removed duplicate function
asiniscalchi Aug 8, 2023
2fbd144
documentaetion
asiniscalchi Aug 8, 2023
96d7374
using compilator type ifer
asiniscalchi Aug 8, 2023
5bf67b0
compiler infer the size of the array
asiniscalchi Aug 8, 2023
2bf2171
added to std feature
asiniscalchi Aug 8, 2023
9e1bf8d
rename variable
asiniscalchi Aug 8, 2023
bee4806
trait documentation
asiniscalchi Aug 8, 2023
7a25ce8
Merge branch 'dev' into refactoring/missing_from_precompile_erc_721
asiniscalchi Aug 9, 2023
874bfae
using pallet errors
asiniscalchi Aug 10, 2023
1322a4f
Erc721Error created
asiniscalchi Aug 10, 2023
1679acb
Erc721Error fix compilation
asiniscalchi Aug 10, 2023
e8e789f
tests green
asiniscalchi Aug 10, 2023
a2763f7
revise docuemntation
asiniscalchi Aug 10, 2023
1f3bf0c
Merge branch 'feature/using_proper_errors' into feature/precompile_er…
asiniscalchi Aug 10, 2023
3308324
fmt
asiniscalchi Aug 10, 2023
790361e
better name of constant
asiniscalchi Aug 10, 2023
dc4f211
create collection has param uri
asiniscalchi Aug 10, 2023
cc5bfdb
Merge branch 'dev' into feature/precompile_erc_721_token_uri
asiniscalchi Aug 11, 2023
2ddf57d
remove unused error
asiniscalchi Aug 11, 2023
4ef9e9c
using sp-std
asiniscalchi Aug 11, 2023
746fed6
using sp-std
asiniscalchi Aug 11, 2023
b0738dd
created CollectionBaseURI
asiniscalchi Aug 11, 2023
053a1ea
created CollectionBaseURI
asiniscalchi Aug 11, 2023
3007682
BaseURILimit is defined
asiniscalchi Aug 11, 2023
d817778
insering the base uri
asiniscalchi Aug 11, 2023
95dac77
insering the base uri
asiniscalchi Aug 11, 2023
d05b7b2
base_uri is set in storage
asiniscalchi Aug 11, 2023
f0dec74
create_collection trait has base_uri as param
asiniscalchi Aug 11, 2023
2a074d0
living_assets_ownership pallet tests are green
asiniscalchi Aug 11, 2023
ffa3835
tests passing
asiniscalchi Aug 11, 2023
c155971
test on base_uri too long
asiniscalchi Aug 11, 2023
cb2c18d
removing use
asiniscalchi Aug 11, 2023
4998cce
use Vec
asiniscalchi Aug 11, 2023
51342a0
testing minimum arguments
asiniscalchi Aug 16, 2023
c39d92f
create_collection has a BondedVec tokenURI
asiniscalchi Aug 16, 2023
a9f7053
removed unued error
asiniscalchi Aug 16, 2023
724d571
fix compilation
asiniscalchi Aug 16, 2023
1d3b5bc
refactoring
asiniscalchi Aug 16, 2023
95a3d07
refactoring
asiniscalchi Aug 16, 2023
e69a2b0
removed legacy doc
asiniscalchi Aug 16, 2023
e8fa0af
BaseURILimit type is not public
asiniscalchi Aug 16, 2023
4b87845
add transfer_from dummy tests and func definitions
magecnion Aug 16, 2023
373e1ba
traits have Error assiciated tyope
asiniscalchi Aug 16, 2023
990d44c
more generiuc signature
asiniscalchi Aug 16, 2023
e9d5b1e
fix compilation
asiniscalchi Aug 16, 2023
e76a680
test moved where concreate impl is tested
asiniscalchi Aug 16, 2023
5ac6078
Merge branch 'dev' into feature/precompile_erc_721_token_uri
asiniscalchi Aug 16, 2023
77120b4
removed harcoded buffer from tests
asiniscalchi Aug 17, 2023
01d7687
refactoring
asiniscalchi Aug 17, 2023
2396f85
checking for BaseURI bounds
asiniscalchi Aug 17, 2023
48a0b35
add transfer_from sender and receiver checks
magecnion Aug 17, 2023
c98517e
add dummy erc721 impl to pallet for compiling
magecnion Aug 17, 2023
de8cacb
Merge branch 'feature/precompile_erc_721_token_uri' of github.com:fre…
magecnion Aug 17, 2023
a5c8e78
erc721 transfer_from tests
magecnion Aug 17, 2023
f121c5c
erc721 precompile final tests
magecnion Aug 17, 2023
38f3aa8
Merge branch 'dev' of github.com:freeverseio/laos-ownership-node into…
magecnion Aug 18, 2023
5b0ee33
add transfer_from func signature to sol contract
magecnion Aug 18, 2023
e857fb0
WIP add getting initial owner for assets storage
magecnion Aug 21, 2023
a1ec297
add check to transfer_from precompile caller must be the owner
magecnion Aug 21, 2023
15d48b1
add transfer_from trait impl to pallet
magecnion Aug 21, 2023
6fcdfb4
fmt
magecnion Aug 21, 2023
200291f
owner_of trait return current owner
magecnion Aug 22, 2023
0e38420
add remix test
magecnion Aug 22, 2023
68aee06
use H160 instead of T::AcountId
magecnion Aug 22, 2023
ca229fc
Merge branch 'dev' of github.com:freeverseio/laos-ownership-node into…
magecnion Aug 22, 2023
89ffbf9
change requests
magecnion Aug 22, 2023
5fdd84b
add eth event
magecnion Aug 22, 2023
cf720b1
from u64 to H160
magecnion Aug 22, 2023
61b9d84
wip
magecnion Aug 23, 2023
6e76010
Merge branch 'dev' into feature/precompile_erc_721_transfer_from
asiniscalchi Aug 24, 2023
c1e7dc6
remove pallet_evm could from living asset pallet
magecnion Aug 24, 2023
5f52255
Merge branch 'feature/precompile_erc_721_transfer_from' of github.com…
magecnion Aug 24, 2023
6b3940d
change requests
magecnion Aug 24, 2023
3874112
Merge branch 'magecnion_change_requests' into feature/precompile_erc_…
magecnion Aug 24, 2023
191bd15
change requests
magecnion Aug 24, 2023
736aa55
change requests
magecnion Aug 24, 2023
dbb52e7
change requests
magecnion Aug 24, 2023
b7d8992
change requests
magecnion Aug 24, 2023
b380cf9
add AccountMapping impl to pallet config
magecnion Aug 25, 2023
e3d23c4
add initial_owner fn to AssetId
magecnion Aug 28, 2023
bdefb24
AccountMapping only have AccountId as generic
magecnion Aug 28, 2023
0fd1b46
AccountMapping runtime test
magecnion Aug 28, 2023
eb40e79
add AssetIdToAddress impl
magecnion Aug 29, 2023
77d572b
Merge branch 'dev' of github.com:freeverseio/laos-ownership-node into…
magecnion Aug 29, 2023
51f5d42
fix test after merge
magecnion Aug 29, 2023
eb026ae
fix test after merge and docs
magecnion Aug 29, 2023
a585737
Merge branch 'dev' of github.com:freeverseio/laos-ownership-node into…
magecnion Aug 30, 2023
2c5d2c2
use Convert substrate trait
magecnion Aug 30, 2023
6465469
use Convert substrate trait when parsing from address to account id
magecnion Aug 30, 2023
8bc3826
fmt
magecnion Aug 31, 2023
5d2d74d
fix
magecnion Aug 31, 2023
3a28060
Merge branch 'dev' of github.com:freeverseio/laos-ownership-node into…
magecnion Aug 31, 2023
2b1242a
fmt
magecnion Aug 31, 2023
dc8cc56
change requests
magecnion Aug 31, 2023
1d8c2b9
fmt
magecnion Aug 31, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ members = [
[workspace.dependencies]
parity-scale-codec = { version = "3.2.2", default-features = false, features = ["derive"] }
hex-literal = "0.4.1"
hex = { version = "0.4.3", default-features = false }
scale-info = { version = "2.7.0", default-features = false, features = ["derive"] }
smallvec = "1.10.0"
num_enum = { version = "0.5.3", default-features = false }
Expand Down
50 changes: 0 additions & 50 deletions pallets/living-assets-ownership/src/functions.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
//! Contains helper and utility functions of the pallet
use super::*;
use frame_support::sp_runtime::traits::One;
use sp_core::{H160, U256};

impl<T: Config> Pallet<T> {
/// See [Self::create_collection]
Expand All @@ -25,52 +24,3 @@ impl<T: Config> Pallet<T> {
Ok(collection_id)
}
}

pub fn convert_asset_id_to_owner(value: U256) -> H160 {
let mut bytes = [0u8; 20];
let value_bytes: [u8; 32] = value.into();
bytes.copy_from_slice(&value_bytes[value_bytes.len() - 20..]);
H160::from(bytes)
}

#[cfg(test)]
mod tests {
use crate::{functions::convert_asset_id_to_owner, H160, U256};

#[test]
fn check_convert_asset_id_to_owner() {
let value = U256::from(5);
let expected_address = H160::from_low_u64_be(5);
assert_eq!(convert_asset_id_to_owner(value), expected_address);
}

#[test]
fn check_two_assets_same_owner() {
// create two different assets
let asset1 = U256::from(
hex::decode("01C0F0f4ab324C46e55D02D0033343B4Be8A55532d").unwrap().as_slice(),
);
let asset2 = U256::from(
hex::decode("03C0F0f4ab324C46e55D02D0033343B4Be8A55532d").unwrap().as_slice(),
);
assert_ne!(asset1, asset2);

// check asset in decimal format
assert_eq!(
U256::from_str_radix("01C0F0f4ab324C46e55D02D0033343B4Be8A55532d", 16).unwrap(),
U256::from_dec_str("2563001357829637001682277476112176020532353127213").unwrap()
);
assert_eq!(
U256::from_str_radix("03C0F0f4ab324C46e55D02D0033343B4Be8A55532d", 16).unwrap(),
U256::from_dec_str("5486004632491442838089647141544742059844218213165").unwrap()
);

let mut owner = [0u8; 20];
owner.copy_from_slice(
hex::decode("C0F0f4ab324C46e55D02D0033343B4Be8A55532d").unwrap().as_slice(),
);
let expected_address = H160::from(owner);
assert_eq!(convert_asset_id_to_owner(asset1), expected_address);
assert_eq!(convert_asset_id_to_owner(asset2), expected_address);
}
}
38 changes: 26 additions & 12 deletions pallets/living-assets-ownership/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,22 @@
/// <https://docs.substrate.io/reference/frame-pallets/>
pub use pallet::*;
use parity_scale_codec::alloc::string::ToString;
use sp_core::{H160, U256};
use sp_core::H160;
use sp_std::vec::Vec;

mod functions;
pub mod traits;

#[frame_support::pallet]
pub mod pallet {

use crate::functions::convert_asset_id_to_owner;

use super::*;
use crate::traits::{AccountMapping, AssetIdToAddress};
use frame_support::{
pallet_prelude::{OptionQuery, ValueQuery, *},
BoundedVec,
};
use frame_system::pallet_prelude::*;
use sp_core::{H160, U256};

/// Collection id type
pub type CollectionId = u64;
Expand All @@ -39,7 +38,7 @@ pub mod pallet {
type RuntimeEvent: From<Event<Self>> + IsType<<Self as frame_system::Config>::RuntimeEvent>;

/// Specifies the advised maximum length for a Base URI.
//s
///
/// The URI standard (RFC 3986) doesn't dictates a limit for the length of URIs.
/// However it seems the max supported length in browsers is 2,048 characters.
///
Expand All @@ -48,6 +47,14 @@ pub mod pallet {
/// (which takes up 33 characters).
#[pallet::constant]
type BaseURILimit: Get<u32>;

/// Type alias for implementing the `AccountMapping` trait for a given account ID type.
/// This allows you to define custom logic for converting between account IDs and H160 addresses.
type AccountMapping: traits::AccountMapping<Self::AccountId>;
magecnion marked this conversation as resolved.
Show resolved Hide resolved

/// Type alias for implementing the `AssetIdToAddress` trait for a given account ID type.
/// This allows you to specify which account should initially own each new asset.
type AssetIdToAddress: traits::AssetIdToAddress<Self::AccountId>;
}

/// Collection counter
Expand All @@ -64,10 +71,10 @@ pub mod pallet {
/// Asset owner
#[pallet::storage]
pub(super) type AssetOwner<T: Config> =
StorageMap<_, Blake2_128Concat, U256, H160, OptionQuery>;
StorageMap<_, Blake2_128Concat, U256, T::AccountId, OptionQuery>;

fn asset_owner<T: Config>(key: U256) -> H160 {
AssetOwner::<T>::get(key).unwrap_or_else(|| convert_asset_id_to_owner(key))
fn asset_owner<T: Config>(key: U256) -> T::AccountId {
AssetOwner::<T>::get(key).unwrap_or_else(|| T::AssetIdToAddress::initial_owner(key))
}

/// Pallet events
Expand All @@ -79,7 +86,7 @@ pub mod pallet {
CollectionCreated { collection_id: CollectionId, who: T::AccountId },
/// Asset transferred to `who`
/// parameters. [asset_id_id, who]
AssetTransferred { asset_id: U256, receiver: H160 },
AssetTransferred { asset_id: U256, receiver: T::AccountId },
}

// Errors inform users that something went wrong.
Expand Down Expand Up @@ -153,7 +160,7 @@ pub mod pallet {

fn owner_of(collection_id: CollectionId, asset_id: U256) -> Result<H160, Self::Error> {
Pallet::<T>::collection_base_uri(collection_id).ok_or(Error::CollectionDoesNotExist)?;
Ok(asset_owner::<T>(asset_id))
Ok(T::AccountMapping::into_h160(asset_owner::<T>(asset_id)))
}

fn transfer_from(
Expand All @@ -165,10 +172,14 @@ pub mod pallet {
) -> Result<(), Self::Error> {
Pallet::<T>::collection_base_uri(collection_id).ok_or(Error::CollectionDoesNotExist)?;
ensure!(origin == from, Error::NoPermission);
ensure!(asset_owner::<T>(asset_id) == from, Error::NoPermission);
ensure!(
T::AccountMapping::into_h160(asset_owner::<T>(asset_id)) == from,
Error::NoPermission
);
ensure!(from != to, Error::CannotTransferSelf);
ensure!(to != H160::zero(), Error::TransferToNullAddress);

let to = T::AccountMapping::into_account_id(to.clone());
AssetOwner::<T>::set(asset_id, Some(to.clone()));
Self::deposit_event(Event::AssetTransferred { asset_id, receiver: to });

Expand Down Expand Up @@ -217,7 +228,10 @@ pub enum CollectionError {
/// # Returns
///
/// * An `H160` representation of the collection ID.
pub fn collection_id_to_address(collection_id: CollectionId) -> H160 {
pub fn collection_id_to_address(collection_id: CollectionId) -> H160
// where
magecnion marked this conversation as resolved.
Show resolved Hide resolved
// T::AccountId: From<[u8; 20]>,
{
let mut bytes = [0u8; 20];
bytes[12..20].copy_from_slice(&collection_id.to_be_bytes());
for (i, byte) in ASSET_PRECOMPILE_ADDRESS_PREFIX.iter().enumerate() {
Expand Down
29 changes: 26 additions & 3 deletions pallets/living-assets-ownership/src/mock.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate as pallet_livingassets_ownership;
use crate::{self as pallet_livingassets_ownership, traits};
use frame_support::traits::{ConstU16, ConstU64};
use sp_core::{ConstU32, H256};
use sp_core::{ConstU32, H160, H256, U256};
use sp_runtime::{
traits::{BlakeTwo256, IdentityLookup},
BuildStorage,
Expand All @@ -9,6 +9,7 @@ use sp_std::{boxed::Box, prelude::*};

type Block = frame_system::mocking::MockBlock<Test>;
type Nonce = u32;
type AccountId = u64;

// Configure a mock runtime to test the pallet.
frame_support::construct_runtime!(
Expand All @@ -30,7 +31,7 @@ impl frame_system::Config for Test {
type Hash = H256;
type Nonce = Nonce;
type Hashing = BlakeTwo256;
type AccountId = u64;
type AccountId = AccountId;
type Lookup = IdentityLookup<Self::AccountId>;
type RuntimeEvent = RuntimeEvent;
type BlockHashCount = ConstU64<250>;
Expand All @@ -48,6 +49,28 @@ impl frame_system::Config for Test {
impl pallet_livingassets_ownership::Config for Test {
type RuntimeEvent = RuntimeEvent;
type BaseURILimit = ConstU32<256>;
type AccountMapping = MockAccountMapping;
type AssetIdToAddress = MockAssetIdToAddress;
}

pub struct MockAccountMapping;
impl traits::AccountMapping<AccountId> for MockAccountMapping {
fn into_h160(account_id: AccountId) -> H160 {
H160::from_low_u64_be(account_id)
}
fn into_account_id(account_id: H160) -> AccountId {
H160::to_low_u64_be(&account_id)
}
}

pub struct MockAssetIdToAddress;
impl traits::AssetIdToAddress<AccountId> for MockAssetIdToAddress {
fn initial_owner(asset_id: U256) -> AccountId {
let mut first_eight_bytes = [0u8; 8];
let asset_id_bytes: [u8; 32] = asset_id.into();
first_eight_bytes.copy_from_slice(&asset_id_bytes[asset_id_bytes.len() - 8..]);
u64::from_be_bytes(first_eight_bytes).into()
}
}

// Build genesis storage according to the mock runtime.
Expand Down
9 changes: 4 additions & 5 deletions pallets/living-assets-ownership/src/tests.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
use core::str::FromStr;

use crate::{
address_to_collection_id, collection_id_to_address, is_collection_address, mock::*, AssetOwner,
CollectionBaseURI, CollectionError, Event,
};
use core::str::FromStr;
use frame_support::assert_ok;
use sp_core::H160;

Expand Down Expand Up @@ -339,7 +338,7 @@ mod traits {
let asset_id = U256::from(
hex::decode("03C0F0f4ab324C46e55D02D0033343B4Be8A55532d").unwrap().as_slice(),
);
let sender = H160::from_str("C0F0f4ab324C46e55D02D0033343B4Be8A55532d").unwrap();
let sender = H160::from_str("0000000000000000000000003343b4be8a55532d").unwrap();
let receiver = H160::from_low_u64_be(BOB);
new_test_ext().execute_with(|| {
System::set_block_number(1);
Expand All @@ -349,9 +348,9 @@ mod traits {
assert_ok!(<LivingAssetsModule as Erc721>::transfer_from(
sender, 1, sender, receiver, asset_id,
));
assert_eq!(AssetOwner::<Test>::get(asset_id).unwrap(), receiver);
assert_eq!(AssetOwner::<Test>::get(asset_id).unwrap(), BOB);
assert_eq!(<LivingAssetsModule as Erc721>::owner_of(1, asset_id).unwrap(), receiver);
System::assert_last_event(Event::AssetTransferred { asset_id, receiver }.into());
System::assert_last_event(Event::AssetTransferred { asset_id, receiver: BOB }.into());
});
}

Expand Down
41 changes: 41 additions & 0 deletions pallets/living-assets-ownership/src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,3 +96,44 @@ pub trait Erc721 {
asset_id: U256,
) -> Result<(), Self::Error>;
}

/// A trait defining the mapping between a generic account ID and an H160 address.
/// This is useful for compatibility with Ethereum-like networks.
pub trait AccountMapping<AccountId> {
magecnion marked this conversation as resolved.
Show resolved Hide resolved
/// Converts a generic account ID into an H160 address.
///
/// # Arguments
///
/// * `account_id` - The generic account ID to convert.
///
/// # Returns
///
/// Returns the H160 address corresponding to the input account ID.
fn into_h160(account_id: AccountId) -> H160;
magecnion marked this conversation as resolved.
Show resolved Hide resolved

/// Converts an H160 address into a generic account ID.
///
/// # Arguments
///
/// * `account_id` - The H160 address to convert.
///
/// # Returns
///
/// Returns the generic account ID corresponding to the input H160 address.
fn into_account_id(account_id: H160) -> AccountId;
magecnion marked this conversation as resolved.
Show resolved Hide resolved
}

/// A trait for associating a given asset ID to an initial account ID.
/// Useful for specifying which account should initially own a new asset.
pub trait AssetIdToAddress<AccountId> {
/// Determines the initial owner of an asset given its ID.
///
/// # Arguments
///
/// * `asset_id` - The asset ID to query for its initial owner.
///
/// # Returns
///
/// Returns the account ID of the initial owner of the asset.
fn initial_owner(asset_id: U256) -> AccountId;
}
1 change: 1 addition & 0 deletions runtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ substrate-wasm-builder = { workspace = true, optional = true }
[dependencies]
parity-scale-codec = { workspace = true, features = ["derive"] }
hex-literal = { workspace = true, optional = true }
hex = { workspace = true }
scale-info = { workspace = true, features = ["derive"] }
smallvec = { workspace = true }

Expand Down
33 changes: 32 additions & 1 deletion runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#[cfg(feature = "std")]
include!(concat!(env!("OUT_DIR"), "/wasm_binary.rs"));

mod tests;
mod weights;
pub mod xcm_config;
use parity_scale_codec as codec;
Expand All @@ -28,7 +29,7 @@ use sp_runtime::{
IdentifyAccount, PostDispatchInfoOf, UniqueSaturatedInto, Verify,
},
transaction_validity::{TransactionSource, TransactionValidity, TransactionValidityError},
ApplyExtrinsicResult, ConsensusEngineId,
AccountId32, ApplyExtrinsicResult, ConsensusEngineId, MultiSignature,
};

use sp_std::prelude::*;
Expand Down Expand Up @@ -469,6 +470,8 @@ impl pallet_collator_selection::Config for Runtime {
impl pallet_living_assets_ownership::Config for Runtime {
type RuntimeEvent = RuntimeEvent;
type BaseURILimit = ConstU32<2015>;
type AccountMapping = AccountMapping;
type AssetIdToAddress = AssetIdToAddress;
}

impl pallet_sudo::Config for Runtime {
Expand All @@ -477,6 +480,34 @@ impl pallet_sudo::Config for Runtime {
type WeightInfo = ();
}

pub struct AccountMapping;
magecnion marked this conversation as resolved.
Show resolved Hide resolved
impl pallet_living_assets_ownership::traits::AccountMapping<AccountId> for AccountMapping {
magecnion marked this conversation as resolved.
Show resolved Hide resolved
fn into_h160(account_id: AccountId) -> H160 {
let mut bytes = [0u8; 20];
let account_id_bytes: [u8; 32] = account_id.into();
bytes.copy_from_slice(&account_id_bytes[account_id_bytes.len() - 20..]);
H160::from(bytes)
}
fn into_account_id(account_id: H160) -> AccountId {
let mut data = [0u8; 32];
data[12..].copy_from_slice(&account_id.0);
AccountId32::from(data)
}
}

pub struct AssetIdToAddress;
impl pallet_living_assets_ownership::traits::AssetIdToAddress<AccountId> for AssetIdToAddress {
fn initial_owner(asset_id: U256) -> AccountId {
let mut bytes = [0u8; 20];
let asset_id_bytes: [u8; 32] = asset_id.into();
bytes.copy_from_slice(&asset_id_bytes[asset_id_bytes.len() - 20..]);
let owner = H160::from(bytes);
<AccountMapping as pallet_living_assets_ownership::traits::AccountMapping<AccountId>>::into_account_id(
owner,
)
}
}

// Frontier

impl pallet_evm_chain_id::Config for Runtime {}
Expand Down
Loading