Skip to content

Commit

Permalink
Merge pull request #6 from oraichain/feat/incentive-fund-manager
Browse files Browse the repository at this point in the history
Feat/incentive fund manager
  • Loading branch information
ducphamle2 authored Sep 4, 2024
2 parents f6d5e25 + eb700aa commit fb3d776
Show file tree
Hide file tree
Showing 56 changed files with 674 additions and 251 deletions.
27 changes: 27 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ derive_more = "0.99.17"
decimal-core = { path = "./packages/decimal-core" }
decimal = { path = "./packages/decimal" }
cosmwasm-testing-util = { git = "https://github.com/oraichain/cosmwasm-testing-util.git", rev = "143348c" }
oraiswap-v3-common = { path = "./packages/oraiswap-v3-common" }
incentives-fund-manager = { path = "./contracts/incentives-fund-manager" }

[profile.release]
opt-level = 3
Expand Down
29 changes: 29 additions & 0 deletions contracts/incentives-fund-manager/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
[package]
name = "incentives-fund-manager"
version = { workspace = true }
authors = { workspace = true }
edition = { workspace = true }
license = { workspace = true }
repository = { workspace = true }
homepage = { workspace = true }
documentation = { workspace = true }

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[lib]
crate-type = ["cdylib", "rlib"]

[features]
# use library feature to disable all instantiate/execute/query exports
library = []

[dependencies]
cosmwasm-std = { workspace = true }
cosmwasm-schema = { workspace = true }
cosmwasm-storage = { workspace = true }
oraiswap-v3-common = { workspace = true }

cw-storage-plus = { workspace = true }
cw2 = { workspace = true }
cw20 = { workspace = true }
thiserror = { workspace = true }
13 changes: 13 additions & 0 deletions contracts/incentives-fund-manager/src/bin/schema.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
use cosmwasm_schema::write_api;
use oraiswap_v3_common::incentives_fund_manager::{
ExecuteMsg, InstantiateMsg, MigrateMsg, QueryMsg,
};

fn main() {
write_api! {
instantiate: InstantiateMsg,
execute: ExecuteMsg,
query: QueryMsg,
migrate: MigrateMsg
}
}
123 changes: 123 additions & 0 deletions contracts/incentives-fund-manager/src/contract.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
use crate::state::{Config, CONFIG};
#[cfg(not(feature = "library"))]
use cosmwasm_std::entry_point;
use cosmwasm_std::{
to_json_binary, Addr, Binary, CosmosMsg, Deps, DepsMut, Env, MessageInfo, Response, StdResult,
};
use cw2::set_contract_version;
use oraiswap_v3_common::{
asset::Asset,
error::ContractError,
incentives_fund_manager::{ExecuteMsg, InstantiateMsg, QueryMsg},
};

// version info for migration info
const CONTRACT_NAME: &str = "crates.io:incentives-fund-manager";
const CONTRACT_VERSION: &str = env!("CARGO_PKG_VERSION");

#[cfg_attr(not(feature = "library"), entry_point)]
pub fn instantiate(
deps: DepsMut,
_env: Env,
info: MessageInfo,
msg: InstantiateMsg,
) -> Result<Response, ContractError> {
set_contract_version(deps.storage, CONTRACT_NAME, CONTRACT_VERSION)?;

CONFIG.save(
deps.storage,
&Config {
owner: msg.owner.unwrap_or(info.sender),
oraiswap_v3: msg.oraiswap_v3,
},
)?;

Ok(Response::default())
}

#[cfg_attr(not(feature = "library"), entry_point)]
pub fn execute(
deps: DepsMut,
_env: Env,
info: MessageInfo,
msg: ExecuteMsg,
) -> Result<Response, ContractError> {
match msg {
ExecuteMsg::UpdateConfig { owner, oraiswap_v3 } => {
execute_update_config(deps, info, owner, oraiswap_v3)
}
ExecuteMsg::SendFund { asset, receiver } => execute_send_fund(deps, info, asset, receiver),
}
}

