Skip to content

Commit

Permalink
Merge pull request #25 from Anchor-Protocol/feat/astroport
Browse files Browse the repository at this point in the history
[Feature]: Astroport migration
  • Loading branch information
MSNTCS authored Jan 12, 2022
2 parents cac5a72 + a7fb3f9 commit 15c9d6f
Show file tree
Hide file tree
Showing 26 changed files with 938 additions and 168 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
## 0.2.0
## 0.3.0
Astroport support for collector and staking contracts.

## 0.2.0
Columbus-5 upgrade compatible version release.
2 changes: 1 addition & 1 deletion contracts/airdrop/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ overflow-checks = true
backtraces = ["cosmwasm-std/backtraces"]

[dependencies]
anchor-token = { version = "0.2.0", path = "../../packages/anchor_token" }
anchor-token = { version = "0.3.0", path = "../../packages/anchor_token" }
cosmwasm-std = { version = "0.16.0" }
cosmwasm-storage = { version = "0.16.0" }
cw20 = { version = "0.8.0" }
Expand Down
4 changes: 2 additions & 2 deletions contracts/collector/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ backtraces = ["cosmwasm-std/backtraces"]
cw20 = { version = "0.8.0" }
cosmwasm-std = { version = "0.16.0" }
cosmwasm-storage = { version = "0.16.0" }
anchor-token = { version = "0.2.0", path = "../../packages/anchor_token" }
anchor-token = { version = "0.3.0", path = "../../packages/anchor_token" }
terra-cosmwasm = "2.2.0"
terraswap = { version = "2.4.0" }
astroport = "0.3.1"
schemars = "0.8.1"
serde = { version = "1.0.103", default-features = false, features = ["derive"] }

