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

chore: make fee aggregation permissionless #226

Merged
merged 4 commits into from
Dec 6, 2023
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/liquidity_hub/fee_collector/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "fee_collector"
version = "1.1.2"
version = "1.1.4"
authors = ["Kerber0x <[email protected]>"]
edition.workspace = true
description = "Contract to collect the fees accrued by the pools and vaults in the liquidity hub"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
"additionalProperties": false
},
{
"description": "Swaps the assets (fees) sitting in the fee collector into the given [AssetInfo] if possible. A [SwapRoute] should be available at the router to be able to make the swaps.",
"description": "Swaps the assets (fees) sitting in the fee collector into the distribution asset set by the fee collector. A [SwapRoute] should be available at the router to be able to make the swaps.",
"type": "object",
"required": [
"aggregate_fees"
Expand All @@ -44,15 +44,11 @@
"aggregate_fees": {
"type": "object",
"required": [
"aggregate_fees_for",
"asset_info"
"aggregate_fees_for"
],
"properties": {
"aggregate_fees_for": {
"$ref": "#/definitions/FeesFor"
},
"asset_info": {
"$ref": "#/definitions/AssetInfo"
}
},
"additionalProperties": false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"additionalProperties": false
},
{
"description": "Swaps the assets (fees) sitting in the fee collector into the given [AssetInfo] if possible. A [SwapRoute] should be available at the router to be able to make the swaps.",
"description": "Swaps the assets (fees) sitting in the fee collector into the distribution asset set by the fee collector. A [SwapRoute] should be available at the router to be able to make the swaps.",
"type": "object",
"required": [
"aggregate_fees"
Expand All @@ -34,15 +34,11 @@
"aggregate_fees": {
"type": "object",
"required": [
"aggregate_fees_for",
"asset_info"
"aggregate_fees_for"
],
"properties": {
"aggregate_fees_for": {
"$ref": "#/definitions/FeesFor"
},
"asset_info": {
"$ref": "#/definitions/AssetInfo"
}
},
"additionalProperties": false
Expand Down
28 changes: 5 additions & 23 deletions contracts/liquidity_hub/fee_collector/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,13 @@ use white_whale::pool_network::router::SwapOperation;
use white_whale::vault_network::vault_factory::VaultsResponse;

use crate::contract::{FEES_AGGREGATION_REPLY_ID, FEES_COLLECTION_REPLY_ID};
use crate::queries::query_distribution_asset;
use crate::state::{read_temporal_asset_infos, store_temporal_asset_info, CONFIG, TMP_EPOCH};
use crate::ContractError;

/// Collects fees accrued by the pools and vaults. If a factory is provided then it only collects the
/// fees from its children.
pub fn collect_fees(
deps: DepsMut,
info: MessageInfo,
env: Env,
collect_fees_for: FeesFor,
) -> Result<Response, ContractError> {
let config: Config = CONFIG.load(deps.storage)?;

// only the owner or the contract itself can aggregate the fees
if info.sender != config.owner && info.sender != env.contract.address {
return Err(ContractError::Unauthorized {});
}

pub fn collect_fees(deps: DepsMut, collect_fees_for: FeesFor) -> Result<Response, ContractError> {
let mut collect_fees_messages: Vec<CosmosMsg> = Vec::new();

match collect_fees_for {
Expand Down Expand Up @@ -170,17 +159,13 @@ pub fn update_config(
/// Aggregates the fees collected into the given asset_info.
pub fn aggregate_fees(
mut deps: DepsMut,
info: MessageInfo,
env: Env,
ask_asset_info: AssetInfo,
aggregate_fees_for: FeesFor,
) -> Result<Response, ContractError> {
let config: Config = CONFIG.load(deps.storage)?;

// only the owner or the contract itself can aggregate the fees
if info.sender != config.owner && info.sender != env.contract.address {
return Err(ContractError::Unauthorized {});
}
// query fee collector to get the distribution asset
let ask_asset_info = query_distribution_asset(deps.as_ref())?;
kerber0x marked this conversation as resolved.
Show resolved Hide resolved

let mut aggregate_fees_messages: Vec<CosmosMsg> = Vec::new();

Expand Down Expand Up @@ -335,7 +320,6 @@ pub fn forward_fees(
info: MessageInfo,
env: Env,
epoch: Epoch,
forward_fees_as: AssetInfo,
) -> Result<Response, ContractError> {
let config = CONFIG.load(deps.storage)?;

Expand Down Expand Up @@ -392,7 +376,6 @@ pub fn forward_fees(
contract_addr: env.contract.address.to_string(),
funds: vec![],
msg: to_binary(&ExecuteMsg::AggregateFees {
asset_info: forward_fees_as.clone(),
aggregate_fees_for: FeesFor::Factory {
factory_addr: config.vault_factory.to_string(),
factory_type: FactoryType::Vault {
Expand All @@ -412,7 +395,6 @@ pub fn forward_fees(
contract_addr: env.contract.address.to_string(),
funds: vec![],
msg: to_binary(&ExecuteMsg::AggregateFees {
asset_info: forward_fees_as.clone(),
aggregate_fees_for: FeesFor::Factory {
factory_addr: config.pool_factory.to_string(),
factory_type: FactoryType::Pool {
Expand All @@ -432,7 +414,7 @@ pub fn forward_fees(
messages.push(pools_fee_aggregation_msg);

// saving the epoch and the asset info to forward the fees as in temp storage
TMP_EPOCH.save(deps.storage, &(epoch, forward_fees_as))?;
TMP_EPOCH.save(deps.storage, &epoch)?;

Ok(Response::new()
.add_attribute("action", "forward_fees")
Expand Down
19 changes: 9 additions & 10 deletions contracts/liquidity_hub/fee_collector/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use white_whale::fee_collector::{
use white_whale::pool_network::asset::{Asset, AssetInfo, ToCoins};

use crate::error::ContractError;
use crate::queries::query_distribution_asset;
use crate::state::{CONFIG, TMP_EPOCH};
use crate::ContractError::MigrateInvalidVersion;
use crate::{commands, migrations, queries};
Expand Down Expand Up @@ -49,10 +50,12 @@ pub fn instantiate(
#[entry_point]
pub fn reply(deps: DepsMut, env: Env, msg: Reply) -> Result<Response, ContractError> {
if msg.id == FEES_AGGREGATION_REPLY_ID {
let (mut epoch, asset_info) = TMP_EPOCH
let mut epoch = TMP_EPOCH
.may_load(deps.storage)?
.ok_or(ContractError::CannotReadEpoch {})?;

let asset_info = query_distribution_asset(deps.as_ref())?;

let token_balance: Uint128 = match asset_info.clone() {
AssetInfo::Token { .. } => {
return Err(ContractError::InvalidContractsFeeAggregation {})
Expand Down Expand Up @@ -108,7 +111,7 @@ pub fn execute(
) -> Result<Response, ContractError> {
match msg {
ExecuteMsg::CollectFees { collect_fees_for } => {
commands::collect_fees(deps, info, env, collect_fees_for)
commands::collect_fees(deps, collect_fees_for)
}
ExecuteMsg::UpdateConfig {
owner,
Expand All @@ -125,14 +128,10 @@ pub fn execute(
pool_factory,
vault_factory,
),
ExecuteMsg::AggregateFees {
asset_info,
aggregate_fees_for,
} => commands::aggregate_fees(deps, info, env, asset_info, aggregate_fees_for),
ExecuteMsg::ForwardFees {
epoch,
forward_fees_as,
} => commands::forward_fees(deps, info, env, epoch, forward_fees_as),
ExecuteMsg::AggregateFees { aggregate_fees_for } => {
commands::aggregate_fees(deps, env, aggregate_fees_for)
}
ExecuteMsg::ForwardFees { epoch, .. } => commands::forward_fees(deps, info, env, epoch),
}
}

Expand Down
15 changes: 14 additions & 1 deletion contracts/liquidity_hub/fee_collector/src/queries.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use cosmwasm_std::{to_binary, Addr, Deps, QueryRequest, StdResult, WasmQuery};

use white_whale::fee_collector::{Config, ContractType, FactoryType, FeesFor};
use white_whale::pool_network;
use white_whale::pool_network::asset::Asset;
use white_whale::pool_network::asset::{Asset, AssetInfo};
use white_whale::pool_network::factory::PairsResponse;
use white_whale::pool_network::pair::ProtocolFeesResponse as ProtocolPairFeesResponse;
use white_whale::vault_network::vault::ProtocolFeesResponse as ProtocolVaultFeesResponse;
Expand Down Expand Up @@ -143,3 +143,16 @@ fn query_fees_for_factory(

Ok(fees)
}

/// Queries the fee collector to get the distribution asset
pub(crate) fn query_distribution_asset(deps: Deps) -> StdResult<AssetInfo> {
let config: Config = CONFIG.load(deps.storage)?;

let fee_distributor_config: white_whale::fee_distributor::Config =
deps.querier.query(&QueryRequest::Wasm(WasmQuery::Smart {
contract_addr: config.fee_distributor.to_string(),
msg: to_binary(&white_whale::fee_distributor::QueryMsg::Config {})?,
}))?;

Ok(fee_distributor_config.distribution_asset)
}
2 changes: 1 addition & 1 deletion contracts/liquidity_hub/fee_collector/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use white_whale::pool_network::asset::AssetInfo;

pub const CONFIG: Item<Config> = Item::new("config");
pub const TMP_ASSET_INFOS: Map<String, AssetInfo> = Map::new("tmp_asset_infos");
pub const TMP_EPOCH: Item<(Epoch, AssetInfo)> = Item::new("tmp_epoch");
pub const TMP_EPOCH: Item<Epoch> = Item::new("tmp_epoch");

pub fn store_temporal_asset_info(deps: DepsMut, asset_info: AssetInfo) -> StdResult<()> {
let key = asset_info
Expand Down
Loading
Loading