Skip to content

Commit

Permalink
chore: start point before major remake of the bonding mechanism
Browse files Browse the repository at this point in the history
  • Loading branch information
kerber0x committed May 13, 2024
1 parent c19ef89 commit 7ea3235
Show file tree
Hide file tree
Showing 16 changed files with 464 additions and 192 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -532,10 +532,28 @@
},
{
"description": "Returns the global index of the contract.",
"type": "string",
"enum": [
"type": "object",
"required": [
"global_index"
]
],
"properties": {
"global_index": {
"type": "object",
"properties": {
"epoch_id": {
"description": "The epoch id to check for the global index. If none is provided, the current global index is returned.",
"type": [
"integer",
"null"
],
"format": "uint64",
"minimum": 0.0
}
},
"additionalProperties": false
}
},
"additionalProperties": false
},
{
"description": "Returns the [RewardBucket]s that can be claimed by an address.",
Expand Down Expand Up @@ -596,7 +614,8 @@
"required": [
"bonded_amount",
"bonded_assets",
"last_updated",
"epoch_id",
"updated_last",
"weight"
],
"properties": {
Expand All @@ -615,7 +634,13 @@
"$ref": "#/definitions/Coin"
}
},
"last_updated": {
"epoch_id": {
"description": "The epoch id the global index was taken a snapshot for",
"type": "integer",
"format": "uint64",
"minimum": 0.0
},
"updated_last": {
"description": "The epoch id at which the total bond was updated.",
"type": "integer",
"format": "uint64",
Expand Down Expand Up @@ -742,7 +767,8 @@
"required": [
"bonded_amount",
"bonded_assets",
"last_updated",
"epoch_id",
"updated_last",
"weight"
],
"properties": {
Expand All @@ -761,7 +787,13 @@
"$ref": "#/definitions/Coin"
}
},
"last_updated": {
"epoch_id": {
"description": "The epoch id the global index was taken a snapshot for",
"type": "integer",
"format": "uint64",
"minimum": 0.0
},
"updated_last": {
"description": "The epoch id at which the total bond was updated.",
"type": "integer",
"format": "uint64",
Expand Down Expand Up @@ -920,7 +952,8 @@
"required": [
"bonded_amount",
"bonded_assets",
"last_updated",
"epoch_id",
"updated_last",
"weight"
],
"properties": {
Expand All @@ -939,7 +972,13 @@
"$ref": "#/definitions/Coin"
}
},
"last_updated": {
"epoch_id": {
"description": "The epoch id the global index was taken a snapshot for",
"type": "integer",
"format": "uint64",
"minimum": 0.0
},
"updated_last": {
"description": "The epoch id at which the total bond was updated.",
"type": "integer",
"format": "uint64",
Expand Down
35 changes: 30 additions & 5 deletions contracts/liquidity_hub/bonding-manager/schema/raw/query.json
Original file line number Diff line number Diff line change
Expand Up @@ -151,10 +151,28 @@
},
{
"description": "Returns the global index of the contract.",
"type": "string",
"enum": [
"type": "object",
"required": [
"global_index"
]
],
"properties": {
"global_index": {
"type": "object",
"properties": {
"epoch_id": {
"description": "The epoch id to check for the global index. If none is provided, the current global index is returned.",
"type": [
"integer",
"null"
],
"format": "uint64",
"minimum": 0.0
}
},
"additionalProperties": false
}
},
"additionalProperties": false
},
{
"description": "Returns the [RewardBucket]s that can be claimed by an address.",
Expand Down Expand Up @@ -215,7 +233,8 @@
"required": [
"bonded_amount",
"bonded_assets",
"last_updated",
"epoch_id",
"updated_last",
"weight"
],
"properties": {
Expand All @@ -234,7 +253,13 @@
"$ref": "#/definitions/Coin"
}
},
"last_updated": {
"epoch_id": {
"description": "The epoch id the global index was taken a snapshot for",
"type": "integer",
"format": "uint64",
"minimum": 0.0
},
"updated_last": {
"description": "The epoch id at which the total bond was updated.",
"type": "integer",
"format": "uint64",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@
"required": [
"bonded_amount",
"bonded_assets",
"last_updated",
"epoch_id",
"updated_last",
"weight"
],
"properties": {
Expand All @@ -55,7 +56,13 @@
"$ref": "#/definitions/Coin"
}
},
"last_updated": {
"epoch_id": {
"description": "The epoch id the global index was taken a snapshot for",
"type": "integer",
"format": "uint64",
"minimum": 0.0
},
"updated_last": {
"description": "The epoch id at which the total bond was updated.",
"type": "integer",
"format": "uint64",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
"required": [
"bonded_amount",
"bonded_assets",
"last_updated",
"epoch_id",
"updated_last",
"weight"
],
"properties": {
Expand All @@ -24,7 +25,13 @@
"$ref": "#/definitions/Coin"
}
},
"last_updated": {
"epoch_id": {
"description": "The epoch id the global index was taken a snapshot for",
"type": "integer",
"format": "uint64",
"minimum": 0.0
},
"updated_last": {
"description": "The epoch id at which the total bond was updated.",
"type": "integer",
"format": "uint64",
Expand Down
44 changes: 15 additions & 29 deletions contracts/liquidity_hub/bonding-manager/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use white_whale_std::pool_network::asset;
use crate::helpers::validate_growth_rate;
use crate::queries::{get_expiring_reward_bucket, query_claimable, query_weight, MAX_PAGE_LIMIT};
use crate::state::{
update_global_weight, update_local_weight, BOND, CONFIG, GLOBAL, LAST_CLAIMED_EPOCH,
update_bond_weight, update_global_weight, BOND, CONFIG, GLOBAL, LAST_CLAIMED_EPOCH,
REWARD_BUCKETS, UNBOND,
};
use crate::{helpers, ContractError};
Expand All @@ -19,14 +19,13 @@ use crate::{helpers, ContractError};
pub(crate) fn bond(
mut deps: DepsMut,
info: MessageInfo,
env: Env,
_env: Env,
asset: Coin,
) -> Result<Response, ContractError> {
println!("bonding");
println!("----bond----");
helpers::validate_buckets(&deps)?;
helpers::validate_claimed(&deps, &info)?;
helpers::validate_bonding_for_current_epoch(&deps, &env)?;
println!("bonding 2");
helpers::validate_bonding_for_current_epoch(&deps)?;

let config = CONFIG.load(deps.storage)?;
let current_epoch: white_whale_std::epoch_manager::epoch_manager::EpochResponse =
Expand All @@ -44,17 +43,19 @@ pub(crate) fn bond(
..asset.clone()
},
created_at_epoch: current_epoch.epoch.id,
updated_last: current_epoch.epoch.id,
..Bond::default()
});

// update local values
bond.asset.amount = bond.asset.amount.checked_add(asset.amount)?;
bond.weight = bond.weight.checked_add(asset.amount)?;
update_local_weight(&mut deps, info.sender.clone(), current_epoch.epoch.id, bond)?;
update_bond_weight(&mut deps, info.sender.clone(), current_epoch.epoch.id, bond)?;

// update global values
let mut global_index = GLOBAL.may_load(deps.storage)?.unwrap_or_default();
// include time term in the weight

global_index.weight = global_index.weight.checked_add(asset.amount)?;
global_index.bonded_amount = global_index.bonded_amount.checked_add(asset.amount)?;
global_index.bonded_assets =
Expand All @@ -81,7 +82,7 @@ pub(crate) fn unbond(
);

helpers::validate_claimed(&deps, &info)?;
helpers::validate_bonding_for_current_epoch(&deps, &env)?;
helpers::validate_bonding_for_current_epoch(&deps)?;
if let Some(mut unbond) = BOND
.key((&info.sender, &asset.denom))
.may_load(deps.storage)?
Expand All @@ -100,7 +101,7 @@ pub(crate) fn unbond(
)?;

// update local values, decrease the bond
unbond = update_local_weight(
unbond = update_bond_weight(
&mut deps,
info.sender.clone(),
current_epoch.epoch.id,
Expand Down Expand Up @@ -364,12 +365,7 @@ pub(crate) fn fill_rewards(
env: Env,
info: MessageInfo,
) -> Result<Response, ContractError> {
println!(
"EPOCHS: {:?}",
REWARD_BUCKETS
.keys(deps.storage, None, None, Order::Descending)
.collect::<Vec<_>>()
);
println!("----fill_rewards----");

// Finding the most recent bucket
let upcoming_bucket_id = match REWARD_BUCKETS
Expand Down Expand Up @@ -406,7 +402,6 @@ pub(crate) fn fill_rewards(
.filter(|coin| coin.denom.ne(distribution_denom.as_str()))
.collect::<Vec<Coin>>();

println!("remanent_coins: {:?}", remanent_coins);
// Each of these helpers will add messages to the messages vector
// and may increment the whale Coin above with the result of the swaps
helpers::handle_lp_tokens(&remanent_coins, &config, &mut submessages)?;
Expand All @@ -419,7 +414,6 @@ pub(crate) fn fill_rewards(
&mut messages,
)?;

println!("here");
// Add the whale to the funds, the whale figure now should be the result
// of all the LP token withdrawals and swaps
// Because we are using minimum receive, it is possible the contract can accumulate micro amounts of whale if we get more than what the swap query returned
Expand All @@ -443,6 +437,7 @@ pub(crate) fn on_epoch_created(
) -> Result<Response, ContractError> {
cw_utils::nonpayable(&info)?;

println!("----on_epoch_created----");
println!("EpochChangedHook: {:?}", current_epoch);
// A new epoch has been created, update rewards bucket and forward the expiring bucket
let config = CONFIG.load(deps.storage)?;
Expand All @@ -455,7 +450,7 @@ pub(crate) fn on_epoch_created(
// This happens only on the very first epoch where Global has not been initialised yet
if global.is_none() {
let initial_global_index = GlobalIndex {
last_updated: current_epoch.id,
updated_last: current_epoch.id,
..Default::default()
};
GLOBAL.save(deps.storage, &initial_global_index)?;
Expand All @@ -471,7 +466,9 @@ pub(crate) fn on_epoch_created(
)?;
}

let global = GLOBAL.load(deps.storage)?;
// Update the global index epoch id field
let mut global = GLOBAL.load(deps.storage)?;
global.epoch_id = current_epoch.id;

// update the global index for the current bucket, take the current snapshot of the global index
REWARD_BUCKETS.update(
Expand All @@ -484,17 +481,6 @@ pub(crate) fn on_epoch_created(
},
)?;

// todo to delete once the testing is done
let all_buckets: Vec<RewardBucket> = REWARD_BUCKETS
.range(deps.storage, None, None, Order::Descending)
.map(|item| {
let (_, bucket) = item?;
Ok(bucket)
})
.collect::<StdResult<Vec<RewardBucket>>>()?;

println!("EPOCHS: {:?}", all_buckets);

// forward fees from the expiring bucket to the new one.
let mut expiring_reward_bucket = get_expiring_reward_bucket(deps.as_ref())?;
if let Some(expiring_bucket) = expiring_reward_bucket.as_mut() {
Expand Down
4 changes: 3 additions & 1 deletion contracts/liquidity_hub/bonding-manager/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,9 @@ pub fn query(deps: Deps, _env: Env, msg: QueryMsg) -> Result<Binary, ContractErr
global_index,
)?)?)
}
QueryMsg::GlobalIndex => Ok(to_json_binary(&queries::query_global_index(deps)?)?),
QueryMsg::GlobalIndex { epoch_id } => Ok(to_json_binary(&queries::query_global_index(
deps, epoch_id,
)?)?),
QueryMsg::Claimable { address } => {
Ok(to_json_binary(&queries::query_claimable(deps, address)?)?)
}
Expand Down
4 changes: 2 additions & 2 deletions contracts/liquidity_hub/bonding-manager/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ pub enum ContractError {
#[error("There are unclaimed rewards available. Claim them before attempting to bond/unbond")]
UnclaimedRewards,

#[error("Trying to bond/unbond at a late time before the new/latest epoch has been created")]
NewEpochNotCreatedYet,
#[error("Trying to bond before an epoch has been created")]
EpochNotCreatedYet,

#[error("Nothing to claim")]
NothingToClaim,
Expand Down
Loading

0 comments on commit 7ea3235

Please sign in to comment.