Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(minor-ampd): add command to send tokens from verifier address #586

Merged
merged 2 commits into from
Aug 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions ampd/src/commands/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand All @@ -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)]
Expand Down
41 changes: 41 additions & 0 deletions ampd/src/commands/send_tokens.rs
Original file line number Diff line number Diff line change
@@ -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)]

Check warning on line 13 in ampd/src/commands/send_tokens.rs

View check run for this annotation

Codecov / codecov/patch

ampd/src/commands/send_tokens.rs#L13

Added line #L13 was not covered by tests
pub struct Args {
pub to_address: nonempty::String,
pub amount: u128,
pub denom: nonempty::String,

Check warning on line 17 in ampd/src/commands/send_tokens.rs

View check run for this annotation

Codecov / codecov/patch

ampd/src/commands/send_tokens.rs#L15-L17

Added lines #L15 - L17 were not covered by tests
}

pub async fn run(config: Config, args: Args) -> Result<Option<String>, 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?;

Check warning on line 22 in ampd/src/commands/send_tokens.rs

View check run for this annotation

Codecov / codecov/patch

ampd/src/commands/send_tokens.rs#L20-L22

Added lines #L20 - L22 were not covered by tests

let tx = MsgSend {
to_address: args
.to_address
.parse::<AccountId>()
.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");

Check warning on line 33 in ampd/src/commands/send_tokens.rs

View check run for this annotation

Codecov / codecov/patch

ampd/src/commands/send_tokens.rs#L24-L33

Added lines #L24 - L33 were not covered by tests

let tx_hash = broadcast_tx(config, tx, pub_key).await?.txhash;

Check warning on line 35 in ampd/src/commands/send_tokens.rs

View check run for this annotation

Codecov / codecov/patch

ampd/src/commands/send_tokens.rs#L35

Added line #L35 was not covered by tests

Ok(Some(format!(
"successfully broadcast send transaction, tx hash: {}",
tx_hash
)))
}

Check warning on line 41 in ampd/src/commands/send_tokens.rs

View check run for this annotation

Codecov / codecov/patch

ampd/src/commands/send_tokens.rs#L37-L41

Added lines #L37 - L41 were not covered by tests
3 changes: 2 additions & 1 deletion ampd/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
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;
Expand Down Expand Up @@ -64,6 +64,7 @@
}
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,

Check warning on line 67 in ampd/src/main.rs

View check run for this annotation

Codecov / codecov/patch

ampd/src/main.rs#L67

Added line #L67 was not covered by tests
};

match result {
Expand Down
Loading