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

feat(hub move): sunset of builders contract on Terra #108

Merged
merged 1 commit into from
Apr 5, 2024
Merged
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: 1 addition & 1 deletion 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 contracts/builder_unlock/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "builder-unlock"
version = "1.2.2"
version = "1.2.2-moved-to-neutron"
authors = ["Astroport"]
edition = "2021"
repository = "https://github.com/astroport-fi/astroport-governance"
Expand Down
119 changes: 74 additions & 45 deletions contracts/builder_unlock/src/contract.rs
Original file line number Diff line number Diff line change
@@ -1,27 +1,25 @@
use astroport::common::{claim_ownership, drop_ownership_proposal, propose_new_owner};
#[cfg(not(feature = "library"))]
use cosmwasm_std::entry_point;

use cosmwasm_std::{
from_binary, to_binary, Addr, Binary, Deps, DepsMut, Env, MessageInfo, Order, Response,
StdError, StdResult, Uint128, WasmMsg,
attr, ensure, from_binary, to_binary, wasm_execute, Addr, Binary, Deps, DepsMut, Env,
MessageInfo, Order, Response, StdError, StdResult, SubMsg, Uint128, WasmMsg,
};
use cw2::{get_contract_version, set_contract_version};
use cw20::{Cw20ExecuteMsg, Cw20ReceiveMsg};
use cw_storage_plus::Bound;

use crate::contract::helpers::compute_unlocked_amount;
use crate::migration::{MigrateMsg, CONFIGV100, STATEV100, STATUSV100};
use astroport_governance::builder_unlock::msg::{
AllocationResponse, ExecuteMsg, InstantiateMsg, QueryMsg, ReceiveMsg, SimulateWithdrawResponse,
StateResponse,
};
use astroport_governance::builder_unlock::{
AllocationParams, AllocationStatus, Config, Schedule, State,
AllocationParams, AllocationStatus, Config, MigrateMsg, Schedule,
};
use astroport_governance::{DEFAULT_LIMIT, MAX_LIMIT};

use crate::state::{CONFIG, OWNERSHIP_PROPOSAL, PARAMS, STATE, STATUS};
use crate::contract::helpers::compute_unlocked_amount;
use crate::state::{CONFIG, OWNERSHIP_PROPOSAL, PARAMS, STATE, STATUS, UNVESTED};

// Version and name used for contract migration.
const CONTRACT_NAME: &str = "builder-unlock";
Expand Down Expand Up @@ -231,6 +229,12 @@ pub fn query(deps: Deps, env: Env, msg: QueryMsg) -> StdResult<Binary> {
QueryMsg::Allocations { start_after, limit } => {
to_binary(&query_allocations(deps, start_after, limit)?)
}
QueryMsg::UnvestedTokens {} => {
let data = UNVESTED
.range(deps.storage, None, None, Order::Ascending)
.collect::<StdResult<Vec<_>>>()?;
to_binary(&data)
}
}
}

