Skip to content

Commit

Permalink
feat: add enum type and update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ashutoshvarma committed Oct 24, 2023
1 parent 880d595 commit 2693743
Show file tree
Hide file tree
Showing 7 changed files with 139 additions and 49 deletions.
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.

9 changes: 8 additions & 1 deletion chain-extensions/types/unified-accounts/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
#![cfg_attr(not(feature = "std"), no_std)]

use num_enum::{IntoPrimitive, TryFromPrimitive};
use parity_scale_codec::{Decode, Encode};
use parity_scale_codec::{Decode, Encode, MaxEncodedLen};

#[repr(u16)]
#[derive(TryFromPrimitive, IntoPrimitive, Decode, Encode)]
Expand All @@ -33,3 +33,10 @@ pub enum Command {
/// Get the mapped Native address if any otheriwse default associated Native address
GetNativeAddressOrDefault = 3,
}

#[derive(Debug, Copy, Clone, PartialEq, Eq, Encode, Decode, MaxEncodedLen)]
#[cfg_attr(feature = "std", derive(scale_info::TypeInfo))]
pub enum UnifiedAddress<T: Encode + Decode> {
Mapped(T),
Default(T),
}
34 changes: 16 additions & 18 deletions chain-extensions/unified-accounts/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,10 @@ use pallet_contracts::chain_extension::{
};
use pallet_evm::AddressMapping;
use parity_scale_codec::Encode;
pub use unified_accounts_chain_extension_types::Command::{self, *};
pub use unified_accounts_chain_extension_types::{
Command::{self, *},
UnifiedAddress,
};

#[derive(DefaultNoBound)]
pub struct UnifiedAccountsExtension<T, UA>(PhantomData<(T, UA)>);
Expand Down Expand Up @@ -63,16 +66,13 @@ where
let base_weight = <T as frame_system::Config>::DbWeight::get().reads(1);
env.charge_weight(base_weight)?;

// read the storage item
let mapped = UA::to_h160(&account_id);

