Skip to content

Commit

Permalink
it finds
Browse files Browse the repository at this point in the history
  • Loading branch information
dzmitry-lahoda committed Apr 11, 2024
1 parent 9ba4f45 commit 5cd348e
Show file tree
Hide file tree
Showing 7 changed files with 133 additions and 81 deletions.
12 changes: 6 additions & 6 deletions crates/cvm-route/src/asset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ impl AssetItem {
bridged: None,
}
}

}

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
Expand All @@ -72,7 +71,6 @@ impl NetworkAssetItem {
}
}


impl AssetItem {
pub fn denom(&self) -> String {
self.local.denom()
Expand Down Expand Up @@ -102,14 +100,16 @@ pub struct BridgeAsset {
)]
pub enum AssetReference {
/// Cosmos SDK native
Native { denom: String },
Cw20 { contract: cosmwasm_std::Addr },
Native {
denom: String,
},
Cw20 {
contract: cosmwasm_std::Addr,
},
// Erc20 { contract: EthAddress },
// SPL20 { mint: Pubkey },
}



impl AssetReference {
pub fn denom(&self) -> String {
match self {
Expand Down
2 changes: 1 addition & 1 deletion crates/cvm-route/src/exchange.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,4 @@ impl ExchangeItem {
closed: None,
}
}
}
}
8 changes: 6 additions & 2 deletions crates/cvm-route/src/transport.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,11 @@ pub struct NetworkToNetworkItem {
}

impl NetworkToNetworkItem {
pub fn new(from_network_id: NetworkId, to_network_id: NetworkId, to_network: OtherNetworkItem) -> Self {
pub fn new(
from_network_id: NetworkId,
to_network_id: NetworkId,
to_network: OtherNetworkItem,
) -> Self {
Self {
from_network_id,
to_network_id,
Expand Down Expand Up @@ -51,7 +55,7 @@ pub struct OtherNetworkItem {
pub use_shortcut: Option<bool>,
}

impl OtherNetworkItem {
impl OtherNetworkItem {
pub fn new() -> Self {
Self {
ics_20: None,
Expand Down
2 changes: 1 addition & 1 deletion crates/cvm-route/src/venue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,4 @@ impl AssetsVenueItem {
to_asset_id,
}
}
}
}
17 changes: 7 additions & 10 deletions mantis/node/src/mantis/blackbox/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ fn new_exchange(exchange: &Exchange) -> CvmInstruction {
}
}

pub async fn get_route(
pub async fn get_routes(
route_provider: &str,
input: IntentBankInput,
cvm_glt: &GetConfigResponse,
Expand Down Expand Up @@ -199,26 +199,23 @@ async fn intent_banks_to_cvm_program(
router_api: &str,
salt: &Vec<u8>,
) -> CvmProgram {
let (a, b) = IntentBankInput::find_intent_amount(
let intents = IntentBankInput::find_intent_amount(
pair_solution.cows.as_ref(),
all_orders,
pair_solution.optimal_price,
cvm_glt,
pair_solution.ab.clone(),
);

log::info!(target:"mantis::solver::", "found for cross chain a: {:?}, b: {:?}", a, b);
log::info!(target:"mantis::solver::", "intents: {:?}", intents);

let mut instructions = vec![];

if a.in_asset_amount.0.gt(&0) {
let mut a_cvm_route = get_route(router_api, a, cvm_glt, salt.as_ref()).await;
instructions.append(&mut a_cvm_route);
}
if b.in_asset_amount.0.gt(&0) {
let mut b_cvm_route = get_route(router_api, b, cvm_glt, salt.as_ref()).await;
instructions.append(&mut b_cvm_route);
for intent in intents.into_iter().filter(|x| x.in_asset_amount.0.gt(&0)) {
let mut cvm_routes = get_routes(router_api, intent, cvm_glt, salt.as_ref()).await;
instructions.append(&mut cvm_routes);
}

log::info!(target: "mantis::solver", "built instructions: {:?}", instructions);

let cvm_program = CvmProgram {
Expand Down
52 changes: 29 additions & 23 deletions mantis/node/src/mantis/solve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ impl IntentBankInput {
optimal_ratio: Ratio<u64>,
cvm_glt: &GetConfigResponse,
pair: DenomPair,
) -> (IntentBankInput, IntentBankInput) {
) -> Vec<IntentBankInput> {
// native calculations
let mut pair = OrderCoinPair::zero(pair.a, pair.b);
let mut a_to_b = Vec::new();
Expand Down Expand Up @@ -75,31 +75,37 @@ impl IntentBankInput {

// making MANTIS route request in CVM form

let mut intents = vec![];
let a_asset = cvm_glt.cvm_asset_by_cw(pair.a.denom);
let b_asset = cvm_glt.cvm_asset_by_cw(pair.b.denom);
if pair.a.amount.u128() > 0 {
let b_received = a_to_b.iter().map(|x| {
let part = Ratio::new(x.1.u128(), pair.a.amount.u128()).msb_limit_unsigned();
let part = CvmBalanceFilter::from((*part.numer(), *part.denom()));
CvmInstruction::Transfer {
to: Destination::Account(CvmAddress::from(x.0.to_string())),
assets: CvmFundsFilter::of(a_asset, part),
}
});
let intent =
IntentBankInput::new(a_asset, pair.a.amount.into(), b_asset, b_received.collect());
intents.push(intent);
}

let b_received = a_to_b.iter().map(|x| {
panic!("{:?} {:?}", x.1.u128(), pair.a.amount.u128());
let part = Ratio::new(x.1.u128(), pair.a.amount.u128()).msb_limit_unsigned();
let part = CvmBalanceFilter::from((*part.numer(), *part.denom()));
CvmInstruction::Transfer {
to: Destination::Account(CvmAddress::from(x.0.to_string())),
assets: CvmFundsFilter::of(a_asset, part),
}
});
let a_received = b_to_a.iter().map(|x| {
let part = Ratio::new(x.1.u128(), pair.b.amount.u128()).msb_limit_unsigned();
let part = CvmBalanceFilter::from((*part.numer(), *part.denom()));
CvmInstruction::Transfer {
to: Destination::Account(CvmAddress::from(x.0.to_string())),
assets: CvmFundsFilter::of(b_asset, part),
}
});

(
IntentBankInput::new(a_asset, pair.a.amount.into(), b_asset, b_received.collect()),
IntentBankInput::new(b_asset, pair.b.amount.into(), a_asset, a_received.collect()),
)
if pair.b.amount.u128() > 0 {
let a_received = b_to_a.iter().map(|x| {
let part = Ratio::new(x.1.u128(), pair.b.amount.u128()).msb_limit_unsigned();
let part = CvmBalanceFilter::from((*part.numer(), *part.denom()));
CvmInstruction::Transfer {
to: Destination::Account(CvmAddress::from(x.0.to_string())),
assets: CvmFundsFilter::of(b_asset, part),
}
});
let intent =
IntentBankInput::new(b_asset, pair.b.amount.into(), a_asset, a_received.collect());
intents.push(intent);
}
intents
}
}

Expand Down
121 changes: 83 additions & 38 deletions mantis/node/tests/cvms.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
use bounded_collections::Get;
use cosmrs::tendermint::block::Height;
use cosmwasm_std::{Addr, Coin, Coins, Empty};
use cvm_route::{asset::{AssetItem, AssetReference, NetworkAssetItem}, exchange::ExchangeItem, transport::{NetworkToNetworkItem, OtherNetworkItem}, venue::AssetsVenueItem};
use cvm_route::{
asset::{AssetItem, AssetReference, NetworkAssetItem},
exchange::ExchangeItem,
transport::{NetworkToNetworkItem, OtherNetworkItem},
venue::AssetsVenueItem,
};
use cw_cvm_outpost::msg::{CvmGlt, HereItem, NetworkItem, OutpostId};
use cw_mantis_order::{OrderItem, OrderSubMsg};
use cw_multi_test::{App, Contract, ContractWrapper, Executor};
Expand Down Expand Up @@ -57,26 +62,25 @@ async fn cvm_devnet_case() {
cvm_address: cw_cvm_outpost_contract.clone(),
};


let cw_mantis_contract = centauri
.instantiate_contract(
cw_mantis_order_code_id,
admin,
&cw_mantis_order_instantiate,
&[],
"composable_mantis_order",
None,
)
.unwrap();
.instantiate_contract(
cw_mantis_order_code_id,
admin,
&cw_mantis_order_instantiate,
&[],
"composable_mantis_order",
None,
)
.unwrap();

let sender = Addr::unchecked("juno16g2rahf5846rxzp3fwlswy08fz8ccuwk03k57y");

let ACoin = |x: u128| Coin {
denom: "a".to_string(),
amount: x.into(),
};

let BCoin = |x : u128| Coin {
let BCoin = |x: u128| Coin {
denom: "b".to_string(),
amount: x.into(),
};
Expand All @@ -85,11 +89,10 @@ async fn cvm_devnet_case() {
owner: sender.clone(),
msg: OrderSubMsg {
wants: ACoin(100),
timeout: centauri.block_info().height+100,
timeout: centauri.block_info().height + 100,
convert: None,
min_fill: None,
virtual_given: None,

},
given: BCoin(100),
order_id: 1u128.into(),
Expand All @@ -99,23 +102,21 @@ async fn cvm_devnet_case() {
owner: sender.clone(),
msg: OrderSubMsg {
wants: BCoin(1000),
timeout: centauri.block_info().height+100,
timeout: centauri.block_info().height + 100,
convert: None,
min_fill: None,
virtual_given: None,

},
given: ACoin(1000),
order_id: 2u128.into(),
};
let active_orders = vec![a_to_b, b_to_a];
let alice = from_mnemonic(
"document prefer nurse marriage flavor cheese west when knee drink sorry minimum thunder tilt cherry behave cute stove elder couch badge gown coral expire"
,
"document prefer nurse marriage flavor cheese west when knee drink sorry minimum thunder tilt cherry behave cute stove elder couch badge gown coral expire",
"m/44'/118'/0'/0/0",).unwrap();
let tip = Tip {
block: Height::default(),
account: cosmos_sdk_proto::cosmos::auth::v1beta1::BaseAccount{
account: cosmos_sdk_proto::cosmos::auth::v1beta1::BaseAccount {
address: alice.public_key().to_string(),
pub_key: Some(alice.public_key().to_any().unwrap()),
account_number: 1,
Expand All @@ -129,28 +130,62 @@ async fn cvm_devnet_case() {
NetworkToNetworkItem::new(2.into(), 1.into(), OtherNetworkItem::new()),
],
assets: vec![
AssetItem::new(11.into(), 1.into(), AssetReference::Native { denom: "a".to_string() } ),
AssetItem::new(12.into(), 1.into(), AssetReference::Native { denom: "ibc/b".to_string() } ),
AssetItem::new(21.into(), 2.into(), AssetReference::Native { denom: "b".to_string() } ),
AssetItem::new(22.into(), 2.into(), AssetReference::Native { denom: "ibc/a".to_string() } ),
AssetItem::new(
11.into(),
1.into(),
AssetReference::Native {
denom: "a".to_string(),
},
),
AssetItem::new(
12.into(),
1.into(),
AssetReference::Native {
denom: "ibc/b".to_string(),
},
),
AssetItem::new(
21.into(),
2.into(),
AssetReference::Native {
denom: "b".to_string(),
},
),
AssetItem::new(
22.into(),
2.into(),
AssetReference::Native {
denom: "ibc/a".to_string(),
},
),
],
exchanges:
vec![
ExchangeItem::new(1.into(), 2.into(), cvm_route::exchange::ExchangeType::OsmosisPoolManagerModuleV1Beta1 { pool_id: 1, token_a: "b".to_string(), token_b: "ibc/a".to_string() }),
],
networks:
vec![
exchanges: vec![ExchangeItem::new(
1.into(),
2.into(),
cvm_route::exchange::ExchangeType::OsmosisPoolManagerModuleV1Beta1 {
pool_id: 1,
token_a: "b".to_string(),
token_b: "ibc/a".to_string(),
},
)],
networks: vec![
NetworkItem {
network_id: 1.into(),
outpost: Some(
OutpostId::CosmWasm { contract: cw_cvm_outpost_contract.clone(), executor_code_id: cw_cvm_executor_code_id, admin: sender.clone() }),
outpost: Some(OutpostId::CosmWasm {
contract: cw_cvm_outpost_contract.clone(),
executor_code_id: cw_cvm_executor_code_id,
admin: sender.clone(),
}),
accounts: None,
ibc: None,
},
NetworkItem {
network_id: 2.into(),
outpost: Some(
OutpostId::CosmWasm { contract: cw_cvm_outpost_contract.clone(), executor_code_id: cw_cvm_executor_code_id, admin: sender.clone() }),
outpost: Some(OutpostId::CosmWasm {
contract: cw_cvm_outpost_contract.clone(),
executor_code_id: cw_cvm_executor_code_id,
admin: sender.clone(),
}),
accounts: None,
ibc: None,
},
Expand All @@ -162,15 +197,25 @@ async fn cvm_devnet_case() {
NetworkAssetItem::new(1.into(), 22.into(), 11.into()),
],
asset_venue_items: vec![
AssetsVenueItem::new(cvm_route::venue::VenueId::Exchange(1.into()), 21.into(), 22.into()),
AssetsVenueItem::new(cvm_route::venue::VenueId::Exchange(1.into()), 22.into(), 21.into()),
AssetsVenueItem::new(
cvm_route::venue::VenueId::Exchange(1.into()),
21.into(),
22.into(),
),
AssetsVenueItem::new(
cvm_route::venue::VenueId::Exchange(1.into()),
22.into(),
21.into(),
),
],
});
let solution = mantis_node::mantis::blackbox::solve::<True>(active_orders, &alice, &tip, cvm_glt, router).await;
let solution =
mantis_node::mantis::blackbox::solve::<True>(active_orders, &alice, &tip, cvm_glt, router)
.await;
panic!("solution: {:?}", solution);
}

enum True{}
enum True {}

impl Get<bool> for True {
fn get() -> bool {
Expand Down

0 comments on commit 5cd348e

Please sign in to comment.