Skip to content

Commit

Permalink
move lb-libraries out of shade-protocol
Browse files Browse the repository at this point in the history
  • Loading branch information
kent-3 committed May 26, 2024
1 parent 898165c commit 34fd9b8
Show file tree
Hide file tree
Showing 69 changed files with 579 additions and 1,049 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ members = [
"packages/multi_test",
"packages/multi_derive",
"packages/contract_derive",
"packages/lb_libraries",

# Network setups
#"contracts/airdrop",
Expand Down
15 changes: 5 additions & 10 deletions contracts/liquidity_book/lb_factory/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,15 @@
name = "lb_factory"
version = "0.1.0"
edition = "2021"
exclude = [
# Those files are contract-optimizer artifacts. You might want to commit them for convenience but they should not be part of the source code publication.
"contract.wasm",
"hash.txt",
]
exclude = ["contract.wasm", "hash.txt"]

[lib]
crate-type = ["cdylib", "rlib"]


[[example]]
name = "schema"
path = "schema/schema.rs" # Adjust the path according to your project structure
path = "schema/schema.rs"


[features]
Expand All @@ -25,13 +21,12 @@ schema = []
backtraces = ["cosmwasm-std/backtraces"]




[dependencies]
shade-protocol = { version = "0.1.0", path = "../../../packages/shade_protocol", features = ["liquidity_book_impl"] }
shade-protocol = { path = "../../../packages/shade_protocol" }
lb-libraries = { path = "../../../packages/lb_libraries" }
schemars = "0.8.16"
serde = { version = "1.0" }
cosmwasm-schema = "1.5"
cosmwasm-schema = "2.0.3"
thiserror = { version = "1.0" }
ethnum = { version = "1.5" }
serde_json = "1"
Expand Down
69 changes: 28 additions & 41 deletions contracts/liquidity_book/lb_factory/src/contract.rs
Original file line number Diff line number Diff line change
@@ -1,37 +1,19 @@
use std::collections::HashSet;

use crate::{
prelude::*,
state::*,
types::{LBPair, LBPairInformation, NextPairKey},
};

use lb_libraries::{
math::encoded_sample::EncodedSample,
pair_parameter_helper::PairParameters,
price_helper::PriceHelper,
types::{Bytes32, ContractImplementation, StaticFeeParameters},
};
use shade_protocol::{
admin::helpers::{validate_admin, AdminPermissions},
c_std::{
shd_entry_point,
to_binary,
Addr,
Binary,
ContractInfo,
CosmosMsg,
Deps,
DepsMut,
Env,
MessageInfo,
Reply,
Response,
StdError,
StdResult,
SubMsg,
SubMsgResult,
WasmMsg,
},
lb_libraries::{
math::encoded_sample::EncodedSample,
pair_parameter_helper::PairParameters,
price_helper::PriceHelper,
types::{Bytes32, ContractInstantiationInfo, StaticFeeParameters},
shd_entry_point, to_binary, Addr, Binary, ContractInfo, CosmosMsg, Deps, DepsMut, Env,
MessageInfo, Reply, Response, StdError, StdResult, SubMsg, SubMsgResult, WasmMsg,
},
liquidity_book::{
lb_factory::*,
Expand All @@ -43,6 +25,7 @@ use shade_protocol::{
swap::core::TokenType,
utils::callback::ExecuteCallback,
};
use std::collections::HashSet;

pub static _OFFSET_IS_PRESET_OPEN: u8 = 255;
pub static _MIN_BIN_STEP: u8 = 1; // 0.001%
Expand All @@ -66,10 +49,10 @@ pub fn instantiate(
},
owner: msg.owner.unwrap_or_else(|| info.sender.clone()),
fee_recipient: msg.fee_recipient,
lb_pair_implementation: ContractInstantiationInfo::default(),
lb_token_implementation: ContractInstantiationInfo::default(),
lb_pair_implementation: ContractImplementation::default(),
lb_token_implementation: ContractImplementation::default(),
admin_auth: msg.admin_auth.into_valid(deps.api)?,
staking_contract_implementation: ContractInstantiationInfo::default(),
staking_contract_implementation: ContractImplementation::default(),
recover_staking_funds_receiver: msg.recover_staking_funds_receiver,
query_auth: msg.query_auth.into_valid(deps.api)?,
max_bins_per_swap: msg.max_bins_per_swap,
Expand Down Expand Up @@ -214,7 +197,7 @@ fn try_set_lb_pair_implementation(
deps: DepsMut,
_env: Env,
info: MessageInfo,
new_lb_pair_implementation: ContractInstantiationInfo,
new_lb_pair_implementation: ContractImplementation,
) -> Result<Response> {
let config = STATE.load(deps.storage)?;
validate_admin(
Expand Down Expand Up @@ -248,7 +231,7 @@ fn try_set_lb_token_implementation(
deps: DepsMut,
_env: Env,
info: MessageInfo,
new_lb_token_implementation: ContractInstantiationInfo,
new_lb_token_implementation: ContractImplementation,
) -> Result<Response> {
let config = STATE.load(deps.storage)?;
validate_admin(
Expand Down Expand Up @@ -282,7 +265,7 @@ fn try_set_staking_contract_implementation(
deps: DepsMut,
_env: Env,
info: MessageInfo,
new_implementation: ContractInstantiationInfo,
new_implementation: ContractImplementation,
) -> Result<Response> {
let config = STATE.load(deps.storage)?;
validate_admin(
Expand Down Expand Up @@ -341,7 +324,7 @@ fn try_create_lb_pair(
.map_err(|_| Error::BinStepHasNoPreset { bin_step })?;
let is_owner = info.sender == config.owner;

if !_is_preset_open(preset.0.0) && !is_owner {
if !_is_preset_open(preset.0 .0) && !is_owner {
return Err(Error::PresetIsLockedForUsers {
user: info.sender,
bin_step,
Expand Down Expand Up @@ -523,13 +506,17 @@ fn try_set_pair_preset(

PRESETS.save(deps.storage, bin_step, &preset)?;

STAKING_PRESETS.save(deps.storage, bin_step, &StakingPreset {
total_reward_bins,
rewards_distribution_algorithm,
epoch_staking_index,
epoch_staking_duration,
expiry_staking_duration,
})?;
STAKING_PRESETS.save(
deps.storage,
bin_step,
&StakingPreset {
total_reward_bins,
rewards_distribution_algorithm,
epoch_staking_index,
epoch_staking_duration,
expiry_staking_duration,
},
)?;

STATE.save(deps.storage, &state)?;

Expand Down Expand Up @@ -1136,7 +1123,7 @@ fn query_open_bin_steps(deps: Deps) -> Result<Binary> {
for bin_step in hashset {
let preset = PRESETS.load(deps.storage, bin_step)?;

if _is_preset_open(preset.0.0) {
if _is_preset_open(preset.0 .0) {
open_bin_steps.push(bin_step)
}
}
Expand Down
21 changes: 9 additions & 12 deletions contracts/liquidity_book/lb_factory/src/error.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@
//! ### Custom Errors for LB_Factory contract.
use shade_protocol::{
c_std::{Addr, StdError},
lb_libraries::{
bin_helper::BinError,
fee_helper::FeeError,
math::{
liquidity_configurations::LiquidityConfigurationsError,
u128x128_math::U128x128MathError,
u256x256_math::U256x256MathError,
},
oracle_helper::OracleError,
pair_parameter_helper::PairParametersError,
use lb_libraries::{
bin_helper::BinError,
fee_helper::FeeError,
math::{
liquidity_configurations::LiquidityConfigurationsError, u128x128_math::U128x128MathError,
u256x256_math::U256x256MathError,
},
oracle_helper::OracleError,
pair_parameter_helper::PairParametersError,
};
use shade_protocol::c_std::{Addr, StdError};

#[derive(thiserror::Error, Debug)]
pub enum LBFactoryError {
Expand Down
8 changes: 4 additions & 4 deletions contracts/liquidity_book/lb_factory/src/state.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use crate::types::{LBPair, LBPairInformation, NextPairKey};
use std::collections::HashSet;

use lb_libraries::{pair_parameter_helper::PairParameters, types::ContractImplementation};
use shade_protocol::{
c_std::{Addr, ContractInfo, Storage},
cosmwasm_schema::cw_serde,
lb_libraries::{pair_parameter_helper::PairParameters, types::ContractInstantiationInfo},
liquidity_book::lb_pair::RewardsDistributionAlgorithm,
secret_storage_plus::{AppendStore, Item, Map},
storage::{singleton, singleton_read, ReadonlySingleton, Singleton},
Expand Down Expand Up @@ -56,9 +56,9 @@ pub struct State {
pub contract_info: ContractInfo,
pub owner: Addr,
pub fee_recipient: Addr,
pub lb_pair_implementation: ContractInstantiationInfo,
pub lb_token_implementation: ContractInstantiationInfo,
pub staking_contract_implementation: ContractInstantiationInfo,
pub lb_pair_implementation: ContractImplementation,
pub lb_token_implementation: ContractImplementation,
pub staking_contract_implementation: ContractImplementation,
pub admin_auth: Contract,
pub query_auth: Contract,
pub recover_staking_funds_receiver: Addr,
Expand Down
2 changes: 1 addition & 1 deletion contracts/liquidity_book/lb_factory/src/types.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use shade_protocol::{cosmwasm_schema::cw_serde, swap::core::TokenType};

pub use shade_protocol::lb_libraries::types::{LBPair, LBPairInformation};
pub use lb_libraries::types::{LBPair, LBPairInformation};

#[cw_serde]
pub struct NextPairKey {
Expand Down
23 changes: 7 additions & 16 deletions contracts/liquidity_book/lb_pair/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,14 @@
name = "lb_pair"
version = "0.1.0"
edition = "2021"
exclude = [
# Those files are contract-optimizer artifacts. You might want to commit them for convenience but they should not be part of the source code publication.
"contract.wasm",
"hash.txt",
]
exclude = ["contract.wasm", "hash.txt"]

[lib]
crate-type = ["cdylib", "rlib"]

[[example]]
name = "schema"
path = "schema/schema.rs" # Adjust the path according to your project structure
path = "schema/schema.rs"


[features]
Expand All @@ -24,22 +20,17 @@ schema = []
backtraces = ["cosmwasm-std/backtraces"]

[dependencies]
shade-protocol = { version = "0.1.0", path = "../../../packages/shade_protocol", features = ["liquidity_book_impl"] }
shade-protocol = { path = "../../../packages/shade_protocol" }
lb-libraries = { path = "../../../packages/lb_libraries" }
schemars = "0.8.16"
serde = { version = "1.0" }
serde-json-wasm = { version = "1.0"}
cosmwasm-schema = "1.5"
serde-json-wasm = { version = "1.0" }
cosmwasm-schema = "2.0.3"
thiserror = { version = "1.0" }
ethnum = { version = "1.5" }
serde_json = "1"

[dev-dependencies]
anyhow = "1"
cosmwasm-std = { package = "secret-cosmwasm-std", version = "1.1.11" }
shade-multi-test = { path = "../../../packages/multi_test", features = [
"admin",
"lb_pair",
"lb_token",
"snip20",
] }

shade-multi-test = { path = "../../../packages/multi_test", features = ["admin", "lb_pair", "lb_token", "snip20"] }
62 changes: 24 additions & 38 deletions contracts/liquidity_book/lb_pair/src/contract.rs
Original file line number Diff line number Diff line change
@@ -1,39 +1,22 @@
use crate::{execute::*, helper::*, prelude::*, query::*, state::*};
use lb_libraries::{
lb_token::state_structs::LbPair,
math::{sample_math::OracleSample, tree_math::TreeUint24, u24::U24},
oracle_helper::Oracle,
pair_parameter_helper::PairParameters,
};
use shade_protocol::{
admin::helpers::{validate_admin, AdminPermissions},
c_std::{
from_binary,
shd_entry_point,
to_binary,
Addr,
Binary,
ContractInfo,
CosmosMsg,
Deps,
DepsMut,
Env,
MessageInfo,
Reply,
Response,
StdError,
StdResult,
SubMsg,
SubMsgResult,
Uint128,
Uint256,
WasmMsg,
from_binary, shd_entry_point, to_binary, Addr, Binary, ContractInfo, CosmosMsg, Deps,
DepsMut, Env, MessageInfo, Reply, Response, StdError, StdResult, SubMsg, SubMsgResult,
Uint128, Uint256, WasmMsg,
},
contract_interfaces::{
liquidity_book::{lb_pair::*, lb_staking, lb_token},
swap::core::TokenType,
},
lb_libraries::{
lb_token::state_structs::LbPair,
math::{sample_math::OracleSample, tree_math::TreeUint24, u24::U24},
oracle_helper::Oracle,
pair_parameter_helper::PairParameters,
viewing_keys::ViewingKey,
},
swap::core::ViewingKey,
};
use std::vec;

Expand Down Expand Up @@ -185,17 +168,20 @@ pub fn instantiate(
rewards_distribution_algorithm: msg.rewards_distribution_algorithm,
},
)?;
EPHEMERAL_STORAGE.save(deps.storage, &EphemeralStruct {
lb_token_code_hash: msg.lb_token_implementation.code_hash,
staking_contract: msg.staking_contract_implementation,
token_x_symbol,
token_y_symbol,
epoch_index: state.rewards_epoch_index,
epoch_duration: msg.epoch_staking_duration,
expiry_duration: msg.expiry_staking_duration,
recover_funds_receiver: msg.recover_staking_funds_receiver,
query_auth: msg.query_auth,
})?;
EPHEMERAL_STORAGE.save(
deps.storage,
&EphemeralStruct {
lb_token_code_hash: msg.lb_token_implementation.code_hash,
staking_contract: msg.staking_contract_implementation,
token_x_symbol,
token_y_symbol,
epoch_index: state.rewards_epoch_index,
epoch_duration: msg.epoch_staking_duration,
expiry_duration: msg.expiry_staking_duration,
recover_funds_receiver: msg.recover_staking_funds_receiver,
query_auth: msg.query_auth,
},
)?;

response = response.add_messages(messages);

Expand Down
Loading

0 comments on commit 34fd9b8

Please sign in to comment.