Skip to content

Commit

Permalink
Merge pull request #3 from ComposableFi/dz/3
Browse files Browse the repository at this point in the history
feat: end to end cosmos call
  • Loading branch information
dzmitry-lahoda authored Nov 19, 2023
2 parents 2aa1a36 + 2ad53e8 commit 32f96b4
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 16 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
7. Deploy


# CFMM

- query
1 change: 1 addition & 0 deletions mantis/node/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,4 @@ itertools = { workspace = true }
derive_more = { workspace = true }
serde-json-wasm = { workspace = true }
tuples = { workspace = true }
cosmwasm-std = {workspace = true}
123 changes: 108 additions & 15 deletions mantis/node/src/bin/mantis.rs
Original file line number Diff line number Diff line change
@@ -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::*,
Expand All @@ -10,21 +23,37 @@ 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(),
"centauri",
)
.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;
};
}
}
Expand All @@ -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");
}
Expand Down
1 change: 1 addition & 0 deletions mantis/node/src/mantis/cosmos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 32f96b4

Please sign in to comment.