Skip to content

Commit

Permalink
Merge pull request #5 from oraichain/feat/incentive
Browse files Browse the repository at this point in the history
Feat/incentive
  • Loading branch information
trung2891 authored Aug 1, 2024
2 parents d31b30c + 8d37109 commit 3f28364
Show file tree
Hide file tree
Showing 37 changed files with 3,743 additions and 877 deletions.
951 changes: 373 additions & 578 deletions Cargo.lock

Large diffs are not rendered by default.

15 changes: 5 additions & 10 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ members = ["packages/*", "contracts/*", "wasm"]


[workspace.package]
version = "0.1.0"
version = "0.2.0"
authors = ["Oraichain Labs"]
edition = "2021"
license = "MIT"
Expand All @@ -19,19 +19,17 @@ exclude = [
]

[workspace.dependencies]
cosmwasm-std = { version = "=1.2" }
cosmwasm-schema = { version = "=1.2" }
cosmwasm-storage = { version = "=1.2" }
cosmwasm-vm = { version = "=1.2" }
cosmwasm-std = { version = "1.5" }
cosmwasm-schema = { version = "1.5" }
cosmwasm-storage = { version = "1.5" }
cosmwasm-vm = { version = "1.5" }

thiserror = "1.0.26"

cw2 = { version = "1.0.1" }
cw20 = { version = "1.0.1" }
cw20-base = { version = "1.0.1" }
cw-storage-plus = { version = "1.0.1" }
cw-multi-test = "0.16.6"
cw-utils = "0.16.0"
derive_more = "0.99.17"
decimal-core = { path = "./packages/decimal-core" }
decimal = { path = "./packages/decimal" }
Expand All @@ -46,6 +44,3 @@ rpath = false
lto = true
overflow-checks = true
panic = 'abort'

[patch.crates-io]
curve25519-dalek = { git = 'https://github.com/dalek-cryptography/curve25519-dalek.git' }
4 changes: 1 addition & 3 deletions contracts/oraiswap-v3/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,10 @@ cosmwasm-storage = { workspace = true }
cw-storage-plus = { workspace = true }
cw2 = { workspace = true }
cw20 = { workspace = true }
cw-utils = { workspace = true }
thiserror = { workspace = true }
decimal = { workspace = true }
derive_more = { workspace = true }

[dev-dependencies]
cw-multi-test = { workspace = true }
cw20-base = { workspace = true, features = ["library"] }
cosmwasm-testing-util = { git = "https://github.com/oraichain/cosmwasm-testing-util.git", rev = "aaa09cf" }
cosmwasm-testing-util = { git = "https://github.com/oraichain/cosmwasm-testing-util.git", rev = "1b9c412" }
147 changes: 97 additions & 50 deletions contracts/oraiswap-v3/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::msg::{ExecuteMsg, InstantiateMsg, MigrateMsg, QueryMsg};
use crate::state::CONFIG;
use crate::{entrypoints::*, Config};

use cosmwasm_std::{to_binary, Binary, Deps, DepsMut, Env, MessageInfo, Response, StdResult};
use cosmwasm_std::{to_json_binary, Binary, Deps, DepsMut, Env, MessageInfo, Response, StdResult};
use cw2::set_contract_version;

// version info for migration info
Expand Down Expand Up @@ -109,6 +109,7 @@ pub fn execute(
init_tick,
} => create_pool(
deps,
info,
env,
token_0,
token_1,
Expand Down Expand Up @@ -151,44 +152,81 @@ pub fn execute(
extension.slippage_limit_lower,
extension.slippage_limit_upper,
),
ExecuteMsg::CreateIncentive {
pool_key,
reward_token,
total_reward,
reward_per_sec,
start_timestamp,
} => create_incentive(
deps,
env,
info,
pool_key,
reward_token,
total_reward,
reward_per_sec,
start_timestamp,
),
ExecuteMsg::ClaimIncentive { index } => claim_incentives(deps, env, info, index),
ExecuteMsg::UpdateIncentive {
pool_key,
incentive_id,
remaining_reward,
start_timestamp,
reward_per_sec,
} => update_incentive(
deps,
env,
info,
pool_key,
incentive_id,
remaining_reward,
start_timestamp,
reward_per_sec,
),
}
}

