Skip to content

Commit

Permalink
slice issue
Browse files Browse the repository at this point in the history
  • Loading branch information
dzmitry-lahoda committed Apr 14, 2024
1 parent da77786 commit a8bc3fe
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 42 deletions.
3 changes: 3 additions & 0 deletions contracts/cosmwasm/outpost/src/contract/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ pub fn query(deps: Deps, _env: Env, msg: msg::QueryMsg) -> Result<Binary> {
GetAllAssetIds {} => crate::state::assets::get_all_assets(deps)
.and_then(|x| Ok(to_json_binary(&x)?))
.map_err(Into::into),
GetAllAssetVenues {} => crate::state::exchange::get_all_exchange_venues(deps)
.and_then(|x| Ok(to_json_binary(&x)?))
.map_err(Into::into),
// GetRoute { program } => router::get_route(deps, program),
}
}
79 changes: 39 additions & 40 deletions contracts/cosmwasm/outpost/src/state/exchange.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,47 +43,49 @@ pub(crate) fn force_exchange(
}

pub(crate) const EXCHANGE: Map<u128, cvm_route::exchange::ExchangeItem> = Map::new("exchange");
pub type VenueKey = (u128, u128, u128);
pub(crate) const EXCHANGE_VENUE: Map<VenueKey, cvm_route::venue::AssetsVenueItem> =
Map::new("venues");

pub type VenuePairId = (u128, u128);
// pub type VenueMultiMap<'a> =
// IndexedMap<'a, (VenuePairId, u128), cvm_route::venue::AssetsVenueItem, VenueIndexes<'a>>;

pub type VenueMultiMap<'a> =
IndexedMap<'a, (VenuePairId, u128), cvm_route::venue::AssetsVenueItem, VenueIndexes<'a>>;
// pub struct VenueIndexes<'a> {
// pub pair_first:
// MultiIndex<'a, VenuePairId, cvm_route::venue::AssetsVenueItem, (VenuePairId, u128)>,
// }

pub struct VenueIndexes<'a> {
pub pair_first:
MultiIndex<'a, VenuePairId, cvm_route::venue::AssetsVenueItem, (VenuePairId, u128)>,
}
// impl<'a> cw_storage_plus::IndexList<cvm_route::venue::AssetsVenueItem> for VenueIndexes<'a> {
// fn get_indexes(
// &'_ self,
// ) -> Box<
// dyn Iterator<Item = &'_ dyn cw_storage_plus::Index<cvm_route::venue::AssetsVenueItem>> + '_,
// > {
// let v: Vec<&dyn cw_storage_plus::Index<cvm_route::venue::AssetsVenueItem>> =
// vec![&self.pair_first];
// Box::new(v.into_iter())
// }
// }

impl<'a> cw_storage_plus::IndexList<cvm_route::venue::AssetsVenueItem> for VenueIndexes<'a> {
fn get_indexes(
&'_ self,
) -> Box<
dyn Iterator<Item = &'_ dyn cw_storage_plus::Index<cvm_route::venue::AssetsVenueItem>> + '_,
> {
let v: Vec<&dyn cw_storage_plus::Index<cvm_route::venue::AssetsVenueItem>> =
vec![&self.pair_first];
Box::new(v.into_iter())
}
}

pub const fn venues<'a>() -> VenueMultiMap<'a> {
let indexes = VenueIndexes {
pair_first: MultiIndex::new(
|_pk: &[u8], d: &cvm_route::venue::AssetsVenueItem| {
(d.from_asset_id.into(), d.to_asset_id.into())
},
"exchange_id_pair",
"pair",
),
};
IndexedMap::new("venues", indexes)
}
// pub const fn venues<'a>() -> VenueMultiMap<'a> {
// let pk_name = "venues";
// let indexes = VenueIndexes {
// pair_first: MultiIndex::new(
// |_pk: &[u8], d: &cvm_route::venue::AssetsVenueItem| {
// (d.from_asset_id.into(), d.to_asset_id.into())
// },
// "pk_name",
// "pair",
// ),
// };
// IndexedMap::new("venues", indexes)
// }

pub const EXCHANGE_VENUE: IndexedMap<
(VenuePairId, u128),
cvm_route::venue::AssetsVenueItem,
VenueIndexes,
> = venues();
// pub const EXCHANGE_VENUE: IndexedMap<
// (VenuePairId, u128),
// cvm_route::venue::AssetsVenueItem,
// VenueIndexes,
// > = venues();

pub(crate) fn force_assets_venue(
_: auth::Admin,
Expand All @@ -94,10 +96,7 @@ pub(crate) fn force_assets_venue(
cvm_route::venue::VenueId::Exchange(exchange_id) => {
EXCHANGE_VENUE.save(
deps.storage,
(
(msg.from_asset_id.0 .0, msg.to_asset_id.0 .0),
exchange_id.0,
),
(msg.from_asset_id.0 .0, msg.to_asset_id.0 .0, exchange_id.0),
&msg,
)?;
Ok(BatchResponse::new().add_event(
Expand Down
6 changes: 6 additions & 0 deletions crates/cvm-runtime/src/outpost/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,12 @@ pub enum QueryMsg {
returns(GetConfigResponse)
)]
GetConfig {},

#[cfg_attr(
feature = "json-schema", // all(feature = "json-schema", not(target_arch = "wasm32")),
returns(Vec<AssetsVenueItem>)
)]
GetAllAssetVenues {},
// /// So given program, contract returns route which will follow
// #[cfg_attr(
// feature = "json-schema", // all(feature = "json-schema", not(target_arch = "wasm32")),
Expand Down
5 changes: 3 additions & 2 deletions mantis/node/tests/cvms.rs
Original file line number Diff line number Diff line change
Expand Up @@ -309,11 +309,12 @@ async fn cvm_devnet_case() {
x.height += 2;
});

let query = cvm_runtime::outpost::QueryMsg::GetAllAssetIds {};
let cvm_glt: Vec<AssetItem> = centauri
let query = cvm_runtime::outpost::QueryMsg::GetConfig {};
let cvm_glt: CvmGlt = centauri
.wrap()
.query_wasm_smart(cw_cvm_outpost_contract, &query)
.unwrap();

centauri
.execute_contract(
sender.clone(),
Expand Down

0 comments on commit a8bc3fe

Please sign in to comment.