From c71c881cf7af4de75263b17119d6d01484df6566 Mon Sep 17 00:00:00 2001 From: CJ Cobb <46455409+cjcobb23@users.noreply.github.com> Date: Mon, 19 Aug 2024 16:42:26 -0600 Subject: [PATCH] feat(minor-ampd): add command to send tokens from verifier address (#586) --- ampd/src/commands/mod.rs | 3 +++ ampd/src/commands/send_tokens.rs | 41 ++++++++++++++++++++++++++++++++ ampd/src/main.rs | 3 ++- 3 files changed, 46 insertions(+), 1 deletion(-) create mode 100644 ampd/src/commands/send_tokens.rs diff --git a/ampd/src/commands/mod.rs b/ampd/src/commands/mod.rs index 5f4d091d2..15d6f80be 100644 --- a/ampd/src/commands/mod.rs +++ b/ampd/src/commands/mod.rs @@ -20,6 +20,7 @@ pub mod daemon; pub mod deregister_chain_support; pub mod register_chain_support; pub mod register_public_key; +pub mod send_tokens; pub mod verifier_address; #[derive(Debug, Subcommand, Valuable)] @@ -36,6 +37,8 @@ pub enum SubCommand { RegisterPublicKey(register_public_key::Args), /// Query the verifier address VerifierAddress, + /// Send tokens from the verifier account to a specified address + SendTokens(send_tokens::Args), } #[derive(Debug, Deserialize, Serialize, PartialEq)] diff --git a/ampd/src/commands/send_tokens.rs b/ampd/src/commands/send_tokens.rs new file mode 100644 index 000000000..c89fa6ddb --- /dev/null +++ b/ampd/src/commands/send_tokens.rs @@ -0,0 +1,41 @@ +use axelar_wasm_std::nonempty; +use cosmrs::bank::MsgSend; +use cosmrs::tx::Msg; +use cosmrs::{AccountId, Coin}; +use error_stack::Result; +use report::ResultCompatExt; +use valuable::Valuable; + +use crate::commands::{broadcast_tx, verifier_pub_key}; +use crate::config::Config; +use crate::{Error, PREFIX}; + +#[derive(clap::Args, Debug, Valuable)] +pub struct Args { + pub to_address: nonempty::String, + pub amount: u128, + pub denom: nonempty::String, +} + +pub async fn run(config: Config, args: Args) -> Result, Error> { + let coin = Coin::new(args.amount, args.denom.as_str()).change_context(Error::InvalidInput)?; + let pub_key = verifier_pub_key(config.tofnd_config.clone()).await?; + + let tx = MsgSend { + to_address: args + .to_address + .parse::() + .change_context(Error::InvalidInput)?, + from_address: pub_key.account_id(PREFIX).change_context(Error::Tofnd)?, + amount: vec![coin], + } + .into_any() + .expect("failed to serialize proto message"); + + let tx_hash = broadcast_tx(config, tx, pub_key).await?.txhash; + + Ok(Some(format!( + "successfully broadcast send transaction, tx hash: {}", + tx_hash + ))) +} diff --git a/ampd/src/main.rs b/ampd/src/main.rs index d5881bfb5..916beed6e 100644 --- a/ampd/src/main.rs +++ b/ampd/src/main.rs @@ -6,7 +6,7 @@ use std::process::ExitCode; use ::config::{Config as cfg, Environment, File, FileFormat, FileSourceFile}; use ampd::commands::{ bond_verifier, daemon, deregister_chain_support, register_chain_support, register_public_key, - verifier_address, SubCommand, + send_tokens, verifier_address, SubCommand, }; use ampd::config::Config; use ampd::Error; @@ -64,6 +64,7 @@ async fn main() -> ExitCode { } Some(SubCommand::RegisterPublicKey(args)) => register_public_key::run(cfg, args).await, Some(SubCommand::VerifierAddress) => verifier_address::run(cfg.tofnd_config).await, + Some(SubCommand::SendTokens(args)) => send_tokens::run(cfg, args).await, }; match result {