merge_coins/
merge_coins.rs1use std::{str::FromStr, time::Duration};
6
7use iota_config::{IOTA_CLIENT_CONFIG, iota_config_dir};
8use iota_faucet::FaucetError;
9use iota_keys::keystore::AccountKeystore;
10use iota_sdk::wallet_context::WalletContext;
11use iota_sdk_transaction_builder::WaitForTransaction;
12use iota_sdk_types::ObjectId;
13use iota_types::gas_coin::GasCoin;
14use tracing::info;
15
16#[tokio::main]
17async fn main() -> Result<(), anyhow::Error> {
18 let wallet = create_wallet_context(60)?;
19 let active_address = wallet
20 .active_address()
21 .map_err(|err| FaucetError::Wallet(err.to_string()))?;
22 println!("SimpleFaucet::new with active address: {active_address}");
23
24 Ok(())
39}
40
41async fn _split_coins_equally(
42 gas_coin: &str,
43 wallet: WalletContext,
44 count: u64,
45) -> Result<(), anyhow::Error> {
46 let active_address = wallet
47 .active_address()
48 .map_err(|err| FaucetError::Wallet(err.to_string()))?;
49 let client = wallet.get_grpc_client().await?;
50 let coin_object_id = ObjectId::from_str(gas_coin).unwrap();
51
52 let mut builder = client.transaction_builder(active_address);
53 builder.divide_coin(coin_object_id, count);
54 builder.gas_budget(50000000000);
55
56 let signer = wallet.config().keystore().get_key(&active_address)?;
57 let effects = builder
58 .execute(signer.as_keypair()?, WaitForTransaction::Finalized)
59 .await?;
60 println!("{effects:?}");
61 Ok(())
62}
63
64async fn _merge_coins(gas_coin: &str, wallet: WalletContext) -> Result<(), anyhow::Error> {
65 let active_address = wallet
66 .active_address()
67 .map_err(|err| FaucetError::Wallet(err.to_string()))?;
68 let client = wallet.get_grpc_client().await?;
69 let small_coins = wallet
72 .gas_objects(active_address)
73 .await
74 .map_err(|e| FaucetError::Wallet(e.to_string()))?
75 .iter()
76 .map(|q| GasCoin::try_from(&q.1).unwrap())
78 .filter(|coin| coin.0.balance.value() <= 10000000000)
80 .collect::<Vec<GasCoin>>();
81
82 let signer = wallet.config().keystore().get_key(&active_address)?;
83
84 for chunk in small_coins.chunks(254) {
86 let total_balance: u64 = chunk.iter().map(|coin| coin.0.balance.value()).sum();
87
88 let mut coin_vector = chunk
89 .iter()
90 .map(|coin| *coin.id())
91 .collect::<Vec<ObjectId>>();
92
93 coin_vector.insert(0, ObjectId::from_str(gas_coin).unwrap());
95
96 let mut builder = client.transaction_builder(active_address);
99 builder
100 .pay_iota([(active_address, total_balance)])
101 .gas(coin_vector);
102 builder.gas_budget(1000000);
103
104 builder
105 .execute(signer.as_keypair()?, WaitForTransaction::Finalized)
106 .await?;
107 }
108 Ok(())
109}
110
111pub fn create_wallet_context(timeout_secs: u64) -> Result<WalletContext, anyhow::Error> {
112 let wallet_conf = iota_config_dir()?.join(IOTA_CLIENT_CONFIG);
113 info!("Initialize wallet from config path: {wallet_conf:?}");
114 Ok(WalletContext::new(&wallet_conf)?.with_request_timeout(Duration::from_secs(timeout_secs)))
115}