From 033140100650dd22f40599bc39717994caef78d0 Mon Sep 17 00:00:00 2001 From: ben2x4 Date: Wed, 25 Jan 2023 16:33:59 -0800 Subject: [PATCH] update migration contracts so staked balances can still be queried --- contracts/junoswap-staking/src/contract.rs | 15 +++++++++++++++ contracts/junoswap-staking/src/msg.rs | 9 +++++++++ contracts/raw-migration/src/contract.rs | 14 ++++++++++++++ contracts/raw-migration/src/msg.rs | 8 ++++++++ 4 files changed, 46 insertions(+) diff --git a/contracts/junoswap-staking/src/contract.rs b/contracts/junoswap-staking/src/contract.rs index 0361e98..0f954ea 100644 --- a/contracts/junoswap-staking/src/contract.rs +++ b/contracts/junoswap-staking/src/contract.rs @@ -1,3 +1,4 @@ +use std::cmp::min; #[cfg(not(feature = "library"))] use cosmwasm_std::entry_point; use cosmwasm_std::{ @@ -11,6 +12,7 @@ use wyndex::asset::{Asset, AssetInfo}; use crate::error::ContractError; use crate::msg::{ExecuteMsg, MigrateMsg, QueryMsg}; +use crate::msg::QueryMsg::TotalStakedAtHeight; use crate::state::{MigrateConfig, MigrateStakersConfig, DESTINATION, MIGRATION}; // this is the contract we are migrating from @@ -32,6 +34,8 @@ pub fn instantiate( #[cfg_attr(not(feature = "library"), entry_point)] pub fn query(_deps: Deps, _env: Env, msg: QueryMsg) -> Result { + // Block before the migration was started + let migration_height: u64 = 6634356; match msg { QueryMsg::MigrationFinished {} => { let no_stakers = stake_cw20::state::STAKED_BALANCES @@ -39,6 +43,17 @@ pub fn query(_deps: Deps, _env: Env, msg: QueryMsg) -> Result { + let new_height = min(migration_height,height.unwrap_or(u64::MAX)); + let staked_total = stake_cw20::state::STAKED_TOTAL.may_load_at_height(_deps.storage, new_height)?.unwrap_or_default(); + Ok(to_binary(&stake_cw20::msg::TotalStakedAtHeightResponse{ total: staked_total, height: new_height })?) + }, + QueryMsg::StakedBalanceAtHeight { address, height } => { + let address = _deps.api.addr_validate(&address)?; + let new_height = min(migration_height,height.unwrap_or(u64::MAX)); + let balance = stake_cw20::state::STAKED_BALANCES.may_load_at_height(_deps.storage, &address, new_height)?.unwrap_or_default(); + Ok(to_binary(&stake_cw20::msg::StakedBalanceAtHeightResponse{ balance, height: new_height })?) } } } diff --git a/contracts/junoswap-staking/src/msg.rs b/contracts/junoswap-staking/src/msg.rs index 06b7283..cc77766 100644 --- a/contracts/junoswap-staking/src/msg.rs +++ b/contracts/junoswap-staking/src/msg.rs @@ -1,4 +1,6 @@ use cosmwasm_schema::{cw_serde, QueryResponses}; +use cosmwasm_std::Uint128; +use stake_cw20::msg::{StakedBalanceAtHeightResponse, TotalStakedAtHeightResponse}; #[cw_serde] #[derive(QueryResponses)] @@ -6,6 +8,13 @@ pub enum QueryMsg { /// Checks whether all stakers have been migrated #[returns(bool)] MigrationFinished {}, + #[returns(StakedBalanceAtHeightResponse)] + StakedBalanceAtHeight { + address: String, + height: Option, + }, + #[returns(TotalStakedAtHeightResponse)] + TotalStakedAtHeight { height: Option } } /// For existing contract, we need to specify which pool it can be withdrawn into diff --git a/contracts/raw-migration/src/contract.rs b/contracts/raw-migration/src/contract.rs index 6b9dd4e..c36516d 100644 --- a/contracts/raw-migration/src/contract.rs +++ b/contracts/raw-migration/src/contract.rs @@ -1,3 +1,4 @@ +use std::cmp::min; #[cfg(not(feature = "library"))] use cosmwasm_std::entry_point; use cosmwasm_std::{ @@ -35,6 +36,8 @@ pub fn instantiate( #[cfg_attr(not(feature = "library"), entry_point)] pub fn query(_deps: Deps, _env: Env, msg: QueryMsg) -> Result { + // Block before the migration was started + let migration_height: u64 = 6634356; match msg { QueryMsg::MigrationFinished {} => { let no_stakers = stake_cw20::state::STAKED_BALANCES @@ -42,6 +45,17 @@ pub fn query(_deps: Deps, _env: Env, msg: QueryMsg) -> Result { + let new_height = min(migration_height,height.unwrap_or(u64::MAX)); + let staked_total = stake_cw20::state::STAKED_TOTAL.may_load_at_height(_deps.storage, new_height)?.unwrap_or_default(); + Ok(to_binary(&stake_cw20::msg::TotalStakedAtHeightResponse{ total: staked_total, height: new_height })?) + }, + QueryMsg::StakedBalanceAtHeight { address, height } => { + let address = _deps.api.addr_validate(&address)?; + let new_height = min(migration_height,height.unwrap_or(u64::MAX)); + let balance = stake_cw20::state::STAKED_BALANCES.may_load_at_height(_deps.storage, &address, new_height)?.unwrap_or_default(); + Ok(to_binary(&stake_cw20::msg::StakedBalanceAtHeightResponse{ balance, height: new_height })?) } } } diff --git a/contracts/raw-migration/src/msg.rs b/contracts/raw-migration/src/msg.rs index 02cd97a..147d0c6 100644 --- a/contracts/raw-migration/src/msg.rs +++ b/contracts/raw-migration/src/msg.rs @@ -1,5 +1,6 @@ use cosmwasm_schema::{cw_serde, QueryResponses}; use cosmwasm_std::Decimal; +use stake_cw20::msg::{StakedBalanceAtHeightResponse, TotalStakedAtHeightResponse}; #[cw_serde] pub struct InstantiateMsg {} @@ -10,6 +11,13 @@ pub enum QueryMsg { /// Checks whether all stakers have been migrated #[returns(bool)] MigrationFinished {}, + #[returns(StakedBalanceAtHeightResponse)] + StakedBalanceAtHeight { + address: String, + height: Option, + }, + #[returns(TotalStakedAtHeightResponse)] + TotalStakedAtHeight { height: Option } } /// For existing contract, we need to specify which pool it can be withdrawn into