let is_mapped = mapped.is_some();
let evm_address = mapped.unwrap_or_else(|| {
// fallback to default account_id
T::DefaultNativeToEvm::into_h160(account_id)
});
let evm_address = if let Some(h160) = UA::to_h160(&account_id) {
UnifiedAddress::Mapped(h160)
} else {
UnifiedAddress::Default(T::DefaultNativeToEvm::into_h160(account_id))
};
// write to buffer
(evm_address, is_mapped).using_encoded(|r| env.write(r, false, None))?;
evm_address.using_encoded(|r| env.write(r, false, None))?;
}
GetNativeAddress => {
let evm_address: EvmAddress = env.read_as()?;
Expand All @@ -89,16 +89,14 @@ where
env.charge_weight(base_weight)?;

// read the storage item
let mapped = UA::to_account_id(&evm_address);

let is_mapped = mapped.is_some();
let native_address = mapped.unwrap_or_else(|| {
// fallback to default evm_address
T::DefaultEvmToNative::into_account_id(evm_address)
});
let native_address = if let Some(native) = UA::to_account_id(&evm_address) {
UnifiedAddress::Mapped(native)
} else {
UnifiedAddress::Default(T::DefaultEvmToNative::into_account_id(evm_address))
};

// write to buffer
(native_address, is_mapped).using_encoded(|r| env.write(r, false, None))?;
native_address.using_encoded(|r| env.write(r, false, None))?;
}
};
Ok(RetVal::Converging(0))
Expand Down
132 changes: 107 additions & 25 deletions tests/ink-contracts/au_ce_getters.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"source": {
"hash": "0x5eb72ad6cdb6869bb8419ae07ff533bca787346360e58713c679f14679e188f7",
"hash": "0x0cfe2b4d2b98a121c0ce38b07d826f1e4d0c7d9870ff84239cd554b32904617d",
"language": "ink! 4.3.0",
"compiler": "rustc 1.72.0",
"build_info": {
Expand Down Expand Up @@ -52,32 +52,32 @@
"displayName": [
"Balance"
],
"type": 14
"type": 16
},
"blockNumber": {
"displayName": [
"BlockNumber"
],
"type": 17
"type": 19
},
"chainExtension": {
"displayName": [
"ChainExtension"
],
"type": 18
"type": 20
},
"hash": {
"displayName": [
"Hash"
],
"type": 15
"type": 17
},
"maxEventTopics": 4,
"timestamp": {
"displayName": [
"Timestamp"
],
"type": 16
"type": 18
}
},
"events": [],
Expand Down Expand Up @@ -105,7 +105,7 @@
"docs": [],
"label": "to_h160",
"mutates": false,
"payable": true,
"payable": false,
"returnType": {
"displayName": [
"ink",
Expand All @@ -131,7 +131,7 @@
"docs": [],
"label": "to_h160_or_default",
"mutates": false,
"payable": true,
"payable": false,
"returnType": {
"displayName": [
"ink",
Expand All @@ -157,13 +157,13 @@
"docs": [],
"label": "to_account_id",
"mutates": false,
"payable": true,
"payable": false,
"returnType": {
"displayName": [
"ink",
"MessageResult"
],
"type": 11
"type": 12
},
"selector": "0x0000002c"
},
Expand All @@ -183,13 +183,13 @@
"docs": [],
"label": "to_account_id_or_default",
"mutates": false,
"payable": true,
"payable": false,
"returnType": {
"displayName": [
"ink",
"MessageResult"
],
"type": 13
"type": 14
},
"selector": "0x0000002d"
}
Expand Down Expand Up @@ -429,7 +429,7 @@
{
"fields": [
{
"type": 8
"type": 11
}
],
"index": 0,
Expand All @@ -450,7 +450,7 @@
"params": [
{
"name": "T",
"type": 8
"type": 11
},
{
"name": "E",
Expand All @@ -471,7 +471,48 @@
{
"fields": [
{
"type": 12
"type": 8,
"typeName": "T"
}
],
"index": 0,
"name": "Mapped"
},
{
"fields": [
{
"type": 8,
"typeName": "T"
}
],
"index": 1,
"name": "Default"
}
]
}
},
"params": [
{
"name": "T",
"type": 8
}
],
"path": [
"unified_accounts_chain_extension_types",
"UnifiedAddress"
]
}
},
{
"id": 12,
"type": {
"def": {
"variant": {
"variants": [
{
"fields": [
{
"type": 13
}
],
"index": 0,
Expand All @@ -492,7 +533,7 @@
"params": [
{
"name": "T",
"type": 12
"type": 13
},
{
"name": "E",
Expand All @@ -505,7 +546,7 @@
}
},
{
"id": 12,
"id": 13,
"type": {
"def": {
"variant": {
Expand Down Expand Up @@ -538,15 +579,15 @@
}
},
{
"id": 13,
"id": 14,
"type": {
"def": {
"variant": {
"variants": [
{
"fields": [
{
"type": 3
"type": 15
}
],
"index": 0,
Expand All @@ -567,7 +608,7 @@
"params": [
{
"name": "T",
"type": 3
"type": 15
},
{
"name": "E",
Expand All @@ -580,15 +621,56 @@
}
},
{
"id": 14,
"id": 15,
"type": {
"def": {
"variant": {
"variants": [
{
"fields": [
{
"type": 3,
"typeName": "T"
}
],
"index": 0,
"name": "Mapped"
},
{
"fields": [
{
"type": 3,
"typeName": "T"
}
],
"index": 1,
"name": "Default"
}
]
}
},
"params": [
{
"name": "T",
"type": 3
}
],
"path": [
"unified_accounts_chain_extension_types",
"UnifiedAddress"
]
}
},
{
"id": 16,
"type": {
"def": {
"primitive": "u128"
}
}
},
{
"id": 15,
"id": 17,
"type": {
"def": {
"composite": {
Expand All @@ -608,23 +690,23 @@
}
},
{
"id": 16,
"id": 18,
"type": {
"def": {
"primitive": "u64"
}
}
},
{
"id": 17,
"id": 19,
"type": {
"def": {
"primitive": "u32"
}
}
},
{
"id": 18,
"id": 20,
"type": {
"def": {
"variant": {}
Expand Down
Binary file modified tests/ink-contracts/au_ce_getters.wasm
Binary file not shown.
1 change: 1 addition & 0 deletions tests/integration/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ pallet-evm-precompile-assets-erc20 = { workspace = true }
pallet-evm-precompile-dispatch = { workspace = true }
pallet-unified-accounts = { workspace = true }
precompile-utils = { workspace = true }
unified-accounts-chain-extension-types = { workspace = true }

astar-primitives = { workspace = true }
astar-runtime = { workspace = true, features = ["std"], optional = true }
Expand Down
Loading

0 comments on commit 2693743

Please sign in to comment.