94 lines
2.3 KiB
Rust
94 lines
2.3 KiB
Rust
|
use std::{error::Error, process::Command};
|
||
|
|
||
|
use cosmrs::{tendermint::chain::Id, AccountId};
|
||
|
use serde::{Deserialize, Serialize};
|
||
|
|
||
|
pub trait WasmdClient {
|
||
|
type Address: AsRef<str>;
|
||
|
type Query: ToString;
|
||
|
type ChainId: AsRef<str>;
|
||
|
type Error;
|
||
|
|
||
|
fn query_smart<R: FromVec>(
|
||
|
&self,
|
||
|
contract: &Self::Address,
|
||
|
query: Self::Query,
|
||
|
) -> Result<R, Self::Error>;
|
||
|
|
||
|
fn tx_execute<M: ToString>(
|
||
|
&self,
|
||
|
contract: &Self::Address,
|
||
|
chain_id: &Id,
|
||
|
gas: u64,
|
||
|
sender: String,
|
||
|
msg: M,
|
||
|
) -> Result<(), Self::Error>;
|
||
|
}
|
||
|
|
||
|
#[derive(Clone, Debug)]
|
||
|
pub struct CliWasmdClient;
|
||
|
|
||
|
#[derive(Clone, Debug, Serialize, Deserialize)]
|
||
|
pub struct QueryResult<T> {
|
||
|
pub data: T,
|
||
|
}
|
||
|
|
||
|
pub trait FromVec: Sized {
|
||
|
fn from_vec(value: Vec<u8>) -> Self;
|
||
|
}
|
||
|
|
||
|
impl<T: for<'any> Deserialize<'any>> FromVec for T {
|
||
|
fn from_vec(value: Vec<u8>) -> Self {
|
||
|
serde_json::from_slice(&value).unwrap()
|
||
|
}
|
||
|
}
|
||
|
|
||
|
impl WasmdClient for CliWasmdClient {
|
||
|
type Address = AccountId;
|
||
|
type Query = serde_json::Value;
|
||
|
type ChainId = Id;
|
||
|
type Error = Box<dyn Error>;
|
||
|
|
||
|
fn query_smart<R: FromVec>(
|
||
|
&self,
|
||
|
contract: &Self::Address,
|
||
|
query: Self::Query,
|
||
|
) -> Result<R, Self::Error> {
|
||
|
let mut wasmd = Command::new("wasmd");
|
||
|
let command = wasmd
|
||
|
.args(["query", "wasm"])
|
||
|
.args(["contract-state", "smart", contract.as_ref()])
|
||
|
.arg(query.to_string())
|
||
|
.args(["--output", "json"]);
|
||
|
|
||
|
let output = command.output()?;
|
||
|
println!("{:?} => {:?}", command, output);
|
||
|
|
||
|
let query_result = R::from_vec(output.stdout);
|
||
|
Ok(query_result)
|
||
|
}
|
||
|
|
||
|
fn tx_execute<M: ToString>(
|
||
|
&self,
|
||
|
contract: &Self::Address,
|
||
|
chain_id: &Id,
|
||
|
gas: u64,
|
||
|
sender: String,
|
||
|
msg: M,
|
||
|
) -> Result<(), Self::Error> {
|
||
|
let mut wasmd = Command::new("wasmd");
|
||
|
let command = wasmd
|
||
|
.args(["tx", "wasm"])
|
||
|
.args(["execute", contract.as_ref(), &msg.to_string()])
|
||
|
.args(["--chain-id", chain_id.as_ref()])
|
||
|
.args(["--gas", &gas.to_string()])
|
||
|
.args(["--from", sender.as_ref()])
|
||
|
.arg("-y");
|
||
|
|
||
|
let output = command.output()?;
|
||
|
println!("{:?} => {:?}", command, output);
|
||
|
|
||
|
Ok(())
|
||
|
}
|
||
|
}
|