Skip to content

Commit d823aa6

Browse files
authored
Merge pull request #4 from apollodao/dev/osmosis-vault-unstake-withdraw
2 parents 58edffc + 2880d2d commit d823aa6

File tree

6 files changed

+40
-26
lines changed

6 files changed

+40
-26
lines changed

Cargo.lock

+7-6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ osmosis = []
1515
apollo-proto-rust = {branch = "master", git = "https://github.com/apollodao/apollo-proto-rust"}
1616
cosmwasm-std = {version = "1.0", features = ["stargate"]}
1717
cw-asset = "2.1.0"
18-
cw-token = {git = "https://github.com/apollodao/cw-token.git", tag = "v0.3.0" }
19-
# cw-token = { path = "../../../cw-token" }
2018
cw-storage-plus = "0.13"
19+
cw-token = {git = "https://github.com/apollodao/cw-token.git", tag = "v0.3.0"}
20+
cw-utils = "0.13"
2121
cw20 = "0.13"
2222
cw20-base = "0.13"
2323
osmo-bindings = "0.5.1"

src/implementations/osmosis/osmosis.rs

+12
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,15 @@ use std::time::Duration;
44
use apollo_proto_rust::osmosis::gamm::v1beta1::{
55
MsgExitPool, MsgJoinPool, MsgSwapExactAmountIn, SwapAmountInRoute,
66
};
7+
use cw_utils::Duration as CwDuration;
8+
79
use apollo_proto_rust::osmosis::lockup::{MsgBeginUnlocking, MsgLockTokens};
810
use apollo_proto_rust::osmosis::superfluid::{
911
MsgLockAndSuperfluidDelegate, MsgSuperfluidUnbondLock,
1012
};
1113
use apollo_proto_rust::utils::encode;
1214
use apollo_proto_rust::OsmosisTypeURLs;
15+
1316
use cosmwasm_std::{Addr, Coin, CosmosMsg, Decimal, Deps, Response, StdError, StdResult, Uint128};
1417
use cw_asset::{Asset, AssetInfoBase, AssetList};
1518
use cw_storage_plus::Item;
@@ -235,6 +238,11 @@ impl Staking for OsmosisStaking {
235238
// Rewards are automatically distributed to stakers every epoch.
236239
Ok(Response::new())
237240
}
241+
242+
fn get_lockup_duration(&self) -> Result<CwDuration, CwDexError> {
243+
let std_duration: Duration = Duration::from_nanos(self.lockup_duration);
244+
Ok(CwDuration::Time(std_duration.as_secs()))
245+
}
238246
}
239247

240248
/// Implementation of superfluid staking for osmosis.
@@ -278,4 +286,8 @@ impl Staking for OsmosisSuperfluidStaking {
278286
// Rewards are automatically distributed to stakers every epoch.
279287
Ok(Response::new())
280288
}
289+
290+
fn get_lockup_duration(&self) -> Result<CwDuration, CwDexError> {
291+
todo!()
292+
}
281293
}

src/implementations/osmosis/osmosis_math.rs

+14-14
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::{convert::TryInto, ops::Sub, str::FromStr};
22

33
use cosmwasm_std::{Coin, Decimal, Deps, StdError, StdResult, Uint128, QueryRequest};
4-
use osmo_bindings::{ OsmosisQuery, PoolStateResponse};
4+
use osmo_bindings::{OsmosisQuery, PoolStateResponse};
55

