Skip to content

Commit

Permalink
updated IBC verification
Browse files Browse the repository at this point in the history
  • Loading branch information
M-Daeva committed Sep 17, 2023
1 parent af86673 commit a055903
Show file tree
Hide file tree
Showing 22 changed files with 322 additions and 221 deletions.
9 changes: 8 additions & 1 deletion Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ cw-multi-test = "0.16.5"
speculoos = "0.11.0"
kujira = { git = "https://github.com/Team-Kujira/kujira-rs" }
osmosis-std = "0.19.1"
bech32 = "0.9.1"

eldorado-base = { path = "./packages/eldorado-base" }
mantaswap-mocks = { path = "./packages/mantaswap-mocks" }
Expand Down
4 changes: 2 additions & 2 deletions config/kujira-testnet-config.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
"PREFIX": "kujira",
"CHAIN_ID": "harpoon-4",
"RPC": "https://kujira-testnet-rpc.polkachu.com:443",
"CONTRACT_CODE": "2422",
"CONTRACT_ADDRESS": "kujira1mmhrcaxwe6qtcpnd7ua46qywwyndhpcfzrn8atyslfcwttjwkrcsq0v6f7"
"CONTRACT_CODE": "2448",
"CONTRACT_ADDRESS": "kujira1wlkn8px6y5jfp9suqusdjmnhysd4594qsju87694gl5mlckjwscs87sfdg"
}
8 changes: 7 additions & 1 deletion config/osmosis-testnet-config.json
Original file line number Diff line number Diff line change
@@ -1 +1,7 @@
{ "PREFIX":"osmo", "CHAIN_ID":"osmo-test-5", "RPC":"https://rpc.osmotest5.osmosis.zone:443", "CONTRACT_CODE":"4257", "CONTRACT_ADDRESS":"osmo1n9qe5jjq3w3mc4pgz7jswknrluq6dwzdw2hzc8apddqw4k0p2ulqg5xxee" }
{
"PREFIX": "osmo",
"CHAIN_ID": "osmo-test-5",
"RPC": "https://rpc.osmotest5.osmosis.zone:443",
"CONTRACT_CODE": "4288",
"CONTRACT_ADDRESS": "osmo1pmhdtae4zyvjveva9f6a9tgenslyr46w5ws67u7447473cn9gqxqhl92d7"
}
2 changes: 1 addition & 1 deletion contracts/eldorado-aggregator-kujira/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "eldorado-aggregator-kujira"
version = "1.0.0"
version = "1.1.0"
authors = ["M. Daeva <[email protected]>"]
edition = "2021"

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"contract_name": "eldorado-aggregator-kujira",
"contract_version": "1.0.0",
"contract_version": "1.1.0",
"idl_version": "1.0.0",
"instantiate": {
"$schema": "http://json-schema.org/draft-07/schema#",
Expand Down Expand Up @@ -47,7 +47,7 @@
"additionalProperties": false
},
{
"description": "Called by vault to: 1) swap native Kuji -> token on Kujira (or don't swap if Kuji is asked asset) 2) send token on Kujira to user address on Kujira or other Cosmos network",
"description": "Called by vault to: 1) swap native Kuji -> token on Kujira 2) send token on Kujira to user address on Kujira or other Cosmos network",
"type": "object",
"required": [
"swap_out"
Expand Down Expand Up @@ -272,13 +272,17 @@
"type": "object",
"required": [
"admin",
"chain_id",
"ibc_timeout",
"router"
],
"properties": {
"admin": {
"$ref": "#/definitions/Addr"
},
"chain_id": {
"type": "string"
},
"ibc_timeout": {
"type": "integer",
"format": "uint64",
Expand Down
128 changes: 81 additions & 47 deletions contracts/eldorado-aggregator-kujira/src/actions/execute.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
use cosmwasm_std::{
to_binary, Coin, CosmosMsg, Deps, DepsMut, Env, MessageInfo, Response, SubMsg, WasmMsg,
to_binary, Addr, Coin, CosmosMsg, Deps, DepsMut, Env, MessageInfo, Response, SubMsg, WasmMsg,
};

use cw_utils::{must_pay, nonpayable, one_coin};

use eldorado_base::{
converters::get_addr_by_prefix,
eldorado_aggregator_kujira::state::{
Config, CONFIG, DENOM_KUJI, RECIPIENT_PARAMETERS, SWAP_IN_REPLY, SWAP_OUT_REPLY,
Config, BASE_DENOM, BASE_PREFIX, CHAIN_ID_DEV, CONFIG, RECIPIENT_PARAMETERS, SWAP_IN_REPLY,
SWAP_OUT_REPLY,
},
error::ContractError,
mantaswap,
Expand All @@ -22,25 +24,15 @@ pub fn try_swap_in(
) -> Result<Response, ContractError> {
let coin = one_coin(&info).map_err(|e| ContractError::CustomError { val: e.to_string() })?;
let wasm_msg = get_wasm_msg(
&deps.as_ref(),
env.contract.address.as_str(),
deps,
&env,
&vault_address,
&mantaswap_msg,
&vec![coin],
&None,
)?;
let submsg = SubMsg::reply_on_success(CosmosMsg::Wasm(wasm_msg), SWAP_IN_REPLY);

RECIPIENT_PARAMETERS.update(
deps.storage,
|mut x| -> Result<Vec<RecipientParameters>, ContractError> {
x.push(RecipientParameters {
recipient_address: deps.api.addr_validate(&vault_address)?,
channel_id: None,
});

Ok(x)
},
)?;

Ok(Response::new()
.add_submessage(submsg)
.add_attributes([("action", "try_swap_in")]))
Expand All @@ -54,34 +46,22 @@ pub fn try_swap_out(
mantaswap_msg: mantaswap::msg::ExecuteMsg,
channel_id: Option<String>,
) -> Result<Response, ContractError> {
verify_ibc_parameters(&mantaswap_msg, &channel_id)?;

let amount = must_pay(&info, DENOM_KUJI)
let amount = must_pay(&info, BASE_DENOM)
.map_err(|e| ContractError::CustomError { val: e.to_string() })?;
let coin = Coin {
denom: DENOM_KUJI.to_string(),
denom: BASE_DENOM.to_string(),
amount,
};
let wasm_msg = get_wasm_msg(
&deps.as_ref(),
env.contract.address.as_str(),
deps,
&env,
&user_address,
&mantaswap_msg,
&vec![coin],
&channel_id,
)?;
let submsg = SubMsg::reply_on_success(CosmosMsg::Wasm(wasm_msg), SWAP_OUT_REPLY);

RECIPIENT_PARAMETERS.update(
deps.storage,
|mut x| -> Result<Vec<RecipientParameters>, ContractError> {
x.push(RecipientParameters {
recipient_address: deps.api.addr_validate(&user_address)?,
channel_id,
});

Ok(x)
},
)?;

Ok(Response::new()
.add_submessage(submsg)
.add_attributes([("action", "try_swap_out")]))
Expand Down Expand Up @@ -123,22 +103,37 @@ pub fn try_update_config(
}

fn get_wasm_msg(
deps: &Deps,
deps: DepsMut,
env: &Env,
recipient_address: &str,
mantaswap_msg: &mantaswap::msg::ExecuteMsg,
funds: &Vec<Coin>,
channel_id: &Option<String>,
) -> Result<WasmMsg, ContractError> {
let recipient = deps.api.addr_validate(recipient_address)?;
let router = CONFIG.load(deps.storage)?.router;

let recipient_address = verify_ibc_parameters(
deps.as_ref(),
env,
mantaswap_msg,
channel_id,
recipient_address,
)?;

let mut min_return_funds: Option<Vec<Coin>> = None;

let swap_msg = match mantaswap_msg {
mantaswap::msg::ExecuteMsg::Swap {
stages, min_return, ..
} => mantaswap::msg::ExecuteMsg::Swap {
stages: stages.to_owned(),
recipient: Some(recipient),
min_return: min_return.to_owned(),
},
} => {
min_return_funds = min_return.to_owned();

mantaswap::msg::ExecuteMsg::Swap {
stages: stages.to_owned(),
recipient: Some(env.contract.address.clone()),
min_return: min_return.to_owned(),
}
}
_ => Err(ContractError::WrongMantaswapMsg)?,
};

Expand All @@ -148,25 +143,64 @@ fn get_wasm_msg(
funds: funds.to_owned(),
};

let min_return_funds = min_return_funds.ok_or(ContractError::CoinIsNotFound)?;
let Coin { denom, .. } = min_return_funds
.get(0)
.ok_or(ContractError::CoinIsNotFound)?;

RECIPIENT_PARAMETERS.update(
deps.storage,
|mut x| -> Result<Vec<RecipientParameters>, ContractError> {
x.push(RecipientParameters {
recipient_address,
channel_id: channel_id.to_owned(),
denom_out: denom.to_string(),
});

Ok(x)
},
)?;

Ok(wasm_msg)
}

fn verify_ibc_parameters(
deps: Deps,
env: &Env,
mantaswap_msg: &mantaswap::msg::ExecuteMsg,
channel_id: &Option<String>,
) -> Result<(), ContractError> {
recipient_address: &str,
) -> Result<Addr, ContractError> {
let address_parts = recipient_address.split('1').collect::<Vec<&str>>();
let prefix = address_parts
.first()
.ok_or(ContractError::PrefixIsNotFound)?;

match mantaswap_msg {
mantaswap::msg::ExecuteMsg::Swap {
min_return: Some(coins),
..
} => {
let Coin { denom, .. } = coins.get(0).ok_or(ContractError::CoinIsNotFound)?;

if channel_id.is_some() && !denom.contains("ibc/") {
Err(ContractError::AssetIsNotIbcToken)?;
let Coin { denom, .. } = coins.first().ok_or(ContractError::CoinIsNotFound)?;

if channel_id.is_some() && !(denom.contains("ibc/") && (prefix != &BASE_PREFIX)) {
Err(ContractError::WrongIbcParameters {
prefix: prefix.to_string(),
ibc_token: denom.to_string(),
channel_id: channel_id.to_owned(),
})?;
}

Ok(())
let address = if env.block.chain_id == CHAIN_ID_DEV {
deps.api.addr_validate(recipient_address)?
} else {
deps.api
.addr_validate(&get_addr_by_prefix(recipient_address, BASE_PREFIX)?)?;

Addr::unchecked(recipient_address)
};

Ok(address)
}
_ => Err(ContractError::WrongMantaswapMsg)?,
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,17 @@ const CONTRACT_VERSION: &str = env!("CARGO_PKG_VERSION");

pub fn try_instantiate(
deps: DepsMut,
_env: Env,
env: Env,
info: MessageInfo,
msg: InstantiateMsg,
) -> Result<Response, ContractError> {
let admin = &info.sender;
let router = &deps.api.addr_validate(&msg.router_address)?;

CONFIG.save(deps.storage, &Config::new(admin, router))?;
CONFIG.save(
deps.storage,
&Config::new(admin, router, &env.block.chain_id),
)?;

RECIPIENT_PARAMETERS.save(deps.storage, &vec![])?;

Expand Down
1 change: 1 addition & 0 deletions contracts/eldorado-aggregator-kujira/src/actions/other.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ fn parse_attributes(
let RecipientParameters {
recipient_address,
channel_id,
..
} = recipient_parameters_list
.get(0)
.ok_or(ContractError::RecipientParametersAreNotFound)?;
Expand Down
Loading

0 comments on commit a055903

Please sign in to comment.