Skip to content

Commit b8c1fab

Browse files
feat(eip7702): Add Biz Smart Contract Account Type (#4319)
* fix(eip7702): Add `UserOperationMode` * Add `erc4337.biz_account.abi.json` ABI * fix(eip7702): Add `test_barz_transfer_erc7702_eoa` test * fix(eip7702): Fix `Biz.execute4337Ops()` * fix(eip7702): Minor changes * fix(eip7702): Rename `UserOperationMode` to `SCAccountType`
1 parent df42d6b commit b8c1fab

File tree

5 files changed

+786
-37
lines changed

5 files changed

+786
-37
lines changed

rust/tw_evm/src/abi/prebuild/erc4337.rs

+73-1
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,26 @@
33
// Copyright © 2017 Trust Wallet.
44

55
use crate::abi::contract::Contract;
6+
use crate::abi::param_token::NamedToken;
67
use crate::abi::param_type::ParamType;
78
use crate::abi::token::Token;
8-
use crate::abi::AbiResult;
9+
use crate::abi::{AbiError, AbiErrorKind, AbiResult};
910
use crate::address::Address;
1011
use lazy_static::lazy_static;
12+
use tw_coin_entry::error::prelude::{OrTWError, ResultContext};
1113
use tw_memory::Data;
1214
use tw_number::U256;
1315

1416
/// Generated via https://remix.ethereum.org
1517
/// https://github.com/eth-infinitism/account-abstraction/blob/develop/contracts/samples/SimpleAccount.sol
1618
const ERC4337_SIMPLE_ACCOUNT_ABI: &str = include_str!("resource/erc4337.simple_account.abi.json");
19+
const ERC4337_BIZ_ACCOUNT_ABI: &str = include_str!("resource/erc4337.biz_account.abi.json");
1720

1821
lazy_static! {
1922
static ref ERC4337_SIMPLE_ACCOUNT: Contract =
2023
serde_json::from_str(ERC4337_SIMPLE_ACCOUNT_ABI).unwrap();
24+
static ref ERC4337_BIZ_ACCOUNT: Contract =
25+
serde_json::from_str(ERC4337_BIZ_ACCOUNT_ABI).unwrap();
2126
}
2227

2328
pub struct ExecuteArgs {
@@ -38,6 +43,15 @@ impl Erc4337SimpleAccount {
3843
])
3944
}
4045

46+
pub fn encode_execute_4337_op(args: ExecuteArgs) -> AbiResult<Data> {
47+
let func = ERC4337_BIZ_ACCOUNT.function("execute4337Op")?;
48+
func.encode_input(&[
49+
Token::Address(args.to),
50+
Token::u256(args.value),
51+
Token::Bytes(args.data),
52+
])
53+
}
54+
4155
pub fn encode_execute_batch<I>(args: I) -> AbiResult<Data>
4256
where
4357
I: IntoIterator<Item = ExecuteArgs>,
@@ -66,4 +80,62 @@ impl Erc4337SimpleAccount {
6680
Token::array(ParamType::Bytes, datas),
6781
])
6882
}
83+
84+
pub fn encode_execute_4337_ops<I>(args: I) -> AbiResult<Data>
85+
where
86+
I: IntoIterator<Item = ExecuteArgs>,
87+
{
88+
let func = ERC4337_BIZ_ACCOUNT.function("execute4337Ops")?;
89+
90+
// `tuple[]`, where each item is a tuple of (address, uint256, bytes).
91+
let array_param = func
92+
.inputs
93+
.first()
94+
.or_tw_err(AbiErrorKind::Error_internal)
95+
.context("'Biz.execute4337Ops()' should contain only one argument")?;
96+
97+
let ParamType::Array {
98+
kind: array_elem_type,
99+
} = array_param.kind.clone()
100+
else {
101+
return AbiError::err(AbiErrorKind::Error_internal).with_context(|| {
102+
format!(
103+
"'Biz.execute4337Ops()' input argument should be an array, found: {:?}",
104+
array_param.kind
105+
)
106+
});
107+
};
108+
109+
let ParamType::Tuple {
110+
params: tuple_params,
111+
} = array_elem_type.as_ref()
112+
else {
113+
return AbiError::err(AbiErrorKind::Error_internal).with_context(|| {
114+
format!(
115+
"'Biz.execute4337Ops()' input argument should be an array of tuples, found: {array_elem_type:?}",
116+
)
117+
});
118+
};
119+
120+
if tuple_params.len() != 3 {
121+
return AbiError::err(AbiErrorKind::Error_internal).with_context(|| {
122+
format!(
123+
"'Biz.execute4337Ops()' input argument should be an array of tuples with 3 elements, found: {}", tuple_params.len()
124+
)
125+
});
126+
}
127+
128+
let array_tokens = args
129+
.into_iter()
130+
.map(|call| Token::Tuple {
131+
params: vec![
132+
NamedToken::with_param_and_token(&tuple_params[0], Token::Address(call.to)),
133+
NamedToken::with_param_and_token(&tuple_params[1], Token::u256(call.value)),
134+
NamedToken::with_param_and_token(&tuple_params[2], Token::Bytes(call.data)),
135+
],
136+
})
137+
.collect();
138+
139+
func.encode_input(&[Token::array(*array_elem_type, array_tokens)])
140+
}
69141
}

0 commit comments

Comments
 (0)