-
Notifications
You must be signed in to change notification settings - Fork 12
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
Feat/unbond #LIDO-84 #16
Merged
Merged
Changes from all commits
Commits
Show all changes
27 commits
Select commit
Hold shift + click to select a range
cb8cc32
feat: build arm64
ratik e4b4d49
feat: voucher contract
ratik 91bd23f
feat: updated factory
ratik 9cf1399
chore: bad idea
ratik a83cd52
fix: $
ratik a1687d8
fix: use human address for voucher contract
ratik 55b1e00
feat: voucher init test
ratik b4cec6f
feat: first try
ratik 42cd026
feat: validate nft test
ratik 04b23e5
feat: nft withdraw
ratik acd0eae
fix: tests
ratik e1b70e8
chore: unused
ratik 7c729a5
chore: minor improvement
ratik 5f43d83
fix: move packages
ratik ec9ffbf
chore: simplify
ratik 789714f
fix: remove unused
ratik 7553a8d
chore: simplify
ratik 9fb7286
chore: unused
ratik 8f0ca69
chore: doc
ratik 97a3e29
feat: separate withdrawal manager
ratik 674a21c
fix: smart queries
ratik a8f1849
feat: update ts client
ratik 4903027
fix: typo
ratik 1154fa1
fix: tests
ratik f0effca
fix: test
ratik 6d7f4ec
Merge branch 'main' into feat/unbond
ratik 0fcabf0
fix: test
ratik File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,14 +4,21 @@ use cosmwasm_std::{ | |
Deps, DepsMut, Env, MessageInfo, Response, StdResult, Uint128, WasmMsg, | ||
}; | ||
use cw2::set_contract_version; | ||
|
||
use lido_staking_base::helpers::answer::response; | ||
use lido_staking_base::msg::core::{ExecuteMsg, InstantiateMsg, QueryMsg}; | ||
use lido_staking_base::msg::token::ExecuteMsg as TokenExecuteMsg; | ||
use lido_staking_base::state::core::CONFIG; | ||
use lido_staking_base::msg::{ | ||
core::{ExecuteMsg, InstantiateMsg, QueryMsg}, | ||
token::ExecuteMsg as TokenExecuteMsg, | ||
withdrawal_voucher::ExecuteMsg as VoucherExecuteMsg, | ||
}; | ||
use lido_staking_base::state::core::{ | ||
UnbondBatchStatus, UnbondItem, CONFIG, UNBOND_BATCHES, UNBOND_BATCH_ID, | ||
}; | ||
use lido_staking_base::state::withdrawal_voucher::{Metadata, Trait}; | ||
use neutron_sdk::bindings::{msg::NeutronMsg, query::NeutronQuery}; | ||
use std::str::FromStr; | ||
use std::vec; | ||
const CONTRACT_NAME: &str = concat!("crates.io:lido-neutron-contracts__", env!("CARGO_PKG_NAME")); | ||
const CONTRACT_NAME: &str = concat!("crates.io:lido-staking__", env!("CARGO_PKG_NAME")); | ||
const CONTRACT_VERSION: &str = env!("CARGO_PKG_VERSION"); | ||
|
||
#[cfg_attr(not(feature = "library"), entry_point)] | ||
|
@@ -22,14 +29,29 @@ pub fn instantiate( | |
msg: InstantiateMsg, | ||
) -> ContractResult<Response<NeutronMsg>> { | ||
set_contract_version(deps.storage, CONTRACT_NAME, CONTRACT_VERSION)?; | ||
|
||
CONFIG.save(deps.storage, &msg.clone().into())?; | ||
let attrs: Vec<Attribute> = vec![ | ||
attr("token_contract", msg.token_contract), | ||
attr("puppeteer_contract", msg.puppeteer_contract), | ||
attr("strategy_contract", msg.strategy_contract), | ||
attr("owner", msg.owner), | ||
attr("token_contract", &msg.token_contract), | ||
attr("puppeteer_contract", &msg.puppeteer_contract), | ||
attr("strategy_contract", &msg.strategy_contract), | ||
attr("base_denom", &msg.base_denom), | ||
attr("owner", &msg.owner), | ||
]; | ||
CONFIG.save(deps.storage, &msg.into())?; | ||
//an empty unbonding batch added as it's ready to be used on unbond action | ||
UNBOND_BATCH_ID.save(deps.storage, &0u128)?; | ||
UNBOND_BATCHES.save( | ||
deps.storage, | ||
0u128, | ||
&lido_staking_base::state::core::UnbondBatch { | ||
total_amount: Uint128::zero(), | ||
expected_amount: Uint128::zero(), | ||
unbond_items: vec![], | ||
status: UnbondBatchStatus::New, | ||
slashing_effect: None, | ||
unbonded_amount: None, | ||
withdrawed_amount: None, | ||
}, | ||
)?; | ||
Ok(response("instantiate", CONTRACT_NAME, attrs)) | ||
} | ||
|
||
|
@@ -38,13 +60,18 @@ pub fn query(deps: Deps<NeutronQuery>, env: Env, msg: QueryMsg) -> StdResult<Bin | |
match msg { | ||
QueryMsg::Config {} => to_json_binary(&CONFIG.load(deps.storage)?), | ||
QueryMsg::ExchangeRate {} => to_json_binary(&query_exchange_rate(deps, env)?), | ||
QueryMsg::UnbondBatch { batch_id } => query_unbond_batch(deps, batch_id), | ||
} | ||
} | ||
|
||
fn query_exchange_rate(_deps: Deps<NeutronQuery>, _env: Env) -> StdResult<Decimal> { | ||
Decimal::from_str("1.01") | ||
} | ||
|
||
fn query_unbond_batch(deps: Deps<NeutronQuery>, batch_id: Uint128) -> StdResult<Binary> { | ||
to_json_binary(&UNBOND_BATCHES.load(deps.storage, batch_id.into())?) | ||
} | ||
|
||
#[cfg_attr(not(feature = "library"), entry_point)] | ||
pub fn execute( | ||
deps: DepsMut<NeutronQuery>, | ||
|
@@ -54,24 +81,50 @@ pub fn execute( | |
) -> ContractResult<Response<NeutronMsg>> { | ||
match msg { | ||
ExecuteMsg::Bond { receiver } => execute_bond(deps, env, info, receiver), | ||
ExecuteMsg::Unbond { amount } => execute_unbond(deps, env, info, amount), | ||
ExecuteMsg::Unbond {} => execute_unbond(deps, env, info), | ||
ExecuteMsg::UpdateConfig { | ||
token_contract, | ||
puppeteer_contract, | ||
strategy_contract, | ||
owner, | ||
ld_denom, | ||
} => execute_update_config( | ||
deps, | ||
env, | ||
info, | ||
token_contract, | ||
puppeteer_contract, | ||
strategy_contract, | ||
owner, | ||
ld_denom, | ||
), | ||
ExecuteMsg::FakeProcessBatch { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this for PoC only? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yes |
||
batch_id, | ||
unbonded_amount, | ||
} => execute_fake_process_batch(deps, env, info, batch_id, unbonded_amount), | ||
} | ||
} | ||
|
||
fn execute_fake_process_batch( | ||
deps: DepsMut<NeutronQuery>, | ||
_env: Env, | ||
_info: MessageInfo, | ||
batch_id: Uint128, | ||
unbonded_amount: Uint128, | ||
) -> ContractResult<Response<NeutronMsg>> { | ||
let mut attrs = vec![attr("action", "fake_process_batch")]; | ||
let mut unbond_batch = UNBOND_BATCHES.load(deps.storage, batch_id.into())?; | ||
unbond_batch.unbonded_amount = Some(unbonded_amount); | ||
unbond_batch.status = UnbondBatchStatus::Unbonded; | ||
unbond_batch.slashing_effect = Some( | ||
Decimal::from_str(&unbonded_amount.to_string())? | ||
/ Decimal::from_str(&unbond_batch.expected_amount.to_string())?, | ||
); | ||
UNBOND_BATCHES.save(deps.storage, batch_id.into(), &unbond_batch)?; | ||
attrs.push(attr("batch_id", batch_id.to_string())); | ||
attrs.push(attr("unbonded_amount", unbonded_amount.to_string())); | ||
Ok(response("execute-fake_process_batch", CONTRACT_NAME, attrs)) | ||
} | ||
|
||
fn execute_bond( | ||
deps: DepsMut<NeutronQuery>, | ||
env: Env, | ||
|
@@ -104,7 +157,7 @@ fn execute_bond( | |
let exchange_rate = query_exchange_rate(deps.as_ref(), env)?; | ||
attrs.push(attr("exchange_rate", exchange_rate.to_string())); | ||
|
||
let issue_amount = amount * exchange_rate; | ||
let issue_amount = amount * (Decimal::one() / exchange_rate); | ||
attrs.push(attr("issue_amount", issue_amount.to_string())); | ||
|
||
let receiver = receiver.map_or(Ok::<String, ContractError>(info.sender.to_string()), |a| { | ||
|
@@ -131,12 +184,12 @@ fn check_denom(_denom: String) -> ContractResult<()> { | |
|
||
fn execute_update_config( | ||
deps: DepsMut<NeutronQuery>, | ||
_env: Env, | ||
info: MessageInfo, | ||
token_contract: Option<String>, | ||
puppeteer_contract: Option<String>, | ||
strategy_contract: Option<String>, | ||
owner: Option<String>, | ||
ld_denom: Option<String>, | ||
) -> ContractResult<Response<NeutronMsg>> { | ||
let mut config = CONFIG.load(deps.storage)?; | ||
ensure_eq!(config.owner, info.sender, ContractError::Unauthorized {}); | ||
|
@@ -158,15 +211,96 @@ fn execute_update_config( | |
config.owner = owner.clone(); | ||
attrs.push(attr("owner", owner)); | ||
} | ||
if let Some(ld_denom) = ld_denom { | ||
config.ld_denom = Some(ld_denom.clone()); | ||
attrs.push(attr("ld_denom", ld_denom)); | ||
} | ||
CONFIG.save(deps.storage, &config)?; | ||
Ok(response("execute-update_config", CONTRACT_NAME, attrs)) | ||
} | ||
|
||
fn execute_unbond( | ||
_deps: DepsMut<NeutronQuery>, | ||
_env: Env, | ||
_info: MessageInfo, | ||
_amount: Uint128, | ||
deps: DepsMut<NeutronQuery>, | ||
env: Env, | ||
info: MessageInfo, | ||
) -> ContractResult<Response<NeutronMsg>> { | ||
unimplemented!("todo"); | ||
let mut attrs = vec![attr("action", "unbond")]; | ||
let unbond_batch_id = UNBOND_BATCH_ID.load(deps.storage)?; | ||
ensure_eq!( | ||
info.funds.len(), | ||
1, | ||
ContractError::InvalidFunds { | ||
reason: "Must be one token".to_string(), | ||
} | ||
); | ||
let config = CONFIG.load(deps.storage)?; | ||
let ld_denom = config.ld_denom.ok_or(ContractError::LDDenomIsNotSet {})?; | ||
let amount = info.funds[0].amount; | ||
let denom = info.funds[0].denom.to_string(); | ||
ensure_eq!( | ||
denom, | ||
ld_denom, | ||
ContractError::InvalidFunds { | ||
reason: "Must be LD token".to_string(), | ||
} | ||
); | ||
let mut unbond_batch = UNBOND_BATCHES.load(deps.storage, unbond_batch_id)?; | ||
let exchange_rate = query_exchange_rate(deps.as_ref(), env)?; | ||
attrs.push(attr("exchange_rate", exchange_rate.to_string())); | ||
let expected_amount = amount * exchange_rate; | ||
unbond_batch.unbond_items.push(UnbondItem { | ||
sender: info.sender.to_string(), | ||
amount, | ||
expected_amount, | ||
}); | ||
unbond_batch.total_amount += amount; | ||
unbond_batch.expected_amount += expected_amount; | ||
|
||
attrs.push(attr("expected_amount", expected_amount.to_string())); | ||
UNBOND_BATCHES.save(deps.storage, unbond_batch_id, &unbond_batch)?; | ||
let extension = Some(Metadata { | ||
description: Some("Withdrawal voucher".into()), | ||
name: "LDV voucher".to_string(), | ||
batch_id: unbond_batch_id.to_string(), | ||
amount, | ||
expected_amount, | ||
attributes: Some(vec![ | ||
Trait { | ||
display_type: None, | ||
trait_type: "unbond_batch_id".to_string(), | ||
value: unbond_batch_id.to_string(), | ||
}, | ||
Trait { | ||
display_type: None, | ||
trait_type: "received_amount".to_string(), | ||
value: amount.to_string(), | ||
}, | ||
Trait { | ||
display_type: None, | ||
trait_type: "expected_amount".to_string(), | ||
value: expected_amount.to_string(), | ||
}, | ||
Trait { | ||
display_type: None, | ||
trait_type: "exchange_rate".to_string(), | ||
value: exchange_rate.to_string(), | ||
}, | ||
]), | ||
}); | ||
let msg = CosmosMsg::Wasm(WasmMsg::Execute { | ||
contract_addr: config.withdrawal_voucher_contract, | ||
msg: to_json_binary(&VoucherExecuteMsg::Mint { | ||
owner: info.sender.to_string(), | ||
token_id: unbond_batch_id.to_string() | ||
+ "_" | ||
+ info.sender.to_string().as_str() | ||
+ "_" | ||
+ &unbond_batch.unbond_items.len().to_string(), | ||
token_uri: None, | ||
extension, | ||
})?, | ||
funds: vec![], | ||
}); | ||
|
||
Ok(response("execute-unbond", CONTRACT_NAME, attrs).add_message(msg)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you add comment why are you adding this initial batch please?