Skip to content

Commit

Permalink
Merge pull request #7 from ComposableFi/dz/5
Browse files Browse the repository at this point in the history
feat: end to end ready to redeploy
  • Loading branch information
dzmitry-lahoda authored Nov 22, 2023
2 parents 779e1ca + d6a5aa7 commit 46cef33
Show file tree
Hide file tree
Showing 13 changed files with 109 additions and 72 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.

4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,6 @@ tuples = { version = "^1.14.0" }


bip32 = { version = "^0.5.1", default-features = false, features = ["alloc", "secp256k1", "mnemonic", "bip39"] }
prost-types = { version = "^0.12.3", default-features = false }
prost-types = { version = "^0.12.3", default-features = false }

num-traits = { version = "^0.2.17", default-features = false }
12 changes: 10 additions & 2 deletions contracts/cosmwasm/order/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,11 +107,19 @@ pub struct Cow {
pub order_id: OrderId,
/// how much of order to be solved by from bank for all aggregated cows
pub cow_amount: Amount,
/// amount of order to be taken (100% in case of full fill, can be less in case of partial)
pub taken: Option<Amount>,
/// amount user should get after order executed
pub given: Amount,
}
impl Cow {
pub fn new(order_id: OrderId, cow_amount: Amount, given: Amount) -> Self {
assert!(cow_amount <= given);
Self {
order_id,
cow_amount,
given,
}
}
}

