Skip to content

Commit

Permalink
finising converting
Browse files Browse the repository at this point in the history
  • Loading branch information
tubackkhoa committed Nov 3, 2022
1 parent 5c00c03 commit 1e690dd
Show file tree
Hide file tree
Showing 35 changed files with 704 additions and 1,372 deletions.
44 changes: 44 additions & 0 deletions 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 Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[workspace]
members = ["packages/*", "contracts/oraiswap_token","contracts/oraiswap_oracle","contracts/oraiswap_pair","contracts/oraiswap_factory","contracts/oraiswap_router"]
members = ["packages/*", "contracts/*"]

[profile.release]
opt-level = 3
Expand Down
34 changes: 16 additions & 18 deletions contracts/oraiswap_converter/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ pub fn update_config(deps: DepsMut, info: MessageInfo, owner: Addr) -> StdResult

store_config(deps.storage, &config)?;

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

fn div_ratio_decimal(nominator: Uint128, denominator: Decimal) -> Uint128 {
Expand Down Expand Up @@ -93,8 +93,8 @@ pub fn receive_cw20(

Ok(Response::new().add_message(message).add_attributes(vec![
("action", "convert_token"),
("from_amount", cw20_msg.amount),
("to_amount", amount),
("from_amount", &cw20_msg.amount.to_string()),
("to_amount", &amount.to_string()),
]))
}
Ok(Cw20HookMsg::ConvertReverse { from }) => {
Expand All @@ -120,8 +120,8 @@ pub fn receive_cw20(

Ok(Response::new().add_message(message).add_attributes(vec![
("action", "convert_token_reverse"),
("from_amount", cw20_msg.amount),
("to_amount", amount),
("from_amount", &cw20_msg.amount.to_string()),
("to_amount", &amount.to_string()),
]))
} else {
return Err(StdError::generic_err("invalid cw20 hook message"));
Expand Down Expand Up @@ -154,7 +154,7 @@ pub fn update_pair(

store_token_ratio(deps.storage, &asset_key, &token_ratio)?;

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

pub fn unregister_pair(deps: DepsMut, info: MessageInfo, from: TokenInfo) -> StdResult<Response> {
Expand All @@ -167,25 +167,23 @@ pub fn unregister_pair(deps: DepsMut, info: MessageInfo, from: TokenInfo) -> Std

token_ratio_remove(deps.storage, &asset_key);

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

pub fn convert(deps: DepsMut, _env: Env, info: MessageInfo) -> StdResult<Response> {
let mut messages: Vec<CosmosMsg> = vec![];
let mut attributes: Vec<Attribute> = vec![];
attributes.push(("action", "convert_token"));
attributes.push(("action", "convert_token").into());

for native_coin in info.funds {
let asset_key = native_coin.denom.as_bytes();
let amount = native_coin.amount;
attributes.extend(vec![
("denom", native_coin.denom.clone()),
("from_amount", amount.clone()),
]);
attributes.push(("denom", native_coin.denom.clone()).into());
attributes.push(("from_amount", amount.to_string()).into());
let token_ratio = read_token_ratio(deps.storage, asset_key)?;
let to_amount = amount * token_ratio.ratio;

attributes.push(("to_amount", to_amount));
attributes.push(("to_amount", to_amount).into());

let message = Asset {
info: token_ratio.info,
Expand Down Expand Up @@ -226,9 +224,9 @@ pub fn convert_reverse(

Ok(Response::new().add_message(message).add_attributes(vec![
("action", "convert_token_reverse"),
("denom", native_coin.denom.clone()),
("from_amount", native_coin.amount.clone()),
("to_amount", amount),
("denom", native_coin.denom.as_str()),
("from_amount", &native_coin.amount.to_string()),
("to_amount", &amount.to_string()),
]))
} else {
return Err(StdError::generic_err("invalid cw20 hook message"));
Expand Down Expand Up @@ -270,7 +268,7 @@ pub fn withdraw_tokens(
return Err(StdError::generic_err("unauthorized"));
}
let mut messages: Vec<CosmosMsg> = vec![];
let mut attributes: Vec<Attribute> = vec![("action", "withdraw_tokens")];
let mut attributes: Vec<Attribute> = vec![("action", "withdraw_tokens").into()];

for asset in asset_infos {
let balance = asset.query_pool(&deps.querier, env.contract.address.clone())?;
Expand All @@ -280,7 +278,7 @@ pub fn withdraw_tokens(
}
.into_msg(None, &deps.querier, owner.clone())?;
messages.push(message);
attributes.push(("amount", balance.to_string()))
attributes.push(("amount", balance.to_string()).into())
}

Ok(Response::new()
Expand Down
7 changes: 1 addition & 6 deletions contracts/oraiswap_factory/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,7 @@ The factory contract can perform creation of oraiswap pair contract and also be
```json
{
"paid_code_id": "123",
"token_code_id": "123",
"init_hook": {
"msg": "123",
"contract_addr": "orai..."
}
"token_code_id": "123"
}
```

Expand Down Expand Up @@ -109,7 +105,6 @@ Register verified pair contract and token contract for pair contract creation. T
/// Pair contract code ID, which is used to
pub pair_code_id: u64,
pub token_code_id: u64,
pub init_hook: Option<InitHook>,
}
```

Expand Down
8 changes: 4 additions & 4 deletions contracts/oraiswap_factory/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,13 @@ pub fn execute(
owner,
token_code_id,
pair_code_id,
} => handle_update_config(deps, env, info, owner, token_code_id, pair_code_id),
ExecuteMsg::CreatePair { asset_infos } => handle_create_pair(deps, env, info, asset_infos),
} => execute_update_config(deps, env, info, owner, token_code_id, pair_code_id),
ExecuteMsg::CreatePair { asset_infos } => execute_create_pair(deps, env, info, asset_infos),
}
}

// Only owner can execute it
pub fn handle_update_config(
pub fn execute_update_config(
deps: DepsMut,
_env: Env,
info: MessageInfo,
Expand Down Expand Up @@ -94,7 +94,7 @@ pub fn handle_update_config(
}

// Anyone can execute it to create swap pair
pub fn handle_create_pair(
pub fn execute_create_pair(
deps: DepsMut,
env: Env,
_info: MessageInfo,
Expand Down
2 changes: 1 addition & 1 deletion contracts/oraiswap_oracle/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ crate-type = ["cdylib", "rlib"]
[features]
# for more explicit tests, cargo test --features=backtraces
backtraces = ["cosmwasm-std/backtraces"]
# use library feature to disable all init/handle/query exports
# use library feature to disable all init/execute/query exports
library = []

[dependencies]
Expand Down
20 changes: 10 additions & 10 deletions contracts/oraiswap_oracle/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,15 +67,15 @@ pub fn execute(
ExecuteMsg::UpdateExchangeRate {
denom,
exchange_rate,
} => handle_update_exchange_rate(deps, info, denom, exchange_rate),
ExecuteMsg::DeleteExchangeRate { denom } => handle_delete_exchange_rate(deps, info, denom),
ExecuteMsg::UpdateTaxCap { cap, denom } => handle_update_tax_cap(deps, info, denom, cap),
ExecuteMsg::UpdateTaxRate { rate } => handle_update_tax_rate(deps, info, rate),
ExecuteMsg::UpdateAdmin { admin } => handle_update_admin(deps, info, admin),
} => execute_update_exchange_rate(deps, info, denom, exchange_rate),
ExecuteMsg::DeleteExchangeRate { denom } => execute_delete_exchange_rate(deps, info, denom),
ExecuteMsg::UpdateTaxCap { cap, denom } => execute_update_tax_cap(deps, info, denom, cap),
ExecuteMsg::UpdateTaxRate { rate } => execute_update_tax_rate(deps, info, rate),
ExecuteMsg::UpdateAdmin { admin } => execute_update_admin(deps, info, admin),
}
}

pub fn handle_update_tax_cap(
pub fn execute_update_tax_cap(
deps: DepsMut,
info: MessageInfo,
denom: String,
Expand All @@ -96,7 +96,7 @@ pub fn handle_update_tax_cap(
Ok(Response::default())
}

pub fn handle_update_tax_rate(
pub fn execute_update_tax_rate(
deps: DepsMut,
info: MessageInfo,
rate: Decimal,
Expand All @@ -117,7 +117,7 @@ pub fn handle_update_tax_rate(
Ok(Response::default())
}

pub fn handle_update_admin(
pub fn execute_update_admin(
deps: DepsMut,
info: MessageInfo,
admin: Addr,
Expand All @@ -138,7 +138,7 @@ pub fn handle_update_admin(
Ok(Response::default())
}

pub fn handle_update_exchange_rate(
pub fn execute_update_exchange_rate(
deps: DepsMut,
info: MessageInfo,
denom: String,
Expand All @@ -157,7 +157,7 @@ pub fn handle_update_exchange_rate(
Ok(Response::default())
}

pub fn handle_delete_exchange_rate(
pub fn execute_delete_exchange_rate(
deps: DepsMut,
info: MessageInfo,
denom: String,
Expand Down
5 changes: 2 additions & 3 deletions contracts/oraiswap_pair/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

This is mainly used from oraiswap factory contract to create new oraiswap pair. It initialize all swap created parameters which can be updated later with owner key.

It creates liquidity token contract as init response, and execute init hook to register created liquidity token contract to self.
It creates liquidity token contract as init response, and execute reply on success to register created liquidity token contract to self.

```rust
{
Expand All @@ -16,8 +16,7 @@ It creates liquidity token contract as init response, and execute init hook to r
pub token_code_id: u64,
/// Oracle contract address for global parameters
pub oracle_addr: Addr,
/// Hook for post initalization, such as update the instance contract address to proxy contract map
pub init_hook: Option<InitHook>,

}
```

Expand Down
6 changes: 3 additions & 3 deletions contracts/oraiswap_rewarder/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,11 @@ pub fn update_config(

store_config(deps.storage, &config)?;

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

/// Distribute
/// Anyone can handle distribute operation to distribute
/// Anyone can execute distribute operation to distribute
pub fn distribute(deps: DepsMut, env: Env, asset_infos: Vec<AssetInfo>) -> StdResult<Response> {
let config: Config = read_config(deps.storage)?;
let staking_contract = deps.api.addr_humanize(&config.staking_contract)?;
Expand Down Expand Up @@ -128,7 +128,7 @@ pub fn distribute(deps: DepsMut, env: Env, asset_infos: Vec<AssetInfo>) -> StdRe
msg: to_binary(&StakingExecuteMsg::DepositReward { rewards })?,
funds: vec![],
}))
.add_attribute(("action", "distribute")))
.add_attribute("action", "distribute"))
}

#[cfg_attr(not(feature = "library"), entry_point)]
Expand Down
2 changes: 1 addition & 1 deletion contracts/oraiswap_router/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ If the token is ow20 Orai, we can convert it to native first

```
{
"handle_swap_operations":{
"execute_swap_operations":{
"operations":[
{
"orai_swap":{
Expand Down
13 changes: 5 additions & 8 deletions contracts/oraiswap_router/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use cosmwasm_std::{
};
use oraiswap::error::ContractError;

use crate::operations::{handle_swap_operation, handle_swap_operations};
use crate::operations::{execute_swap_operation, execute_swap_operations};
use crate::state::{Config, CONFIG};

use cw20::Cw20ReceiveMsg;
Expand Down Expand Up @@ -50,9 +50,9 @@ pub fn execute(
operations,
minimum_receive,
to,
} => handle_swap_operations(deps, env, info.sender, operations, minimum_receive, to),
} => execute_swap_operations(deps, env, info.sender, operations, minimum_receive, to),
ExecuteMsg::ExecuteSwapOperation { operation, to } => {
handle_swap_operation(deps, env, info, operation, to)
execute_swap_operation(deps, env, info, operation, to)
}

ExecuteMsg::AssertMinimumReceive {
Expand Down Expand Up @@ -85,11 +85,8 @@ pub fn receive_cw20(
minimum_receive,
to,
} => {
let receiver = match to {
Some(addr) => deps.api.addr_validate(addr.as_str()).ok(),
None => None,
};
handle_swap_operations(deps, env, sender, operations, minimum_receive, receiver)
let receiver = to.map_or(None, |addr| deps.api.addr_validate(addr.as_str()).ok());
execute_swap_operations(deps, env, sender, operations, minimum_receive, receiver)
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions contracts/oraiswap_router/src/operations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use oraiswap::router::{ExecuteMsg, SwapOperation};

/// Execute swap operation
/// swap all offer asset to ask asset
pub fn handle_swap_operation(
pub fn execute_swap_operation(
deps: DepsMut,
env: Env,
info: MessageInfo,
Expand Down Expand Up @@ -74,7 +74,7 @@ pub fn handle_swap_operation(
Ok(Response::new().add_messages(messages))
}

pub fn handle_swap_operations(
pub fn execute_swap_operations(
deps: DepsMut,
env: Env,
sender: Addr,
Expand Down
Loading

0 comments on commit 1e690dd

Please sign in to comment.