-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathexample.rs
281 lines (245 loc) · 10.1 KB
/
example.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
use candid::candid_method;
use ic_cdk::api::management_canister::http_request::{HttpResponse, TransformArgs};
use ic_cdk_macros::{self, update, query};
use std::str::FromStr;
use ic_web3::transports::ICHttp;
use ic_web3::Web3;
use ic_web3::ic::{get_eth_addr, KeyInfo};
use ic_web3::{
contract::{Contract, Options},
ethabi::ethereum_types::{U64, U256},
types::{Address, TransactionParameters, BlockId},
};
//const URL: &str = "https://ethereum.publicnode.com";
const URL: &str = "https://eth-goerli.g.alchemy.com/v2/0QCHDmgIEFRV48r1U1QbtOyFInib3ZAm";
const CHAIN_ID: u64 = 5;
//const KEY_NAME: &str = "dfx_test_key";
const KEY_NAME: &str = "test_key_1";
const TOKEN_ABI: &[u8] = include_bytes!("../src/contract/res/token.json");
type Result<T, E> = std::result::Result<T, E>;
#[query(name = "transform")]
#[candid_method(query, rename = "transform")]
fn transform(response: TransformArgs) -> HttpResponse {
let mut t = response.response;
t.headers = vec![];
t
}
#[update(name = "get_block")]
#[candid_method(update, rename = "get_block")]
async fn get_block(number: u64) -> Result<String, String> {
let w3 = match ICHttp::new(URL, None) {
Ok(v) => { Web3::new(v) },
Err(e) => { return Err(e.to_string()) },
};
let block_id = BlockId::from(U64::from(number));
let block = w3.eth().block(block_id).await.map_err(|e| format!("get block error: {}", e))?;
ic_cdk::println!("block: {:?}", block.clone().unwrap());
Ok(serde_json::to_string(&block.unwrap()).unwrap())
}
#[update(name = "get_eth_gas_price")]
#[candid_method(update, rename = "get_eth_gas_price")]
async fn get_eth_gas_price() -> Result<String, String> {
let w3 = match ICHttp::new(URL, None) {
Ok(v) => { Web3::new(v) },
Err(e) => { return Err(e.to_string()) },
};
let gas_price = w3.eth().gas_price().await.map_err(|e| format!("get gas price failed: {}", e))?;
ic_cdk::println!("gas price: {}", gas_price);
Ok(format!("{}", gas_price))
}
// get canister's ethereum address
#[update(name = "get_canister_addr")]
#[candid_method(update, rename = "get_canister_addr")]
async fn get_canister_addr() -> Result<String, String> {
match get_eth_addr(None, None, KEY_NAME.to_string()).await {
Ok(addr) => { Ok(hex::encode(addr)) },
Err(e) => { Err(e) },
}
}
#[update(name = "get_tx_count")]
#[candid_method(update, rename = "get_tx_count")]
async fn get_tx_count(addr: String) -> Result<u64, String> {
let w3 = match ICHttp::new(URL, None) {
Ok(v) => { Web3::new(v) },
Err(e) => { return Err(e.to_string()) },
};
let from_addr = Address::from_str(&addr).unwrap();
let tx_count = w3.eth()
.transaction_count(from_addr, None)
.await
.map_err(|e| format!("get tx count error: {}", e))?;
Ok(tx_count.as_u64())
}
#[update(name = "get_eth_balance")]
#[candid_method(update, rename = "get_eth_balance")]
async fn get_eth_balance(addr: String) -> Result<String, String> {
let w3 = match ICHttp::new(URL, None) {
Ok(v) => { Web3::new(v) },
Err(e) => { return Err(e.to_string()) },
};
let balance = w3.eth().balance(Address::from_str(&addr).unwrap(), None).await.map_err(|e| format!("get balance failed: {}", e))?;
Ok(format!("{}", balance))
}
#[update(name = "batch_request")]
#[candid_method(update, rename = "batch_request")]
async fn batch_request() -> Result<String, String> {
let http = ICHttp::new(URL, None).map_err(|e| format!("init ICHttp failed: {}", e))?;
let w3 = Web3::new(ic_web3::transports::Batch::new(http));
let block_number = w3.eth().block_number();
let gas_price = w3.eth().gas_price();
let balance = w3.eth().balance(Address::from([0u8; 20]), None);
let result = w3.transport().submit_batch().await.map_err(|e| format!("batch request err: {}", e))?;
ic_cdk::println!("batch request result: {:?}", result);
let block_number = block_number.await.map_err(|e| format!("get block number err: {}", e))?;
ic_cdk::println!("block number: {:?}", block_number);
let gas_price = gas_price.await.map_err(|e| format!("get gas price err: {}", e))?;
ic_cdk::println!("gas price: {:?}", gas_price);
let balance = balance.await.map_err(|e| format!("get balance err: {}", e))?;
ic_cdk::println!("balance: {:?}", balance);
Ok("done".into())
}
// send tx to eth
#[update(name = "send_eth")]
#[candid_method(update, rename = "send_eth")]
async fn send_eth(to: String, value: u64, nonce: Option<u64>) -> Result<String, String> {
// ecdsa key info
let derivation_path = vec![ic_cdk::id().as_slice().to_vec()];
let key_info = KeyInfo{ derivation_path: derivation_path, key_name: KEY_NAME.to_string(), ecdsa_sign_cycles: None };
// get canister eth address
let from_addr = get_eth_addr(None, None, KEY_NAME.to_string())
.await
.map_err(|e| format!("get canister eth addr failed: {}", e))?;
// get canister the address tx count
let w3 = match ICHttp::new(URL, None) {
Ok(v) => { Web3::new(v) },
Err(e) => { return Err(e.to_string()) },
};
let tx_count: U256 = if let Some(count) = nonce {
count.into()
} else {
let v = w3.eth()
.transaction_count(from_addr, None)
.await
.map_err(|e| format!("get tx count error: {}", e))?;
v
};
ic_cdk::println!("canister eth address {} tx count: {}", hex::encode(from_addr), tx_count);
// construct a transaction
let to = Address::from_str(&to).unwrap();
let tx = TransactionParameters {
to: Some(to),
nonce: Some(tx_count), // remember to fetch nonce first
value: U256::from(value),
gas_price: Some(U256::from(100_000_000_000u64)), // 100 gwei
gas: U256::from(21000),
..Default::default()
};
// sign the transaction and get serialized transaction + signature
let signed_tx = w3.accounts()
.sign_transaction(tx, hex::encode(from_addr), key_info, CHAIN_ID)
.await
.map_err(|e| format!("sign tx error: {}", e))?;
match w3.eth().send_raw_transaction(signed_tx.raw_transaction).await {
Ok(txhash) => {
ic_cdk::println!("txhash: {}", hex::encode(txhash.0));
Ok(format!("{}", hex::encode(txhash.0)))
},
Err(_e) => { Ok(hex::encode(signed_tx.message_hash)) },
}
}
// query a contract, token balance
#[update(name = "token_balance")]
#[candid_method(update, rename = "token_balance")]
async fn token_balance(contract_addr: String, addr: String) -> Result<String, String> {
// goerli weth: 0xb4fbf271143f4fbf7b91a5ded31805e42b2208d6
// account: 0x9c9fcF808B82e5fb476ef8b7A1F5Ad61Dc597625
let w3 = match ICHttp::new(URL, None) {
Ok(v) => { Web3::new(v) },
Err(e) => { return Err(e.to_string()) },
};
let contract_address = Address::from_str(&contract_addr).unwrap();
let contract = Contract::from_json(
w3.eth(),
contract_address,
TOKEN_ABI
).map_err(|e| format!("init contract failed: {}", e))?;
let addr = Address::from_str(&addr).unwrap();
let balance: U256 = contract
.query("balanceOf", (addr,), None, Options::default(), None)
.await
.map_err(|e| format!("query contract error: {}", e))?;
ic_cdk::println!("balance of {} is {}", addr, balance);
Ok(format!("{}", balance))
}
// call a contract, transfer some token to addr
#[update(name = "send_token")]
#[candid_method(update, rename = "send_token")]
async fn send_token(token_addr: String, addr: String, value: u64, nonce: Option<u64>) -> Result<String, String> {
// ecdsa key info
let derivation_path = vec![ic_cdk::id().as_slice().to_vec()];
let key_info = KeyInfo{ derivation_path: derivation_path, key_name: KEY_NAME.to_string(), ecdsa_sign_cycles: None };
// get canister eth address
let from_addr = get_eth_addr(None, None, KEY_NAME.to_string())
.await
.map_err(|e| format!("get canister eth addr failed: {}", e))?;
let w3 = match ICHttp::new(URL, None) {
Ok(v) => { Web3::new(v) },
Err(e) => { return Err(e.to_string()) },
};
let contract_address = Address::from_str(&token_addr).unwrap();
let contract = Contract::from_json(
w3.eth(),
contract_address,
TOKEN_ABI
).map_err(|e| format!("init contract failed: {}", e))?;
let canister_addr = get_eth_addr(None, None, KEY_NAME.to_string())
.await
.map_err(|e| format!("get canister eth addr failed: {}", e))?;
// add nonce to options
let tx_count: U256 = if let Some(count) = nonce {
count.into()
} else {
let v = w3.eth()
.transaction_count(from_addr, None)
.await
.map_err(|e| format!("get tx count error: {}", e))?;
v
};
// get gas_price
let gas_price = w3.eth()
.gas_price()
.await
.map_err(|e| format!("get gas_price error: {}", e))?;
// legacy transaction type is still ok
let options = Options::with(|op| {
op.nonce = Some(tx_count);
op.gas_price = Some(gas_price);
op.transaction_type = Some(U64::from(2)) //EIP1559_TX_ID
});
let to_addr = Address::from_str(&addr).unwrap();
let txhash = contract
.signed_call("transfer", (to_addr, value,), options, hex::encode(canister_addr), key_info, CHAIN_ID)
.await
.map_err(|e| format!("token transfer failed: {}", e))?;
ic_cdk::println!("txhash: {}", hex::encode(txhash));
Ok(format!("{}", hex::encode(txhash)))
}
// call a contract, transfer some token to addr
#[update(name = "rpc_call")]
#[candid_method(update, rename = "rpc_call")]
async fn rpc_call(body: String) -> Result<String, String> {
let w3 = match ICHttp::new(URL, None) {
Ok(v) => { Web3::new(v) },
Err(e) => { return Err(e.to_string()) },
};
let res = w3.json_rpc_call(body.as_ref()).await.map_err(|e| format!("{}", e))?;
ic_cdk::println!("result: {}", res);
Ok(format!("{}", res))
}
#[cfg(not(any(target_arch = "wasm32", test)))]
fn main() {
candid::export_service!();
std::print!("{}", __export_service());
}
#[cfg(any(target_arch = "wasm32", test))]
fn main() {}