sui_faucet/
local_faucet.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
// Copyright (c) Mysten Labs, Inc.
// SPDX-License-Identifier: Apache-2.0

use std::fmt;
use std::sync::Arc;

use anyhow::bail;
use tokio::sync::Mutex;
use tokio::time::Duration;
use tracing::info;

use crate::FaucetConfig;
use crate::FaucetError;
use sui_sdk::{
    rpc_types::{SuiTransactionBlockResponse, SuiTransactionBlockResponseOptions},
    types::quorum_driver_types::ExecuteTransactionRequestType,
};

use crate::CoinInfo;
use shared_crypto::intent::Intent;
use sui_keys::keystore::AccountKeystore;
use sui_sdk::rpc_types::SuiTransactionBlockEffectsAPI;
use sui_sdk::types::programmable_transaction_builder::ProgrammableTransactionBuilder;
use sui_sdk::types::{
    base_types::{ObjectID, SuiAddress},
    gas_coin::GasCoin,
    transaction::{Transaction, TransactionData},
};
use sui_sdk::wallet_context::WalletContext;

const GAS_BUDGET: u64 = 10_000_000;
const NUM_RETRIES: u8 = 2;

pub struct LocalFaucet {
    wallet: WalletContext,
    active_address: SuiAddress,
    coin_id: Arc<Mutex<ObjectID>>,
    coin_amount: u64,
    num_coins: usize,
}

/// We do not just derive(Debug) because WalletContext and the WriteAheadLog do not implement Debug / are also hard
/// to implement Debug.
impl fmt::Debug for LocalFaucet {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        f.debug_struct("SimpleFaucet")
            .field("faucet_wallet", &self.active_address)
            .field("coin_amount", &self.coin_amount)
            .finish()
    }
}

impl LocalFaucet {
    pub async fn new(
        mut wallet: WalletContext,
        config: FaucetConfig,
    ) -> Result<Arc<Self>, FaucetError> {
        let (coins, active_address) = find_gas_coins_and_address(&mut wallet, &config).await?;
        info!("Starting faucet with address: {:?}", active_address);

        Ok(Arc::new(LocalFaucet {
            wallet,
            active_address,
            coin_id: Arc::new(Mutex::new(*coins[0].id())),
            coin_amount: config.amount,
            num_coins: config.num_coins,
        }))
    }

    /// Make transaction and execute it.
    pub async fn local_request_execute_tx(
        &self,
        recipient: SuiAddress,
    ) -> Result<Vec<CoinInfo>, FaucetError> {
        let gas_price = self
            .wallet
            .get_reference_gas_price()
            .await
            .map_err(|e| FaucetError::internal(format!("Failed to get gas price: {}", e)))?;

        let mut ptb = ProgrammableTransactionBuilder::new();
        let recipients = vec![recipient; self.num_coins];
        let amounts = vec![self.coin_amount; self.num_coins];
        ptb.pay_sui(recipients, amounts)
            .map_err(FaucetError::internal)?;

        let ptb = ptb.finish();

        let coin_id = self.coin_id.lock().await;
        let coin_id_ref = self
            .wallet
            .get_object_ref(*coin_id)
            .await
            .map_err(|e| FaucetError::internal(format!("Failed to get object ref: {}", e)))?;
        let tx_data = TransactionData::new_programmable(
            self.active_address,
            vec![coin_id_ref],
            ptb,
            GAS_BUDGET,
            gas_price,
        );

        let tx = self
            .execute_txn_with_retries(tx_data, *coin_id, NUM_RETRIES)
            .await
            .map_err(FaucetError::internal)?;

        let Some(ref effects) = tx.effects else {
            return Err(FaucetError::internal(
                "Failed to get coin id from response".to_string(),
            ));
        };

        let coins: Vec<CoinInfo> = effects
            .created()
            .iter()
            .map(|o| CoinInfo {
                amount: self.coin_amount,
                id: o.object_id(),
                transfer_tx_digest: *effects.transaction_digest(),
            })
            .collect();

        Ok(coins)
    }

