Skip to content

Commit 5cd348e

Browse files
it finds
1 parent 9ba4f45 commit 5cd348e

File tree

7 files changed

+133
-81
lines changed

7 files changed

+133
-81
lines changed

crates/cvm-route/src/asset.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@ impl AssetItem {
4747
bridged: None,
4848
}
4949
}
50-
5150
}
5251

5352
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
@@ -72,7 +71,6 @@ impl NetworkAssetItem {
7271
}
7372
}
7473

75-
7674
impl AssetItem {
7775
pub fn denom(&self) -> String {
7876
self.local.denom()
@@ -102,14 +100,16 @@ pub struct BridgeAsset {
102100
)]
103101
pub enum AssetReference {
104102
/// Cosmos SDK native
105-
Native { denom: String },
106-
Cw20 { contract: cosmwasm_std::Addr },
103+
Native {
104+
denom: String,
105+
},
106+
Cw20 {
107+
contract: cosmwasm_std::Addr,
108+
},
107109
// Erc20 { contract: EthAddress },
108110
// SPL20 { mint: Pubkey },
109111
}
110112

111-
112-
113113
impl AssetReference {
114114
pub fn denom(&self) -> String {
115115
match self {

crates/cvm-route/src/exchange.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,4 +46,4 @@ impl ExchangeItem {
4646
closed: None,
4747
}
4848
}
49-
}
49+
}

crates/cvm-route/src/transport.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,11 @@ pub struct NetworkToNetworkItem {
2121
}
2222

2323
impl NetworkToNetworkItem {
24-
pub fn new(from_network_id: NetworkId, to_network_id: NetworkId, to_network: OtherNetworkItem) -> Self {
24+
pub fn new(
25+
from_network_id: NetworkId,
26+
to_network_id: NetworkId,
27+
to_network: OtherNetworkItem,
28+
) -> Self {
2529
Self {
2630
from_network_id,
2731
to_network_id,
@@ -51,7 +55,7 @@ pub struct OtherNetworkItem {
5155
pub use_shortcut: Option<bool>,
5256
}
5357

54-
impl OtherNetworkItem {
58+
impl OtherNetworkItem {
5559
pub fn new() -> Self {
5660
Self {
5761
ics_20: None,

crates/cvm-route/src/venue.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,4 @@ impl AssetsVenueItem {
3535
to_asset_id,
3636
}
3737
}
38-
}
38+
}

mantis/node/src/mantis/blackbox/mod.rs

+7-10
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ fn new_exchange(exchange: &Exchange) -> CvmInstruction {
110110
}
111111
}
112112

113-
pub async fn get_route(
113+
pub async fn get_routes(
114114
route_provider: &str,
115115
input: IntentBankInput,
116116
cvm_glt: &GetConfigResponse,
@@ -199,26 +199,23 @@ async fn intent_banks_to_cvm_program(
199199
router_api: &str,
200200
salt: &Vec<u8>,
201201
) -> CvmProgram {
202-
let (a, b) = IntentBankInput::find_intent_amount(
202+
let intents = IntentBankInput::find_intent_amount(
203203
pair_solution.cows.as_ref(),
204204
all_orders,
205205
pair_solution.optimal_price,
206206
cvm_glt,
207207
pair_solution.ab.clone(),
208208
);
209209

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

212212
let mut instructions = vec![];
213213

214-
if a.in_asset_amount.0.gt(&0) {
215-
let mut a_cvm_route = get_route(router_api, a, cvm_glt, salt.as_ref()).await;
216-
instructions.append(&mut a_cvm_route);
217-
}
218-
if b.in_asset_amount.0.gt(&0) {
219-
let mut b_cvm_route = get_route(router_api, b, cvm_glt, salt.as_ref()).await;
220-
instructions.append(&mut b_cvm_route);
214+
for intent in intents.into_iter().filter(|x| x.in_asset_amount.0.gt(&0)) {
215+
let mut cvm_routes = get_routes(router_api, intent, cvm_glt, salt.as_ref()).await;
216+
instructions.append(&mut cvm_routes);
221217
}
218+
222219
log::info!(target: "mantis::solver", "built instructions: {:?}", instructions);
223220

224221
let cvm_program = CvmProgram {

mantis/node/src/mantis/solve.rs

+29-23
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ impl IntentBankInput {
4343
optimal_ratio: Ratio<u64>,
4444
cvm_glt: &GetConfigResponse,
4545
pair: DenomPair,
46-
) -> (IntentBankInput, IntentBankInput) {
46+
) -> Vec<IntentBankInput> {
4747
// native calculations
4848
let mut pair = OrderCoinPair::zero(pair.a, pair.b);
4949
let mut a_to_b = Vec::new();
@@ -75,31 +75,37 @@ impl IntentBankInput {
7575

7676
// making MANTIS route request in CVM form
7777

78+
let mut intents = vec![];
7879
let a_asset = cvm_glt.cvm_asset_by_cw(pair.a.denom);
7980
let b_asset = cvm_glt.cvm_asset_by_cw(pair.b.denom);
81+
if pair.a.amount.u128() > 0 {
82+
let b_received = a_to_b.iter().map(|x| {
83+
let part = Ratio::new(x.1.u128(), pair.a.amount.u128()).msb_limit_unsigned();
84+
let part = CvmBalanceFilter::from((*part.numer(), *part.denom()));
85+
CvmInstruction::Transfer {
86+
to: Destination::Account(CvmAddress::from(x.0.to_string())),
87+
assets: CvmFundsFilter::of(a_asset, part),
88+
}
89+
});
90+
let intent =
91+
IntentBankInput::new(a_asset, pair.a.amount.into(), b_asset, b_received.collect());
92+
intents.push(intent);
93+
}
8094

81-
let b_received = a_to_b.iter().map(|x| {
82-
panic!("{:?} {:?}", x.1.u128(), pair.a.amount.u128());
83-
let part = Ratio::new(x.1.u128(), pair.a.amount.u128()).msb_limit_unsigned();
84-
let part = CvmBalanceFilter::from((*part.numer(), *part.denom()));
85-
CvmInstruction::Transfer {
86-
to: Destination::Account(CvmAddress::from(x.0.to_string())),
87-
assets: CvmFundsFilter::of(a_asset, part),
88-
}
89-
});
90-
let a_received = b_to_a.iter().map(|x| {
91-
let part = Ratio::new(x.1.u128(), pair.b.amount.u128()).msb_limit_unsigned();
92-
let part = CvmBalanceFilter::from((*part.numer(), *part.denom()));
93-
CvmInstruction::Transfer {
94-
to: Destination::Account(CvmAddress::from(x.0.to_string())),
95-
assets: CvmFundsFilter::of(b_asset, part),
96-
}
97-
});
98-
99-
(
100-
IntentBankInput::new(a_asset, pair.a.amount.into(), b_asset, b_received.collect()),
101-
IntentBankInput::new(b_asset, pair.b.amount.into(), a_asset, a_received.collect()),
102-
)
95+
if pair.b.amount.u128() > 0 {
96+
let a_received = b_to_a.iter().map(|x| {
97+
let part = Ratio::new(x.1.u128(), pair.b.amount.u128()).msb_limit_unsigned();
98+
let part = CvmBalanceFilter::from((*part.numer(), *part.denom()));
99+
CvmInstruction::Transfer {
100+
to: Destination::Account(CvmAddress::from(x.0.to_string())),
101+
assets: CvmFundsFilter::of(b_asset, part),
102+
}
103+
});
104+
let intent =
105+
IntentBankInput::new(b_asset, pair.b.amount.into(), a_asset, a_received.collect());
106+
intents.push(intent);
107+
}
108+
intents
103109
}
104110
}
105111

mantis/node/tests/cvms.rs

+83-38
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
use bounded_collections::Get;
22
use cosmrs::tendermint::block::Height;
33
use cosmwasm_std::{Addr, Coin, Coins, Empty};
4-
use cvm_route::{asset::{AssetItem, AssetReference, NetworkAssetItem}, exchange::ExchangeItem, transport::{NetworkToNetworkItem, OtherNetworkItem}, venue::AssetsVenueItem};
4+
use cvm_route::{
5+
asset::{AssetItem, AssetReference, NetworkAssetItem},
6+
exchange::ExchangeItem,
7+
transport::{NetworkToNetworkItem, OtherNetworkItem},
8+
venue::AssetsVenueItem,
9+
};
510
use cw_cvm_outpost::msg::{CvmGlt, HereItem, NetworkItem, OutpostId};
611
use cw_mantis_order::{OrderItem, OrderSubMsg};
712
use cw_multi_test::{App, Contract, ContractWrapper, Executor};
@@ -57,26 +62,25 @@ async fn cvm_devnet_case() {
5762
cvm_address: cw_cvm_outpost_contract.clone(),
5863
};
5964

60-
6165
let cw_mantis_contract = centauri
62-
.instantiate_contract(
63-
cw_mantis_order_code_id,
64-
admin,
65-
&cw_mantis_order_instantiate,
66-
&[],
67-
"composable_mantis_order",
68-
None,
69-
)
70-
.unwrap();
66+
.instantiate_contract(
67+
cw_mantis_order_code_id,
68+
admin,
69+
&cw_mantis_order_instantiate,
70+
&[],
71+
"composable_mantis_order",
72+
None,
73+
)
74+
.unwrap();
7175

7276
let sender = Addr::unchecked("juno16g2rahf5846rxzp3fwlswy08fz8ccuwk03k57y");
73-
77+
7478
let ACoin = |x: u128| Coin {
7579
denom: "a".to_string(),
7680
amount: x.into(),
7781
};
7882

79-
let BCoin = |x : u128| Coin {
83+
let BCoin = |x: u128| Coin {
8084
denom: "b".to_string(),
8185
amount: x.into(),
8286
};
@@ -85,11 +89,10 @@ async fn cvm_devnet_case() {
8589
owner: sender.clone(),
8690
msg: OrderSubMsg {
8791
wants: ACoin(100),
88-
timeout: centauri.block_info().height+100,
92+
timeout: centauri.block_info().height + 100,
8993
convert: None,
9094
min_fill: None,
9195
virtual_given: None,
92-
9396
},
9497
given: BCoin(100),
9598
order_id: 1u128.into(),
@@ -99,23 +102,21 @@ async fn cvm_devnet_case() {
99102
owner: sender.clone(),
100103
msg: OrderSubMsg {
101104
wants: BCoin(1000),
102-
timeout: centauri.block_info().height+100,
105+
timeout: centauri.block_info().height + 100,
103106
convert: None,
104107
min_fill: None,
105108
virtual_given: None,
106-
107109
},
108110
given: ACoin(1000),
109111
order_id: 2u128.into(),
110112
};
111113
let active_orders = vec![a_to_b, b_to_a];
112114
let alice = from_mnemonic(
113-
"document prefer nurse marriage flavor cheese west when knee drink sorry minimum thunder tilt cherry behave cute stove elder couch badge gown coral expire"
114-
,
115+
"document prefer nurse marriage flavor cheese west when knee drink sorry minimum thunder tilt cherry behave cute stove elder couch badge gown coral expire",
115116
"m/44'/118'/0'/0/0",).unwrap();
116117
let tip = Tip {
117118
block: Height::default(),
118-
account: cosmos_sdk_proto::cosmos::auth::v1beta1::BaseAccount{
119+
account: cosmos_sdk_proto::cosmos::auth::v1beta1::BaseAccount {
119120
address: alice.public_key().to_string(),
120121
pub_key: Some(alice.public_key().to_any().unwrap()),
121122
account_number: 1,
@@ -129,28 +130,62 @@ async fn cvm_devnet_case() {
129130
NetworkToNetworkItem::new(2.into(), 1.into(), OtherNetworkItem::new()),
130131
],
131132
assets: vec![
132-
AssetItem::new(11.into(), 1.into(), AssetReference::Native { denom: "a".to_string() } ),
133-
AssetItem::new(12.into(), 1.into(), AssetReference::Native { denom: "ibc/b".to_string() } ),
134-
AssetItem::new(21.into(), 2.into(), AssetReference::Native { denom: "b".to_string() } ),
135-
AssetItem::new(22.into(), 2.into(), AssetReference::Native { denom: "ibc/a".to_string() } ),
133+
AssetItem::new(
134+
11.into(),
135+
1.into(),
136+
AssetReference::Native {
137+
denom: "a".to_string(),
138+
},
139+
),
140+
AssetItem::new(
141+
12.into(),
142+
1.into(),
143+
AssetReference::Native {
144+
denom: "ibc/b".to_string(),
145+
},
146+
),
147+
AssetItem::new(
148+
21.into(),
149+
2.into(),
150+
AssetReference::Native {
151+
denom: "b".to_string(),
152+
},
153+
),
154+
AssetItem::new(
155+
22.into(),
156+
2.into(),
157+
AssetReference::Native {
158+
denom: "ibc/a".to_string(),
159+
},
160+
),
136161
],
137-
exchanges:
138-
vec![
139-
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() }),
140-
],
141-
networks:
142-
vec![
162+
exchanges: vec![ExchangeItem::new(
163+
1.into(),
164+
2.into(),
165+
cvm_route::exchange::ExchangeType::OsmosisPoolManagerModuleV1Beta1 {
166+
pool_id: 1,
167+
token_a: "b".to_string(),
168+
token_b: "ibc/a".to_string(),
169+
},
170+
)],
171+
networks: vec![
143172
NetworkItem {
144173
network_id: 1.into(),
145-
outpost: Some(
146-
OutpostId::CosmWasm { contract: cw_cvm_outpost_contract.clone(), executor_code_id: cw_cvm_executor_code_id, admin: sender.clone() }),
174+
outpost: Some(OutpostId::CosmWasm {
175+
contract: cw_cvm_outpost_contract.clone(),
176+
executor_code_id: cw_cvm_executor_code_id,
177+
admin: sender.clone(),
178+
}),
147179
accounts: None,
148180
ibc: None,
149181
},
150182
NetworkItem {
151183
network_id: 2.into(),
152-
outpost: Some(
153-
OutpostId::CosmWasm { contract: cw_cvm_outpost_contract.clone(), executor_code_id: cw_cvm_executor_code_id, admin: sender.clone() }),
184+
outpost: Some(OutpostId::CosmWasm {
185+
contract: cw_cvm_outpost_contract.clone(),
186+
executor_code_id: cw_cvm_executor_code_id,
187+
admin: sender.clone(),
188+
}),
154189
accounts: None,
155190
ibc: None,
156191
},
@@ -162,15 +197,25 @@ async fn cvm_devnet_case() {
162197
NetworkAssetItem::new(1.into(), 22.into(), 11.into()),
163198
],
164199
asset_venue_items: vec![
165-
AssetsVenueItem::new(cvm_route::venue::VenueId::Exchange(1.into()), 21.into(), 22.into()),
166-
AssetsVenueItem::new(cvm_route::venue::VenueId::Exchange(1.into()), 22.into(), 21.into()),
200+
AssetsVenueItem::new(
201+
cvm_route::venue::VenueId::Exchange(1.into()),
202+
21.into(),
203+
22.into(),
204+
),
205+
AssetsVenueItem::new(
206+
cvm_route::venue::VenueId::Exchange(1.into()),
207+
22.into(),
208+
21.into(),
209+
),
167210
],
168211
});
169-
let solution = mantis_node::mantis::blackbox::solve::<True>(active_orders, &alice, &tip, cvm_glt, router).await;
212+
let solution =
213+
mantis_node::mantis::blackbox::solve::<True>(active_orders, &alice, &tip, cvm_glt, router)
214+
.await;
170215
panic!("solution: {:?}", solution);
171216
}
172217

173-
enum True{}
218+
enum True {}
174219

175220
impl Get<bool> for True {
176221
fn get() -> bool {

0 commit comments

Comments
 (0)