From 4cb589843b43a73080c8c4681adc2d07bc6d111f Mon Sep 17 00:00:00 2001 From: dzmitry-lahoda Date: Mon, 8 Apr 2024 23:46:23 +0100 Subject: [PATCH] fixed storage for venue info --- contracts/cosmwasm/outpost/src/assets.rs | 1 - .../cosmwasm/outpost/src/contract/execute.rs | 4 +- .../cosmwasm/outpost/src/contract/sudo.rs | 2 +- contracts/cosmwasm/outpost/src/error.rs | 2 +- contracts/cosmwasm/outpost/src/executor.rs | 2 +- contracts/cosmwasm/outpost/src/prelude.rs | 2 +- .../cosmwasm/outpost/src/state/exchange.rs | 72 ++++++++++++++++++- contracts/cosmwasm/outpost/src/state/mod.rs | 9 ++- .../cosmwasm/outpost/src/state/venues.rs | 8 --- crates/cvm-route/src/lib.rs | 3 - crates/cvm/src/asset.rs | 2 +- crates/mantis-cw/src/ordered_tuple.rs | 1 + 12 files changed, 82 insertions(+), 26 deletions(-) delete mode 100644 contracts/cosmwasm/outpost/src/state/venues.rs diff --git a/contracts/cosmwasm/outpost/src/assets.rs b/contracts/cosmwasm/outpost/src/assets.rs index 12b1b392..b990a9f6 100644 --- a/contracts/cosmwasm/outpost/src/assets.rs +++ b/contracts/cosmwasm/outpost/src/assets.rs @@ -3,7 +3,6 @@ use crate::{ batch::BatchResponse, error::{ContractError, Result}, events::make_event, - prelude::*, state::{ self, assets::{ASSETS, LOCAL_ASSETS}, diff --git a/contracts/cosmwasm/outpost/src/contract/execute.rs b/contracts/cosmwasm/outpost/src/contract/execute.rs index 2a96a62c..d05e709a 100644 --- a/contracts/cosmwasm/outpost/src/contract/execute.rs +++ b/contracts/cosmwasm/outpost/src/contract/execute.rs @@ -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), } } @@ -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 } => { diff --git a/contracts/cosmwasm/outpost/src/contract/sudo.rs b/contracts/cosmwasm/outpost/src/contract/sudo.rs index 37a35472..3852e28b 100644 --- a/contracts/cosmwasm/outpost/src/contract/sudo.rs +++ b/contracts/cosmwasm/outpost/src/contract/sudo.rs @@ -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 { diff --git a/contracts/cosmwasm/outpost/src/error.rs b/contracts/cosmwasm/outpost/src/error.rs index 037f0056..26ccc84c 100644 --- a/contracts/cosmwasm/outpost/src/error.rs +++ b/contracts/cosmwasm/outpost/src/error.rs @@ -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 = core::result::Result; diff --git a/contracts/cosmwasm/outpost/src/executor.rs b/contracts/cosmwasm/outpost/src/executor.rs index c1eb0ba4..dac0ca65 100644 --- a/contracts/cosmwasm/outpost/src/executor.rs +++ b/contracts/cosmwasm/outpost/src/executor.rs @@ -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)?, diff --git a/contracts/cosmwasm/outpost/src/prelude.rs b/contracts/cosmwasm/outpost/src/prelude.rs index 9fecdccc..13a9718a 100644 --- a/contracts/cosmwasm/outpost/src/prelude.rs +++ b/contracts/cosmwasm/outpost/src/prelude.rs @@ -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::*; + diff --git a/contracts/cosmwasm/outpost/src/state/exchange.rs b/contracts/cosmwasm/outpost/src/state/exchange.rs index b27567f0..42511d1e 100644 --- a/contracts/cosmwasm/outpost/src/state/exchange.rs +++ b/contracts/cosmwasm/outpost/src/state/exchange.rs @@ -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, @@ -23,6 +24,14 @@ pub fn get_all_exchanges(deps: Deps) -> StdResult> { .collect() } +pub fn get_all_exchange_venues(deps: Deps) -> StdResult> { + 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, @@ -35,3 +44,62 @@ pub(crate) fn force_exchange( } pub(crate) const EXCHANGE: Map = 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 for VenueIndexes<'a> { + fn get_indexes( + &'_ self, + ) -> Box< + dyn Iterator> + '_, + > { + let v: Vec<&dyn cw_storage_plus::Index> = + 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 { + 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"), + } +} diff --git a/contracts/cosmwasm/outpost/src/state/mod.rs b/contracts/cosmwasm/outpost/src/state/mod.rs index 94a64757..dd03ff28 100644 --- a/contracts/cosmwasm/outpost/src/state/mod.rs +++ b/contracts/cosmwasm/outpost/src/state/mod.rs @@ -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 = Item::new("this"); @@ -33,6 +32,6 @@ pub(crate) fn get_config(deps: cosmwasm_std::Deps<'_>) -> Result Result { - todo!() -} \ No newline at end of file diff --git a/crates/cvm-route/src/lib.rs b/crates/cvm-route/src/lib.rs index 27180fb1..91f29746 100644 --- a/crates/cvm-route/src/lib.rs +++ b/crates/cvm-route/src/lib.rs @@ -7,6 +7,3 @@ pub mod exchange; mod prelude; pub mod transport; pub mod venue; -use cvm::asset::*; -use cvm::exchange::*; -use cvm::network::*; diff --git a/crates/cvm/src/asset.rs b/crates/cvm/src/asset.rs index b2172017..668c56db 100644 --- a/crates/cvm/src/asset.rs +++ b/crates/cvm/src/asset.rs @@ -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. diff --git a/crates/mantis-cw/src/ordered_tuple.rs b/crates/mantis-cw/src/ordered_tuple.rs index e32f836c..d8ff9c46 100644 --- a/crates/mantis-cw/src/ordered_tuple.rs +++ b/crates/mantis-cw/src/ordered_tuple.rs @@ -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 {