diff --git a/Cargo.lock b/Cargo.lock index b91c9dcd..87bf4c1c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1377,6 +1377,7 @@ dependencies = [ "clap", "cosmos-sdk-proto", "cosmrs", + "cosmwasm-std", "cw-mantis-order", "derive_more", "itertools 0.12.0", diff --git a/README.md b/README.md index 768c589a..e26fa81a 100644 --- a/README.md +++ b/README.md @@ -14,4 +14,9 @@ For `issues` and `high level docs` go to `ComposableFi/composable` repo 7. Converts to Input and output back 8. TX solution. 6. Run for a while. -7. Deploy \ No newline at end of file +7. Deploy + + +# CFMM + +- query \ No newline at end of file diff --git a/mantis/node/Cargo.toml b/mantis/node/Cargo.toml index 8212775a..3e3714e9 100644 --- a/mantis/node/Cargo.toml +++ b/mantis/node/Cargo.toml @@ -26,3 +26,4 @@ itertools = { workspace = true } derive_more = { workspace = true } serde-json-wasm = { workspace = true } tuples = { workspace = true } +cosmwasm-std = {workspace = true} \ No newline at end of file diff --git a/mantis/node/src/bin/mantis.rs b/mantis/node/src/bin/mantis.rs index 925cdd0d..ec0caeef 100644 --- a/mantis/node/src/bin/mantis.rs +++ b/mantis/node/src/bin/mantis.rs @@ -1,7 +1,20 @@ -use cosmos_sdk_proto::cosmwasm::wasm::v1::QuerySmartContractStateRequest; +use cosmos_sdk_proto::{ + cosmos::{auth::v1beta1::BaseAccount, base::v1beta1::Coin}, + cosmwasm::{self, wasm::v1::QuerySmartContractStateRequest}, +}; +use cosmos_sdk_proto::{traits::Message, Any}; +use cosmrs::{ + tendermint::chain, + tx::{Msg, SignDoc}, +}; -use cosmrs::{cosmwasm::MsgExecuteContract, rpc::Client}; -use cw_mantis_order::OrderItem; +use cosmrs::{ + cosmwasm::MsgExecuteContract, + rpc::Client, + tx::{self, Fee, SignerInfo}, + AccountId, +}; +use cw_mantis_order::{OrderItem, OrderSubMsg}; use mantis_node::{ mantis::{args::*, cosmos::*}, prelude::*, @@ -10,7 +23,8 @@ use mantis_node::{ #[tokio::main] async fn main() { let args = MantisArgs::parsed(); - let read_client = create_wasm_query_client(&args.centauri).await; + let wasm_read_client = create_wasm_query_client(&args.centauri).await; + let mut cosmos_query_client = create_cosmos_query_client(&args.centauri).await; let signer = mantis_node::mantis::beaker::cli::support::signer::from_mnemonic( args.wallet.as_str(), @@ -18,13 +32,28 @@ async fn main() { ) .expect("mnemonic"); - - let mut write_client = create_wasm_write_client(&args.centauri).await; - loop { + let acc = query_cosmos_account( + &args.centauri, + signer + .public_key() + .account_id("centauri") + .expect("key") + .to_string(), + ) + .await; if let Some(assets) = args.simulate.clone() { - simulate_order(&mut write_client, args.order_contract.clone(), assets).await; + simulate_order( + &mut write_client, + &mut cosmos_query_client, + args.order_contract.clone(), + assets, + &signer, + acc, + &args.centauri, + ) + .await; }; } } @@ -40,15 +69,79 @@ async fn main() { /// timeout is also randomized starting from 10 to 100 blocks /// /// Also calls `timeout` so old orders are cleaned. -async fn simulate_order(write_client: &mut CosmWasmWriteClient, order_contract: String, _assets: String) { - if std::time::Instant::now().elapsed().as_millis() % 10 == 0 { - +async fn simulate_order( + write_client: &mut CosmWasmWriteClient, + cosmos_query_client: &mut CosmosQueryClient, + order_contract: String, + asset: String, + signing_key: &cosmrs::crypto::secp256k1::SigningKey, + acc: BaseAccount, + rpc: &str, +) { + if std::time::Instant::now().elapsed().as_millis() % 100 == 0 { + let auth_info = SignerInfo::single_direct(Some(signing_key.public_key()), acc.sequence) + .auth_info(Fee { + amount: vec![], + gas_limit: 100_000_000, + payer: None, + granter: None, + }); + + use cosmrs::tendermint::block::Height; + let rpc_client: cosmrs::rpc::HttpClient = cosmrs::rpc::HttpClient::new(rpc).unwrap(); + let status = rpc_client + .status() + .await + .expect("status") + .sync_info + .latest_block_height; + let msg = MsgExecuteContract { - sender: todo!(), - contract: todo!(), - msg: todo!(), - funds: todo!(), + sender: signing_key + .public_key() + .account_id("centauri") + .expect("account"), + contract: AccountId::from_str(&order_contract).expect("contract"), + msg: serde_json_wasm::to_vec(&cw_mantis_order::ExecMsg::Order { + msg: OrderSubMsg { + wants: cosmwasm_std::Coin { + amount: 1000u128.into(), + denom: asset.to_string(), + }, + transfer: None, + timeout: status.value() + 100, + min_fill: None, + }, + }) + .expect("json"), + funds: vec![cosmrs::Coin { + amount: 1000u128.into(), + denom: cosmrs::Denom::from_str("ppica").expect("denom"), + }], }; + let msg = msg.to_any().expect("proto"); + + let tx_body = tx::Body::new( + vec![msg], + "mantis-solver", + Height::try_from(status.value() + 100).unwrap(), + ); + + let sign_doc = SignDoc::new( + &tx_body, + &auth_info, + &chain::Id::try_from("centauri-1").expect("id"), + acc.account_number, + ) + .unwrap(); + + let tx_raw = sign_doc.sign(&signing_key).unwrap(); + let result = tx_raw + .broadcast_commit(&rpc_client) + .await + .expect("broadcasted"); + assert!(!result.check_tx.code.is_err(), "err"); + assert!(!result.tx_result.code.is_err(), "err"); //let result = write_client.execute_contract(request).await.expect("executed"); } diff --git a/mantis/node/src/mantis/cosmos.rs b/mantis/node/src/mantis/cosmos.rs index 08f511a9..f18916c6 100644 --- a/mantis/node/src/mantis/cosmos.rs +++ b/mantis/node/src/mantis/cosmos.rs @@ -27,6 +27,7 @@ pub async fn query_cosmos_account(rpc: &str, address : String) -> cosmos_sdk_pro BaseAccount::decode(account.account.expect("some").value.as_slice()).expect("decode") } + pub async fn create_wasm_query_client(rpc: &str) -> CosmWasmReadClient { let url = tonic::transport::Endpoint::from_str(rpc).expect("url"); cosmos_sdk_proto::cosmwasm::wasm::v1::query_client::QueryClient::connect(url)