Skip to content

Commit

Permalink
fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
dzmitry-lahoda committed Mar 26, 2024
1 parent 73ceed7 commit 67a61e5
Show file tree
Hide file tree
Showing 19 changed files with 100 additions and 63 deletions.
26 changes: 22 additions & 4 deletions contracts/cosmwasm/order/src/validation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,40 @@ use crate::{OrderItem, SolvedOrder};

/// Validate solver can solve order he tells.
/// Minimal requirement is that CVM salt is unique to solver
pub fn validate_solver(as_ref: cosmwasm_std::Deps<'_>, sender: &Addr, order: &OrderItem) -> StdResult<()> {
pub fn validate_solver(
as_ref: cosmwasm_std::Deps<'_>,
sender: &Addr,
order: &OrderItem,
) -> StdResult<()> {
Ok(())
}

/// Validate program is sane
pub(crate) fn validate_program(as_ref: cosmwasm_std::Deps<'_>, cvm_program: &cvm_runtime::Program<Vec<cvm_runtime::Instruction<Vec<u8>, cvm_runtime::shared::XcAddr, cvm_runtime::Funds>>>, order: &OrderItem) -> StdResult<()> {
pub(crate) fn validate_program(
as_ref: cosmwasm_std::Deps<'_>,
cvm_program: &cvm_runtime::Program<
Vec<cvm_runtime::Instruction<Vec<u8>, cvm_runtime::shared::XcAddr, cvm_runtime::Funds>>,
>,
order: &OrderItem,
) -> StdResult<()> {
Ok(())
}

/// Validate solver can solver amount he claimed
pub(crate) fn validate_solvers(deps: &cosmwasm_std::DepsMut<'_>, solution: &crate::SolutionItem, all_orders: &[SolvedOrder]) -> StdResult<()> {
pub(crate) fn validate_solvers(
deps: &cosmwasm_std::DepsMut<'_>,
solution: &crate::SolutionItem,
all_orders: &[SolvedOrder],
) -> StdResult<()> {
Ok(())
}

/// Validate solver program is sane
/// Minimal requirement is that CVM salt is unique to solver
pub(crate) fn validate_routes(deps: &cosmwasm_std::DepsMut<'_>, solution: &crate::SolutionItem, all_orders: &[SolvedOrder]) -> StdResult<()> {
pub(crate) fn validate_routes(
deps: &cosmwasm_std::DepsMut<'_>,
solution: &crate::SolutionItem,
all_orders: &[SolvedOrder],
) -> StdResult<()> {
Ok(())
}
1 change: 1 addition & 0 deletions crates/cvm-glt/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

12 changes: 8 additions & 4 deletions crates/cvm-runtime/src/outpost/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,11 @@ pub struct GetConfigResponse {
}

impl GetConfigResponse {
pub fn get_network_for_asset(&self, asset_id:AssetId ) -> NetworkId {
self.assets.iter().find(|x| x.asset_id == asset_id).map(|x| x.network_id).expect("network_id")
}
}
pub fn get_network_for_asset(&self, asset_id: AssetId) -> NetworkId {
self.assets
.iter()
.find(|x| x.asset_id == asset_id)
.map(|x| x.network_id)
.expect("network_id")
}
}
1 change: 0 additions & 1 deletion crates/cvm-runtime/src/program.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,3 @@ pub struct Program<Instructions> {
/// list of instructions to be executed
pub instructions: Instructions,
}

1 change: 0 additions & 1 deletion crates/cvm-runtime/src/shared.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ pub type CvmProgram = crate::Program<Vec<CvmInstruction>>;

pub type CvmSpawnRef<'a> = (&'a CvmProgram, &'a CvmFundsFilter);


impl Default for CvmProgram {
fn default() -> Self {
Self {
Expand Down
4 changes: 1 addition & 3 deletions crates/cvm/src/asset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,11 +159,9 @@ impl From<(u64, u64)> for Amount {
}

impl Amount {


pub const MAX_PARTS: u64 = 1_000_000_000_000_000_000;

pub fn one() -> Self{
pub fn one() -> Self {
Self::absolute(1)
}

Expand Down
2 changes: 1 addition & 1 deletion mantis/node/src/bin/mantis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ async fn simulate_orders(simulate_args: &SimulateArgs) {
)
.expect("mnemonic");
let gas = args.gas;
let mut cosmos_query_client = create_cosmos_query_client(&args.rpc_centauri).await;
let mut cosmos_query_client = create_cocsmos_query_client(&args.rpc_centauri).await;
let mut write_client = create_wasm_write_client(&args.rpc_centauri).await;
log::info!("Simulating orders");

Expand Down
8 changes: 2 additions & 6 deletions mantis/node/src/mantis/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ pub struct GltArgs {
pub command: GltCommands,
}


#[derive(Debug, Subcommand)]
pub enum GltCommands {
// given offchain configuration, validates is
Expand All @@ -34,7 +33,6 @@ pub enum GltCommands {
/// adds specific things to offchain config
Add,
}


#[derive(Debug, Parser)]
pub struct IdArgs {
Expand Down Expand Up @@ -70,22 +68,20 @@ pub struct SharedArgs {
/// Order contract on Centauri
#[arg(long)]
pub order_contract: String,
/// wallet to use.
/// wallet to use.
/// For now BIP39 normalized English mnemonic empty passphrase with Kepler default derivation supported
#[arg(long)]
pub wallet: String,


#[arg(long, default_value_t = 1_000_000_000)]
pub gas: Gas,

}

#[derive(clap::Parser, Debug)]
pub struct SimulateArgs {
#[command(flatten)]
pub shared: SharedArgs,

/// CVM contract on Centauri. Optional, only if consider routing via cross chain CVM.
#[arg(long)]
pub cvm_contract: Option<String>,
Expand Down
12 changes: 8 additions & 4 deletions mantis/node/src/mantis/autopilot.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
use cosmrs::{tx::Msg, Gas};

use crate::{mantis::cosmos::{client::{simulate_and_set_fee, tx_broadcast_single_signed_msg}, cosmwasm::to_exec_signed}, prelude::*};
use crate::{
mantis::cosmos::{
client::{simulate_and_set_fee, tx_broadcast_single_signed_msg},
cosmwasm::to_exec_signed,
},
prelude::*,
};

use super::cosmos::client::{CosmWasmWriteClient, CosmosQueryClient, Tip};

Expand Down Expand Up @@ -31,6 +37,4 @@ pub async fn cleanup(
}

/// move protocol forwards, cranks auctions ending and also cleans up old orders
async fn _move(){

}
async fn _move() {}
39 changes: 28 additions & 11 deletions mantis/node/src/mantis/blackbox/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
use blackbox_rs::{prelude::*, types::*, Client};
/// Given total amount it, order owners and desired out, produce CVM program from and by requesting route
use cvm_runtime::{
network, outpost::GetConfigResponse, proto::cvm, shared::{CvmFundsFilter, CvmInstruction, CvmProgram, Displayed, XcAddr}, Amount, AssetId, ExchangeId
network,
outpost::GetConfigResponse,
proto::cvm,
shared::{CvmFundsFilter, CvmInstruction, CvmProgram, Displayed, XcAddr},
Amount, AssetId, ExchangeId,
};

/// input batched summarized from users for routing
Expand Down Expand Up @@ -29,14 +33,17 @@ impl BankInput {
}

/// given route and CVM stub with amount, build it to the end
fn build_next(current: &mut CvmProgram, next: &mut [NextItem], glt: &GetConfigResponse, salt: &[u8]) {
fn build_next(
current: &mut CvmProgram,
next: &mut [NextItem],
glt: &GetConfigResponse,
salt: &[u8],
) {
match next.split_first_mut() {
Some((head, rest)) => match head {
NextItem::Exchange(exchange) => {
let exchange = new_exchange(exchange);
current
.instructions
.push(exchange);
current.instructions.push(exchange);
build_next(current, rest, &glt, salt);
}
NextItem::Spawn(spawn) => {
Expand All @@ -52,13 +59,18 @@ fn build_next(current: &mut CvmProgram, next: &mut [NextItem], glt: &GetConfigRe
}
}

fn new_spawn(spawn: &Spawn, program: CvmProgram, glt: &GetConfigResponse, salt: &[u8]) -> CvmInstruction {
fn new_spawn(
spawn: &Spawn,
program: CvmProgram,
glt: &GetConfigResponse,
salt: &[u8],
) -> CvmInstruction {
let in_asset_id = match spawn.in_asset_id.as_ref().expect("in_asset_id") {
InAssetId::Variant1(id) => id.parse().expect("in_asset_id"),
_ => panic!("in_asset_id"),
};

let in_amount : Amount = match spawn.in_asset_amount.as_ref().expect("in_asset_amount") {
let in_amount: Amount = match spawn.in_asset_amount.as_ref().expect("in_asset_amount") {
InAssetAmount::Variant0(x) => (*x).try_into().expect("in_asset_amount"),
InAssetAmount::Variant1(x) => x.parse().expect("in_asset_amount"),
InAssetAmount::Variant2(x) => Amount::try_floor_f64(*x).expect("in_asset_amount"),
Expand All @@ -78,8 +90,8 @@ fn new_spawn(spawn: &Spawn, program: CvmProgram, glt: &GetConfigResponse, salt:
CvmInstruction::Spawn {
program,
network_id,
salt : salt.to_vec(),
assets: CvmFundsFilter::one(in_asset_id, in_amount)
salt: salt.to_vec(),
assets: CvmFundsFilter::one(in_asset_id, in_amount),
}
}

Expand All @@ -93,7 +105,7 @@ fn new_exchange(exchange: &Exchange) -> CvmInstruction {
_ => panic!("in_asset_id"),
};

let in_amount : Amount = match &exchange.in_asset_amount {
let in_amount: Amount = match &exchange.in_asset_amount {
InAssetAmount::Variant0(x) => (*x).try_into().expect("in_asset_amount"),
InAssetAmount::Variant1(x) => x.parse().expect("in_asset_amount"),
InAssetAmount::Variant2(x) => Amount::try_floor_f64(*x).expect("in_asset_amount"),
Expand All @@ -112,7 +124,12 @@ fn new_exchange(exchange: &Exchange) -> CvmInstruction {
}

/// `order_accounts` - account of order where to dispatch amounts (part of whole)
pub async fn route(server: &str, input: BankInput, glt: &GetConfigResponse, salt: &[u8]) -> CvmProgram {
pub async fn route(
server: &str,
input: BankInput,
glt: &GetConfigResponse,
salt: &[u8],
) -> CvmProgram {
let blackbox: Client = Client::new(server);
let mut route = blackbox
.simulator_router_simulator_router_get(
Expand Down
2 changes: 1 addition & 1 deletion mantis/node/src/mantis/cosmos/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,4 +173,4 @@ pub async fn simulate_and_set_fee(
granter: None,
});
auth_info
}
}
2 changes: 1 addition & 1 deletion mantis/node/src/mantis/cosmos/cosmwasm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,4 @@ pub fn parse_coin_pair(pair: &String) -> (Coin, Coin) {
.collect();
assert_eq!(pair.len(), 2);
(pair[0].clone(), pair[1].clone())
}
}
4 changes: 2 additions & 2 deletions mantis/node/src/mantis/cosmos/cvm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ use super::client::Tip;

/// given key and latest block and sequence number, produces binary salt for cross chain block
/// isolating execution of one cross chain transaction from other
pub fn get_salt(signing_key: &cosmrs::crypto::secp256k1::SigningKey, tip:&Tip) -> Vec<u8> {
pub fn get_salt(signing_key: &cosmrs::crypto::secp256k1::SigningKey, tip: &Tip) -> Vec<u8> {
let mut base = signing_key.public_key().to_bytes().to_vec();
base.extend(tip.block.value().to_be_bytes().to_vec());
base.extend(tip.account.sequence.to_be_bytes().to_vec());
base
}
}
2 changes: 1 addition & 1 deletion mantis/node/src/mantis/cosmos/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
pub mod client;
pub mod cosmwasm;
pub mod signer;
pub mod cvm;
pub mod signer;
12 changes: 6 additions & 6 deletions mantis/node/src/mantis/indexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ use crate::mantis::cosmos::cosmwasm::smart_query;

use super::cosmos::client::{CosmWasmReadClient, Tip};


pub async fn get_all_orders(
order_contract: &String,
cosmos_query_client: &mut CosmWasmReadClient,
Expand All @@ -21,10 +20,11 @@ pub async fn get_all_orders(
all_orders
}

pub async fn get_cvm_glt(contract: &String, cosmos_query_client: &mut CosmWasmReadClient,
pub async fn get_cvm_glt(
contract: &String,
cosmos_query_client: &mut CosmWasmReadClient,
) -> GetConfigResponse {
let query = cvm_runtime::outpost::QueryMsg::GetConfig {};
let cvm_glt = smart_query::<_, GetConfigResponse>(contract, query, cosmos_query_client)
.await;
cvm_glt
}
let cvm_glt = smart_query::<_, GetConfigResponse>(contract, query, cosmos_query_client).await;
cvm_glt
}
8 changes: 4 additions & 4 deletions mantis/node/src/mantis/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
pub mod args;
pub mod cosmos;
pub mod solve;
pub mod autopilot;
pub mod blackbox;
pub mod cosmos;
pub mod indexer;
pub mod autopilot;
pub mod simulate;
pub mod simulate;
pub mod solve;
23 changes: 12 additions & 11 deletions mantis/node/src/mantis/simulate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,20 @@ use cosmrs::{tendermint::block::Height, tx::Msg, Gas};
use cw_mantis_order::{OrderItem, OrderSolution, OrderSubMsg};

use crate::{
mantis::cosmos::{client::{simulate_and_set_fee, tx_broadcast_single_signed_msg}, cosmwasm::to_exec_signed_with_fund}, prelude::*, solver::{orderbook::OrderList, solution::Solution, types::OrderType}
mantis::cosmos::{
client::{simulate_and_set_fee, tx_broadcast_single_signed_msg},
cosmwasm::to_exec_signed_with_fund,
},
prelude::*,
solver::{orderbook::OrderList, solution::Solution, types::OrderType},
};

use super::cosmos::{client::{timeout, CosmWasmWriteClient, CosmosQueryClient, Tip}, cosmwasm::parse_coin_pair};
use super::cosmos::{
client::{timeout, CosmWasmWriteClient, CosmosQueryClient, Tip},
cosmwasm::parse_coin_pair,
};

pub fn randomize_order(
pair: &String,
tip: Height,
) -> (cw_mantis_order::ExecMsg, cosmrs::Coin) {
pub fn randomize_order(pair: &String, tip: Height) -> (cw_mantis_order::ExecMsg, cosmrs::Coin) {
let pair = parse_coin_pair(pair);

let pair = if rand::random::<bool>() {
Expand Down Expand Up @@ -39,16 +44,13 @@ pub fn randomize_order(
(msg, fund)
}


pub fn randomize_coin(coin_0_amount: u128) -> u128 {
let delta_0 = 1.max(coin_0_amount / 10);
let coin_0_random = rand_distr::Uniform::new(coin_0_amount - delta_0, coin_0_amount + delta_0);
let coin_0_random: u128 = coin_0_random.sample(&mut rand::thread_rng());
coin_0_random
}



/// `assets` - is comma separate list. each entry is amount u64 glued with alphanumeric denomination
/// that is splitted into array of CosmWasm coins.
/// one coin is chosen as given,
Expand Down Expand Up @@ -77,7 +79,6 @@ pub async fn simulate_order(

let auth_info = simulate_and_set_fee(signing_key, &tip.account, gas).await;


let msg = to_exec_signed_with_fund(signing_key, order_contract, msg, fund);

let result = tx_broadcast_single_signed_msg(
Expand All @@ -90,4 +91,4 @@ pub async fn simulate_order(
.await;

println!("simulated tx {:?}", result.height)
}
}
2 changes: 1 addition & 1 deletion mantis/node/src/mantis/solve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ pub fn do_cows(all_orders: Vec<OrderItem>) -> SolutionsPerPair {
let filled = x.amount_out.to_u128().expect("u128");
OrderSolution {
order_id: x.id,
cow_out_amount: filled.into(),
cow_out_amount: filled.into(),
cross_chain_part: Some(OrderAmount::All),
}
})
Expand Down
Loading

0 comments on commit 67a61e5

Please sign in to comment.