    async fn execute_txn(
        &self,
        tx_data: &TransactionData,
        coin_id: ObjectID,
    ) -> Result<SuiTransactionBlockResponse, anyhow::Error> {
        let signature = self
            .wallet
            .config
            .keystore
            .sign_secure(&self.active_address, &tx_data, Intent::sui_transaction())
            .map_err(FaucetError::internal)?;
        let tx = Transaction::from_data(tx_data.clone(), vec![signature]);

        let client = self.wallet.get_client().await?;

        Ok(client
            .quorum_driver_api()
            .execute_transaction_block(
                tx.clone(),
                SuiTransactionBlockResponseOptions::new().with_effects(),
                Some(ExecuteTransactionRequestType::WaitForLocalExecution),
            )
            .await
            .map_err(|e| {
                FaucetError::internal(format!(
                    "Failed to execute PaySui transaction for coin {:?}, with err {:?}",
                    coin_id, e
                ))
            })?)
    }

    async fn execute_txn_with_retries(
        &self,
        tx: TransactionData,
        coin_id: ObjectID,
        num_retries: u8,
    ) -> Result<SuiTransactionBlockResponse, anyhow::Error> {
        let mut retry_delay = Duration::from_millis(500);
        let mut i = 0;

        loop {
            if i == num_retries {
                bail!("Failed to execute transaction after {num_retries} retries",);
            }
            let res = self.execute_txn(&tx, coin_id).await;

            if res.is_ok() {
                return res;
            }
            i += 1;
            tokio::time::sleep(retry_delay).await;
            retry_delay *= 2;
        }
    }

    pub fn get_coin_amount(&self) -> u64 {
        self.coin_amount
    }
}

/// Finds gas coins with sufficient balance and returns the address to use as the active address
/// for the faucet. If the initial active address in the wallet does not have enough gas coins,
/// it will iterate through the addresses to find one with sufficient gas coins.
async fn find_gas_coins_and_address(
    wallet: &mut WalletContext,
    config: &FaucetConfig,
) -> Result<(Vec<GasCoin>, SuiAddress), FaucetError> {
    let active_address = wallet
        .active_address()
        .map_err(|e| FaucetError::Wallet(e.to_string()))?;

    for address in std::iter::once(active_address).chain(wallet.get_addresses().into_iter()) {
        let coins: Vec<_> = wallet
            .gas_objects(address)
            .await
            .map_err(|e| FaucetError::Wallet(e.to_string()))?
            .iter()
            .filter_map(|(balance, obj)| {
                if *balance >= config.amount {
                    GasCoin::try_from(obj).ok()
                } else {
                    None
                }
            })
            .collect();

        if !coins.is_empty() {
            return Ok((coins, address));
        }
    }

    Err(FaucetError::Wallet(
        "No address found with sufficient coins".to_string(),
    ))
}

#[cfg(test)]
mod tests {

    use super::*;
    use test_cluster::TestClusterBuilder;

    #[tokio::test]
    async fn test_local_faucet_execute_txn() {
        // Setup test cluster
        let cluster = TestClusterBuilder::new().build().await;
        let client = cluster.sui_client().clone();

        let config = FaucetConfig::default();
        let local_faucet = LocalFaucet::new(cluster.wallet, config).await.unwrap();

        // Test execute_txn
        let recipient = SuiAddress::random_for_testing_only();
        let tx = local_faucet.local_request_execute_tx(recipient).await;

        assert!(tx.is_ok());

        let coins = client
            .coin_read_api()
            .get_coins(recipient, None, None, None)
            .await
            .unwrap();

        assert_eq!(coins.data.len(), local_faucet.num_coins);

        let tx = local_faucet.local_request_execute_tx(recipient).await;
        assert!(tx.is_ok());
        let coins = client
            .coin_read_api()
            .get_coins(recipient, None, None, None)
            .await
            .unwrap();

        assert_eq!(coins.data.len(), 2 * local_faucet.num_coins);
    }

    #[tokio::test]
    async fn test_find_gas_coins_and_address() {
        let mut cluster = TestClusterBuilder::new().build().await;
        let wallet = cluster.wallet_mut();
        let config = FaucetConfig::default();

        // Test find_gas_coins_and_address
        let result = find_gas_coins_and_address(wallet, &config).await;
        assert!(result.is_ok());

        let (coins, _) = result.unwrap();
        assert!(!coins.is_empty());
        assert!(coins.iter().map(|c| c.value()).sum::<u64>() >= config.amount);
    }
}