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
use std::time::{Duration, Instant};

use web3::{
    types::{Block, BlockId, SignedTransaction, Transaction, TransactionReceipt, H160, H256, U256, BlockNumber},
    Error, Web3,
};

use crate::{
    accounts::Account, address::Address, transport::CPCHttp, types::TransactionParameters,
};

#[derive(Debug, Clone)]
pub struct CPCWeb3 {
    pub web3: Web3<CPCHttp>,
}

impl CPCWeb3 {
    pub fn new(url: &str) -> Result<Self, Error> {
        let transport = CPCHttp::new(url)?;
        let web3 = web3::Web3::new(transport);
        Ok(Self { web3 })
    }
    pub async fn block_number(&self) -> Result<u64, Error> {
        let current_block = self.web3.eth().block_number().await?;
        Ok(current_block.as_u64())
    }

    pub async fn block(&self, number: Option<u32>) -> Result<Option<Block<H256>>, Error> {
        let number = match number {
            Some(n) => n.into(),
            None => BlockNumber::Latest,
        };
        self.web3.eth().block(BlockId::Number(number)).await
    }

    pub async fn block_with_txs(&self, number: Option<u32>) -> Result<Option<Block<Transaction>>, Error> {
        let number = match number {
            Some(n) => n.into(),
            None => BlockNumber::Latest,
        };
        self.web3
            .eth()
            .block_with_txs(BlockId::Number(number))
            .await
    }

    pub async fn balance(&self, address: &Address) -> Result<U256, Error> {
        let balance = self.web3.eth().balance(address.h160, None).await?;
        Ok(balance)
    }

    pub async fn sign_transaction(
        &self,
        account: &Account,
        tx: &TransactionParameters,
    ) -> Result<SignedTransaction, Error> {
        let signed = tx.sign(&account.secret_key);
        Ok(signed)
    }

    pub async fn gas_price(&self) -> Result<U256, Error> {
        Ok(self.web3.eth().gas_price().await?)
    }

    pub async fn transaction_count(&self, address: &Address) -> Result<U256, Error> {
        self.web3.eth().transaction_count(address.h160, None).await
    }

    pub async fn submit_signed_raw_tx(&self, signed: &SignedTransaction) -> Result<H256, Error> {
        self.web3
            .eth()
            .send_raw_transaction(signed.raw_transaction.clone())
            .await
    }

    pub async fn wait_tx(
        &self,
        tx_hash: &H256,
    ) -> Result<TransactionReceipt, Box<dyn std::error::Error>> {
        let start = Instant::now();
        let timeout = Duration::from_secs(20);
        loop {
            let receipt = self.web3.eth().transaction_receipt(*tx_hash).await?;
            if receipt.is_some() {
                return Ok(receipt.unwrap());
            }
            if start.elapsed() >= timeout {
                return Err("Waiting for transaction receipt timed out".into());
            }
        }
    }

    pub async fn estimate_gas(&self, req: &TransactionParameters) -> Result<U256, Error> {
        self.web3
            .eth()
            .estimate_gas(req.to_call_request(), None)
            .await
    }

    pub async fn transaction_receipt(
        &self,
        tx_hash: &H256,
    ) -> Result<Option<TransactionReceipt>, Error> {
        self.web3.eth().transaction_receipt(*tx_hash).await
    }

    pub async fn is_contract(&self, addr: H160) -> Result<bool, Error> {
        let code = self.web3.eth().code(addr, None).await?;
        Ok(code.0.len() > 0)
    }
}

#[cfg(test)]
mod tests {
    use crate::types::Bytes;

    use super::*;

    #[tokio::test]
    async fn test_block_number() {
        let web3 = CPCWeb3::new("https://civilian.cpchain.io").unwrap();
        let number = web3.block_number().await.unwrap();
        println!("{:?}", number);
        assert!(number > 0);
    }

    #[tokio::test]
    async fn test_get_block_with_txs() {
        let web3 = CPCWeb3::new("https://civilian.cpchain.io").unwrap();
        let block = web3.block_with_txs(Some(100)).await.unwrap();
        assert!(block.is_some());
        let block = block.unwrap();
        assert!(block.number.unwrap().as_u32() == 100);
        assert!(block.transactions.len() == 0);
        assert!(block.size.unwrap().as_u32() == 1263);
        assert!(block.hash.unwrap().to_string().to_lowercase() == "0x1b91…4aef");
        let block = web3.block_with_txs(Some(10504047)).await.unwrap();
        assert!(block.is_some());
        let block = block.unwrap();
        assert!(block.number.unwrap().as_u32() == 10504047);
        assert!(block.transactions.len() == 24);
        assert!(block.size.unwrap().as_u32() == 8619);
        assert!(block.hash.unwrap().to_string().to_lowercase() == "0x37e1…0e7b");
        // Check transaction
        let tx = &block.transactions[0];
        assert!(tx.hash.to_string() == "0x89cd…6ada");
        assert!(tx.from.unwrap().to_string() == "0x5e17…b6f0");
        assert!(tx.to.unwrap().to_string() == "0x2a18…fcd0");
        assert!(tx.value.to_string() == "0");
        assert!(tx.gas.to_string() == "2300000");
        assert!(tx.gas_price.unwrap().to_string() == "18000000000");
        let tx = &block.transactions.last().unwrap();
        assert!(tx.hash.to_string() == "0xc5b0…5e62");

        // Get unexists block
        let block = web3.block_with_txs(Some(100000000)).await.unwrap();
        assert!(block.is_none());
    }