/// Allows owner can adjust config
///
/// # Parameters
/// - `owner`: new owner
/// - `oraiswap_v3`: new oraiswapV3 contract
///
/// # Errors
/// - Reverts the call when the caller is an unauthorized user
///
fn execute_update_config(
deps: DepsMut,
info: MessageInfo,
owner: Option<Addr>,
oraiswap_v3: Option<Addr>,
) -> Result<Response, ContractError> {
let mut config = CONFIG.load(deps.storage)?;
if info.sender != config.owner {
return Err(ContractError::Unauthorized {});
}

if let Some(owner) = owner {
config.owner = owner;
}
if let Some(oraiswap_v3) = oraiswap_v3 {
config.oraiswap_v3 = oraiswap_v3;
}
CONFIG.save(deps.storage, &config)?;

Ok(Response::new().add_attribute("action", "update_config"))
}

/// Allows oraiswap_v3_contract can send fund
///
/// # Parameters
/// - `asset`: asset to send.
/// - `receiver`: receiver address
///
/// # Errors
/// - Reverts the call when the caller is an unauthorized user or contract not enough fund
///
fn execute_send_fund(
deps: DepsMut,
info: MessageInfo,
asset: Asset,
receiver: Addr,
) -> Result<Response, ContractError> {
let config = CONFIG.load(deps.storage)?;
if info.sender != config.oraiswap_v3 {
return Err(ContractError::Unauthorized {});
}

let mut msgs: Vec<CosmosMsg> = vec![];
asset.transfer(
&mut msgs,
&MessageInfo {
sender: receiver,
funds: vec![],
},
)?;

Ok(Response::new()
.add_messages(msgs)
.add_attribute("action", "send_fund"))
}

#[cfg_attr(not(feature = "library"), entry_point)]
pub fn query(deps: Deps, _env: Env, msg: QueryMsg) -> StdResult<Binary> {
match msg {
QueryMsg::Config {} => to_json_binary(&CONFIG.load(deps.storage)?),
}
}
2 changes: 2 additions & 0 deletions contracts/incentives-fund-manager/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pub mod contract;
pub mod state;
11 changes: 11 additions & 0 deletions contracts/incentives-fund-manager/src/state.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
use cosmwasm_schema::cw_serde;
use cosmwasm_std::Addr;
use cw_storage_plus::Item;

pub const CONFIG: Item<Config> = Item::new("config");

#[cw_serde]
pub struct Config {
pub owner: Addr,
pub oraiswap_v3: Addr,
}
3 changes: 3 additions & 0 deletions contracts/oraiswap-v3/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ thiserror = { workspace = true }
decimal = { workspace = true }
derive_more = { workspace = true }

oraiswap-v3-common = { workspace = true }

[dev-dependencies]
cw20-base = { workspace = true, features = ["library"] }
cosmwasm-testing-util = { workspace = true }
incentives-fund-manager = { workspace = true }
4 changes: 3 additions & 1 deletion contracts/oraiswap-v3/src/contract.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
#[cfg(not(feature = "library"))]
use cosmwasm_std::entry_point;

use crate::error::ContractError;
use crate::msg::{ExecuteMsg, InstantiateMsg, MigrateMsg, QueryMsg};
use crate::state::CONFIG;
use crate::{entrypoints::*, Config};
use oraiswap_v3_common::error::ContractError;

use cosmwasm_std::{to_json_binary, Binary, Deps, DepsMut, Env, MessageInfo, Response, StdResult};
use cw2::set_contract_version;
Expand All @@ -25,6 +25,7 @@ pub fn instantiate(
fee_tiers: vec![],
admin: info.sender,
protocol_fee: msg.protocol_fee,
incentives_fund_manager: msg.incentives_fund_manager,
};
CONFIG.save(deps.storage, &config)?;

