Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
uint committed Nov 8, 2023
1 parent ac82676 commit 55a83a0
Show file tree
Hide file tree
Showing 8 changed files with 24 additions and 45 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ cw-storage-plus = "1.0"
cw-utils = "1.0"
cw-controllers = "1.0"
cw2 = "1.0"
osmosis-std = "0.20.1"
schemars = "0.8.11"
serde = { version = "1.0.152", default-features = false, features = ["derive"] }
thiserror = "1.0.38"
Expand Down
7 changes: 3 additions & 4 deletions contracts/consumer/remote-price-feed/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
[package]
name = "mesh-remote-price-feed"
description = "Returns a fixed price for assets to the converter contract - for tests or gov-defined prices"
description = "Returns exchange rates of assets synchronized with an Osmosis price provider"
version = { workspace = true }
edition = { workspace = true }
license = { workspace = true }
repository = { workspace = true }
license = { workspace = true }
repository = { workspace = true }

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[lib]
crate-type = ["cdylib", "rlib"]

Expand Down
16 changes: 9 additions & 7 deletions contracts/consumer/remote-price-feed/README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
# Simple Price Feed
# Remote Price Feed

This is the simplest usable implementation of the [price feed API](../../../packages/apis/src/price_feed_api.rs).
One address is set to be the owner of the price feed, and only the owner can update the price.
This implements the [price feed API](../../../packages/apis/src/price_feed_api.rs).

The main usage is for tests, where we can set arbitrary prices for assets with a simple call.
However, if we set the owner to the x/gov module address, this could be used for assets where no on-chain data
is available.
This contract provides exchange rate data by contacting an [Osmosis Price Provider](../osmosis-price-provider).

It is intended to be used as a reference implementation for other price feed implementations.
A single trading pair has to be configured on instantiation, along with the IBC endpoint. In case multiple trading pairs need to be synced, multiple contracts must be deployed.

For this contract to work correctly:

- An IBC connection to [Osmosis Price Provider](../osmosis-price-provider) must be opened.
- A`SudoMsg::EndBlock {}` must be sent to the contract regularly. This will allow the contract to request regular updates of locally stored data.
2 changes: 0 additions & 2 deletions contracts/consumer/remote-price-feed/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,6 @@ impl RemotePriceFeedContract {
}
}

/// Sets up the contract with an initial price.
/// If the owner is not set in the message, it defaults to info.sender.
#[msg(instantiate)]
pub fn instantiate(
&self,
Expand Down
4 changes: 2 additions & 2 deletions contracts/consumer/remote-price-feed/src/ibc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ use crate::msg::AuthorizedEndpoint;
use crate::state::PriceInfo;

/// This is the maximum version of the Mesh Security protocol that we support
const SUPPORTED_IBC_PROTOCOL_VERSION: &str = "0.11.0";
const SUPPORTED_IBC_PROTOCOL_VERSION: &str = "0.1.0";
/// This is the minimum version that we are compatible with
const MIN_IBC_PROTOCOL_VERSION: &str = "0.11.0";
const MIN_IBC_PROTOCOL_VERSION: &str = "0.1.0";

// IBC specific state
pub const AUTH_ENDPOINT: Item<AuthorizedEndpoint> = Item::new("auth_endpoint");
Expand Down
2 changes: 1 addition & 1 deletion contracts/osmosis-price-provider/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,4 @@ schemars = { workspace = true }
serde = { workspace = true }
sylvia = { workspace = true }
thiserror = { workspace = true }
osmosis-std = "0.20.1"
osmosis-std = { workspace = true }
8 changes: 1 addition & 7 deletions contracts/osmosis-price-provider/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,6 @@ pub enum ContractError {
#[error("You must start the channel handshake on this side, it doesn't support OpenTry")]
IbcOpenTryDisallowed,

#[error("The price provider contract does not accept acks")]
#[error("The price provider contract does not accept acks or timeouts")]
IbcPacketAckDisallowed,

#[error("A subscription for the provided denom does not exist")]
SubscriptionDoesNotExist,

#[error("A subscription already exists for the provided denom")]
SubscriptionAlreadyExists,
}
29 changes: 7 additions & 22 deletions contracts/osmosis-price-provider/src/ibc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,12 @@ use cosmwasm_std::entry_point;

use cosmwasm_std::{
from_slice, to_binary, DepsMut, Env, Ibc3ChannelOpenResponse, IbcBasicResponse,
IbcChannelCloseMsg, IbcChannelConnectMsg, IbcChannelOpenMsg, IbcChannelOpenResponse, IbcMsg,
IbcChannelCloseMsg, IbcChannelConnectMsg, IbcChannelOpenMsg, IbcChannelOpenResponse,
IbcPacketAckMsg, IbcPacketReceiveMsg, IbcPacketTimeoutMsg, IbcReceiveResponse, IbcTimeout,
StdError, Timestamp,
};

use mesh_apis::ibc::{
validate_channel_order, PriceFeedProviderAck, ProtocolVersion, RemotePriceFeedPacket,
};
use mesh_apis::ibc::{PriceFeedProviderAck, ProtocolVersion, RemotePriceFeedPacket};

use crate::{contract::OsmosisPriceProvider, error::ContractError};

Expand All @@ -20,10 +18,10 @@ const SUPPORTED_IBC_PROTOCOL_VERSION: &str = "0.1.0";
/// This is the minimum version that we are compatible with
const MIN_IBC_PROTOCOL_VERSION: &str = "0.1.0";

const TIMEOUT: u64 = 60 * 60;
const TIMEOUT_IN_SECS: u64 = 600;

pub fn packet_timeout(now: &Timestamp) -> IbcTimeout {
let timeout = now.plus_seconds(TIMEOUT);
let timeout = now.plus_seconds(TIMEOUT_IN_SECS);
IbcTimeout::with_timestamp(timeout)
}

Expand All @@ -40,9 +38,6 @@ pub fn ibc_channel_open(
IbcChannelOpenMsg::OpenTry { .. } => return Err(ContractError::IbcOpenTryDisallowed),
};

// verify the ordering is correct
validate_channel_order(&channel.order)?;

// Check the version. If provided, ensure it is compatible.
// If not provided, use our most recent version.
let version = if channel.version.is_empty() {
Expand Down Expand Up @@ -138,9 +133,6 @@ pub fn ibc_packet_receive(
}

#[cfg_attr(not(feature = "library"), entry_point)]
/// We get ACKs on sync state without much to do.
/// If it succeeded, take no action. If it errored, we can't do anything else and let it go.
/// We just log the error cases so they can be detected.
pub fn ibc_packet_ack(
_deps: DepsMut,
_env: Env,
Expand All @@ -150,17 +142,10 @@ pub fn ibc_packet_ack(
}

#[cfg_attr(not(feature = "library"), entry_point)]
/// The most we can do here is retry the packet, hoping it will eventually arrive.
pub fn ibc_packet_timeout(
_deps: DepsMut,
env: Env,
msg: IbcPacketTimeoutMsg,
_env: Env,
_msg: IbcPacketTimeoutMsg,
) -> Result<IbcBasicResponse, ContractError> {
// Play it again, Sam.
let msg = IbcMsg::SendPacket {
channel_id: msg.packet.src.channel_id,
data: msg.packet.data,
timeout: packet_timeout(&env.block.time),
};
Ok(IbcBasicResponse::new().add_message(msg))
Err(ContractError::IbcPacketAckDisallowed)
}

0 comments on commit 55a83a0

Please sign in to comment.