Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implement unbond all to wynd lsd #4

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,5 @@
node_ci_test/build
node_ci_test/node_modules
node_ci_test/package-lock.json

.DS_Store
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
members = ["packages/*", "contracts/*"]

[workspace.package]
version = "1.2.1"
version = "1.2.2"
edition = "2021"
license = "GPL 3.0"
repository = "https://github.com/cosmorama/wynd-lsd"
Expand Down
94 changes: 54 additions & 40 deletions contracts/lsd-hub/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,14 +137,10 @@ pub fn execute(
match msg {
ExecuteMsg::Receive(msg) => execute::handle_receive(deps, env, info, msg),
ExecuteMsg::Claim {} => execute::claim(deps, env, info),
ExecuteMsg::Bond {} => execute::bond(deps, env, info),
ExecuteMsg::Reinvest {} => execute::reinvest(deps, env),
ExecuteMsg::SetValidators { new_validators } => {
execute::set_validators(deps, info, env, new_validators)
}
ExecuteMsg::UpdateLiquidityDiscount { new_discount } => {
execute::update_liquidity_discount(deps, info, new_discount)
}
ExecuteMsg::Bond {} => Err(ContractError::BondingDisabled {}),
ExecuteMsg::EmergencyUnbondAll {} => execute::emergency_unbond_all(deps, env, info),
// Disable all other functions
_ => Err(ContractError::Unauthorized {}),
}
}

Expand All @@ -159,8 +155,8 @@ mod execute {
use super::*;
use crate::state::CleanedSupply;
use cosmwasm_std::{
from_binary, to_binary, BankMsg, Coin, CosmosMsg, DistributionMsg, Timestamp, Uint128,
WasmMsg,
from_binary, to_binary, BankMsg, Coin, CosmosMsg, DistributionMsg, StakingMsg, Timestamp,
Uint128, WasmMsg,
};
use cw20::{Cw20ExecuteMsg, Cw20ReceiveMsg};
use cw_utils::{must_pay, Expiration};
Expand Down Expand Up @@ -388,6 +384,54 @@ mod execute {
.add_attribute("action", "update_liquidity_discount")
.add_attribute("liquidity_discount", new_discount.to_string()))
}

pub fn emergency_unbond_all(
deps: DepsMut,
env: Env,
info: MessageInfo,
) -> Result<Response, ContractError> {
// Only owner can call this
if info.sender != Addr::unchecked("juno1s33zct2zhhaf60x4a90cpe9yquw99jj0zen8pt") {
return Err(ContractError::NotOwner {});
}

let mut messages = vec![];

// Query all delegations from chain state
let delegations = deps.querier.query_all_delegations(&env.contract.address)?;

for delegation in delegations {
messages.push(StakingMsg::Undelegate {
validator: delegation.validator,
amount: delegation.amount,
});
}

if messages.is_empty() {
return Err(ContractError::NoDelegationsFound {});
}

// Update contract state
let mut supply = SUPPLY.load(deps.storage)?;
supply.total_unbonding += messages
.iter()
.map(|msg| match msg {
StakingMsg::Undelegate { amount, .. } => amount.amount,
_ => Uint128::zero(),
})
.sum::<Uint128>();

supply.total_bonded = Uint128::zero();
SUPPLY.save(deps.storage, &supply)?;

// Clear bonded state
BONDED.save(deps.storage, &vec![])?;

Ok(Response::new()
.add_messages(messages)
.add_attribute("action", "emergency_unbond_all")
.add_attribute("amount_unbonded", supply.total_unbonding))
}
}