Expand Down Expand Up @@ -193,6 +194,7 @@ pub fn query(deps: Deps, env: Env, msg: QueryMsg) -> StdResult<Binary> {
match msg {
QueryMsg::Admin {} => to_json_binary(&query_admin(deps)?),
QueryMsg::ProtocolFee {} => to_json_binary(&get_protocol_fee(deps)?),
QueryMsg::IncentivesFundManager {} => to_json_binary(&get_incentives_fund_manager(deps)?),
QueryMsg::Position { owner_id, index } => {
to_json_binary(&get_position(deps, owner_id, index)?)
}
Expand Down
8 changes: 6 additions & 2 deletions contracts/oraiswap-v3/src/entrypoints/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,15 @@ use decimal::{CheckedOps, Decimal};

use crate::{
check_tick, compute_swap_step,
interface::{Approval, Asset, AssetInfo, CalculateSwapResult, SwapHop},
interface::{Approval, CalculateSwapResult, SwapHop},
sqrt_price::{get_max_tick, get_min_tick, SqrtPrice},
state::{self},
token_amount::TokenAmount,
ContractError, PoolKey, Position, Tick, UpdatePoolTick, MAX_SQRT_PRICE, MIN_SQRT_PRICE,
PoolKey, Position, Tick, UpdatePoolTick, MAX_SQRT_PRICE, MIN_SQRT_PRICE,
};
use oraiswap_v3_common::{
asset::{Asset, AssetInfo},
error::ContractError,
};

pub trait TimeStampExt {
Expand Down
38 changes: 32 additions & 6 deletions contracts/oraiswap-v3/src/entrypoints/execute.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,23 @@
use crate::error::ContractError;
use crate::fee_growth::FeeGrowth;
use crate::incentive::IncentiveRecord;
use crate::interface::{Asset, AssetInfo, CalculateSwapResult, Cw721ReceiveMsg, SwapHop};
use crate::interface::{CalculateSwapResult, Cw721ReceiveMsg, SwapHop};
use crate::liquidity::Liquidity;
use crate::percentage::Percentage;
use crate::sqrt_price::SqrtPrice;
use crate::state::{self, CONFIG, POOLS};
use crate::token_amount::TokenAmount;
use crate::{calculate_min_amount_out, check_tick, FeeTier, Pool, PoolKey, Position};
use oraiswap_v3_common::asset::{Asset, AssetInfo};
use oraiswap_v3_common::error::ContractError;
use oraiswap_v3_common::incentives_fund_manager;

use super::{
check_can_send, create_tick, remove_tick_and_flip_bitmap, swap_internal, swap_route_internal,
transfer_nft, update_approvals, TimeStampExt,
};
use cosmwasm_std::{attr, Addr, Attribute, Binary, DepsMut, Env, MessageInfo, Response};
use cosmwasm_std::{
attr, wasm_execute, Addr, Attribute, Binary, DepsMut, Env, MessageInfo, Response,
};
use cw20::Expiration;
use decimal::Decimal;

Expand Down Expand Up @@ -540,6 +544,7 @@ pub fn claim_incentives(
index: u32,
) -> Result<Response, ContractError> {
let mut position = state::get_position(deps.storage, &info.sender, index)?;
let config = CONFIG.load(deps.storage)?;

let lower_tick = state::get_tick(deps.storage, &position.pool_key, position.lower_tick_index)?;
let upper_tick = state::get_tick(deps.storage, &position.pool_key, position.upper_tick_index)?;
Expand All @@ -558,7 +563,15 @@ pub fn claim_incentives(

let mut msgs = vec![];
for asset in incentives.clone() {
asset.transfer(&mut msgs, &info)?;
msgs.push(wasm_execute(
config.incentives_fund_manager.clone(),
&incentives_fund_manager::ExecuteMsg::SendFund {
asset,
receiver: info.sender.clone(),
},
vec![],
)?);
// asset.transfer(&mut msgs, &info)?;
}

let mut event_attributes: Vec<Attribute> = vec![];
Expand Down Expand Up @@ -710,8 +723,21 @@ pub fn remove_position(
let mut msgs = vec![];
asset_0.transfer(&mut msgs, &info)?;
asset_1.transfer(&mut msgs, &info)?;
for asset in incentives {
asset.transfer(&mut msgs, &info)?;
// claim incentives
let config = CONFIG.load(deps.storage)?;
for asset in incentives.clone() {
msgs.push(
wasm_execute(
config.incentives_fund_manager.clone(),
&incentives_fund_manager::ExecuteMsg::SendFund {
asset,
receiver: info.sender.clone(),
},
vec![],
)?
.into(),
);
// asset.transfer(&mut msgs, &info)?;
}

event_attributes.append(&mut vec![
Expand Down
Loading

0 comments on commit fb3d776

Please sign in to comment.