#[cfg_attr(not(feature = "library"), entry_point)]
pub fn query(deps: Deps, env: Env, msg: QueryMsg) -> StdResult<Binary> {
match msg {
QueryMsg::Admin {} => to_binary(&query_admin(deps)?),
QueryMsg::ProtocolFee {} => to_binary(&get_protocol_fee(deps)?),
QueryMsg::Position { owner_id, index } => to_binary(&get_position(deps, owner_id, index)?),
QueryMsg::Admin {} => to_json_binary(&query_admin(deps)?),
QueryMsg::ProtocolFee {} => to_json_binary(&get_protocol_fee(deps)?),
QueryMsg::Position { owner_id, index } => {
to_json_binary(&get_position(deps, owner_id, index)?)
}
QueryMsg::Positions {
owner_id,
limit,
offset,
} => to_binary(&get_positions(deps, owner_id, limit, offset)?),
QueryMsg::FeeTierExist { fee_tier } => to_binary(&fee_tier_exist(deps, fee_tier)?),
} => to_json_binary(&get_positions(deps, owner_id, limit, offset)?),
QueryMsg::FeeTierExist { fee_tier } => to_json_binary(&fee_tier_exist(deps, fee_tier)?),
QueryMsg::Pool {
token_0,
token_1,
fee_tier,
} => to_binary(&get_pool(deps, token_0, token_1, fee_tier)?),
QueryMsg::Pools { limit, start_after } => to_binary(&get_pools(deps, limit, start_after)?),
QueryMsg::Tick { key, index } => to_binary(&get_tick(deps, key, index)?),
} => to_json_binary(&get_pool(deps, token_0, token_1, fee_tier)?),
QueryMsg::Pools { limit, start_after } => {
to_json_binary(&get_pools(deps, limit, start_after)?)
}
QueryMsg::Tick { key, index } => to_json_binary(&get_tick(deps, key, index)?),
QueryMsg::IsTickInitialized { key, index } => {
to_binary(&is_tick_initialized(deps, key, index)?)
to_json_binary(&is_tick_initialized(deps, key, index)?)
}
QueryMsg::FeeTiers {} => to_binary(&get_fee_tiers(deps)?),
QueryMsg::FeeTiers {} => to_json_binary(&get_fee_tiers(deps)?),
QueryMsg::PositionTicks { owner, offset } => {
to_binary(&get_position_ticks(deps, owner, offset)?)
to_json_binary(&get_position_ticks(deps, owner, offset)?)
}
QueryMsg::UserPositionAmount { owner } => {
to_binary(&get_user_position_amount(deps, owner)?)
to_json_binary(&get_user_position_amount(deps, owner)?)
}
QueryMsg::TickMap {
pool_key,
lower_tick_index,
upper_tick_index,
x_to_y,
} => to_binary(&get_tickmap(
} => to_json_binary(&get_tickmap(
deps,
pool_key,
lower_tick_index,
Expand All @@ -198,24 +236,24 @@ pub fn query(deps: Deps, env: Env, msg: QueryMsg) -> StdResult<Binary> {
QueryMsg::LiquidityTicks {
pool_key,
tick_indexes,
} => to_binary(&get_liquidity_ticks(deps, pool_key, tick_indexes)?),
} => to_json_binary(&get_liquidity_ticks(deps, pool_key, tick_indexes)?),
QueryMsg::LiquidityTicksAmount {
pool_key,
lower_tick,
upper_tick,
} => to_binary(&get_liquidity_ticks_amount(
} => to_json_binary(&get_liquidity_ticks_amount(
deps, pool_key, lower_tick, upper_tick,
)?),
QueryMsg::PoolsForPair { token_0, token_1 } => {
to_binary(&get_all_pools_for_pair(deps, token_0, token_1)?)
to_json_binary(&get_all_pools_for_pair(deps, token_0, token_1)?)
}
QueryMsg::Quote {
pool_key,
x_to_y,
amount,
by_amount_in,
sqrt_price_limit,
} => to_binary(&quote(
} => to_json_binary(&quote(
deps,
env,
pool_key,
Expand All @@ -225,12 +263,12 @@ pub fn query(deps: Deps, env: Env, msg: QueryMsg) -> StdResult<Binary> {
sqrt_price_limit,
)?),
QueryMsg::QuoteRoute { amount_in, swaps } => {
to_binary(&quote_route(deps, env, amount_in, swaps)?)
to_json_binary(&quote_route(deps, env, amount_in, swaps)?)
}
QueryMsg::OwnerOf {
token_id,
include_expired,
} => to_binary(&query_owner_of(
} => to_json_binary(&query_owner_of(
deps,
env,
token_id,
Expand All @@ -241,19 +279,19 @@ pub fn query(deps: Deps, env: Env, msg: QueryMsg) -> StdResult<Binary> {
include_expired,
start_after,
limit,
} => to_binary(&query_all_approvals(
} => to_json_binary(&query_all_approvals(
deps,
env,
owner,
include_expired.unwrap_or(false),
start_after,
limit,
)?),
QueryMsg::NftInfo { token_id } => to_binary(&query_nft_info(deps, token_id)?),
QueryMsg::NftInfo { token_id } => to_json_binary(&query_nft_info(deps, token_id)?),
QueryMsg::AllNftInfo {
token_id,
include_expired,
} => to_binary(&query_all_nft_info(
} => to_json_binary(&query_all_nft_info(
deps,
env,
token_id,
Expand All @@ -263,43 +301,52 @@ pub fn query(deps: Deps, env: Env, msg: QueryMsg) -> StdResult<Binary> {
owner,
start_after,
limit,
} => to_binary(&query_tokens(deps, owner, start_after, limit)?),
} => to_json_binary(&query_tokens(deps, owner, start_after, limit)?),
QueryMsg::AllTokens { start_after, limit } => {
to_binary(&query_all_tokens(deps, start_after, limit)?)
to_json_binary(&query_all_tokens(deps, start_after, limit)?)
}
QueryMsg::NumTokens {} => to_json_binary(&query_num_tokens(deps)?),
QueryMsg::PositionIncentives { owner_id, index } => {
to_json_binary(&query_position_incentives(deps, env, owner_id, index)?)
}
QueryMsg::PoolsByPoolKeys { pool_keys } => {
to_json_binary(&get_pools_with_pool_keys(deps, pool_keys)?)
}
QueryMsg::AllPosition { limit, start_after } => {
to_json_binary(&query_all_positions(deps, limit, start_after)?)
}
QueryMsg::NumTokens {} => to_binary(&query_num_tokens(deps)?),
}
}

#[cfg_attr(not(feature = "library"), entry_point)]
pub fn migrate(deps: DepsMut, _env: Env, _msg: MigrateMsg) -> Result<Response, ContractError> {
let original_version =
cw_utils::ensure_from_older_version(deps.storage, CONTRACT_NAME, CONTRACT_VERSION)?;
cw2::ensure_from_older_version(deps.storage, CONTRACT_NAME, CONTRACT_VERSION)?;

// query all position, then update token id
let positions: Vec<_> = crate::state::POSITIONS
.range_raw(deps.storage, None, None, cosmwasm_std::Order::Ascending)
.collect();
let mut token_id = 0;
for item in positions {
if let Ok((key, mut position)) = item {
token_id += 1;
position.token_id = token_id;
let account_id = &key[..key.len() - 4];
let index = u32::from_be_bytes(key[key.len() - 4..].try_into().unwrap());
// update position and its index
crate::state::POSITIONS.save(deps.storage, &key, &position)?;
crate::state::POSITION_KEYS_BY_TOKEN_ID.save(
deps.storage,
position.token_id,
&(account_id.to_vec(), index),
)?;
}
}
// // query all position, then update token id
// let positions: Vec<_> = crate::state::POSITIONS
// .range_raw(deps.storage, None, None, cosmwasm_std::Order::Ascending)
// .collect();
// let mut token_id = 0;
// for item in positions {
// if let Ok((key, mut position)) = item {
// token_id += 1;
// position.token_id = token_id;
// let account_id = &key[..key.len() - 4];
// let index = u32::from_be_bytes(key[key.len() - 4..].try_into().unwrap());
// // update position and its index
// crate::state::POSITIONS.save(deps.storage, &key, &position)?;
// crate::state::POSITION_KEYS_BY_TOKEN_ID.save(
// deps.storage,
// position.token_id,
// &(account_id.to_vec(), index),
// )?;
// }
// }

// update total token id, first time token count is total token ids
crate::state::TOKEN_COUNT.save(deps.storage, &token_id)?;
crate::state::TOKEN_ID.save(deps.storage, &token_id)?;
// // update total token id, first time token count is total token ids
// crate::state::TOKEN_COUNT.save(deps.storage, &token_id)?;
// crate::state::TOKEN_ID.save(deps.storage, &token_id)?;

Ok(Response::new().add_attribute("new_version", original_version.to_string()))
}
4 changes: 2 additions & 2 deletions contracts/oraiswap-v3/src/entrypoints/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -230,8 +230,8 @@ pub fn swap_internal(
amount: calculate_swap_result.amount_out.into(),
};

asset_0.transfer_from(msgs, &info, contract_address.to_string())?;
asset_1.transfer(msgs, &info)?;
asset_0.transfer_from(msgs, info, contract_address.to_string())?;
asset_1.transfer(msgs, info)?;

Ok(calculate_swap_result)
}
Expand Down
Loading

0 comments on commit 3f28364

Please sign in to comment.