66
pub fn osmosis_calculate_join_pool_shares(
77
deps: Deps<OsmosisQuery>,
@@ -95,11 +95,11 @@ fn calc_join_pool_shares_double_sided(
9595

9696
// feeRatio returns the fee ratio that is defined as follows:
9797
// 1 - ((1 - normalizedTokenWeightOut) * swapFee)
98-
fn fee_ratio(normalized_weight: Decimal, swap_fee: Decimal) -> Decimal {
98+
fn _fee_ratio(normalized_weight: Decimal, swap_fee: Decimal) -> Decimal {
9999
Decimal::one().sub(Decimal::one().sub(normalized_weight) * swap_fee)
100100
}
101101

102-
fn calc_join_pool_shares_single_sided(
102+
fn _calc_join_pool_shares_single_sided(
103103
token_in: &Coin,
104104
total_shares: Uint128,
105105
provided_asset_pool_balance: Uint128,
@@ -128,8 +128,8 @@ fn calc_join_pool_shares_single_sided(
128128
// sdk.OneDec()).Neg()
129129

130130
let token_in_amount_after_fee =
131-
token_in.amount * fee_ratio(provided_asset_normalized_weight, swap_fee);
132-
let pool_amount_out = osmosis_solve_constant_function_invariant(
131+
token_in.amount * _fee_ratio(provided_asset_normalized_weight, swap_fee);
132+
let pool_amount_out = _osmosis_solve_constant_function_invariant(
133133
provided_asset_pool_balance.checked_add(token_in_amount_after_fee)?,
134134
provided_asset_pool_balance,
135135
provided_asset_normalized_weight,
@@ -238,7 +238,7 @@ pub fn osmosis_calculate_exit_pool_amounts(
238238
/// The y_to_weight_ratio calculation is a workaround that works only for dual pools with
239239
/// even weight of the two assets. Go function in osmosis code can be found here:
240240
/// https://github.com/osmosis-labs/osmosis/blob/main/x/gamm/pool-models/balancer/amm.go#L94
241-
fn osmosis_solve_constant_function_invariant(
241+
fn _osmosis_solve_constant_function_invariant(
242242
token_balance_fixed_before: Uint128,
243243
token_balance_fixed_after: Uint128,
244244
token_weight_fixed: Decimal,
@@ -258,13 +258,13 @@ fn osmosis_solve_constant_function_invariant(
258258
// paranthetical := sdk.OneDec().Sub(yToWeightRatio)
259259
// amountY := tokenBalanceUnknownBefore.Mul(paranthetical)
260260
// return amountY
261-
let y_to_weight_ratio = osmosis_pow(y, weight_ratio)?;
261+
let y_to_weight_ratio = _osmosis_pow(y, weight_ratio)?;
262262
let paranthetical = Decimal::one() - y_to_weight_ratio;
263263
let amount_y = token_balance_unknown_before * paranthetical;
264264
return Ok(amount_y);
265265
}
266266

267-
fn osmosis_pow(base: Decimal, exp: Decimal) -> StdResult<Decimal> {
267+
fn _osmosis_pow(base: Decimal, exp: Decimal) -> StdResult<Decimal> {
268268
if base >= Decimal::from_ratio(2u128, 1u128) {
269269
return Err(StdError::generic_err("base must be lesser than two"));
270270
}
@@ -288,15 +288,15 @@ fn osmosis_pow(base: Decimal, exp: Decimal) -> StdResult<Decimal> {
288288
}
289289

290290
// fractionalPow := PowApprox(base, fractional, powPrecision)
291-
let fractional_pow = osmosis_pow_approx(base, fractional, Decimal::from_ratio(1u128, 1u128));
291+
let fractional_pow = _osmosis_pow_approx(base, fractional, Decimal::from_ratio(1u128, 1u128));
292292

293293
// return integerPow.Mul(fractionalPow)
294294
return Ok(integer_pow.checked_mul(fractional_pow)?);
295295
}
296296

297297
// Contract: 0 < base <= 2
298298
// 0 <= exp < 1.
299-
fn osmosis_pow_approx(base: Decimal, exp: Decimal, precision: Decimal) -> Decimal {
299+
fn _osmosis_pow_approx(base: Decimal, exp: Decimal, precision: Decimal) -> Decimal {
300300
if exp.is_zero() {
301301
return Decimal::one();
302302
}
@@ -341,7 +341,7 @@ fn osmosis_pow_approx(base: Decimal, exp: Decimal, precision: Decimal) -> Decima
341341
// term := sdk.OneDec()
342342
// sum := sdk.OneDec()
343343
// negative := false
344-
let (x, x_neg) = osmosis_abs_difference_with_sign(base, Decimal::one());
344+
let (x, x_neg) = _osmosis_abs_difference_with_sign(base, Decimal::one());
345345
let mut term = Decimal::one();
346346
let mut sum = Decimal::one();
347347
let mut negative = false;
@@ -363,7 +363,7 @@ fn osmosis_pow_approx(base: Decimal, exp: Decimal, precision: Decimal) -> Decima
363363
// // To avoid expensive big.Int allocation, we reuse bigK variable.
364364
// // On this line, bigK == i-1.
365365
// c, cneg := AbsDifferenceWithSign(a, bigK)
366-
let (c, c_neg) = osmosis_abs_difference_with_sign(a, big_k);
366+
let (c, c_neg) = _osmosis_abs_difference_with_sign(a, big_k);
367367

368368
// // On this line, bigK == i.
369369
// bigK.Set(sdk.NewDec(i))
@@ -414,7 +414,7 @@ fn osmosis_pow_approx(base: Decimal, exp: Decimal, precision: Decimal) -> Decima
414414

415415
// AbsDifferenceWithSign returns | a - b |, (a - b).sign()
416416
// a is mutated and returned.
417-
fn osmosis_abs_difference_with_sign(a: Decimal, b: Decimal) -> (Decimal, bool) {
417+
fn _osmosis_abs_difference_with_sign(a: Decimal, b: Decimal) -> (Decimal, bool) {
418418
if a >= b {
419419
(a - b, false)
420420
} else {
@@ -899,4 +899,4 @@ mod tests {
899899
// })
900900
// }
901901
// }
902-
}
902+
}

src/pool.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
use cosmwasm_std::{CosmosMsg, Deps, MessageInfo};
2-
use cosmwasm_std::{CustomQuery, Empty};
1+
use cosmwasm_std::CustomQuery;
2+
use cosmwasm_std::{CosmosMsg, Deps};
33
use cw_asset::{Asset, AssetList};
44
use serde::{de::DeserializeOwned, Serialize};
55

src/staking.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
use cosmwasm_std::{Deps, Empty, Response};
1+
use cosmwasm_std::{Deps, Response};
22
use cw_asset::Asset;
3+
use cw_utils::Duration as CwDuration;
34
use serde::{de::DeserializeOwned, Serialize};
45

56
use crate::CwDexError;
@@ -8,5 +9,5 @@ pub trait Staking: Clone + Serialize + DeserializeOwned {
89
fn stake(&self, deps: Deps, asset: Asset) -> Result<Response, CwDexError>;
910
fn unstake(&self, deps: Deps, asset: Asset) -> Result<Response, CwDexError>;
1011
fn claim_rewards(&self) -> Result<Response, CwDexError>;
11-
// TODO: add pending rewards query?
12+
fn get_lockup_duration(&self) -> Result<CwDuration, CwDexError>;
1213
}

0 commit comments

Comments
 (0)