Skip to content

Commit

Permalink
Merge pull request #450 from quasar-finance/fix/compounding
Browse files Browse the repository at this point in the history
Fix/compounding
  • Loading branch information
0xLaurenzo authored Aug 8, 2023
2 parents c580768 + 8a8d7fb commit d20f46f
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 5 deletions.
2 changes: 1 addition & 1 deletion smart-contracts/.cargo/config
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ schema = "run --example schema"
lint = "clippy -- -D warnings"
unit-test = "test --lib"
test-vault = "test --lib -p basic-vault"
clip = "cargo clippy -- --D warnings --A deprecated"
clip = "clippy -- --D warnings --A deprecated"
1 change: 1 addition & 0 deletions smart-contracts/contracts/lp-strategy/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
- readd proper lock behaviour
- Do not allow opentry messages to clog up our state
- Compare users' shares to their owned amount of queued shares instead of all queued shares
- make it so that the primitive compounds

## V0.1.1 08-05-2023

Expand Down
20 changes: 16 additions & 4 deletions smart-contracts/contracts/lp-strategy/src/ibc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use crate::state::{
LpCache, OngoingDeposit, PendingBond, CHANNELS, CONFIG, IBC_LOCK, IBC_TIMEOUT_TIME,
ICA_CHANNEL, ICQ_CHANNEL, LP_SHARES, OSMO_LOCK, PENDING_ACK, RECOVERY_ACK, REJOIN_QUEUE,
SIMULATED_EXIT_RESULT, SIMULATED_JOIN_AMOUNT_IN, SIMULATED_JOIN_RESULT, TIMED_OUT,
TOTAL_VAULT_BALANCE, TRAPS,
TOTAL_VAULT_BALANCE, TRAPS, USABLE_COMPOUND_BALANCE,
};
use crate::unbond::{batch_unbond, transfer_batch_unbond, PendingReturningUnbonds};
use cosmos_sdk_proto::cosmos::bank::v1beta1::QueryBalanceResponse;
Expand Down Expand Up @@ -317,7 +317,17 @@ pub fn handle_transfer_ack(
}
},
)?;
let total_amount = transferred_amount + failed_bonds_amount;

// add in usable base token compound balance to include rewards
// TODO: In an ideal world, I'd also fix share_out_min_amount to adjust for the amount we are compounding
// However, share_out_min_amount is already broken (doens't include failed_bonds_amount), the fix would live in ibc_util.rs L:78 (this would only fix failed_join_amount)
// a better fix would be to get the last state of the pool and do the math on our side to then also be able to include this USABLE_COMPOUND_BALANCE
// howver, if we are going to deprecate this vault, I'd argue it's not worth it - seeing as the compound balance would just be "extra money" for a user anyway
// TODO: remove this comment
let usable_base_token_compound_balance = USABLE_COMPOUND_BALANCE.load(storage)?;

let total_amount =
transferred_amount + failed_bonds_amount + usable_base_token_compound_balance;

let pending_rejoins: StdResult<Vec<OngoingDeposit>> = REJOIN_QUEUE.iter(storage)?.collect();
pending.bonds.append(&mut pending_rejoins?);
Expand Down Expand Up @@ -370,7 +380,9 @@ pub fn handle_icq_ack(
})?,
);

let usable_base_balance = get_usable_compound_balance(storage, base_balance)?;
// free base_token osmo side balance but subtracted out anything from trapped_errors, saved for use in transfer ack
let usable_base_token_compound_balance = get_usable_compound_balance(storage, base_balance)?;
USABLE_COMPOUND_BALANCE.save(storage, &usable_base_token_compound_balance)?;

// TODO the quote balance should be able to be compounded aswell
let _quote_balance = QueryBalanceResponse::decode(resp.responses[1].value.as_ref())?
Expand Down Expand Up @@ -447,7 +459,7 @@ pub fn handle_icq_ack(

let total_balance = calc_total_balance(
storage,
usable_base_balance,
usable_base_token_compound_balance,
&exit_pool.tokens_out,
spot_price,
)?;
Expand Down
6 changes: 6 additions & 0 deletions smart-contracts/contracts/lp-strategy/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,12 @@ pub(crate) const LP_SHARES: Item<LpCache> = Item::new("lp_shares");

// the latest known ica balance
pub(crate) const TOTAL_VAULT_BALANCE: Item<Uint128> = Item::new("total_vault_balance");
/// the "free balance" on osmo side, this is the amount of tokens that we can compound into the pool
/// actually, we also subtract out any failed bond attempts & any failed unbond attempts (trapped_errors, and failed join pool)
/// failed join pool is bonded back in via a different mechanism
/// trapped errors are not bonded back in, but they are not lost either
/// the only part that isn't accounted for is the failed returnTransfer attempts, but we never run that path simultaneously due to locks to it's fine
pub(crate) const USABLE_COMPOUND_BALANCE: Item<Uint128> = Item::new("usable_compound_balance");

// TODO we probably want to change this to an OngoingDeposit
pub(crate) const BONDING_CLAIMS: Map<(&Addr, &str), Uint128> = Map::new("bonding_claims");
Expand Down

0 comments on commit d20f46f

Please sign in to comment.