ghost-eye/src/network/predefined_txs.rs
Uncle Stretch b922aa75f7
naive implementation of sending multiple transactions
Signed-off-by: Uncle Stretch <uncle.stretch@ghostchain.io>
2025-01-22 15:32:58 +03:00

62 lines
1.8 KiB
Rust

use color_eyre::Result;
use subxt::{
ext::sp_core::{Pair as PairT, sr25519::Pair},
tx::{PairSigner, TxProgress},
OnlineClient,
};
use tokio::sync::mpsc::UnboundedSender;
use crate::{
action::Action,
types::ActionLevel,
casper::{CasperExtrinsicParamsBuilder, CasperConfig},
casper_network,
};
pub async fn transfer_balance(
action_tx: &UnboundedSender<Action>,
api: &OnlineClient<CasperConfig>,
sender: &[u8; 32],
receiver: &[u8; 32],
amount: &u128,
mut maybe_nonce: Option<&mut u32>,
) -> Result<TxProgress<CasperConfig, OnlineClient<CasperConfig>>> {
let receiver_id = subxt::utils::MultiAddress::Id(
subxt::utils::AccountId32::from(*receiver)
);
let transfer_tx = casper_network::tx()
.balances()
.transfer_allow_death(receiver_id, *amount);
let tx_params = match maybe_nonce {
Some(ref mut nonce) => {
**nonce = nonce.saturating_add(1);
CasperExtrinsicParamsBuilder::new()
.nonce(nonce.saturating_sub(1) as u64)
.build()
},
None => CasperExtrinsicParamsBuilder::new().build(),
};
let pair = Pair::from_seed(sender);
let signer = PairSigner::<CasperConfig, Pair>::new(pair);
match api
.tx()
.sign_and_submit_then_watch(&transfer_tx, &signer, tx_params)
.await {
Ok(tx_progress) => {
action_tx.send(Action::WalletLog(
format!("transfer transaction {} sent", tx_progress.extrinsic_hash()),
ActionLevel::Info))?;
Ok(tx_progress)
},
Err(err) => {
action_tx.send(Action::WalletLog(
format!("error during transfer: {err}"), ActionLevel::Error))?;
Err(err.into())
}
}
}