#[cw_serde]
pub struct SolvedOrder {
Expand Down
3 changes: 2 additions & 1 deletion mantis/node/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,5 @@ tuples = { workspace = true }
cosmwasm-std = {workspace = true}

serde ={ workspace = true }
prost-types = {workspace = true}
prost-types = {workspace = true}
num-traits = {workspace = true}
66 changes: 40 additions & 26 deletions mantis/node/src/bin/mantis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use cosmrs::{
tx::{self, Fee, SignerInfo},
AccountId,
};
use cw_mantis_order::{OrderItem, OrderSubMsg};
use cw_mantis_order::{Cow, OrderItem, OrderSubMsg, SolutionSubMsg};
use mantis_node::{
mantis::{
args::*,
Expand Down Expand Up @@ -47,11 +47,11 @@ async fn main() {
print!("client 2");

loop {
let (block, account) =
let tip =
get_latest_block_and_account_by_key(&args.rpc_centauri, &args.grpc_centauri, &signer)
.await;
println!("tip: {:?}", tip);

println!("acc: {:?}", account);
if let Some(assets) = args.simulate.clone() {
if std::time::Instant::now().elapsed().as_millis() % 1000 == 0 {
simulate_order(
Expand All @@ -60,15 +60,14 @@ async fn main() {
args.order_contract.clone(),
assets,
&signer,
&account,
&block,
&args.rpc_centauri,
&tip,
)
.await;
};
};

let (block, account) =
let tip =
get_latest_block_and_account_by_key(&args.rpc_centauri, &args.grpc_centauri, &signer)
.await;

Expand All @@ -78,18 +77,19 @@ async fn main() {
&mut cosmos_query_client,
args.order_contract.clone(),
&signer,
&account,
&block,
&args.rpc_centauri,
&tip,
)
.await;
};

solve(
&mut wasm_read_client,
&mut write_client,
&mut wasm_read_client,
&args.order_contract,
&args.cvm_contract,
&signer,
&args.rpc_centauri,
&tip,
)
.await;
}
Expand All @@ -112,10 +112,10 @@ async fn simulate_order(
order_contract: String,
asset: String,
signing_key: &cosmrs::crypto::secp256k1::SigningKey,
account: &BaseAccount,
block: &cosmrs::tendermint::block::Height,
rpc: &str,
tip: &Tip,
) {
println!("========================= simulate_order =========================");
let coins: Vec<_> = asset
.split(',')
.map(|x| cosmwasm_std::Coin::from_str(x).expect("coin"))
Expand All @@ -126,15 +126,15 @@ async fn simulate_order(
(coins[1].clone(), coins[0].clone())
};

let auth_info = simulate_and_set_fee(signing_key, &account).await;
let auth_info = simulate_and_set_fee(signing_key, &tip.account).await;
let msg = cw_mantis_order::ExecMsg::Order {
msg: OrderSubMsg {
wants: cosmwasm_std::Coin {
amount: coins.0.amount,
denom: coins.0.denom.clone(),
},
transfer: None,
timeout: block.value() + 100,
timeout: tip.timeout(100),
min_fill: None,
},
};
Expand All @@ -149,11 +149,10 @@ async fn simulate_order(

tx_broadcast_single_signed_msg(
msg.to_any().expect("proto"),
block,
auth_info,
account,
rpc,
signing_key,
&tip,
)
.await;

Expand All @@ -165,23 +164,22 @@ async fn cleanup(
cosmos_query_client: &mut CosmosQueryClient,
order_contract: String,
signing_key: &cosmrs::crypto::secp256k1::SigningKey,
account: &BaseAccount,
block: &cosmrs::tendermint::block::Height,
rpc: &str,
tip: &Tip,
) {
let auth_info = simulate_and_set_fee(signing_key, &account).await;
println!("========================= cleanup =========================");
let auth_info = simulate_and_set_fee(signing_key, &tip.account).await;
let msg = cw_mantis_order::ExecMsg::Timeout {
orders: vec![],
solutions: vec![],
};
let msg = to_exec_signed(signing_key, order_contract, msg);
tx_broadcast_single_signed_msg(
msg.to_any().expect("proto"),
block,
auth_info,
account,
rpc,
signing_key,
tip,
)
.await;
}
Expand All @@ -194,13 +192,16 @@ async fn cleanup(
/// gets CVM routing data
/// uses cfmm algorithm
async fn solve(
read: &mut CosmWasmReadClient,
write: &mut CosmWasmWriteClient,
write_client: &mut CosmWasmWriteClient,
cosmos_query_client: &mut CosmWasmReadClient,
order_contract: &String,
cvm_contract: &String,
signing_key: &cosmrs::crypto::secp256k1::SigningKey,
rpc: &str,
tip: &Tip,
) {
println!("========================= solve =========================");
let query = cw_mantis_order::QueryMsg::GetAllOrders {};
let all_orders: Vec<OrderItem> = smart_query(order_contract, query, read).await;
let all_orders: Vec<OrderItem> = smart_query(order_contract, query, cosmos_query_client).await;
let all_orders = all_orders.into_iter().group_by(|x| {
let mut ab = (x.given.denom.clone(), x.msg.wants.denom.clone());
ab.sort_selection();
Expand Down Expand Up @@ -242,7 +243,20 @@ async fn solve(
let mut solution = Solution::new(orders.value.clone());
solution = solution.match_orders(optimal_price);
solution.print();
let cows = solution.orders.value.into_iter().map(|x| {
let filled = x.amount_filled.ceil().to_u128().expect("u128");
Cow {
order_id: x.id,
cow_amount: filled.into(),
given: filled.into(),
}
});
let solution = SolutionSubMsg {
cows: cows.collect(),
route: None,
timeout: tip.timeout(12),
};


let msg = cw_mantis_order::ExecMsg::Solve { msg: solution };
}
}
4 changes: 2 additions & 2 deletions mantis/node/src/mantis/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ pub struct MantisArgs {
/// the node hosting order contract
#[arg(long)]
pub rpc_centauri: String,

#[arg(long)]
pub grpc_centauri: String,

/// the node with pools
#[arg(long)]
pub osmosis: String,
Expand Down
45 changes: 20 additions & 25 deletions mantis/node/src/mantis/cosmos/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,19 @@ pub type CosmWasmReadClient =
pub type CosmosQueryClient =
cosmos_sdk_proto::cosmos::auth::v1beta1::query_client::QueryClient<Channel>;


/// tip of chain to use for tx formation
pub struct Tip{
#[derive(Debug)]
pub struct Tip {
pub block: Height,
pub account: cosmos_sdk_proto::cosmos::auth::v1beta1::BaseAccount,
}

impl Tip {
pub fn timeout(&self, delta: u32) -> u64 {
self.block.value() + delta as u64
}
}

pub async fn create_cosmos_query_client(rpc: &str) -> CosmosQueryClient {
use cosmos_sdk_proto::cosmos::auth::v1beta1::query_client::*;
use cosmos_sdk_proto::cosmos::auth::v1beta1::*;
Expand Down Expand Up @@ -63,33 +69,26 @@ pub async fn create_wasm_write_client(rpc: &str) -> CosmWasmWriteClient {
.expect("connected")
}

pub async fn get_latest_block_and_account(
rpc: &str,
grpc:&str,
address: String,
) -> (
cosmrs::tendermint::block::Height,
cosmos_sdk_proto::cosmos::auth::v1beta1::BaseAccount,
) {
pub async fn get_latest_block_and_account(rpc: &str, grpc: &str, address: String) -> Tip {
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 account = query_cosmos_account(grpc, address).await;
(status, account)
let account: BaseAccount = query_cosmos_account(grpc, address).await;
Tip {
block: status,
account,
}
}

pub async fn get_latest_block_and_account_by_key(
rpc: &str,
grpc: &str,
address: &cosmrs::crypto::secp256k1::SigningKey,
) -> (
cosmrs::tendermint::block::Height,
cosmos_sdk_proto::cosmos::auth::v1beta1::BaseAccount,
) {
) -> Tip {
get_latest_block_and_account(
rpc,
grpc,
Expand All @@ -98,7 +97,8 @@ pub async fn get_latest_block_and_account_by_key(
.account_id("centauri")
.expect("key")
.to_string(),
).await
)
.await
}

/// latest chain state
Expand Down Expand Up @@ -133,23 +133,18 @@ pub async fn sign_and_tx_tendermint(

pub async fn tx_broadcast_single_signed_msg(
msg: Any,
block: &Height,
auth_info: tx::AuthInfo,
account: &cosmos_sdk_proto::cosmos::auth::v1beta1::BaseAccount,
rpc: &str,
signing_key: &cosmrs::crypto::secp256k1::SigningKey,
tip: &Tip,
) {
let tx_body = tx::Body::new(
vec![msg],
"",
Height::try_from(block.value() + 100).unwrap(),
);
let tx_body = tx::Body::new(vec![msg], "", Height::try_from(tip.timeout(100)).unwrap());

let sign_doc = SignDoc::new(
&tx_body,
&auth_info,
&chain::Id::try_from("centauri-1").expect("id"),
account.account_number,
tip.account.account_number,
)
.unwrap();

Expand Down
Loading

0 comments on commit 46cef33

Please sign in to comment.