Skip to content

Commit

Permalink
fixed storage for venue info
Browse files Browse the repository at this point in the history
  • Loading branch information
dzmitry-lahoda committed Apr 8, 2024
1 parent e947cf9 commit 4cb5898
Show file tree
Hide file tree
Showing 12 changed files with 82 additions and 26 deletions.
1 change: 0 additions & 1 deletion contracts/cosmwasm/outpost/src/assets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use crate::{
batch::BatchResponse,
error::{ContractError, Result},
events::make_event,
prelude::*,
state::{
self,
assets::{ASSETS, LOCAL_ASSETS},
Expand Down
4 changes: 2 additions & 2 deletions contracts/cosmwasm/outpost/src/contract/execute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ fn handle_config_msg(
}
Ok(aggregated)
}
ConfigSubMsg::ForceAssetsVenue(msg) => state::venues::force_assets_venue(auth, deps, msg),
ConfigSubMsg::ForceAssetsVenue(msg) => state::exchange::force_assets_venue(auth, deps, msg),
}
}

Expand Down Expand Up @@ -158,7 +158,7 @@ fn transfer_from_user(
*asset_id, denom,
))?;
if *program_amount != u128::from(*host_amount) {
return Err(ContractError::ProgramAmountNotEqualToHostAmount)?;
Err(ContractError::ProgramAmountNotEqualToHostAmount)?;
}
}
cvm_route::asset::AssetReference::Cw20 { contract } => {
Expand Down
2 changes: 1 addition & 1 deletion contracts/cosmwasm/outpost/src/contract/sudo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::{error::ContractError, state};
use cosmwasm_std::{entry_point, wasm_execute, Coin, DepsMut, Env, Event, Response};

use ibc_apps_more::types::hook::{IBCLifecycleComplete, SudoMsg};
use ibc_core_host_types::identifiers::ChannelId;


#[cfg_attr(not(feature = "library"), entry_point)]
pub fn sudo(deps: DepsMut, env: Env, msg: SudoMsg) -> crate::error::Result {
Expand Down
2 changes: 1 addition & 1 deletion contracts/cosmwasm/outpost/src/error.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use cosmwasm_std::{IbcOrder, Response, StdError};
use cvm_runtime::{AssetId, NetworkId};
use ibc_core_host_types::{error::IdentifierError, identifiers::ChannelId};
use ibc_core_host_types::{error::IdentifierError};
use thiserror::Error;

pub type Result<T = Response, E = ContractError> = core::result::Result<T, E>;
Expand Down
2 changes: 1 addition & 1 deletion contracts/cosmwasm/outpost/src/executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ pub(crate) fn force_instantiate(
let config = load_this(deps.storage)?;
let executor_code_id = match config.outpost.expect("expected setup") {
OutpostId::CosmWasm {
executor_code_id: executor_code_id,
executor_code_id,
..
} => executor_code_id,
//OutpostId::Evm { .. } => Err(ContractError::RuntimeUnsupportedOnNetwork)?,
Expand Down
2 changes: 1 addition & 1 deletion contracts/cosmwasm/outpost/src/prelude.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ pub use cw_storage_plus::Map;
pub use ibc_core_host_types::identifiers::ChannelId;
pub use serde::{Deserialize, Serialize};

pub use cvm::*;

72 changes: 70 additions & 2 deletions contracts/cosmwasm/outpost/src/state/exchange.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use cosmwasm_std::{Deps, DepsMut, Order, StdResult};
use cvm_route::exchange::ExchangeItem;

use cvm_route::{exchange::ExchangeItem, venue::AssetsVenueItem};
use cvm_runtime::exchange::ExchangeId;
use cw_storage_plus::Map;
use cw_storage_plus::{IndexedMap, Map, MultiIndex};

use crate::{
auth,
Expand All @@ -23,6 +24,14 @@ pub fn get_all_exchanges(deps: Deps) -> StdResult<Vec<ExchangeItem>> {
.collect()
}

pub fn get_all_exchange_venues(deps: Deps) -> StdResult<Vec<AssetsVenueItem>> {
EXCHANGE_VENUE
.range(deps.storage, None, None, Order::Ascending)
.map(|x| x.map(|(_, x)| x))
.collect()
}


pub(crate) fn force_exchange(
_: auth::Admin,
deps: DepsMut,
Expand All @@ -35,3 +44,62 @@ pub(crate) fn force_exchange(
}

pub(crate) const EXCHANGE: Map<u128, cvm_route::exchange::ExchangeItem> = Map::new("exchange");

pub type VenuePairId = (u128, u128);

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,)>,
}

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 EXCHANGE_VENUE: IndexedMap<(VenuePairId, u128), cvm_route::venue::AssetsVenueItem, VenueIndexes> =
venues();

pub(crate) fn force_assets_venue(
_: auth::Admin,
deps: DepsMut,
msg: AssetsVenueItem,
) -> Result<BatchResponse> {
match msg.venue_id {
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,
)?;
Ok(BatchResponse::new().add_event(
make_event("venue.forced")
.add_attribute("from_asset_id", msg.from_asset_id.to_string())
.add_attribute("to_asset_id", msg.to_asset_id.to_string()),
))
}
cvm_route::venue::VenueId::Transfer => panic!("no special handling for transfer"),
}
}
9 changes: 4 additions & 5 deletions contracts/cosmwasm/outpost/src/state/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,14 @@ pub mod executors;
pub mod ics27;
pub mod network;
pub mod tracking;
pub mod venues;
use crate::{error::ContractError, prelude::*};

use cosmwasm_std::{StdResult, Storage};
use cvm_route::transport::OtherNetworkItem;
use cvm_runtime::outpost::{GetConfigResponse, NetworkItem};

use cvm_runtime::outpost::{GetConfigResponse};
use cw_storage_plus::Item;

use cvm_runtime::NetworkId;


const CONFIG: Item<HereItem> = Item::new("this");

Expand All @@ -33,6 +32,6 @@ pub(crate) fn get_config(deps: cosmwasm_std::Deps<'_>) -> Result<GetConfigRespon
exchanges,
networks: network::get_all_networks(deps)?,
network_assets: assets::get_all_network_assets(deps)?,
asset_venue_items: vec![],
asset_venue_items: exchange::get_all_exchange_venues(deps)?,
})
}
8 changes: 0 additions & 8 deletions contracts/cosmwasm/outpost/src/state/venues.rs

This file was deleted.

3 changes: 0 additions & 3 deletions crates/cvm-route/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,3 @@ pub mod exchange;
mod prelude;
pub mod transport;
pub mod venue;
use cvm::asset::*;
use cvm::exchange::*;
use cvm::network::*;
2 changes: 1 addition & 1 deletion crates/cvm/src/asset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use cw_storage_plus::{Key, Prefixer};
use crate::shared::Displayed;
use core::ops::Add;
use cosmwasm_std::{Uint128, Uint256};
use num::{One, Zero};
use num::Zero;
use serde::{Deserialize, Serialize};

/// Newtype for CVM assets ID. Must be unique for each asset and must never change.
Expand Down
1 change: 1 addition & 0 deletions crates/mantis-cw/src/ordered_tuple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use cosmwasm_std::{StdError, StdResult};
use cw_storage_plus::{Key, KeyDeserialize, Prefixer, PrimaryKey};
pub use tuples::*;

/// Ordered tuple can be considered as edge of undirected graph
#[cw_serde]
#[derive(Eq)]
pub struct OrderedTuple2<T> {
Expand Down

0 comments on commit 4cb5898

Please sign in to comment.