Expand Down
20 changes: 13 additions & 7 deletions contracts/collector/schema/config_response.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,32 @@
"type": "object",
"required": [
"anchor_token",
"distributor_contract",
"astroport_factory",
"gov_contract",
"reward_factor",
"terraswap_factory"
"reward_factor"
],
"properties": {
"anchor_token": {
"type": "string"
},
"distributor_contract": {
"astroport_factory": {
"type": "string"
},
"gov_contract": {
"type": "string"
},
"max_spread": {
"anyOf": [
{
"$ref": "#/definitions/Decimal"
},
{
"type": "null"
}
]
},
"reward_factor": {
"$ref": "#/definitions/Decimal"
},
"terraswap_factory": {
"type": "string"
}
},
"definitions": {
Expand Down
39 changes: 37 additions & 2 deletions contracts/collector/schema/execute_msg.json
Original file line number Diff line number Diff line change
@@ -1,17 +1,52 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "ExecuteMsg",
"anyOf": [
"oneOf": [
{
"description": "Update config interface to enable reward_factor update",
"description": "Update config interface to enable reward_factor update ## NOTE: for updating `max spread` it should be either (true, none) or (true, \"0.1\") if we do not want to update it it should be (false, none)",
"type": "object",
"required": [
"update_config"
],
"properties": {
"update_config": {
"type": "object",
"required": [
"max_spread"
],
"properties": {
"astroport_factory": {
"type": [
"string",
"null"
]
},
"gov_contract": {
"type": [
"string",
"null"
]
},
"max_spread": {
"type": "array",
"items": [
{
"type": "boolean"
},
{
"anyOf": [
{
"$ref": "#/definitions/Decimal"
},
{
"type": "null"
}
]
}
],
"maxItems": 2,
"minItems": 2
},
"reward_factor": {
"anyOf": [
{
Expand Down
20 changes: 13 additions & 7 deletions contracts/collector/schema/instantiate_msg.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,32 @@
"type": "object",
"required": [
"anchor_token",
"distributor_contract",
"astroport_factory",
"gov_contract",
"reward_factor",
"terraswap_factory"
"reward_factor"
],
"properties": {
"anchor_token": {
"type": "string"
},
"distributor_contract": {
"astroport_factory": {
"type": "string"
},
"gov_contract": {
"type": "string"
},
"max_spread": {
"anyOf": [
{
"$ref": "#/definitions/Decimal"
},
{
"type": "null"
}
]
},
"reward_factor": {
"$ref": "#/definitions/Decimal"
},
"terraswap_factory": {
"type": "string"
}
},
"definitions": {
Expand Down
2 changes: 1 addition & 1 deletion contracts/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
80 changes: 54 additions & 26 deletions contracts/collector/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,18 @@
use cosmwasm_std::entry_point;

use cosmwasm_std::{
attr, to_binary, Binary, Coin, CosmosMsg, Decimal, Deps, DepsMut, Env, MessageInfo, Reply,
Response, StdError, StdResult, SubMsg, WasmMsg,
attr, to_binary, Addr, Binary, Coin, CosmosMsg, Decimal, Deps, DepsMut, Env, MessageInfo,
Reply, Response, StdError, StdResult, SubMsg, WasmMsg,
};

use crate::state::{read_config, store_config, Config};

use crate::migration::migrate_config;
use anchor_token::collector::{ConfigResponse, ExecuteMsg, InstantiateMsg, MigrateMsg, QueryMsg};
use astroport::asset::{Asset, AssetInfo, PairInfo};
use astroport::pair::ExecuteMsg as AstroportExecuteMsg;
use astroport::querier::{query_balance, query_pair_info, query_token_balance};
use cw20::Cw20ExecuteMsg;
use terraswap::asset::{Asset, AssetInfo, PairInfo};
use terraswap::pair::ExecuteMsg as TerraswapExecuteMsg;
use terraswap::querier::{query_balance, query_pair_info, query_token_balance};

#[cfg_attr(not(feature = "library"), entry_point)]
pub fn instantiate(
Expand All @@ -25,10 +26,10 @@ pub fn instantiate(
deps.storage,
&Config {
gov_contract: deps.api.addr_canonicalize(&msg.gov_contract)?,
terraswap_factory: deps.api.addr_canonicalize(&msg.terraswap_factory)?,
astroport_factory: deps.api.addr_canonicalize(&msg.astroport_factory)?,
anchor_token: deps.api.addr_canonicalize(&msg.anchor_token)?,
distributor_contract: deps.api.addr_canonicalize(&msg.distributor_contract)?,
reward_factor: msg.reward_factor,
max_spread: msg.max_spread,
},
)?;

Expand All @@ -38,7 +39,19 @@ pub fn instantiate(
#[cfg_attr(not(feature = "library"), entry_point)]
pub fn execute(deps: DepsMut, env: Env, info: MessageInfo, msg: ExecuteMsg) -> StdResult<Response> {
match msg {
ExecuteMsg::UpdateConfig { reward_factor } => update_config(deps, info, reward_factor),
ExecuteMsg::UpdateConfig {
reward_factor,
gov_contract,
astroport_factory,
max_spread,
} => update_config(
deps,
info,
reward_factor,
gov_contract,
astroport_factory,
max_spread,
),
ExecuteMsg::Sweep { denom } => sweep(deps, env, denom),
}
}
Expand All @@ -47,6 +60,9 @@ pub fn update_config(
deps: DepsMut,
info: MessageInfo,
reward_factor: Option<Decimal>,
gov_contract: Option<String>,
astroport_factory: Option<String>,
max_spread: (bool, Option<Decimal>),
) -> StdResult<Response> {
let mut config: Config = read_config(deps.storage)?;
if deps.api.addr_canonicalize(info.sender.as_str())? != config.gov_contract {
Expand All @@ -57,6 +73,17 @@ pub fn update_config(
config.reward_factor = reward_factor;
}

if let Some(gov_contract) = gov_contract {
config.gov_contract = deps.api.addr_canonicalize(gov_contract.as_str())?;
}
if let Some(astroport_factory) = astroport_factory {
config.astroport_factory = deps.api.addr_canonicalize(astroport_factory.as_str())?;
}

if max_spread.0 {
config.max_spread = max_spread.1
}

store_config(deps.storage, &config)?;
Ok(Response::default())
}
Expand All @@ -70,17 +97,17 @@ const SWEEP_REPLY_ID: u64 = 1;
pub fn sweep(deps: DepsMut, env: Env, denom: String) -> StdResult<Response> {
let config: Config = read_config(deps.storage)?;
let anchor_token = deps.api.addr_humanize(&config.anchor_token)?;
let terraswap_factory_addr = deps.api.addr_humanize(&config.terraswap_factory)?;
let astroport_factory_addr = deps.api.addr_humanize(&config.astroport_factory)?;

let pair_info: PairInfo = query_pair_info(
&deps.querier,
terraswap_factory_addr,
astroport_factory_addr,
&[
AssetInfo::NativeToken {
denom: denom.to_string(),
},
AssetInfo::Token {
contract_addr: anchor_token.to_string(),
contract_addr: Addr::unchecked(anchor_token),
},
],
)?;
Expand All @@ -99,13 +126,13 @@ pub fn sweep(deps: DepsMut, env: Env, denom: String) -> StdResult<Response> {
Ok(Response::new()
.add_submessage(SubMsg::reply_on_success(
CosmosMsg::Wasm(WasmMsg::Execute {
contract_addr: pair_info.contract_addr,
msg: to_binary(&TerraswapExecuteMsg::Swap {
contract_addr: pair_info.contract_addr.into_string(),
msg: to_binary(&AstroportExecuteMsg::Swap {
offer_asset: Asset {
amount,
..swap_asset
},
max_spread: None,
max_spread: config.max_spread,
belief_price: None,
to: None,
})?,
Expand Down Expand Up @@ -160,14 +187,11 @@ pub fn distribute(deps: DepsMut, env: Env) -> StdResult<Response> {
}));
}

// burn the left amount
if !left_amount.is_zero() {
messages.push(CosmosMsg::Wasm(WasmMsg::Execute {
contract_addr: deps.api.addr_humanize(&config.anchor_token)?.to_string(),
msg: to_binary(&Cw20ExecuteMsg::Transfer {
recipient: deps
.api
.addr_humanize(&config.distributor_contract)?
.to_string(),
msg: to_binary(&Cw20ExecuteMsg::Burn {
amount: left_amount,
})?,
funds: vec![],
Expand All @@ -192,22 +216,26 @@ pub fn query_config(deps: Deps) -> StdResult<ConfigResponse> {
let state = read_config(deps.storage)?;
let resp = ConfigResponse {
gov_contract: deps.api.addr_humanize(&state.gov_contract)?.to_string(),
terraswap_factory: deps
astroport_factory: deps
.api
.addr_humanize(&state.terraswap_factory)?
.addr_humanize(&state.astroport_factory)?
.to_string(),
anchor_token: deps.api.addr_humanize(&state.anchor_token)?.to_string(),
distributor_contract: deps
.api
.addr_humanize(&state.distributor_contract)?
.to_string(),
reward_factor: state.reward_factor,
max_spread: state.max_spread,
};

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
migrate_config(
deps.storage,
deps.api.addr_canonicalize(&msg.astroport_factory)?,
msg.max_spread,
)?;

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

#[cfg(test)]
Expand Down
38 changes: 38 additions & 0 deletions contracts/collector/src/migration.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};

use crate::state::{store_config, Config, KEY_CONFIG};
use cosmwasm_std::{CanonicalAddr, Decimal, StdResult, Storage};
use cosmwasm_storage::ReadonlySingleton;

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
pub struct LegacyConfig {
pub gov_contract: CanonicalAddr, // collected rewards receiver
pub terraswap_factory: CanonicalAddr, // astroport factory contract
pub anchor_token: CanonicalAddr, // anchor token address
pub distributor_contract: CanonicalAddr, // distributor contract to sent back rewards
pub reward_factor: Decimal, // reward distribution rate to gov contract, left rewards sent back to distributor contract
}

fn read_legacy_config(storage: &dyn Storage) -> StdResult<LegacyConfig> {
ReadonlySingleton::new(storage, KEY_CONFIG).load()
}

pub fn migrate_config(
storage: &mut dyn Storage,
astroport_factory: CanonicalAddr,
max_spread: Decimal,
) -> StdResult<()> {
let legacy_config: LegacyConfig = read_legacy_config(storage)?;

store_config(
storage,
&Config {
gov_contract: legacy_config.gov_contract,
astroport_factory,
anchor_token: legacy_config.anchor_token,
reward_factor: legacy_config.reward_factor,
max_spread: Some(max_spread),
},
)
}
Loading

0 comments on commit 15c9d6f

Please sign in to comment.