#[cfg_attr(not(feature = "library"), entry_point)]
Expand Down Expand Up @@ -682,36 +726,6 @@ pub mod migration {
pub fn migrate(deps: DepsMut, _env: Env, msg: MigrateMsg) -> Result<Response, ContractError> {
let version = ensure_from_older_version(deps.storage, CONTRACT_NAME, CONTRACT_VERSION)?;

if version < "1.1.0".parse::<Version>().unwrap() {
use cw_storage_plus::Item;
let old_storage: Item<migration::OldSupply> = Item::new("supply");
let old_supply = old_storage.load(deps.storage)?;

let new_supply = Supply {
bond_denom: old_supply.bond_denom,
issued: old_supply.issued,
total_bonded: old_supply.total_bonded,
claims: old_supply.claims,
total_unbonding: old_supply.total_unbonding,
};
SUPPLY.save(deps.storage, &new_supply)?;

BONDED.save(deps.storage, &old_supply.bonded)?;

// UNBONDING doesn't need to be saved; This Map with current state it should be empty
ensure!(
old_supply.unbonding.is_empty(),
ContractError::MigrationFailed {}
);
}

if let Some(new_owner) = msg.new_owner {
CONFIG.update::<_, StdError>(deps.storage, |mut config| {
config.owner = deps.api.addr_validate(&new_owner)?;
Ok(config)
})?;
}

Ok(Response::new())
}

Expand Down
9 changes: 9 additions & 0 deletions contracts/lsd-hub/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,15 @@ pub enum ContractError {

#[error("Migration failed - unbondings vector is not empty")]
MigrationFailed {},

#[error("Bonding is currently disabled")]
BondingDisabled {},

#[error("You are not the owner")]
NotOwner {},

#[error("No delegations found")]
NoDelegationsFound {},
}

impl From<OverflowError> for ContractError {
Expand Down
2 changes: 2 additions & 0 deletions contracts/lsd-hub/src/msg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ pub enum ExecuteMsg {
},
/// Updates the liquidity discount used for the [`QueryMsg::TargetValue`] query
UpdateLiquidityDiscount { new_discount: Decimal },
/// Emergency function to unbond all staked tokens
EmergencyUnbondAll {},
}

#[cw_serde]
Expand Down
24 changes: 24 additions & 0 deletions deploy.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#/bin/sh
junod tx wasm instantiate 3 '{"treasury": "juno1dcugtegqgswzjvdrl4cr88vv9v75wjrssucaf2", "commission": "0.09", "owner": "juno1dcugtegqgswzjvdrl4cr88vv9v75wjrssucaf2", "validators": [["junovaloper1q5860u6ducms20qu5emqwvju63ztrl5d8t055g", "0.3"],["junovaloper12szygttgeu2u0ffvfuz0ejmnx4lqwydnztwwvy", "0.3"], ["junovaloper1h3zfnp2lcdd6ddmpyft40zegsddqtvsqyz3249", "0.4"]], "cw20_init": {"cw20_code_id": 1, "decimals": 6, "label": "dimi lsd", "name": "wyJUNO", "symbol": "wyJUNO", "initial_balances": [] }, "epoch_period": 3600, "unbond_period": 3600, "max_concurrent_unbondings": 7, "liquidity_discount": "0.03"}' --from drip --label "culo" --admin juno1dcugtegqgswzjvdrl4cr88vv9v75wjrssucaf2 --gas 10000000

junod tx wasm execute juno1hrpna9v7vs3stzyd4z3xf00676kf78zpe2u5ksvljswn2vnjp3ys7tlgu0 '{"bond": {}}' --from drip --amount 2000000000ujuno --gas 1000000
junod q wasm contract-state smart juno1suhgf5svhu4usrurvxzlgn54ksxmn8gljarjtxqnapv8kjnp4nrsf8smqw '{"balance": {"address": "juno1hj5fveer5cjtn4wd6wstzugjfdxzl0xps73ftl"}}'

junod q wasm contract-state smart juno1hrpna9v7vs3stzyd4z3xf00676kf78zpe2u5ksvljswn2vnjp3ys7tlgu0 '{"validator_set":{}}'

junod tx wasm execute juno1hrpna9v7vs3stzyd4z3xf00676kf78zpe2u5ksvljswn2vnjp3ys7tlgu0 '{"reinvest": {}}' --from drip --gas 1000000


junod tx wasm execute juno1suhgf5svhu4usrurvxzlgn54ksxmn8gljarjtxqnapv8kjnp4nrsf8smqw '{"send":{"amount": "1900000000" , "contract": "juno1hrpna9v7vs3stzyd4z3xf00676kf78zpe2u5ksvljswn2vnjp3ys7tlgu0", "msg": "eyJ1bmJvbmQiOiB7fX0="}}' --from drip --gas 1000000

junod q wasm contract-state smart juno1hrpna9v7vs3stzyd4z3xf00676kf78zpe2u5ksvljswn2vnjp3ys7tlgu0 '{"claims":{"address": "juno1hj5fveer5cjtn4wd6wstzugjfdxzl0xps73ftl"}}'

junod tx wasm execute juno1hrpna9v7vs3stzyd4z3xf00676kf78zpe2u5ksvljswn2vnjp3ys7tlgu0 '{"claim":{}}' --from drip --gas 1000000

--- docker

junod tx staking unbond junovaloper1g8ejmp8yjjp99t5grgc3mvan5mlya8fnmn84s0 4999990000000ujuno --from validator --home /var/cosmos-chain/juno --keyring-backend test


// bonded 626F6E646564
// stake info 7374616B655F696E666F