Expand Down Expand Up @@ -856,61 +860,86 @@ fn query_simulate_withdraw(
///
/// * **msg** is an object of type [`Empty`].
#[cfg_attr(not(feature = "library"), entry_point)]
pub fn migrate(deps: DepsMut, _env: Env, msg: MigrateMsg) -> StdResult<Response> {
pub fn migrate(deps: DepsMut, env: Env, msg: MigrateMsg) -> StdResult<Response> {
let contract_version = get_contract_version(deps.storage)?;

let mut resp = Response::default();

match contract_version.contract.as_ref() {
"builder-unlock" => match contract_version.version.as_ref() {
"1.0.0" => {
let state_v100 = STATEV100.load(deps.storage)?;
STATE.save(
deps.storage,
&State {
total_astro_deposited: state_v100.total_astro_deposited,
remaining_astro_tokens: state_v100.remaining_astro_tokens,
unallocated_tokens: Uint128::zero(),
},
)?;

let keys = STATUSV100
.keys(deps.storage, None, None, cosmwasm_std::Order::Ascending {})
.map(|v| Ok(v?.to_string()))
.collect::<Result<Vec<String>, StdError>>()?;

for key in keys {
let status_v100 = STATUSV100.load(deps.storage, &Addr::unchecked(&key))?;
let status = AllocationStatus {
astro_withdrawn: status_v100.astro_withdrawn,
unlocked_amount_checkpoint: Uint128::zero(),
};
STATUS.save(deps.storage, &Addr::unchecked(key), &status)?;
// This migration sunsets this contract on Terra.
// No further maintenance or migrations will be performed.
// All builders will still be able to withdraw unlocked tokens by `msg.lock_from_ts`.
// Remaining tokens will be burned.
"1.2.2" => {
ensure!(
msg.lock_from_ts > env.block.time.seconds(),
StdError::generic_err("msg.lock_from_ts must be in the future")
);

let mut total_unvested_astro = Uint128::zero();

let allocations = PARAMS
.range(deps.storage, None, None, Order::Ascending)
.collect::<StdResult<Vec<_>>>()?;

for (builder, mut params) in allocations {
let status = STATUS.load(deps.storage, &builder)?;

let astro_unlocked = compute_unlocked_amount(
msg.lock_from_ts,
params.amount,
&params.unlock_schedule,
status.unlocked_amount_checkpoint,
);

let unvested_astro = params.amount - astro_unlocked;
if !unvested_astro.is_zero() {
UNVESTED.save(deps.storage, &builder, &unvested_astro)?;
}

total_unvested_astro += unvested_astro;

params.amount = astro_unlocked;
params.unlock_schedule.duration =
msg.lock_from_ts - params.unlock_schedule.start_time;

STATUS.save(deps.storage, &builder, &status)?;
PARAMS.save(deps.storage, &builder, &params)?;
}

let config_v100 = CONFIGV100.load(deps.storage)?;
STATE.update::<_, StdError>(deps.storage, |mut state| {
state.remaining_astro_tokens -= total_unvested_astro;
state.total_astro_deposited -= total_unvested_astro;
Ok(state)
})?;

CONFIG.save(
deps.storage,
&Config {
owner: config_v100.owner,
astro_token: config_v100.astro_token,
max_allocations_amount: msg.max_allocations_amount,
let config = CONFIG.load(deps.storage)?;
let burn_msg = wasm_execute(
&config.astro_token,
&Cw20ExecuteMsg::Burn {
amount: total_unvested_astro,
},
vec![],
)?;

resp.messages.push(SubMsg::new(burn_msg));
resp.attributes
.push(attr("unvested_astro", total_unvested_astro))
}
"1.1.0" => {}
"1.2.0" => {}
_ => return Err(StdError::generic_err("Contract can't be migrated!")),
},
_ => return Err(StdError::generic_err("Contract can't be migrated!")),
};

set_contract_version(deps.storage, CONTRACT_NAME, CONTRACT_VERSION)?;

Ok(Response::new()
.add_attribute("previous_contract_name", &contract_version.contract)
.add_attribute("previous_contract_version", &contract_version.version)
.add_attribute("new_contract_name", CONTRACT_NAME)
.add_attribute("new_contract_version", CONTRACT_VERSION))
Ok(resp.add_attributes([
attr("previous_contract_name", contract_version.contract),
attr("previous_contract_version", contract_version.version),
attr("new_contract_name", CONTRACT_NAME),
attr("new_contract_version", CONTRACT_VERSION),
]))
}

//----------------------------------------------------------------------------------------
Expand Down
1 change: 0 additions & 1 deletion contracts/builder_unlock/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
pub mod contract;
mod migration;
pub mod state;
41 changes: 0 additions & 41 deletions contracts/builder_unlock/src/migration.rs

This file was deleted.

3 changes: 2 additions & 1 deletion contracts/builder_unlock/src/state.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use astroport::common::OwnershipProposal;
use cosmwasm_std::Addr;
use cosmwasm_std::{Addr, Uint128};
use cw_storage_plus::{Item, Map};

use astroport_governance::builder_unlock::{AllocationParams, AllocationStatus, Config, State};
Expand All @@ -12,5 +12,6 @@ pub const STATE: Item<State> = Item::new("state");
pub const PARAMS: Map<&Addr, AllocationParams> = Map::new("params");
/// The status of each unlock schedule
pub const STATUS: Map<&Addr, AllocationStatus> = Map::new("status");
pub const UNVESTED: Map<&Addr, Uint128> = Map::new("unvested");
/// Contains a proposal to change contract ownership
pub const OWNERSHIP_PROPOSAL: Item<OwnershipProposal> = Item::new("ownership_proposal");
8 changes: 8 additions & 0 deletions packages/astroport-governance/src/builder_unlock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,11 @@ impl AllocationStatus {
}
}

#[cw_serde]
pub struct MigrateMsg {
pub lock_from_ts: u64,
}

pub mod msg {
use crate::builder_unlock::Schedule;
use cosmwasm_schema::{cw_serde, QueryResponses};
Expand Down Expand Up @@ -225,6 +230,9 @@ pub mod msg {
start_after: Option<String>,
limit: Option<u32>,
},
/// Returns unvested tokens. (builder addr, amount)
#[returns(Vec<(String, Uint128)>)]
UnvestedTokens {},
}

pub type ConfigResponse = Config;
Expand Down
2 changes: 1 addition & 1 deletion packages/astroport-governance/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ pub use astroport;

// Default pagination constants
pub const DEFAULT_LIMIT: u32 = 10;
pub const MAX_LIMIT: u32 = 30;
pub const MAX_LIMIT: u32 = 200;
Loading