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
use crate::amm::WETH_CONTRACT_ADDRESS;
use crate::{client::Web3, jsonrpc::error::Web3Error};
use clarity::abi::Token;
use clarity::Address;
use clarity::{abi::encode_call, PrivateKey, Uint256};
use std::time::Duration;
use tokio::time::timeout as future_timeout;
impl Web3 {
pub async fn wrap_eth(
&self,
amount: Uint256,
secret: PrivateKey,
weth_address: Option<Address>,
wait_timeout: Option<Duration>,
) -> Result<Uint256, Web3Error> {
let own_address = secret.to_public_key().unwrap();
let sig = "deposit()";
let tokens = [];
let payload = encode_call(sig, &tokens).unwrap();
let weth_address = weth_address.unwrap_or(*WETH_CONTRACT_ADDRESS);
let txid = self
.send_transaction(weth_address, payload, amount, own_address, secret, vec![])
.await?;
if let Some(timeout) = wait_timeout {
future_timeout(
timeout,
self.wait_for_transaction(txid.clone(), timeout, None),
)
.await??;
}
Ok(txid)
}
pub async fn unwrap_eth(
&self,
amount: Uint256,
secret: PrivateKey,
weth_address: Option<Address>,
wait_timeout: Option<Duration>,
) -> Result<Uint256, Web3Error> {
let own_address = secret.to_public_key().unwrap();
let sig = "withdraw(uint256)";
let tokens = [Token::Uint(amount)];
let payload = encode_call(sig, &tokens).unwrap();
let weth_address = weth_address.unwrap_or(*WETH_CONTRACT_ADDRESS);
let txid = self
.send_transaction(
weth_address,
payload,
0u16.into(),
own_address,
secret,
vec![],
)
.await?;
if let Some(timeout) = wait_timeout {
future_timeout(
timeout,
self.wait_for_transaction(txid.clone(), timeout, None),
)
.await??;
}
Ok(txid)
}
}