Skip to content
This repository has been archived by the owner on Jan 11, 2024. It is now read-only.

IPC-299: Implement cli cross-msg methods #311

Merged
merged 12 commits into from
Sep 14, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
52 changes: 35 additions & 17 deletions ipc/cli/src/commands/crossmsg/fund.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@

use async_trait::async_trait;
use clap::Args;
use std::fmt::Debug;
use fvm_shared::address::Address;
use ipc_sdk::subnet_id::SubnetID;
use std::{fmt::Debug, str::FromStr};

use crate::commands::get_ipc_agent_url;
use crate::sdk::IpcAgentClient;
use crate::{CommandLineHandler, GlobalArguments};
use crate::{
f64_to_token_amount, get_ipc_provider, require_fil_addr_from_str, CommandLineHandler,
GlobalArguments,
};

/// The command to send funds to a subnet from parent
pub(crate) struct Fund;
Expand All @@ -20,18 +23,33 @@ impl CommandLineHandler for Fund {
async fn handle(global: &GlobalArguments, arguments: &Self::Arguments) -> anyhow::Result<()> {
log::debug!("fund operation with args: {:?}", arguments);

let url = get_ipc_agent_url(&arguments.ipc_agent_url, global)?;
let client = IpcAgentClient::default_from_url(url);
let epoch = client
.fund(
&arguments.subnet,
arguments.from.clone(),
arguments.to.clone(),
arguments.amount,
)
.await?;
let mut provider = get_ipc_provider(global)?;
let subnet = SubnetID::from_str(&arguments.subnet)?;
let from = match &arguments.from {
Some(address) => Some(Address::from_str(address)?),
None => None,
};
let to = match &arguments.to {
Some(address) => Some(require_fil_addr_from_str(address)?),
None => None,
};
let gateway_addr = match &arguments.gateway_address {
Some(address) => Some(Address::from_str(address)?),
None => None,
};

log::info!("funded subnet: {:} at epoch: {epoch:}", arguments.subnet);
println!(
"fund performed in epoch: {:?}",
provider
.fund(
subnet,
gateway_addr,
from,
to,
f64_to_token_amount(arguments.amount)?,
)
.await?,
);

Ok(())
}
Expand All @@ -40,8 +58,8 @@ impl CommandLineHandler for Fund {
#[derive(Debug, Args)]
#[command(about = "Send funds from a parent to a child subnet")]
pub(crate) struct FundArgs {
#[arg(long, short, help = "The JSON RPC server url for ipc agent")]
pub ipc_agent_url: Option<String>,
#[arg(long, short, help = "The gateway address of the subnet")]
pub gateway_address: Option<String>,
#[arg(long, short, help = "The address to send funds from")]
pub from: Option<String>,
#[arg(
Expand Down
27 changes: 2 additions & 25 deletions ipc/cli/src/commands/crossmsg/propagate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,10 @@
//! Propagate cli command handler.

use async_trait::async_trait;
use base64::Engine;
use clap::Args;
use std::fmt::Debug;

use crate::commands::get_ipc_agent_url;
use crate::config::json_rpc_methods;
use crate::server::propagate::PropagateParams;
use crate::{CommandLineHandler, GlobalArguments};
use ipc_provider::jsonrpc::{JsonRpcClient, JsonRpcClientImpl};

/// The command to propagate a message in the postbox.
pub(crate) struct Propagate;
Expand All @@ -20,26 +15,8 @@ pub(crate) struct Propagate;
impl CommandLineHandler for Propagate {
type Arguments = PropagateArgs;

async fn handle(global: &GlobalArguments, arguments: &Self::Arguments) -> anyhow::Result<()> {
log::debug!("propagate operation with args: {:?}", arguments);

let url = get_ipc_agent_url(&arguments.ipc_agent_url, global)?;
let json_rpc_client = JsonRpcClientImpl::new(url, None);

let postbox_msg_key =
base64::engine::general_purpose::STANDARD.decode(&arguments.postbox_msg_key)?;
let params = PropagateParams {
subnet: arguments.subnet.clone(),
from: arguments.from.clone(),
postbox_msg_key,
};
json_rpc_client
.request::<()>(json_rpc_methods::PROPAGATE, serde_json::to_value(params)?)
.await?;

log::info!("propagated subnet: {:}", arguments.subnet);

Ok(())
async fn handle(_global: &GlobalArguments, _arguments: &Self::Arguments) -> anyhow::Result<()> {
todo!()
}
}

Expand Down
52 changes: 35 additions & 17 deletions ipc/cli/src/commands/crossmsg/release.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@

use async_trait::async_trait;
use clap::Args;
use std::fmt::Debug;
use fvm_shared::address::Address;
use ipc_sdk::subnet_id::SubnetID;
use std::{fmt::Debug, str::FromStr};

use crate::commands::get_ipc_agent_url;
use crate::sdk::IpcAgentClient;
use crate::{CommandLineHandler, GlobalArguments};
use crate::{
f64_to_token_amount, get_ipc_provider, require_fil_addr_from_str, CommandLineHandler,
GlobalArguments,
};

/// The command to release funds from a child to a parent
pub(crate) struct Release;
Expand All @@ -20,18 +23,33 @@ impl CommandLineHandler for Release {
async fn handle(global: &GlobalArguments, arguments: &Self::Arguments) -> anyhow::Result<()> {
log::debug!("release operation with args: {:?}", arguments);

let url = get_ipc_agent_url(&arguments.ipc_agent_url, global)?;
let client = IpcAgentClient::default_from_url(url);
let epoch = client
.release(
&arguments.subnet,
arguments.from.clone(),
arguments.to.clone(),
arguments.amount,
)
.await?;
let mut provider = get_ipc_provider(global)?;
let subnet = SubnetID::from_str(&arguments.subnet)?;
let from = match &arguments.from {
Some(address) => Some(Address::from_str(address)?),
None => None,
};
let to = match &arguments.to {
Some(address) => Some(require_fil_addr_from_str(address)?),
None => None,
};
let gateway_addr = match &arguments.gateway_address {
Some(address) => Some(require_fil_addr_from_str(address)?),
None => None,
};

log::info!("released subnet: {:} at epoch {epoch:}", arguments.subnet);
println!(
"release performed in epoch: {:?}",
provider
.release(
subnet,
gateway_addr,
from,
to,
f64_to_token_amount(arguments.amount)?,
)
.await?,
);

Ok(())
}
Expand All @@ -40,8 +58,8 @@ impl CommandLineHandler for Release {
#[derive(Debug, Args)]
#[command(about = "Release operation in the gateway actor")]
pub(crate) struct ReleaseArgs {
#[arg(long, short, help = "The JSON RPC server url for ipc agent")]
pub ipc_agent_url: Option<String>,
#[arg(long, short, help = "The gateway address of the subnet")]
pub gateway_address: Option<String>,
#[arg(long, short, help = "The address that releases funds")]
pub from: Option<String>,
#[arg(
Expand Down
30 changes: 19 additions & 11 deletions ipc/cli/src/commands/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,27 @@

// mod checkpoint;
mod config;
// mod crossmsg;
mod crossmsg;
// mod daemon;
mod subnet;
mod util;
mod wallet;

// use crate::commands::checkpoint::CheckpointCommandsArgs;
// use crate::commands::crossmsg::CrossMsgsCommandsArgs;
use crate::commands::crossmsg::CrossMsgsCommandsArgs;
// use crate::commands::daemon::{LaunchDaemon, LaunchDaemonArgs};
use crate::commands::util::UtilCommandsArgs;
// use crate::server::{new_evm_keystore_from_path, new_keystore_from_path};
use crate::GlobalArguments;
use anyhow::{Context, Result};

use clap::{Command, CommandFactory, Parser, Subcommand};
use clap_complete::{generate, Generator, Shell};
use fvm_shared::econ::TokenAmount;
use ipc_provider::manager::evm::ethers_address_to_fil_address;

use std::fmt::Debug;
use std::io;
use std::str::FromStr;

use crate::commands::config::ConfigCommandsArgs;
use crate::commands::wallet::WalletCommandsArgs;
Expand All @@ -42,7 +43,7 @@ enum Commands {
Config(ConfigCommandsArgs),
Subnet(SubnetCommandsArgs),
Wallet(WalletCommandsArgs),
// CrossMsg(CrossMsgsCommandsArgs),
CrossMsg(CrossMsgsCommandsArgs),
// Checkpoint(CheckpointCommandsArgs),
Util(UtilCommandsArgs),
}
Expand Down Expand Up @@ -107,7 +108,7 @@ pub async fn cli() -> anyhow::Result<()> {
// Commands::Daemon(args) => LaunchDaemon::handle(global, args).await,
Commands::Config(args) => args.handle(global).await,
Commands::Subnet(args) => args.handle(global).await,
// Commands::CrossMsg(args) => args.handle(global).await,
Commands::CrossMsg(args) => args.handle(global).await,
Commands::Wallet(args) => args.handle(global).await,
// Commands::Checkpoint(args) => args.handle(global).await,
Commands::Util(args) => args.handle(global).await,
Expand All @@ -134,12 +135,19 @@ pub(crate) fn f64_to_token_amount(f: f64) -> anyhow::Result<TokenAmount> {
Ok(TokenAmount::from_nano(nano as u128))
}

// pub(crate) fn get_evm_keystore(path: &Option<String>) -> Result<PersistentKeyStore<EthKeyAddress>> {
// match path {
// Some(p) => new_evm_keystore_from_path(p),
// None => new_evm_keystore_from_path(&default_repo_path()),
// }
// }
/// Receives a f/eth-address as an input and returns the corresponding
/// filecoin or delegated address, respectively
pub(crate) fn require_fil_addr_from_str(s: &str) -> anyhow::Result<fvm_shared::address::Address> {
let addr = match fvm_shared::address::Address::from_str(s) {
Err(_) => {
// see if it is an eth address
let addr = ethers::types::Address::from_str(s)?;
ethers_address_to_fil_address(&addr)?
}
Ok(addr) => addr,
};
Ok(addr)
}

#[cfg(test)]
mod tests {
Expand Down
6 changes: 3 additions & 3 deletions ipc/cli/src/commands/subnet/list_subnets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@

use async_trait::async_trait;
use clap::Args;
use fvm_shared::{address::Address, econ::TokenAmount};
use fvm_shared::econ::TokenAmount;
use ipc_sdk::subnet_id::SubnetID;
use std::fmt::Debug;
use std::str::FromStr;

use crate::{get_ipc_provider, CommandLineHandler, GlobalArguments};
use crate::{get_ipc_provider, require_fil_addr_from_str, CommandLineHandler, GlobalArguments};

/// The command to create a new subnet actor.
pub(crate) struct ListSubnets;
Expand All @@ -25,7 +25,7 @@ impl CommandLineHandler for ListSubnets {
let subnet = SubnetID::from_str(&arguments.subnet)?;

let gateway_addr = match &arguments.gateway_address {
Some(address) => Some(Address::from_str(address)?),
Some(address) => Some(require_fil_addr_from_str(address)?),
None => None,
};

Expand Down
5 changes: 2 additions & 3 deletions ipc/cli/src/commands/subnet/list_validators.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,10 @@

use async_trait::async_trait;
use clap::Args;
use fvm_shared::address::Address;
use ipc_sdk::subnet_id::SubnetID;
use std::{fmt::Debug, str::FromStr};

use crate::{get_ipc_provider, CommandLineHandler, GlobalArguments};
use crate::{get_ipc_provider, require_fil_addr_from_str, CommandLineHandler, GlobalArguments};

/// The command to create a new subnet actor.
pub(crate) struct ListValidators;
Expand All @@ -24,7 +23,7 @@ impl CommandLineHandler for ListValidators {
let subnet = SubnetID::from_str(&arguments.subnet)?;

let gateway_addr = match &arguments.gateway_address {
Some(address) => Some(Address::from_str(address)?),
Some(address) => Some(require_fil_addr_from_str(address)?),
None => None,
};

Expand Down
25 changes: 10 additions & 15 deletions ipc/cli/src/commands/subnet/send_value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@
use async_trait::async_trait;
use clap::Args;
use fvm_shared::address::Address;
use ipc_provider::manager::evm::ethers_address_to_fil_address;
use ipc_sdk::subnet_id::SubnetID;
use std::{fmt::Debug, str::FromStr};

use crate::{f64_to_token_amount, get_ipc_provider, CommandLineHandler, GlobalArguments};
use crate::{
f64_to_token_amount, get_ipc_provider, require_fil_addr_from_str, CommandLineHandler,
GlobalArguments,
};

pub(crate) struct SendValue;

Expand All @@ -27,20 +29,13 @@ impl CommandLineHandler for SendValue {
None => None,
};

// try to get the `to` as an FVM address and an Eth
// address. We should include a wrapper type to make
// this easier through the whole code base.
let to = match Address::from_str(&arguments.to) {
Err(_) => {
// see if it is an eth address
let addr = ethers::types::Address::from_str(&arguments.to)?;
ethers_address_to_fil_address(&addr)?
}
Ok(addr) => addr,
};

provider
.send_value(&subnet, from, to, f64_to_token_amount(arguments.amount)?)
.send_value(
&subnet,
from,
require_fil_addr_from_str(&arguments.to)?,
f64_to_token_amount(arguments.amount)?,
)
.await
}
}
Expand Down
Loading