cleanup in transaction section

Signed-off-by: Uncle Stretch <uncle.stretch@ghostchain.io>
This commit is contained in:
Uncle Stretch 2025-02-07 17:23:44 +03:00
parent 0777e6ebf0
commit adf1d94cbe
Signed by: str3tch
GPG Key ID: 84F3190747EE79AA
2 changed files with 111 additions and 287 deletions

View File

@ -2,7 +2,7 @@
name = "ghost-eye" name = "ghost-eye"
authors = ["str3tch <stretch@ghostchain.io>"] authors = ["str3tch <stretch@ghostchain.io>"]
description = "Application for interacting with Casper/Ghost nodes that are exposing RPC only to the localhost" description = "Application for interacting with Casper/Ghost nodes that are exposing RPC only to the localhost"
version = "0.3.19" version = "0.3.20"
edition = "2021" edition = "2021"
[dependencies] [dependencies]

View File

@ -19,48 +19,19 @@ pub async fn transfer_balance(
sender: &[u8; 32], sender: &[u8; 32],
receiver: &[u8; 32], receiver: &[u8; 32],
amount: &u128, amount: &u128,
mut maybe_nonce: Option<&mut u32>, maybe_nonce: Option<&mut u32>,
) -> Result<TxProgress<CasperConfig, OnlineClient<CasperConfig>>> { ) -> Result<TxProgress<CasperConfig, OnlineClient<CasperConfig>>> {
let receiver_id = subxt::utils::MultiAddress::Id( let receiver_id = subxt::utils::MultiAddress::Id(subxt::utils::AccountId32::from(*receiver));
subxt::utils::AccountId32::from(*receiver) let transfer_tx = casper_network::tx().balances().transfer_allow_death(receiver_id, *amount);
); inner_sign_and_submit_then_watch(
action_tx,
let transfer_tx = casper_network::tx() api,
.balances() sender,
.transfer_allow_death(receiver_id, *amount); maybe_nonce,
Box::new(transfer_tx),
let tx_params = match maybe_nonce { "transfer",
Some(ref mut nonce) => { ActionTarget::WalletLog,
**nonce = nonce.saturating_add(1); ).await
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::EventLog(
format!("transfer transaction {} sent", tx_progress.extrinsic_hash()),
ActionLevel::Info,
ActionTarget::WalletLog))?;
Ok(tx_progress)
},
Err(err) => {
action_tx.send(Action::EventLog(
format!("error during transfer: {err}"),
ActionLevel::Error,
ActionTarget::WalletLog))?;
Err(err.into())
}
}
} }
pub async fn bond_extra( pub async fn bond_extra(
@ -68,44 +39,18 @@ pub async fn bond_extra(
api: &OnlineClient<CasperConfig>, api: &OnlineClient<CasperConfig>,
sender: &[u8; 32], sender: &[u8; 32],
amount: &u128, amount: &u128,
mut maybe_nonce: Option<&mut u32>, maybe_nonce: Option<&mut u32>,
) -> Result<TxProgress<CasperConfig, OnlineClient<CasperConfig>>> { ) -> Result<TxProgress<CasperConfig, OnlineClient<CasperConfig>>> {
let transfer_tx = casper_network::tx() let bond_extra_tx = casper_network::tx().staking().bond_extra(*amount);
.staking() inner_sign_and_submit_then_watch(
.bond_extra(*amount); action_tx,
api,
let tx_params = match maybe_nonce { sender,
Some(ref mut nonce) => { maybe_nonce,
**nonce = nonce.saturating_add(1); Box::new(bond_extra_tx),
CasperExtrinsicParamsBuilder::new() "bond extra",
.nonce(nonce.saturating_sub(1) as u64) ActionTarget::ValidatorLog,
.build() ).await
},
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::EventLog(
format!("bond extra transaction {} sent", tx_progress.extrinsic_hash()),
ActionLevel::Info,
ActionTarget::ValidatorLog))?;
Ok(tx_progress)
},
Err(err) => {
action_tx.send(Action::EventLog(
format!("error during bond extra: {err}"),
ActionLevel::Error,
ActionTarget::ValidatorLog))?;
Err(err.into())
}
}
} }
pub async fn bond( pub async fn bond(
@ -113,48 +58,20 @@ pub async fn bond(
api: &OnlineClient<CasperConfig>, api: &OnlineClient<CasperConfig>,
sender: &[u8; 32], sender: &[u8; 32],
amount: &u128, amount: &u128,
mut maybe_nonce: Option<&mut u32>, maybe_nonce: Option<&mut u32>,
) -> Result<TxProgress<CasperConfig, OnlineClient<CasperConfig>>> { ) -> Result<TxProgress<CasperConfig, OnlineClient<CasperConfig>>> {
// auto-stake everything by now // auto-stake everything by now
let reward_destination = let reward_destination = casper_network::runtime_types::pallet_staking::RewardDestination::Staked;
casper_network::runtime_types::pallet_staking::RewardDestination::Staked; let bond_tx = casper_network::tx().staking().bond(*amount, reward_destination);
inner_sign_and_submit_then_watch(
let transfer_tx = casper_network::tx() action_tx,
.staking() api,
.bond(*amount, reward_destination); sender,
maybe_nonce,
let tx_params = match maybe_nonce { Box::new(bond_tx),
Some(ref mut nonce) => { "bond",
**nonce = nonce.saturating_add(1); ActionTarget::ValidatorLog,
CasperExtrinsicParamsBuilder::new() ).await
.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::EventLog(
format!("bond transaction {} sent", tx_progress.extrinsic_hash()),
ActionLevel::Info,
ActionTarget::ValidatorLog))?;
Ok(tx_progress)
},
Err(err) => {
action_tx.send(Action::EventLog(
format!("error during bond: {err}"),
ActionLevel::Error,
ActionTarget::ValidatorLog))?;
Err(err.into())
}
}
} }
pub async fn payout_stakers( pub async fn payout_stakers(
@ -163,45 +80,19 @@ pub async fn payout_stakers(
sender: &[u8; 32], sender: &[u8; 32],
stash: &[u8; 32], stash: &[u8; 32],
era_index: u32, era_index: u32,
mut maybe_nonce: Option<&mut u32>, maybe_nonce: Option<&mut u32>,
) -> Result<TxProgress<CasperConfig, OnlineClient<CasperConfig>>> { ) -> Result<TxProgress<CasperConfig, OnlineClient<CasperConfig>>> {
let stash_id = subxt::utils::AccountId32::from(*stash); let stash_id = subxt::utils::AccountId32::from(*stash);
let transfer_tx = casper_network::tx() let payout_stakers_tx = casper_network::tx().staking().payout_stakers(stash_id, era_index);
.staking() inner_sign_and_submit_then_watch(
.payout_stakers(stash_id, era_index); action_tx,
api,
let tx_params = match maybe_nonce { sender,
Some(ref mut nonce) => { maybe_nonce,
**nonce = nonce.saturating_add(1); Box::new(payout_stakers_tx),
CasperExtrinsicParamsBuilder::new() "payout stakers",
.nonce(nonce.saturating_sub(1) as u64) ActionTarget::ValidatorLog,
.build() ).await
},
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::EventLog(
format!("payout stakers {} sent", tx_progress.extrinsic_hash()),
ActionLevel::Info,
ActionTarget::ValidatorLog))?;
Ok(tx_progress)
},
Err(err) => {
action_tx.send(Action::EventLog(
format!("error during payout stakers: {err}"),
ActionLevel::Error,
ActionTarget::ValidatorLog))?;
Err(err.into())
}
}
} }
pub async fn set_keys( pub async fn set_keys(
@ -209,7 +100,7 @@ pub async fn set_keys(
api: &OnlineClient<CasperConfig>, api: &OnlineClient<CasperConfig>,
sender: &[u8; 32], sender: &[u8; 32],
hashed_keys_str: &String, hashed_keys_str: &String,
mut maybe_nonce: Option<&mut u32>, maybe_nonce: Option<&mut u32>,
) -> Result<TxProgress<CasperConfig, OnlineClient<CasperConfig>>> { ) -> Result<TxProgress<CasperConfig, OnlineClient<CasperConfig>>> {
let (gran_key, babe_key, audi_key, slow_key) = { let (gran_key, babe_key, audi_key, slow_key) = {
let s = hashed_keys_str.trim_start_matches("0x"); let s = hashed_keys_str.trim_start_matches("0x");
@ -220,53 +111,24 @@ pub async fn set_keys(
hex::decode(&s[192..256]).unwrap().as_slice().try_into().unwrap(), hex::decode(&s[192..256]).unwrap().as_slice().try_into().unwrap(),
) )
}; };
let session_keys = runtime_types::casper_runtime::opaque::SessionKeys { let session_keys = runtime_types::casper_runtime::opaque::SessionKeys {
grandpa: runtime_types::sp_consensus_grandpa::app::Public(gran_key), grandpa: runtime_types::sp_consensus_grandpa::app::Public(gran_key),
babe: runtime_types::sp_consensus_babe::app::Public(babe_key), babe: runtime_types::sp_consensus_babe::app::Public(babe_key),
authority_discovery: runtime_types::sp_authority_discovery::app::Public(audi_key), authority_discovery: runtime_types::sp_authority_discovery::app::Public(audi_key),
slow_clap: runtime_types::ghost_slow_clap::sr25519::app_sr25519::Public(slow_key), slow_clap: runtime_types::ghost_slow_clap::sr25519::app_sr25519::Public(slow_key),
}; };
// it seems like there is no check for the second paramter, that's why // it seems like there is no check for the second paramter, that's why
// we it can be anything. // we it can be anything. For example empty vector.
// Not sure TBH let set_keys_tx = casper_network::tx().session().set_keys(session_keys, Vec::new());
let transfer_tx = casper_network::tx() inner_sign_and_submit_then_watch(
.session() action_tx,
.set_keys(session_keys, Vec::new()); api,
sender,
let tx_params = match maybe_nonce { maybe_nonce,
Some(ref mut nonce) => { Box::new(set_keys_tx),
**nonce = nonce.saturating_add(1); "set keys",
CasperExtrinsicParamsBuilder::new() ActionTarget::ValidatorLog,
.nonce(nonce.saturating_sub(1) as u64) ).await
.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::EventLog(
format!("set keys {} sent", tx_progress.extrinsic_hash()),
ActionLevel::Info,
ActionTarget::ValidatorLog))?;
Ok(tx_progress)
},
Err(err) => {
action_tx.send(Action::EventLog(
format!("error during set keys: {err}"),
ActionLevel::Error,
ActionTarget::ValidatorLog))?;
Err(err.into())
}
}
} }
pub async fn validate( pub async fn validate(
@ -274,93 +136,40 @@ pub async fn validate(
api: &OnlineClient<CasperConfig>, api: &OnlineClient<CasperConfig>,
sender: &[u8; 32], sender: &[u8; 32],
percent: u32, percent: u32,
mut maybe_nonce: Option<&mut u32>, maybe_nonce: Option<&mut u32>,
) -> Result<TxProgress<CasperConfig, OnlineClient<CasperConfig>>> { ) -> Result<TxProgress<CasperConfig, OnlineClient<CasperConfig>>> {
let validator_prefs = casper_network::runtime_types::pallet_staking::ValidatorPrefs { let validator_prefs = casper_network::runtime_types::pallet_staking::ValidatorPrefs {
commission: runtime_types::sp_arithmetic::per_things::Perbill(percent), commission: runtime_types::sp_arithmetic::per_things::Perbill(percent),
blocked: percent >= 1_000_000_000u32, blocked: percent >= 1_000_000_000u32,
}; };
let validate_tx = casper_network::tx().staking().validate(validator_prefs);
let transfer_tx = casper_network::tx() inner_sign_and_submit_then_watch(
.staking() action_tx,
.validate(validator_prefs); api,
sender,
let tx_params = match maybe_nonce { maybe_nonce,
Some(ref mut nonce) => { Box::new(validate_tx),
**nonce = nonce.saturating_add(1); "validate",
CasperExtrinsicParamsBuilder::new() ActionTarget::ValidatorLog,
.nonce(nonce.saturating_sub(1) as u64) ).await
.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::EventLog(
format!("set keys {} sent", tx_progress.extrinsic_hash()),
ActionLevel::Info,
ActionTarget::ValidatorLog))?;
Ok(tx_progress)
},
Err(err) => {
action_tx.send(Action::EventLog(
format!("error during set keys: {err}"),
ActionLevel::Error,
ActionTarget::ValidatorLog))?;
Err(err.into())
}
}
} }
pub async fn chill( pub async fn chill(
action_tx: &UnboundedSender<Action>, action_tx: &UnboundedSender<Action>,
api: &OnlineClient<CasperConfig>, api: &OnlineClient<CasperConfig>,
sender: &[u8; 32], sender: &[u8; 32],
mut maybe_nonce: Option<&mut u32>, maybe_nonce: Option<&mut u32>,
) -> Result<TxProgress<CasperConfig, OnlineClient<CasperConfig>>> { ) -> Result<TxProgress<CasperConfig, OnlineClient<CasperConfig>>> {
let transfer_tx = casper_network::tx() let chill_tx = casper_network::tx().staking().chill();
.staking() inner_sign_and_submit_then_watch(
.chill(); action_tx,
api,
let tx_params = match maybe_nonce { sender,
Some(ref mut nonce) => { maybe_nonce,
**nonce = nonce.saturating_add(1); Box::new(chill_tx),
CasperExtrinsicParamsBuilder::new() "chill",
.nonce(nonce.saturating_sub(1) as u64) ActionTarget::ValidatorLog,
.build() ).await
},
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::EventLog(
format!("chill {} sent", tx_progress.extrinsic_hash()),
ActionLevel::Info,
ActionTarget::ValidatorLog))?;
Ok(tx_progress)
},
Err(err) => {
action_tx.send(Action::EventLog(
format!("error during chill: {err}"),
ActionLevel::Error,
ActionTarget::ValidatorLog))?;
Err(err.into())
}
}
} }
pub async fn unbond( pub async fn unbond(
@ -368,12 +177,30 @@ pub async fn unbond(
api: &OnlineClient<CasperConfig>, api: &OnlineClient<CasperConfig>,
sender: &[u8; 32], sender: &[u8; 32],
amount: &u128, amount: &u128,
mut maybe_nonce: Option<&mut u32>, maybe_nonce: Option<&mut u32>,
) -> Result<TxProgress<CasperConfig, OnlineClient<CasperConfig>>> { ) -> Result<TxProgress<CasperConfig, OnlineClient<CasperConfig>>> {
let transfer_tx = casper_network::tx() let unbond_tx = casper_network::tx().staking().unbond(*amount);
.staking() inner_sign_and_submit_then_watch(
.unbond(*amount); action_tx,
api,
sender,
maybe_nonce,
Box::new(unbond_tx),
"unbond",
ActionTarget::ValidatorLog,
).await
}
async fn inner_sign_and_submit_then_watch(
action_tx: &UnboundedSender<Action>,
api: &OnlineClient<CasperConfig>,
sender: &[u8; 32],
mut maybe_nonce: Option<&mut u32>,
tx_call: Box<dyn subxt::tx::Payload>,
tx_name: &str,
target: ActionTarget,
) -> Result<TxProgress<CasperConfig, OnlineClient<CasperConfig>>> {
let signer = PairSigner::<CasperConfig, Pair>::new(Pair::from_seed(sender));
let tx_params = match maybe_nonce { let tx_params = match maybe_nonce {
Some(ref mut nonce) => { Some(ref mut nonce) => {
**nonce = nonce.saturating_add(1); **nonce = nonce.saturating_add(1);
@ -384,25 +211,22 @@ pub async fn unbond(
None => CasperExtrinsicParamsBuilder::new().build(), None => CasperExtrinsicParamsBuilder::new().build(),
}; };
let pair = Pair::from_seed(sender);
let signer = PairSigner::<CasperConfig, Pair>::new(pair);
match api match api
.tx() .tx()
.sign_and_submit_then_watch(&transfer_tx, &signer, tx_params) .sign_and_submit_then_watch(&tx_call, &signer, tx_params)
.await { .await {
Ok(tx_progress) => { Ok(tx_progress) => {
action_tx.send(Action::EventLog( action_tx.send(Action::EventLog(
format!("unbond {} sent", tx_progress.extrinsic_hash()), format!("{tx_name} transaction {} sent", tx_progress.extrinsic_hash()),
ActionLevel::Info, ActionLevel::Info,
ActionTarget::ValidatorLog))?; target))?;
Ok(tx_progress) Ok(tx_progress)
}, },
Err(err) => { Err(err) => {
action_tx.send(Action::EventLog( action_tx.send(Action::EventLog(
format!("error during unbond: {err}"), format!("error during {tx_name} transaction: {err}"),
ActionLevel::Error, ActionLevel::Error,
ActionTarget::ValidatorLog))?; target))?;
Err(err.into()) Err(err.into())
} }
} }