Skip to content

Commit

Permalink
chore: Merge branch 'release/v2_contracts' into feat/bonding-manager-…
Browse files Browse the repository at this point in the history
…whale-lair
  • Loading branch information
0xFable committed May 2, 2024
2 parents e21b2b5 + d196218 commit 4423ed0
Show file tree
Hide file tree
Showing 19 changed files with 1,657 additions and 369 deletions.
2 changes: 1 addition & 1 deletion .github/CODEOWNERS
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# @kerber0x is the repository administrator who ensures compliance with the White Whale Repository Guideline.
# The remaining code owners help maintain the repository.
* @kerber0x @0xFable @kaimen-sano @Sen-Com
* @kerber0x @0xFable @kaimen-sano @Sen-Com @nseguias

66 changes: 65 additions & 1 deletion contracts/liquidity_hub/pool-manager/schema/pool-manager.json
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,16 @@
"null"
]
},
"max_spread": {
"anyOf": [
{
"$ref": "#/definitions/Decimal"
},
{
"type": "null"
}
]
},
"pair_identifier": {
"type": "string"
},
Expand Down Expand Up @@ -836,6 +846,7 @@
"additionalProperties": false
},
{
"description": "Simulates swap operations.",
"type": "object",
"required": [
"simulate_swap_operations"
Expand Down Expand Up @@ -863,6 +874,35 @@
},
"additionalProperties": false
},
{
"description": "Simulates a reverse swap operations, i.e. given the ask asset, how much of the offer asset is needed to perform the swap.",
"type": "object",
"required": [
"reverse_simulate_swap_operations"
],
"properties": {
"reverse_simulate_swap_operations": {
"type": "object",
"required": [
"ask_amount",
"operations"
],
"properties": {
"ask_amount": {
"$ref": "#/definitions/Uint128"
},
"operations": {
"type": "array",
"items": {
"$ref": "#/definitions/SwapOperation"
}
}
},
"additionalProperties": false
}
},
"additionalProperties": false
},
{
"type": "object",
"required": [
Expand Down Expand Up @@ -1211,11 +1251,15 @@
"title": "PairInfoResponse",
"type": "object",
"required": [
"pair_info"
"pair_info",
"total_share"
],
"properties": {
"pair_info": {
"$ref": "#/definitions/PairInfo"
},
"total_share": {
"$ref": "#/definitions/Coin"
}
},
"additionalProperties": false,
Expand Down Expand Up @@ -1378,6 +1422,26 @@
}
}
},
"reverse_simulate_swap_operations": {
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "ReverseSimulateSwapOperationsResponse",
"type": "object",
"required": [
"amount"
],
"properties": {
"amount": {
"$ref": "#/definitions/Uint128"
}
},
"additionalProperties": false,
"definitions": {
"Uint128": {
"description": "A thin wrapper around u128 that is using strings for JSON encoding/decoding, such that the full u128 range can be used for clients that convert JSON numbers to floats, like JavaScript and jq.\n\n# Examples\n\nUse `from` to create instances of this and `u128` to get the value out:\n\n``` # use cosmwasm_std::Uint128; let a = Uint128::from(123u128); assert_eq!(a.u128(), 123);\n\nlet b = Uint128::from(42u64); assert_eq!(b.u128(), 42);\n\nlet c = Uint128::from(70u32); assert_eq!(c.u128(), 70); ```",
"type": "string"
}
}
},
"reverse_simulation": {
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "ReverseSimulationResponse",
Expand Down
10 changes: 10 additions & 0 deletions contracts/liquidity_hub/pool-manager/schema/raw/execute.json
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,16 @@
"null"
]
},
"max_spread": {
"anyOf": [
{
"$ref": "#/definitions/Decimal"
},
{
"type": "null"
}
]
},
"pair_identifier": {
"type": "string"
},
Expand Down
30 changes: 30 additions & 0 deletions contracts/liquidity_hub/pool-manager/schema/raw/query.json
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@
"additionalProperties": false
},
{
"description": "Simulates swap operations.",
"type": "object",
"required": [
"simulate_swap_operations"
Expand Down Expand Up @@ -166,6 +167,35 @@
},
"additionalProperties": false
},
{
"description": "Simulates a reverse swap operations, i.e. given the ask asset, how much of the offer asset is needed to perform the swap.",
"type": "object",
"required": [
"reverse_simulate_swap_operations"
],
"properties": {
"reverse_simulate_swap_operations": {
"type": "object",
"required": [
"ask_amount",
"operations"
],
"properties": {
"ask_amount": {
"$ref": "#/definitions/Uint128"
},
"operations": {
"type": "array",
"items": {
"$ref": "#/definitions/SwapOperation"
}
}
},
"additionalProperties": false
}
},
"additionalProperties": false
},
{
"type": "object",
"required": [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,15 @@
"title": "PairInfoResponse",
"type": "object",
"required": [
"pair_info"
"pair_info",
"total_share"
],
"properties": {
"pair_info": {
"$ref": "#/definitions/PairInfo"
},
"total_share": {
"$ref": "#/definitions/Coin"
}
},
"additionalProperties": false,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "ReverseSimulateSwapOperationsResponse",
"type": "object",
"required": [
"amount"
],
"properties": {
"amount": {
"$ref": "#/definitions/Uint128"
}
},
"additionalProperties": false,
"definitions": {
"Uint128": {
"description": "A thin wrapper around u128 that is using strings for JSON encoding/decoding, such that the full u128 range can be used for clients that convert JSON numbers to floats, like JavaScript and jq.\n\n# Examples\n\nUse `from` to create instances of this and `u128` to get the value out:\n\n``` # use cosmwasm_std::Uint128; let a = Uint128::from(123u128); assert_eq!(a.u128(), 123);\n\nlet b = Uint128::from(42u64); assert_eq!(b.u128(), 42);\n\nlet c = Uint128::from(70u32); assert_eq!(c.u128(), 70); ```",
"type": "string"
}
}
}
70 changes: 57 additions & 13 deletions contracts/liquidity_hub/pool-manager/src/contract.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,29 @@
use crate::error::ContractError;
use crate::helpers::simulate_swap_operations;
use crate::queries::{get_swap_route, get_swap_route_creator, get_swap_routes};
use crate::helpers::{
reverse_simulate_swap_operations, simulate_swap_operations, validate_asset_balance,
};
use crate::queries::{get_pair, get_swap_route, get_swap_route_creator, get_swap_routes};
use crate::router::commands::{add_swap_routes, remove_swap_routes};
use crate::state::{Config, MANAGER_CONFIG, PAIRS, PAIR_COUNTER};
use crate::state::{
Config, SingleSideLiquidityProvisionBuffer, MANAGER_CONFIG, PAIR_COUNTER,
TMP_SINGLE_SIDE_LIQUIDITY_PROVISION,
};
use crate::{liquidity, manager, queries, router, swap};
#[cfg(not(feature = "library"))]
use cosmwasm_std::{
entry_point, to_json_binary, Addr, Api, Binary, Deps, DepsMut, Env, MessageInfo, Response,
};
use cosmwasm_std::{wasm_execute, Reply, StdError};
use cw2::set_contract_version;
use semver::Version;
use white_whale_std::pool_manager::{
ExecuteMsg, FeatureToggle, InstantiateMsg, MigrateMsg, PairInfoResponse, QueryMsg,
ExecuteMsg, FeatureToggle, InstantiateMsg, MigrateMsg, QueryMsg,
};

// version info for migration info
const CONTRACT_NAME: &str = "crates.io:ww-pool-manager";
const CONTRACT_VERSION: &str = env!("CARGO_PKG_VERSION");
pub const SINGLE_SIDE_LIQUIDITY_PROVISION_REPLY_ID: u64 = 1;

#[cfg_attr(not(feature = "library"), entry_point)]
pub fn instantiate(
Expand Down Expand Up @@ -45,6 +52,41 @@ pub fn instantiate(
Ok(Response::default())
}

#[cfg_attr(not(feature = "library"), entry_point)]
pub fn reply(deps: DepsMut, env: Env, msg: Reply) -> Result<Response, ContractError> {
match msg.id {
SINGLE_SIDE_LIQUIDITY_PROVISION_REPLY_ID => {
let SingleSideLiquidityProvisionBuffer {
receiver,
expected_offer_asset_balance_in_contract,
expected_ask_asset_balance_in_contract,
offer_asset_half,
expected_ask_asset,
liquidity_provision_data,
} = TMP_SINGLE_SIDE_LIQUIDITY_PROVISION.load(deps.storage)?;

validate_asset_balance(&deps, &env, &expected_offer_asset_balance_in_contract)?;
validate_asset_balance(&deps, &env, &expected_ask_asset_balance_in_contract)?;

TMP_SINGLE_SIDE_LIQUIDITY_PROVISION.remove(deps.storage);

Ok(Response::default().add_message(wasm_execute(
env.contract.address.into_string(),
&ExecuteMsg::ProvideLiquidity {
slippage_tolerance: liquidity_provision_data.slippage_tolerance,
max_spread: liquidity_provision_data.max_spread,
receiver: Some(receiver),
pair_identifier: liquidity_provision_data.pair_identifier,
unlocking_duration: liquidity_provision_data.unlocking_duration,
lock_position_identifier: liquidity_provision_data.lock_position_identifier,
},
vec![offer_asset_half, expected_ask_asset],
)?))
}
_ => Err(StdError::generic_err("reply id not found").into()),
}
}

#[cfg_attr(not(feature = "library"), entry_point)]
pub fn execute(
deps: DepsMut,
Expand All @@ -70,6 +112,7 @@ pub fn execute(
pair_identifier,
),
ExecuteMsg::ProvideLiquidity {
max_spread,
slippage_tolerance,
receiver,
pair_identifier,
Expand All @@ -80,6 +123,7 @@ pub fn execute(
env,
info,
slippage_tolerance,
max_spread,
receiver,
pair_identifier,
unlocking_duration,
Expand Down Expand Up @@ -228,12 +272,12 @@ pub fn query(deps: Deps, env: Env, msg: QueryMsg) -> Result<Binary, ContractErro
offer_amount,
operations,
)?)?),
// QueryMsg::ReverseSimulateSwapOperations {
// ask_amount,
// operations,
// } => Ok(to_binary(&queries::reverse_simulate_swap_operations(
// deps, env, ask_amount, operations,
// )?)?),
QueryMsg::ReverseSimulateSwapOperations {
ask_amount,
operations,
} => Ok(to_json_binary(&reverse_simulate_swap_operations(
deps, ask_amount, operations,
)?)?),
QueryMsg::SwapRoute {
offer_asset_denom,
ask_asset_denom,
Expand All @@ -244,9 +288,9 @@ pub fn query(deps: Deps, env: Env, msg: QueryMsg) -> Result<Binary, ContractErro
)?)?),
QueryMsg::SwapRoutes {} => Ok(to_json_binary(&get_swap_routes(deps)?)?),
QueryMsg::Ownership {} => Ok(to_json_binary(&cw_ownable::get_ownership(deps.storage)?)?),
QueryMsg::Pair { pair_identifier } => Ok(to_json_binary(&PairInfoResponse {
pair_info: PAIRS.load(deps.storage, &pair_identifier)?,
})?),
QueryMsg::Pair { pair_identifier } => {
Ok(to_json_binary(&get_pair(deps, pair_identifier)?)?)
}
QueryMsg::SwapRouteCreator {
offer_asset_denom,
ask_asset_denom,
Expand Down
20 changes: 18 additions & 2 deletions contracts/liquidity_hub/pool-manager/src/error.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use crate::liquidity::commands::MAX_ASSETS_PER_POOL;
use cosmwasm_std::{
CheckedFromRatioError, CheckedMultiplyRatioError, ConversionOverflowError, DivideByZeroError,
Instantiate2AddressError, OverflowError, StdError, Uint128,
CheckedFromRatioError, CheckedMultiplyFractionError, CheckedMultiplyRatioError,
ConversionOverflowError, DivideByZeroError, Instantiate2AddressError, OverflowError, StdError,
Uint128,
};
use cw_ownable::OwnershipError;
use cw_utils::PaymentError;
Expand Down Expand Up @@ -65,6 +66,15 @@ pub enum ContractError {
#[error("{asset} is invalid")]
InvalidAsset { asset: String },

#[error("Trying to provide liquidity without any assets")]
EmptyAssets,

#[error("Invalid single side liquidity provision swap, expected {expected} got {actual}")]
InvalidSingleSideLiquidityProvisionSwap { expected: Uint128, actual: Uint128 },

#[error("Cannot provide single-side liquidity when the pool is empty")]
EmptyPoolForSingleSideLiquidityProvision,

#[error("Pair does not exist")]
UnExistingPair {},

Expand All @@ -80,6 +90,9 @@ pub enum ContractError {
#[error("Failed to compute the LP share with the given deposit")]
LiquidityShareComputation {},

#[error("The amount of LP shares to withdraw is invalid")]
InvalidLpShare,

#[error("Spread limit exceeded")]
MaxSpreadAssertion {},

Expand Down Expand Up @@ -116,6 +129,9 @@ pub enum ContractError {
#[error(transparent)]
CheckedMultiplyRatioError(#[from] CheckedMultiplyRatioError),

#[error(transparent)]
CheckedMultiplyFractionError(#[from] CheckedMultiplyFractionError),

#[error(transparent)]
CheckedFromRatioError(#[from] CheckedFromRatioError),

Expand Down
Loading

0 comments on commit 4423ed0

Please sign in to comment.