Skip to content

Commit de126ba

Browse files
router added
1 parent 16ff6f3 commit de126ba

40 files changed

+1464
-395
lines changed

contracts/liquidity_book/lb_pair/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ serde-json-wasm = { version = "0.5"}
3030
thiserror = { version = "1" }
3131
secret-storage-plus = { git = "https://github.com/securesecrets/secret-plus-utils", version = "0.13.4", optional = true }
3232
ethnum = { version = "1" }
33+
shadeswap-shared = { version = "0.1.0", path = "../../../packages/shadeswap_shared", features = [] }
3334

3435
[dev-dependencies]
3536
anyhow = "1"

contracts/liquidity_book/lb_pair/src/contract.rs

Lines changed: 138 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,15 @@
33
use crate::{prelude::*, state::*};
44
use core::panic;
55
use cosmwasm_std::{
6-
from_binary, to_binary, Addr, BankMsg, Binary, Coin, ContractInfo, CosmosMsg, Deps, DepsMut,
7-
Env, MessageInfo, Response, StdError, StdResult, SubMsgResult, Timestamp, Uint128, Uint256,
8-
WasmMsg,
6+
from_binary, to_binary, Addr, Attribute, BankMsg, Binary, Coin, ContractInfo, CosmosMsg,
7+
Decimal, Deps, DepsMut, Env, MessageInfo, Response, StdError, StdResult, SubMsgResult,
8+
Timestamp, Uint128, Uint256, WasmMsg,
99
};
10+
1011
use ethnum::U256;
1112
use serde::Serialize;
13+
use shade_protocol::lb_libraries::constants::PRECISION;
14+
use shade_protocol::liquidity_book::lb_pair::QueryMsg::GetPairInfo;
1215
use shade_protocol::{
1316
c_std::{shd_entry_point, Reply, SubMsg},
1417
contract_interfaces::liquidity_book::{
@@ -39,6 +42,7 @@ use shade_protocol::{
3942
utils::pad_handle_result,
4043
BLOCK_SIZE,
4144
};
45+
use shadeswap_shared::router::ExecuteMsgResponse;
4246
use std::{collections::HashMap, ops::Sub};
4347
use tokens::TokenType;
4448
use types::{Bytes32, LBPairInformation, MintArrays};
@@ -218,7 +222,7 @@ pub fn execute(deps: DepsMut, env: Env, info: MessageInfo, msg: ExecuteMsg) -> R
218222
};
219223

220224
let swap_for_y: bool;
221-
if info.sender == config.token_x.unique_key() {
225+
if offer.token.unique_key() == config.token_x.unique_key() {
222226
swap_for_y = true;
223227
} else {
224228
swap_for_y = false;
@@ -419,21 +423,38 @@ fn try_swap(
419423
})?;
420424

421425
let mut messages: Vec<CosmosMsg> = Vec::new();
426+
let amount_out;
422427
if swap_for_y {
423-
let msg = BinHelper::transfer_y(amounts_out, token_y, to);
428+
amount_out = amounts_out.decode_y();
429+
let msg = BinHelper::transfer_y(amounts_out, token_y.clone(), to);
424430

425431
if let Some(message) = msg {
426432
messages.push(message);
427433
}
428434
} else {
429-
let msg = BinHelper::transfer_x(amounts_out, token_x, to);
435+
amount_out = amounts_out.decode_x();
436+
let msg = BinHelper::transfer_x(amounts_out, token_x.clone(), to);
430437

431438
if let Some(message) = msg {
432439
messages.push(message);
433440
}
434441
}
435442

436-
Ok(Response::default().add_messages(messages))
443+
Ok(Response::new()
444+
.add_messages(messages)
445+
.add_attributes(vec![
446+
Attribute::new("amount_in", amounts_received),
447+
Attribute::new("amount_out", amount_out.to_string()),
448+
Attribute::new("lp_fee_amount", "0".to_string()), // TODO FILL with correct parameters
449+
Attribute::new("total_fee_amount", "0".to_string()), // TODO FILL with correct parameters
450+
Attribute::new("shade_dao_fee_amount", "0".to_string()), // TODO FILL with correct parameters
451+
Attribute::new("token_in_key", token_x.unique_key()),
452+
Attribute::new("token_out_key", token_y.unique_key()),
453+
])
454+
.set_data(to_binary(&ExecuteMsgResponse::SwapResult {
455+
amount_in: amounts_received,
456+
amount_out: Uint128::from(amount_out),
457+
})?))
437458
}
438459

439460
/// Flash loan tokens from the pool to a receiver contract and execute a callback function.
@@ -1421,6 +1442,7 @@ fn receiver_callback(
14211442
#[shd_entry_point]
14221443
pub fn query(deps: Deps, env: Env, msg: QueryMsg) -> Result<Binary> {
14231444
match msg {
1445+
QueryMsg::GetPairInfo {} => to_binary(&query_pair_info(deps)?).map_err(Error::CwErr),
14241446
QueryMsg::GetFactory {} => to_binary(&query_factory(deps)?).map_err(Error::CwErr),
14251447
QueryMsg::GetTokenX {} => to_binary(&query_token_x(deps)?).map_err(Error::CwErr),
14261448
QueryMsg::GetTokenY {} => to_binary(&query_token_y(deps)?).map_err(Error::CwErr),
@@ -1467,9 +1489,118 @@ pub fn query(deps: Deps, env: Env, msg: QueryMsg) -> Result<Binary> {
14671489
}
14681490
QueryMsg::GetLbToken {} => to_binary(&query_lb_token(deps)?).map_err(Error::CwErr),
14691491
QueryMsg::GetTokens {} => to_binary(&query_tokens(deps)?).map_err(Error::CwErr),
1492+
GetPairInfo {} => todo!(),
1493+
QueryMsg::SwapSimulation { offer, exclude_fee } => {
1494+
to_binary(&query_swap_simulation(deps, env, offer, exclude_fee)?).map_err(Error::CwErr)
1495+
}
14701496
}
14711497
}
14721498

1499+
/// Returns the Liquidity Book Factory.
1500+
///
1501+
/// # Returns
1502+
///
1503+
/// * `factory` - The Liquidity Book Factory
1504+
fn query_pair_info(deps: Deps) -> Result<shadeswap_shared::msg::amm_pair::QueryMsgResponse> {
1505+
let state = CONFIG.load(deps.storage)?;
1506+
1507+
let (mut reserve_x, mut reserve_y) = state.reserves.decode();
1508+
let (protocol_fee_x, protocol_fee_y) = state.protocol_fees.decode();
1509+
1510+
Ok(
1511+
shadeswap_shared::msg::amm_pair::QueryMsgResponse::GetPairInfo {
1512+
liquidity_token: shade_protocol::Contract {
1513+
address: state.lb_token.address,
1514+
code_hash: state.lb_token.code_hash,
1515+
},
1516+
factory: Some(shade_protocol::Contract {
1517+
address: state.factory.address,
1518+
code_hash: state.factory.code_hash,
1519+
}),
1520+
pair: shadeswap_shared::core::TokenPair {
1521+
0: state.token_x,
1522+
1: state.token_y,
1523+
2: false,
1524+
},
1525+
amount_0: Uint128::from(reserve_x),
1526+
amount_1: Uint128::from(reserve_y),
1527+
total_liquidity: Uint128::default(), // no global liquidity, liquidity is calculated on per bin basis
1528+
contract_version: 1, // TODO set this like const AMM_PAIR_CONTRACT_VERSION: u32 = 1;
1529+
fee_info: shadeswap_shared::amm_pair::FeeInfo {
1530+
shade_dao_address: Addr::unchecked(""), // TODO set shade dao address
1531+
lp_fee: shadeswap_shared::core::Fee {
1532+
// TODO set this
1533+
nom: state.pair_parameters.get_base_fee_u64(state.bin_step),
1534+
denom: 1_000_000_000_000_000_000,
1535+
},
1536+
shade_dao_fee: shadeswap_shared::core::Fee {
1537+
// TODO set this
1538+
nom: state.pair_parameters.get_base_fee_u64(state.bin_step),
1539+
denom: 1_000_000_000_000_000_000,
1540+
},
1541+
stable_lp_fee: shadeswap_shared::core::Fee {
1542+
// TODO set this
1543+
nom: state.pair_parameters.get_base_fee_u64(state.bin_step),
1544+
denom: 1_000_000_000_000_000_000,
1545+
},
1546+
stable_shade_dao_fee: shadeswap_shared::core::Fee {
1547+
// TODO set this
1548+
nom: state.pair_parameters.get_base_fee_u64(state.bin_step),
1549+
denom: 1_000_000_000_000_000_000,
1550+
},
1551+
},
1552+
stable_info: None,
1553+
},
1554+
)
1555+
}
1556+
1557+
// / Returns the Liquidity Book Factory.
1558+
// /
1559+
// / # Returns
1560+
// /
1561+
// / * `factory` - The Liquidity Book Factory
1562+
fn query_swap_simulation(
1563+
deps: Deps,
1564+
env: Env,
1565+
offer: shade_protocol::lb_libraries::tokens::TokenAmount,
1566+
exclude_fee: Option<bool>,
1567+
) -> Result<shadeswap_shared::msg::amm_pair::QueryMsgResponse> {
1568+
let state = CONFIG.load(deps.storage)?;
1569+
1570+
let (mut reserve_x, mut reserve_y) = state.reserves.decode();
1571+
let (protocol_fee_x, protocol_fee_y) = state.protocol_fees.decode();
1572+
let mut swap_for_y = false;
1573+
match offer.token {
1574+
token if token == state.token_x => swap_for_y = true,
1575+
token if token == state.token_y => {}
1576+
_ => panic!("No such token"),
1577+
};
1578+
1579+
let res = query_swap_out(deps, env, offer.amount.into(), swap_for_y)?;
1580+
1581+
if (res.amount_in_left.u128() > 0u128) {
1582+
return Err(Error::AmountInLeft {
1583+
amount_left_in: res.amount_in_left,
1584+
total_amount: offer.amount,
1585+
swapped_amount: res.amount_out,
1586+
});
1587+
}
1588+
1589+
let price = Decimal::from_ratio(res.amount_out, offer.amount).to_string();
1590+
1591+
Ok(
1592+
shadeswap_shared::msg::amm_pair::QueryMsgResponse::SwapSimulation {
1593+
total_fee_amount: res.fee,
1594+
lp_fee_amount: res.fee, //TODO lpfee
1595+
shade_dao_fee_amount: res.fee, // dao fee
1596+
result: SwapResult {
1597+
return_amount: res.amount_out,
1598+
},
1599+
price,
1600+
},
1601+
)
1602+
}
1603+
14731604
/// Returns the Liquidity Book Factory.
14741605
///
14751606
/// # Returns

contracts/liquidity_book/lb_pair/src/error.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,13 @@ pub enum LBPairError {
146146
#[error("Id overflows. Id: {id}")]
147147
IdOverflows { id: u32 },
148148

149+
#[error("Amount left unswapped. : Amount Left In: {amount_left_in}, Total Amount: {total_amount}, swapped_amount: {swapped_amount}")]
150+
AmountInLeft {
151+
amount_left_in: Uint128,
152+
total_amount: Uint128,
153+
swapped_amount: Uint128,
154+
},
155+
149156
#[error("Id slippage caught. Active id desired: {active_id_desired}, Id slippage: {id_slippage}, Active id: {active_id}")]
150157
IdSlippageCaught {
151158
active_id_desired: u32,

contracts/liquidity_book/router/src/contract.rs

Lines changed: 33 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,37 @@
1-
use cosmwasm_std::{
2-
entry_point, from_binary, to_binary, Addr, BankMsg, Binary, Coin, CosmosMsg, Deps, DepsMut,
3-
Env, MessageInfo, Reply, Response, StdError, StdResult, Uint128,
4-
};
5-
use shade_protocol::Contract;
6-
use shadeswap_shared::admin::helpers::{validate_admin, AdminPermissions};
7-
use shadeswap_shared::router::{InitMsg, QueryMsgResponse};
8-
use shadeswap_shared::snip20::helpers::send_msg;
9-
use shadeswap_shared::utils::{pad_handle_result, pad_query_result};
10-
use shadeswap_shared::{
11-
amm_pair::QueryMsgResponse as AMMPairQueryReponse,
12-
core::{TokenAmount, TokenType},
13-
router::{ExecuteMsg, InvokeMsg, QueryMsg},
14-
};
15-
161
use crate::{
172
operations::{next_swap, refresh_tokens, swap_tokens_for_exact_tokens},
183
query,
194
state::{config_r, config_w, registered_tokens_list_r, registered_tokens_list_w, Config},
205
};
6+
use cosmwasm_std::{
7+
entry_point,
8+
from_binary,
9+
to_binary,
10+
Addr,
11+
BankMsg,
12+
Binary,
13+
Coin,
14+
CosmosMsg,
15+
Deps,
16+
DepsMut,
17+
Env,
18+
MessageInfo,
19+
Reply,
20+
Response,
21+
StdError,
22+
StdResult,
23+
Uint128,
24+
Uint256,
25+
};
26+
use shade_protocol::{utils::liquidity_book::tokens::TokenType, Contract};
27+
use shadeswap_shared::{
28+
admin::helpers::{validate_admin, AdminPermissions},
29+
amm_pair::QueryMsgResponse as AMMPairQueryReponse,
30+
core::TokenAmount,
31+
router::{ExecuteMsg, InitMsg, InvokeMsg, QueryMsg, QueryMsgResponse},
32+
snip20::helpers::send_msg,
33+
utils::{pad_handle_result, pad_query_result},
34+
};
2135

2236
/// Pad handle responses and log attributes to blocks
2337
/// of 256 bytes to prevent leaking info based on response size
@@ -164,13 +178,11 @@ fn receiver_callback(
164178
path,
165179
recipient,
166180
} => {
167-
let pair_contract_config = query::pair_contract_config(
168-
&deps.querier,
169-
Contract {
181+
let pair_contract_config =
182+
query::pair_contract_config(&deps.querier, Contract {
170183
address: deps.api.addr_validate(&path[0].addr.to_string())?,
171184
code_hash: path[0].code_hash.clone(),
172-
},
173-
)?;
185+
})?;
174186

175187
match pair_contract_config {
176188
AMMPairQueryReponse::GetPairInfo {
@@ -220,7 +232,7 @@ fn receiver_callback(
220232
return Err(StdError::generic_err(format!(
221233
"Could not retrieve PairInfo from {}",
222234
&path[0].addr
223-
)))
235+
)));
224236
}
225237
}
226238
}

contracts/liquidity_book/router/src/operations.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ use cosmwasm_std::{
22
to_binary, Addr, Coin, CosmosMsg, DepsMut, Env, Response, StdError, StdResult, Storage, SubMsg,
33
Uint128, WasmMsg,
44
};
5-
use shade_protocol::Contract;
5+
use shade_protocol::{utils::liquidity_book::tokens::TokenType, Contract};
66
use shadeswap_shared::{
7-
core::{TokenAmount, TokenType},
7+
core::TokenAmount,
88
msg::amm_pair::{
99
ExecuteMsg as AMMPairExecuteMsg, InvokeMsg as AMMPairInvokeMsg,
1010
QueryMsgResponse as AMMPairQueryReponse,

contracts/liquidity_book/router/src/query.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,10 @@ use cosmwasm_std::{
44
to_binary, Binary, Deps, QuerierWrapper, QueryRequest, StdError, StdResult, Uint128, Uint256,
55
WasmQuery,
66
};
7-
use shade_protocol::Contract;
7+
use shade_protocol::{liquidity_book::lb_pair::SwapResult, Contract};
88
use shadeswap_shared::{
99
core::TokenAmount,
10-
msg::amm_pair::{
11-
QueryMsg as AMMPairQueryMsg, QueryMsgResponse as AMMPairQueryReponse, SwapResult,
12-
},
10+
msg::amm_pair::{QueryMsg as AMMPairQueryMsg, QueryMsgResponse as AMMPairQueryReponse},
1311
router::{Hop, QueryMsgResponse},
1412
};
1513

contracts/liquidity_book/router/src/state.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ use cosmwasm_storage::Singleton;
1212
use schemars::JsonSchema;
1313
use serde::{Deserialize, Serialize};
1414

15+
use shade_protocol::utils::liquidity_book::tokens::TokenType;
1516
use shade_protocol::Contract;
1617
use shadeswap_shared::core::TokenAmount;
17-
use shadeswap_shared::core::TokenType;
1818
use shadeswap_shared::router::Hop;
1919

2020
pub static CONFIG: &[u8] = b"config";

contracts/liquidity_book/router/src/test.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ pub mod tests {
2525
use serde::Deserialize;
2626
use serde::Serialize;
2727
use shade_protocol::snip20::Snip20ReceiveMsg;
28+
use shade_protocol::utils::liquidity_book::tokens::TokenType;
2829
use shadeswap_shared::admin::ValidateAdminPermissionResponse;
2930
use shadeswap_shared::amm_pair::FeeInfo;
3031
use shadeswap_shared::core::TokenPair;
@@ -46,7 +47,6 @@ pub mod tests {
4647
use shadeswap_shared::core::ContractInstantiationInfo;
4748
use shadeswap_shared::core::Fee;
4849
use shadeswap_shared::core::TokenAmount;
49-
use shadeswap_shared::core::TokenType;
5050
use shadeswap_shared::router::ExecuteMsg;
5151
use shadeswap_shared::router::Hop;
5252
use shadeswap_shared::router::InitMsg;

contracts/liquidity_book/tests/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,11 @@ rand = "0.8"
3636
anyhow = "1"
3737
# cw-multi-test = { version = "0.16.0", default-features = false }
3838
shade-multi-test = { path = "../../../packages/multi_test", features = [
39+
"admin",
3940
"lb_pair",
4041
"lb_token",
4142
"snip20",
43+
"router",
4244
"lb_factory"
4345
] }
4446

0 commit comments

Comments
 (0)