Skip to content

Commit

Permalink
feat: add support for injective ethereum bridged (peggy) assets
Browse files Browse the repository at this point in the history
* feat: add injective peggy addr validation for native assets

* chore: disable injective feature temporarily

* chore: fix injective feature flag across the board

* chore: allow creating peggy vaults

* refactor: remove unwraps in pool router contract

* ci: fix ci workflow to test injective the feature
  • Loading branch information
kerber0x authored Dec 2, 2022
1 parent e68bcd5 commit 80b3114
Show file tree
Hide file tree
Showing 19 changed files with 253 additions and 25 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci-test-fmt-check.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ jobs:
uses: actions-rs/[email protected]
with:
version: '0.15.0'
args: '-- --test-threads 1'
args: '--features "injective" --locked -- --test-threads 1'

- name: Upload to codecov.io
uses: codecov/codecov-action@v3
Expand Down
6 changes: 3 additions & 3 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
Expand Up @@ -28,7 +28,7 @@ protobuf = { version = "3.1.0", features = ["with-bytes"] }
schemars = "0.8.3"
semver = "1.0.12"
serde = { version = "1.0.137", default-features = false, features = ["derive"] }
terraswap = { path = "./packages/terraswap" }
terraswap = { path = "./packages/terraswap"}
thiserror = "1.0.21"
vault-network = { path = "./packages/vault-network" }
white-whale = { path = "./packages/white-whale" }
Expand Down
1 change: 1 addition & 0 deletions contracts/liquidity_hub/fee_collector/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ exclude = [
crate-type = ["cdylib", "rlib"]

[features]
injective = ["terraswap/injective"]
# for more explicit tests, cargo test --features=backtraces
backtraces = ["cosmwasm-std/backtraces"]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ exclude = [
crate-type = ["cdylib", "rlib"]

[features]
injective = ["terraswap/injective"]
# for quicker tests, cargo test --lib
# for more explicit tests, cargo test --features=backtraces
backtraces = ["cosmwasm-std/backtraces"]
Expand Down
107 changes: 107 additions & 0 deletions contracts/liquidity_hub/pool-network/terraswap_factory/src/testing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -451,6 +451,113 @@ fn create_ibc_tokens_pair() {
);
}

#[cfg(feature = "injective")]
#[test]
fn create_pair_ethereum_asset_and_ibc_token() {
let mut deps = mock_dependencies(&[
coin(
10u128,
"peggy0x87aB3B4C8661e07D6372361211B96ed4Dc36B1B5".to_string(),
),
coin(
10u128,
"ibc/27394FB092D2ECCD56123C74F36E4C1F926001CEADA9CA97EA622B25F41E5EB2".to_string(),
),
]);
deps = init(deps);
deps.querier.with_terraswap_factory(
&[],
&[
(
"peggy0x87aB3B4C8661e07D6372361211B96ed4Dc36B1B5".to_string(),
6u8,
),
(
"ibc/27394FB092D2ECCD56123C74F36E4C1F926001CEADA9CA97EA622B25F41E5EB2".to_string(),
6u8,
),
],
);

let asset_infos = [
AssetInfo::NativeToken {
denom: "peggy0x87aB3B4C8661e07D6372361211B96ed4Dc36B1B5".to_string(),
},
AssetInfo::NativeToken {
denom: "ibc/27394FB092D2ECCD56123C74F36E4C1F926001CEADA9CA97EA622B25F41E5EB2"
.to_string(),
},
];

let msg = ExecuteMsg::CreatePair {
asset_infos: asset_infos.clone(),
pool_fees: PoolFee {
protocol_fee: Fee {
share: Decimal::percent(1u64),
},
swap_fee: Fee {
share: Decimal::percent(1u64),
},
},
};

let env = mock_env();
let info = mock_info("addr0000", &[]);
let res = execute(deps.as_mut(), env, info, msg).unwrap();
assert_eq!(
res.attributes,
vec![
attr("action", "create_pair"),
attr("pair", "peggy0x87a...1B5-ibc/2739...5EB2"),
attr("pair_label", "peggy0x87a...1B5-ibc/2739...5EB2 pair"),
]
);
assert_eq!(
res.messages,
vec![SubMsg {
id: 1,
gas_limit: None,
reply_on: ReplyOn::Success,
msg: WasmMsg::Instantiate {
msg: to_binary(&PairInstantiateMsg {
asset_infos: asset_infos.clone(),
token_code_id: 123u64,
asset_decimals: [6u8, 6u8],
pool_fees: PoolFee {
protocol_fee: Fee {
share: Decimal::percent(1u64),
},
swap_fee: Fee {
share: Decimal::percent(1u64),
},
},
fee_collector_addr: "collector".to_string(),
})
.unwrap(),
code_id: 321u64,
funds: vec![],
label: "peggy0x87a...1B5-ibc/2739...5EB2 pair".to_string(),
admin: Some(MOCK_CONTRACT_ADDR.to_string()),
}
.into(),
},]
);

let raw_infos = [
asset_infos[0].to_raw(deps.as_ref().api).unwrap(),
asset_infos[1].to_raw(deps.as_ref().api).unwrap(),
];

assert_eq!(
TMP_PAIR_INFO.load(&deps.storage).unwrap(),
TmpPairInfo {
asset_infos: raw_infos.clone(),
pair_key: pair_key(&raw_infos),
asset_decimals: [6u8, 6u8],
}
);
}

#[test]
fn fail_to_create_same_pair() {
let mut deps = mock_dependencies(&[coin(10u128, "uusd".to_string())]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ exclude = [
crate-type = ["cdylib", "rlib"]

[features]
injective = ["terraswap/injective"]
# for quicker tests, cargo test --lib
# for more explicit tests, cargo test --features=backtraces
backtraces = ["cosmwasm-std/backtraces"]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "terraswap-router"
version = "1.0.5"
version = "1.0.6"
authors = [
"Terraform Labs, PTE.",
"DELIGHT LABS",
Expand All @@ -23,6 +23,7 @@ exclude = [
crate-type = ["cdylib", "rlib"]

[features]
injective = ["terraswap/injective"]
# for quicker tests, cargo test --lib
# for more explicit tests, cargo test --features=backtraces
backtraces = ["cosmwasm-std/backtraces"]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,10 @@ pub fn execute_swap_operations(
assert_operations(&operations)?;

let to = if let Some(to) = to { to } else { sender };
let target_asset_info = operations.last().unwrap().get_target_asset_info();
let target_asset_info = operations
.last()
.ok_or_else(|| ContractError::Std(StdError::generic_err("Couldn't get swap operation")))?
.get_target_asset_info();

let mut operation_index = 0;
let mut messages: Vec<CosmosMsg> = operations
Expand Down Expand Up @@ -317,8 +320,7 @@ fn reverse_simulate_swap_operations(
ask_amount,
offer_asset_info,
ask_asset_info,
)
.unwrap()
)?
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ documentation = "https://docs.cosmwasm.com"
crate-type = ["cdylib", "rlib"]

[features]
injective = ["terraswap/injective"]
backtraces = ["cosmwasm-std/backtraces"]
# use library feature to disable all init/handle/query exports
library = []
Expand Down
3 changes: 2 additions & 1 deletion contracts/liquidity_hub/vault-network/vault/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "vault"
version = "1.1.2"
version = "1.1.3"
authors = ["kaimen-sano <[email protected]>"]
edition.workspace = true
description = "Contract to handle a single vault that controls an asset"
Expand All @@ -14,6 +14,7 @@ publish.workspace = true
crate-type = ["cdylib", "rlib"]

[features]
injective = ["terraswap/injective"]
backtraces = ["cosmwasm-std/backtraces"]

[dependencies]
Expand Down
31 changes: 19 additions & 12 deletions contracts/liquidity_hub/vault-network/vault/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ use cw2::{get_contract_version, set_contract_version};
use cw20::MinterResponse;
use semver::Version;

use terraswap::asset::Asset;
#[cfg(feature = "injective")]
use terraswap::asset::PEGGY_PREFIX;
use terraswap::asset::{Asset, IBC_PREFIX};
use vault_network::vault::{
Config, ExecuteMsg, InstantiateMsg, MigrateMsg, QueryMsg, INSTANTIATE_LP_TOKEN_REPLY_ID,
};
Expand Down Expand Up @@ -47,21 +49,26 @@ pub fn instantiate(
};
CONFIG.save(deps.storage, &config)?;

let asset_label: String = msg.asset_info.clone().get_label(&deps.as_ref())?;

// cw20 asset symbols are 3-12 characters,
// so we take the first 8 characters of the symbol and append "uLP-" to it
let mut lp_symbol = format!("uLP-{}", asset_label.chars().take(8).collect::<String>());

// in case it's an ibc token, strip away everything from the '/' in 'ibc/'. The resulting
// lp_symbol would be uLP-ibc in that case.
let lp_symbol = format!(
"uLP-{}",
msg.asset_info
.clone()
.get_label(&deps.as_ref())?
.chars()
.take(8)
.collect::<String>()
.splitn(2, '/')
.collect::<Vec<_>>()[0]
);
if asset_label.starts_with(IBC_PREFIX) {
lp_symbol = lp_symbol.splitn(2, '/').collect::<Vec<_>>()[0].to_string();
}

#[cfg(feature = "injective")]
{
// in case it is an Ethereum bridged (peggy) asset on Injective, strip away everything from
// the "0x" in 'peggy0x...'. The resulting lp_symbol would be uLP-peggy in that case.
if asset_label.starts_with(PEGGY_PREFIX) {
lp_symbol = lp_symbol.splitn(2, "0x").collect::<Vec<_>>()[0].to_string();
}
}

let lp_label = format!(
"WW Vault {} LP token",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ publish.workspace = true
crate-type = ["cdylib", "rlib"]

[features]
injective = ["terraswap/injective"]
backtraces = ["cosmwasm-std/backtraces"]

[dependencies]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -303,4 +303,48 @@ mod tests {
})
)
}

#[cfg(feature = "injective")]
#[test]
fn can_create_peggy_token_vault() {
let asset_info = terraswap::asset::AssetInfo::NativeToken {
denom: "peggy0x87aB3B4C8661e07D6372361211B96ed4Dc36B1B5".to_string(),
};

// create a vault
let (res, _, env) = mock_execute(
5,
6,
vault_network::vault_factory::ExecuteMsg::CreateVault {
asset_info: asset_info.clone(),
fees: get_fees(),
},
);

assert_eq!(
res.unwrap(),
Response::new()
.add_attribute("method", "create_vault")
.add_submessage(SubMsg {
id: INSTANTIATE_VAULT_REPLY_ID,
reply_on: ReplyOn::Success,
gas_limit: None,
msg: WasmMsg::Instantiate {
admin: Some(env.contract.address.to_string()),
code_id: 5,
msg: to_binary(&vault_network::vault::InstantiateMsg {
owner: env.contract.address.to_string(),
asset_info,
token_id: 6,
vault_fees: get_fees(),
fee_collector_addr: "fee_collector".to_string()
})
.unwrap(),
funds: vec![],
label: "White Whale peggy0x87a...1B5 Vault".to_string()
}
.into()
})
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ publish.workspace = true
crate-type = ["cdylib", "rlib"]

[features]
injective = ["terraswap/injective"]
backtraces = ["cosmwasm-std/backtraces"]

[dependencies]
Expand Down
3 changes: 2 additions & 1 deletion packages/terraswap/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "terraswap"
version = "2.8.0"
version = "2.8.1"
authors = [
"Terraform Labs, PTE.",
"DELIGHT LABS",
Expand All @@ -19,6 +19,7 @@ documentation = "https://docs.terraswap.io"
# for quicker tests, cargo test --lib
# for more explicit tests, cargo test --features=backtraces
backtraces = ["cosmwasm-std/backtraces"]
injective = []

[dependencies]
uint = "0.9.4"
Expand Down
Loading

0 comments on commit 80b3114

Please sign in to comment.