Skip to content

Commit

Permalink
Merge pull request #80 from Mirror-Protocol/dev/astroport-mir-incentives
Browse files Browse the repository at this point in the history
[Feature] Prepare for Astroport migration
  • Loading branch information
csanti authored Dec 1, 2021
2 parents ffb7d36 + 49bbce1 commit 89dab55
Show file tree
Hide file tree
Showing 31 changed files with 1,079 additions and 96 deletions.
6 changes: 6 additions & 0 deletions contracts/mirror_collector/schema/config_response.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@
"distribution_contract": {
"type": "string"
},
"mir_ust_pair": {
"type": [
"string",
"null"
]
},
"mirror_token": {
"type": "string"
},
Expand Down
8 changes: 7 additions & 1 deletion contracts/mirror_collector/schema/execute_msg.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "ExecuteMsg",
"anyOf": [
"oneOf": [
{
"type": "object",
"required": [
Expand Down Expand Up @@ -47,6 +47,12 @@
"null"
]
},
"mir_ust_pair": {
"type": [
"string",
"null"
]
},
"mirror_token": {
"type": [
"string",
Expand Down
6 changes: 6 additions & 0 deletions contracts/mirror_collector/schema/instantiate_msg.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@
"distribution_contract": {
"type": "string"
},
"mir_ust_pair": {
"type": [
"string",
"null"
]
},
"mirror_token": {
"type": "string"
},
Expand Down
2 changes: 1 addition & 1 deletion contracts/mirror_collector/schema/query_msg.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "QueryMsg",
"anyOf": [
"oneOf": [
{
"type": "object",
"required": [
Expand Down
24 changes: 23 additions & 1 deletion contracts/mirror_collector/src/contract.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::errors::ContractError;
use crate::migration::migrate_config;
use crate::state::{read_config, store_config, Config};
use crate::swap::{convert, luna_swap_hook};
#[cfg(not(feature = "library"))]
Expand All @@ -22,6 +23,11 @@ pub fn instantiate(
_info: MessageInfo,
msg: InstantiateMsg,
) -> StdResult<Response> {
let mir_ust_pair = if let Some(mir_ust_pair) = msg.mir_ust_pair {
Some(deps.api.addr_canonicalize(&mir_ust_pair)?)
} else {
None
};
store_config(
deps.storage,
&Config {
Expand All @@ -34,6 +40,7 @@ pub fn instantiate(
anchor_market: deps.api.addr_canonicalize(&msg.anchor_market)?,
bluna_token: deps.api.addr_canonicalize(&msg.bluna_token)?,
bluna_swap_denom: msg.bluna_swap_denom,
mir_ust_pair,
},
)?;

Expand All @@ -58,6 +65,7 @@ pub fn execute(
anchor_market,
bluna_token,
bluna_swap_denom,
mir_ust_pair,
} => update_config(
deps,
info,
Expand All @@ -70,6 +78,7 @@ pub fn execute(
anchor_market,
bluna_token,
bluna_swap_denom,
mir_ust_pair,
),
ExecuteMsg::Convert { asset_token } => {
let asset_addr = deps.api.addr_validate(&asset_token)?;
Expand All @@ -93,6 +102,7 @@ pub fn update_config(
anchor_market: Option<String>,
bluna_token: Option<String>,
bluna_swap_denom: Option<String>,
mir_ust_pair: Option<String>,
) -> Result<Response<TerraMsgWrapper>, ContractError> {
let mut config: Config = read_config(deps.storage)?;
if config.owner != deps.api.addr_canonicalize(info.sender.as_str())? {
Expand Down Expand Up @@ -135,6 +145,13 @@ pub fn update_config(
config.bluna_swap_denom = bluna_swap_denom;
}

// this triggers switching to use astroport for MIR swaps
if let Some(mir_ust_pair) = mir_ust_pair {
config.mir_ust_pair = Some(deps.api.addr_canonicalize(&mir_ust_pair)?);
}

store_config(deps.storage, &config)?;

Ok(Response::new().add_attributes(vec![attr("action", "update_config")]))
}

Expand Down Expand Up @@ -191,12 +208,17 @@ pub fn query_config(deps: Deps) -> StdResult<ConfigResponse> {
anchor_market: deps.api.addr_humanize(&state.anchor_market)?.to_string(),
bluna_token: deps.api.addr_humanize(&state.bluna_token)?.to_string(),
bluna_swap_denom: state.bluna_swap_denom,
mir_ust_pair: state
.mir_ust_pair
.map(|raw| deps.api.addr_humanize(&raw).unwrap().to_string()),
};

Ok(resp)
}

#[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> {
migrate_config(deps.storage)?;

Ok(Response::default())
}
1 change: 1 addition & 0 deletions contracts/mirror_collector/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
pub mod contract;
mod errors;
mod migration;
pub mod state;
mod swap;

Expand Down
89 changes: 89 additions & 0 deletions contracts/mirror_collector/src/migration.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
use cosmwasm_std::{CanonicalAddr, StdResult, Storage};
use cosmwasm_storage::{singleton, singleton_read, ReadonlySingleton, Singleton};
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};

use crate::state::{Config, KEY_CONFIG};

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
pub struct LegacyConfig {
pub owner: CanonicalAddr,
pub distribution_contract: CanonicalAddr,
pub terraswap_factory: CanonicalAddr,
pub mirror_token: CanonicalAddr,
pub base_denom: String,
pub aust_token: CanonicalAddr,
pub anchor_market: CanonicalAddr,
pub bluna_token: CanonicalAddr,
pub bluna_swap_denom: String,
}

pub fn migrate_config(storage: &mut dyn Storage) -> StdResult<()> {
let legacty_store: ReadonlySingleton<LegacyConfig> = singleton_read(storage, KEY_CONFIG);
let legacy_config: LegacyConfig = legacty_store.load()?;
let config = Config {
owner: legacy_config.owner,
distribution_contract: legacy_config.distribution_contract,
terraswap_factory: legacy_config.terraswap_factory,
mirror_token: legacy_config.mirror_token,
base_denom: legacy_config.base_denom,
aust_token: legacy_config.aust_token,
anchor_market: legacy_config.anchor_market,
bluna_token: legacy_config.bluna_token,
bluna_swap_denom: legacy_config.bluna_swap_denom,
mir_ust_pair: None,
};
let mut store: Singleton<Config> = singleton(storage, KEY_CONFIG);
store.save(&config)?;
Ok(())
}

#[cfg(test)]
mod migrate_tests {
use crate::state::read_config;

use super::*;
use cosmwasm_std::{testing::mock_dependencies, Api};

pub fn config_old_store(storage: &mut dyn Storage) -> Singleton<LegacyConfig> {
Singleton::new(storage, KEY_CONFIG)
}

#[test]
fn test_config_migration() {
let mut deps = mock_dependencies(&[]);
let mut legacy_config_store = config_old_store(&mut deps.storage);
legacy_config_store
.save(&LegacyConfig {
owner: deps.api.addr_canonicalize("owner0000").unwrap(),
terraswap_factory: deps.api.addr_canonicalize("terraswapfactory").unwrap(),
distribution_contract: deps.api.addr_canonicalize("gov0000").unwrap(),
mirror_token: deps.api.addr_canonicalize("mirror0000").unwrap(),
base_denom: "uusd".to_string(),
aust_token: deps.api.addr_canonicalize("aust0000").unwrap(),
anchor_market: deps.api.addr_canonicalize("anchormarket0000").unwrap(),
bluna_token: deps.api.addr_canonicalize("bluna0000").unwrap(),
bluna_swap_denom: "uluna".to_string(),
})
.unwrap();

migrate_config(&mut deps.storage).unwrap();

let config: Config = read_config(&deps.storage).unwrap();
assert_eq!(
config,
Config {
owner: deps.api.addr_canonicalize("owner0000").unwrap(),
terraswap_factory: deps.api.addr_canonicalize("terraswapfactory").unwrap(),
distribution_contract: deps.api.addr_canonicalize("gov0000").unwrap(),
mirror_token: deps.api.addr_canonicalize("mirror0000").unwrap(),
base_denom: "uusd".to_string(),
aust_token: deps.api.addr_canonicalize("aust0000").unwrap(),
anchor_market: deps.api.addr_canonicalize("anchormarket0000").unwrap(),
bluna_token: deps.api.addr_canonicalize("bluna0000").unwrap(),
bluna_swap_denom: "uluna".to_string(),
mir_ust_pair: None,
}
)
}
}
4 changes: 3 additions & 1 deletion contracts/mirror_collector/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use serde::{Deserialize, Serialize};
use cosmwasm_std::{CanonicalAddr, StdResult, Storage};
use cosmwasm_storage::{singleton, singleton_read};

static KEY_CONFIG: &[u8] = b"config";
pub static KEY_CONFIG: &[u8] = b"config";

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
pub struct Config {
Expand All @@ -19,6 +19,8 @@ pub struct Config {
// bLuna params
pub bluna_token: CanonicalAddr,
pub bluna_swap_denom: String,
// when set, use this address instead of querying from terraswap
pub mir_ust_pair: Option<CanonicalAddr>,
}

pub fn store_config(storage: &mut dyn Storage, config: &Config) -> StdResult<()> {
Expand Down
37 changes: 23 additions & 14 deletions contracts/mirror_collector/src/swap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,18 +46,27 @@ fn direct_swap(
let terraswap_factory_addr = deps.api.addr_humanize(&config.terraswap_factory)?;
let asset_token_raw = deps.api.addr_canonicalize(asset_token.as_str())?;

let pair_info: PairInfo = query_pair_info(
&deps.querier,
terraswap_factory_addr,
&[
AssetInfo::NativeToken {
denom: config.base_denom.clone(),
},
AssetInfo::Token {
contract_addr: asset_token.to_string(),
},
],
)?;
let pair_addr: String =
if asset_token_raw == config.mirror_token && config.mir_ust_pair.is_some() {
deps.api
.addr_humanize(config.mir_ust_pair.as_ref().unwrap())?
.to_string()
} else {
let pair_info: PairInfo = query_pair_info(
&deps.querier,
terraswap_factory_addr,
&[
AssetInfo::NativeToken {
denom: config.base_denom.clone(),
},
AssetInfo::Token {
contract_addr: asset_token.to_string(),
},
],
)?;

pair_info.contract_addr
};

let mut messages: Vec<CosmosMsg<TerraMsgWrapper>> = vec![];
if config.mirror_token == asset_token_raw {
Expand All @@ -79,7 +88,7 @@ fn direct_swap(
// deduct tax first
let amount = (swap_asset.deduct_tax(&deps.querier)?).amount;
messages = vec![CosmosMsg::Wasm(WasmMsg::Execute {
contract_addr: pair_info.contract_addr,
contract_addr: pair_addr,
msg: to_binary(&TerraswapExecuteMsg::Swap {
offer_asset: Asset {
amount,
Expand All @@ -103,7 +112,7 @@ fn direct_swap(
messages = vec![CosmosMsg::Wasm(WasmMsg::Execute {
contract_addr: asset_token.to_string(),
msg: to_binary(&Cw20ExecuteMsg::Send {
contract: pair_info.contract_addr,
contract: pair_addr,
amount,
msg: to_binary(&TerraswapCw20HookMsg::Swap {
max_spread: None,
Expand Down
Loading

0 comments on commit 89dab55

Please sign in to comment.