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
use crate::amm::WETH_CONTRACT_ADDRESS;
use crate::{client::Web3, jsonrpc::error::Web3Error};
use clarity::abi::AbiToken;
use clarity::Address;
use clarity::{abi::encode_call, PrivateKey, Uint256};
use std::time::Duration;
use tokio::time::timeout as future_timeout;

// Performs wrapping and unwrapping of eth, along with balance checking
impl Web3 {
    pub async fn wrap_eth(
        &self,
        amount: Uint256,
        secret: PrivateKey,
        weth_address: Option<Address>,
        wait_timeout: Option<Duration>,
    ) -> Result<Uint256, Web3Error> {
        let sig = "deposit()";
        let tokens = [];
        let payload = encode_call(sig, &tokens).unwrap();
        let weth_address = weth_address.unwrap_or(*WETH_CONTRACT_ADDRESS);
        let tx = self
            .prepare_transaction(weth_address, payload, amount, secret, vec![])
            .await?;
        let txid = self.eth_send_raw_transaction(tx.to_bytes()).await?;

        if let Some(timeout) = wait_timeout {
            future_timeout(timeout, self.wait_for_transaction(txid, 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 sig = "withdraw(uint256)";
        let tokens = [AbiToken::Uint(amount)];
        let payload = encode_call(sig, &tokens).unwrap();
        let weth_address = weth_address.unwrap_or(*WETH_CONTRACT_ADDRESS);
        let tx = self
            .prepare_transaction(weth_address, payload, 0u16.into(), secret, vec![])
            .await?;
        let txid = self.eth_send_raw_transaction(tx.to_bytes()).await?;

        if let Some(timeout) = wait_timeout {
            future_timeout(timeout, self.wait_for_transaction(txid, timeout, None)).await??;
        }
        Ok(txid)
    }
}