    #[tokio::test]
    async fn test_get_block() {
        let web3 = CPCWeb3::new("https://civilian.cpchain.io").unwrap();
        let block = web3.block(Some(100)).await.unwrap().unwrap();
        assert!(block.number.unwrap().as_u32() == 100);
    }

    #[tokio::test]
    async fn test_get_latest_block() {
        let web3 = CPCWeb3::new("https://civilian.cpchain.io").unwrap();
        let block = web3.block(None).await.unwrap().unwrap();
        assert!(block.number.unwrap().as_u32() > 100);
    }

    #[tokio::test]
    async fn test_get_balance() {
        let web3 = CPCWeb3::new("http://192.168.0.164:8501").unwrap();
        let balance = web3
            .balance(&Address::from_str("0x7D491C482eBa270700b584888f864177205c5159").unwrap())
            .await
            .unwrap();
        println!("{:?}", balance.as_u128());
    }

    #[tokio::test]
    async fn test_gas_price() {
        let web3 = CPCWeb3::new("https://civilian.cpchain.io").unwrap();
        let gas_price = web3.gas_price().await.unwrap();
        assert_eq!(gas_price, U256::from(18) * U256::exp10(9))
    }

    #[tokio::test]
    async fn test_get_transactions_cnt() {
        let web3 = CPCWeb3::new("https://civilian.cpchain.io").unwrap();
        let cnt = web3
            .transaction_count(
                &Address::from_str("0x1455D180E3adE94ebD9cC324D22a9065d1F5F575").unwrap(),
            )
            .await
            .unwrap();
        assert!(cnt >= U256::from(1065));
        let cnt = web3
            .transaction_count(
                &Address::from_str("0x2455D180E3adE94ebD9cC324D22a9065d1F5F575").unwrap(),
            )
            .await
            .unwrap();
        assert!(cnt == U256::from(0))
    }

    #[tokio::test]
    async fn test_sign_transaction() {
        let web3 = CPCWeb3::new("http://192.168.0.164:8501").unwrap();
        let account = Account::from_phrase(
            "length much pull abstract almost spin hair chest ankle harbor dizzy life",
            None,
        )
        .unwrap();
        println!("{}", account.address);
        let gas_price = web3.gas_price().await.unwrap();
        let nonce = web3.transaction_count(&account.address).await.unwrap();
        let tx_object = TransactionParameters::new(
            41,
            nonce,
            Some(Address::from_str("0x1455D180E3adE94ebD9cC324D22a9065d1F5F575").unwrap().h160),
            300000.into(),
            gas_price,
            U256::exp10(17), //0.1 cpc
            Bytes::default(),
        );

        let estimated_gas = web3.estimate_gas(&tx_object).await.unwrap();
        assert_eq!(estimated_gas, U256::from(21000));

        let signed = web3.sign_transaction(&account, &tx_object).await.unwrap();
        let tx_hash = web3.submit_signed_raw_tx(&signed).await.unwrap();
        println!("{:?} {:?}", tx_hash, signed.transaction_hash);
        assert_eq!(tx_hash, signed.transaction_hash);
        // wait for transaction
        let receipt = web3.wait_tx(&tx_hash).await.unwrap();
        println!("{:?}", receipt);
    }

    #[tokio::test]
    async fn test_is_contract() {
        let web3 = CPCWeb3::new("https://civilian.cpchain.io").unwrap();
        assert_eq!(
            web3.is_contract(
                Address::from_str("0xcf3174cd4dc7c4834d8932f7c3800ab98afc437a")
                    .unwrap()
                    .h160
            )
            .await
            .unwrap(),
            true
        );
        assert_eq!(
            web3.is_contract(
                Address::from_str("0x1455D180E3adE94ebD9cC324D22a9065d1F5F575")
                    .unwrap()
                    .h160
            )
            .await
            .unwrap(),
            false
        );
    }
}