Skip to content

Commit 22d634d

Browse files
committed
feat(evm_icp_bridge): canister API
1 parent fcf1f0b commit 22d634d

File tree

3 files changed

+230
-128
lines changed

3 files changed

+230
-128
lines changed

src/evm_icp_bridge/src/api.rs

Lines changed: 69 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
use alloy::{
2+
eips::Encodable2718,
3+
primitives::{Address, Bytes},
4+
};
5+
16
use crate::{helper::msg_caller, store};
27

38
#[ic_cdk::query]
@@ -6,12 +11,41 @@ fn info() -> Result<store::StateInfo, String> {
611
}
712

813
#[ic_cdk::query]
9-
fn evm_address() -> Result<String, String> {
14+
fn my_evm_address() -> Result<String, String> {
1015
let caller = msg_caller()?;
1116
let addr = store::state::evm_address(&caller);
1217
Ok(addr.to_string())
1318
}
1419

20+
#[ic_cdk::query]
21+
async fn my_pending_logs() -> Result<Vec<store::BridgeLog>, String> {
22+
let caller = msg_caller()?;
23+
let rt = store::state::with(|s| {
24+
s.pending
25+
.iter()
26+
.filter_map(|item| {
27+
if item.user == caller {
28+
Some(item.clone())
29+
} else {
30+
None
31+
}
32+
})
33+
.collect::<Vec<store::BridgeLog>>()
34+
});
35+
Ok(rt)
36+
}
37+
38+
#[ic_cdk::query]
39+
async fn my_finalized_logs(
40+
prev: Option<u64>,
41+
take: Option<u64>,
42+
) -> Result<Vec<store::BridgeLog>, String> {
43+
let caller = msg_caller()?;
44+
let take = take.unwrap_or(10).min(1000) as usize;
45+
let rt = store::state::logs(caller, prev, take);
46+
Ok(rt)
47+
}
48+
1549
#[ic_cdk::update]
1650
async fn bridge(
1751
from_chain: String,
@@ -22,3 +56,37 @@ async fn bridge(
2256
let now_ms = ic_cdk::api::time() / 1_000_000;
2357
store::state::bridge(from_chain, to_chain, icp_amount, caller, now_ms).await
2458
}
59+
60+
#[ic_cdk::update]
61+
async fn erc20_transfer_tx(chain: String, to: String, icp_amount: u128) -> Result<String, String> {
62+
let to_addr = to
63+
.parse::<Address>()
64+
.map_err(|err| format!("invalid to address: {}", err))?;
65+
let caller = msg_caller()?;
66+
let now_ms = ic_cdk::api::time() / 1_000_000;
67+
let (_, signed_tx) =
68+
store::state::build_erc20_transfer_tx(&chain, &caller, &to_addr, icp_amount, now_ms)
69+
.await?;
70+
let data = signed_tx.encoded_2718();
71+
Ok(Bytes::from(data).to_string())
72+
}
73+
74+
#[ic_cdk::update]
75+
async fn erc20_transfer(chain: String, to: String, icp_amount: u128) -> Result<String, String> {
76+
let to_addr = to
77+
.parse::<Address>()
78+
.map_err(|err| format!("invalid to address: {}", err))?;
79+
let caller = msg_caller()?;
80+
let now_ms = ic_cdk::api::time() / 1_000_000;
81+
let (cli, signed_tx) =
82+
store::state::build_erc20_transfer_tx(&chain, &caller, &to_addr, icp_amount, now_ms)
83+
.await?;
84+
let tx_hash = signed_tx.hash().to_string();
85+
86+
let data = signed_tx.encoded_2718();
87+
let _ = cli
88+
.send_raw_transaction(now_ms, Bytes::from(data).to_string())
89+
.await?;
90+
91+
Ok(tx_hash)
92+
}

src/evm_icp_bridge/src/evm.rs

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -78,16 +78,16 @@ impl EvmClient {
7878
hex_to_u64(&res)
7979
}
8080

81-
pub async fn get_balance(&self, now_ms: u64, address: &Address) -> Result<u128, String> {
82-
let res: String = self
83-
.call(
84-
format!("eth_getBalance-{}", now_ms),
85-
"eth_getBalance",
86-
&[address.to_string().into(), "finalized".into()],
87-
)
88-
.await?;
89-
hex_to_u128(&res)
90-
}
81+
// pub async fn get_balance(&self, now_ms: u64, address: &Address) -> Result<u128, String> {
82+
// let res: String = self
83+
// .call(
84+
// format!("eth_getBalance-{}", now_ms),
85+
// "eth_getBalance",
86+
// &[address.to_string().into(), "finalized".into()],
87+
// )
88+
// .await?;
89+
// hex_to_u128(&res)
90+
// }
9191

9292
pub async fn get_transaction_receipt(
9393
&self,
@@ -137,12 +137,12 @@ impl EvmClient {
137137
<Vec<u8>>::from_hex(res).map_err(|err| err.to_string())
138138
}
139139

140-
pub async fn erc20_name(&self, now_ms: u64, contract: &Address) -> Result<String, String> {
141-
let res = self
142-
.call_contract(now_ms, contract, "0x06fdde03".to_string())
143-
.await?;
144-
decode_abi_string(&res)
145-
}
140+
// pub async fn erc20_name(&self, now_ms: u64, contract: &Address) -> Result<String, String> {
141+
// let res = self
142+
// .call_contract(now_ms, contract, "0x06fdde03".to_string())
143+
// .await?;
144+
// decode_abi_string(&res)
145+
// }
146146

147147
pub async fn erc20_symbol(&self, now_ms: u64, contract: &Address) -> Result<String, String> {
148148
let res = self
@@ -325,9 +325,9 @@ fn decode_abi_uint(bytes: &[u8]) -> Result<U256, String> {
325325
Ok(U256::from_be_slice(bytes))
326326
}
327327

328-
fn decode_abi_address(bytes: &[u8]) -> Result<Address, String> {
329-
if bytes.len() != 32 {
330-
return Err("abi address result must be 32 bytes".to_string());
331-
}
332-
Address::try_from(&bytes[12..32]).map_err(|err| err.to_string())
333-
}
328+
// fn decode_abi_address(bytes: &[u8]) -> Result<Address, String> {
329+
// if bytes.len() != 32 {
330+
// return Err("abi address result must be 32 bytes".to_string());
331+
// }
332+
// Address::try_from(&bytes[12..32]).map_err(|err| err.to_string())
333+
// }

0 commit comments

Comments
 (0)