Skip to main content

iota_rpc_loadgen/payload/
pay_iota.rs

1// Copyright (c) Mysten Labs, Inc.
2// Modifications Copyright (c) 2024 IOTA Stiftung
3// SPDX-License-Identifier: Apache-2.0
4
5use async_trait::async_trait;
6use futures::future::join_all;
7use iota_sdk_crypto::{ToFromBech32, simple::SimpleKeypair};
8use iota_sdk_types::{Address, Transaction};
9use iota_types::{quorum_driver_types::ExecuteTransactionRequestType, transaction::TransactionAPI};
10use tracing::debug;
11
12use crate::payload::{
13    PayIota, ProcessPayload, RpcCommandProcessor, SignerInfo,
14    rpc_command_processor::DEFAULT_GAS_BUDGET,
15};
16
17#[async_trait]
18impl<'a> ProcessPayload<'a, &'a PayIota> for RpcCommandProcessor {
19    async fn process(
20        &'a self,
21        _op: &'a PayIota,
22        signer_info: &Option<SignerInfo>,
23    ) -> anyhow::Result<()> {
24        let clients = self.get_clients().await?;
25        let SignerInfo {
26            encoded_keypair,
27            gas_budget,
28            gas_payment,
29        } = signer_info.clone().unwrap();
30        let recipient = Address::random();
31        let amount = 1;
32        let gas_budget = gas_budget.unwrap_or(DEFAULT_GAS_BUDGET);
33        let gas_payments = gas_payment.unwrap();
34
35        let keypair =
36            SimpleKeypair::from_bech32(&encoded_keypair).expect("Decoding keypair should not fail");
37
38        debug!(
39            "Transfer IOTA {} time to {recipient} with {amount} NANOS with {gas_payments:?}",
40            gas_payments.len()
41        );
42
43        let sender = keypair.public_key().derive_address();
44        // TODO: For write operations, we usually just want to submit the transaction to
45        // fullnode Let's figure out what's the best way to support other mode
46        // later
47        let client = clients.first().unwrap();
48        let gas_price = client
49            .governance_api()
50            .get_reference_gas_price()
51            .await
52            .expect("unable to fetch gas price");
53        join_all(gas_payments.iter().map(|gas| async {
54            let tx = Transaction::new_transfer_iota(
55                recipient,
56                sender,
57                Some(amount),
58                self.get_object_ref(client, gas).await,
59                gas_budget,
60                gas_price,
61            );
62            self.sign_and_execute(
63                client,
64                &keypair,
65                tx,
66                ExecuteTransactionRequestType::WaitForEffectsCert,
67            )
68            .await
69        }))
70        .await;
71
72        Ok(())
73    }
74}