Skip to content

Commit

Permalink
Neutron and osmosis tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Kayanski committed Dec 2, 2024
1 parent 3968da3 commit 67e77b1
Show file tree
Hide file tree
Showing 5 changed files with 330 additions and 101 deletions.
10 changes: 7 additions & 3 deletions interchain/scripts/state.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"abstract:account": 11529,
"abstract:ans-host": 11528,
"abstract:cw-staking": 11559,
"abstract:dex": 11532,
"abstract:dex": 11694,
"abstract:ibc-client": 11530,
"abstract:ibc-host": 11531,
"abstract:module-factory": 11526,
Expand All @@ -15,9 +15,11 @@
"default": {
"abstract:account-local-0": "osmo1hafge5xucuc03ar5lkwac0n9yctnxq6xrgygftdl9xl3tzncgghshnykry",
"abstract:account-local-1": "osmo1v9dl0fw5s3jujn6emmlgch7w4ynt02nykj75pnzk8mdw7nmu3p4q65tczp",
"abstract:account-local-9": "osmo1gsk99z7x568h9894lev3muwynay65v4n0npnwn82tazsv47mq33q2v20a2",
"abstract:ans-host": "osmo1wx0qjtlz799pfxl73y2f4dv28nukztpmq2ztavk60v534tat9cdsepnezp",
"abstract:cw-staking": "osmo13kp8fv0t869zcwjh9q52eygxf8w0d8anmzhgrrz5tjju9uw3fscq9mhw4u",
"abstract:dex": "osmo1s8nvehqc0jf99ac2qqg0sqja5ydzxlvht3eml9swl3n736tz9dsqfc4wa4",
"abstract:dex": "osmo1n2kmtqru87hzp905qsc20wkn8kras38eqcefddn3yjah63hza55sv4ysng",
"abstract:dex-local-9": "osmo1n2kmtqru87hzp905qsc20wkn8kras38eqcefddn3yjah63hza55sv4ysng",
"abstract:ibc-client": "osmo1vnt8tpxrnukcydma2x6gy2zzawl9uxmc6las8cak9sd87q67n00qn0wkj7",
"abstract:ibc-host": "osmo1zzgxwn88a3ec34fw4mu8ppsh9eth746l3tl8w5l7qvknrmtldldqz7saee",
"abstract:module-factory": "osmo1vnj802lyyupfaez73w7axfs3xveraxen370vhcnumaufj29r5rxqcxdtus",
Expand All @@ -26,7 +28,7 @@
},
"pion-1": {
"code_ids": {
"abstract:account": 8232,
"abstract:account": 8373,
"abstract:ans-host": 8231,
"abstract:dex": 8372,
"abstract:ibc-client": 8233,
Expand All @@ -39,8 +41,10 @@
"default": {
"abstract:account-local-0": "neutron12x9fcuwl382vv9l22dkcvcx6uxpeqy9rq5fe3jt8cgvqkyjnj2hs5da0m4",
"abstract:account-local-1": "neutron1mxahhvpj467088td8lvysyw7m8v880tzls0sxk0zuljn6tsufqksy8whnv",
"abstract:account-local-71": "neutron1pn4l8vtvvc2gtz8hj5y4rwv2r20hq2pcnn9rnamhppth4vqsyyds2hqmqp",
"abstract:ans-host": "neutron1wx0qjtlz799pfxl73y2f4dv28nukztpmq2ztavk60v534tat9cdsdn69ty",
"abstract:dex": "neutron10hr9m68uh3h2qn8tgsc370zhwsu0wsjz688xpnr6jgyh567wkrpqcv5pzv",
"abstract:dex-local-71": "neutron10hr9m68uh3h2qn8tgsc370zhwsu0wsjz688xpnr6jgyh567wkrpqcv5pzv",
"abstract:ibc-client": "neutron1n9la68zja4l76plwffedj24ar3f6e5kg7j92r26hhzewncghypvs9vk28k",
"abstract:ibc-host": "neutron1c67x66rln9mgrjum5urrgn0tjvup5k9uke8hgdm8tu7hztwrjy4s5ecrmu",
"abstract:module-factory": "neutron1vnj802lyyupfaez73w7axfs3xveraxen370vhcnumaufj29r5rxqv5yh44",
Expand Down
3 changes: 3 additions & 0 deletions modules/contracts/adapters/dex/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -134,4 +134,7 @@ cw-orch = { workspace = true, features = ["daemon"] }
cw-orch-interchain = { workspace = true, features = ["daemon"] }
cw-plus-orch = { workspace = true }
neutron-std = "5.0.1-rc0"
osmosis-std = "0.26.0"
prost = "0.12.3"
prost-types = "0.13.3"
prost_13 = { version = "0.13.3", package = "prost" }
152 changes: 152 additions & 0 deletions modules/contracts/adapters/dex/examples/neutron_testnet.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
use abstract_adapter::objects::pool_id::PoolAddressBase;
use abstract_adapter::objects::{AnsAsset, AssetEntry, PoolMetadata};
use abstract_client::{AbstractClient, Account, Application, Namespace};
use abstract_dex_adapter::interface::DexAdapter;
use abstract_dex_standard::ans_action::DexAnsAction;
use abstract_interface::ExecuteMsgFns;
use abstract_neutron_dex_adapter::NEUTRON;
use cosmwasm_std::{coin, Decimal};
use cw_orch::daemon::{networks::PION_1, Daemon};
use cw_orch::prelude::*;
use neutron_std::types::neutron::dex::DepositOptions;
use neutron_std::types::{
neutron::dex::MsgDeposit,
osmosis::tokenfactory::v1beta1::{MsgCreateDenom, MsgMint},
};
use prost::Message;

pub const NTRN: &str = "untrn";
pub const SUB_DENOM: &str = "pooled";

fn new_denom(chain: &Daemon) -> String {
format!("factory/{}/{}", chain.sender_addr(), SUB_DENOM)
}

#[allow(clippy::type_complexity)]
fn setup_denom_ans(chain: Daemon) -> anyhow::Result<()> {
let deployment = AbstractClient::new(chain.clone())?;

// Create some liquidity
let denom_creation = MsgCreateDenom {
sender: chain.sender_addr().to_string(),
subdenom: SUB_DENOM.to_string(),
};
// Mint to myself
let denom_mint = MsgMint {
sender: chain.sender_addr().to_string(),
amount: Some(neutron_std::types::cosmos::base::v1beta1::Coin {
denom: new_denom(&chain),
amount: "1000000000000".to_string(),
}),
mint_to_address: chain.sender_addr().to_string(),
};

// Deposit some initial liquidity (create pool)
let pool_create_msg = MsgDeposit {
token_a: NTRN.to_string(),
token_b: new_denom(&chain).to_string(),
amounts_a: vec!["100000".to_string()],
amounts_b: vec!["100000".to_string()],
creator: chain.sender_addr().to_string(),
receiver: chain.sender_addr().to_string(),
fees: vec![0],
options: vec![DepositOptions {
disable_autoswap: false,
fail_tx_on_bel: false,
}],
tick_indexes_a_to_b: vec![0],
};

chain.commit_any(
vec![
prost_types::Any {
type_url: MsgCreateDenom::TYPE_URL.to_string(),
value: denom_creation.encode_to_vec(),
},
prost_types::Any {
type_url: MsgMint::TYPE_URL.to_string(),
value: denom_mint.encode_to_vec(),
},
prost_types::Any {
type_url: MsgDeposit::TYPE_URL.to_string(),
value: pool_create_msg.encode_to_vec(),
},
],
None,
)?;

// We need to register some pairs and assets on the ans host contract
// Register NTRN and ATOM assets
deployment
.name_service()
.update_asset_addresses(
vec![
("ntrn".to_string(), cw_asset::AssetInfoBase::native(NTRN)),
(
"new_denom".to_string(),
cw_asset::AssetInfoBase::native(new_denom(&chain)),
),
],
vec![],
)
.unwrap();

deployment
.name_service()
.update_dexes(vec![NEUTRON.into()], vec![])
.unwrap();

deployment
.name_service()
.update_pools(
vec![(
PoolAddressBase::id(0u64),
PoolMetadata::constant_product(
NEUTRON,
vec!["ntrn".to_string(), "new_denom".to_string()],
),
)],
vec![],
)
.unwrap();

Ok(())
}
fn main() -> anyhow::Result<()> {
dotenv::dotenv()?;
env_logger::init();

let chain = Daemon::builder(PION_1).build()?;
// setup_denom_ans(chain.clone())?;

let deployment = AbstractClient::new(chain.clone())?;

let namespace = Namespace::new("hackerhouse")?;
let account = deployment.fetch_or_build_account(namespace.clone(), |b| {
b.namespace(namespace.clone())
.install_adapter::<DexAdapter<Daemon>>()
})?;
let dex_adapter = account.application::<DexAdapter<Daemon>>()?;

// swap 1_000 ntrn to new_denom
let asset = AssetEntry::new("ntrn");
let ask_asset = AssetEntry::new("new_denom");

let swap_value = 1_000u128;

let action = DexAnsAction::Swap {
offer_asset: AnsAsset::new(asset, swap_value),
ask_asset,
max_spread: Some(Decimal::percent(30)),
belief_price: Some(Decimal::percent(1)),
};

dex_adapter.module::<DexAdapter<_>>()?.ans_action(
NEUTRON.into(),
action,
account.as_ref(),
deployment.name_service(),
)?;

Ok(())
}
168 changes: 168 additions & 0 deletions modules/contracts/adapters/dex/examples/osmosis_testnet.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
use abstract_adapter::objects::pool_id::PoolAddressBase;
use abstract_adapter::objects::{AnsAsset, AssetEntry, PoolMetadata, UniquePoolId};
use abstract_adapter::std::ans_host::QueryMsgFns;
use abstract_client::{AbstractClient, Account, Application, Namespace};
use abstract_dex_adapter::interface::DexAdapter;
use abstract_dex_standard::ans_action::DexAnsAction;
use abstract_interface::ExecuteMsgFns;
use abstract_osmosis_adapter::OSMOSIS;
use cosmwasm_std::{coin, coins, Decimal};
use cw_orch::daemon::RUNTIME;
use cw_orch::daemon::{networks::OSMO_5, Daemon};
use cw_orch::prelude::*;
use osmosis_std::types::osmosis::gamm::poolmodels::balancer::v1beta1::MsgCreateBalancerPool;
use osmosis_std::types::osmosis::gamm::v1beta1::PoolAsset;
use osmosis_std::types::osmosis::gamm::v1beta1::PoolParams;
use osmosis_std::types::osmosis::tokenfactory::v1beta1::{MsgCreateDenom, MsgMint};
use prost_13::Message;

pub const OSMO: &str = "uosmo";
pub const SUB_DENOM: &str = "pooled";

fn new_denom(chain: &Daemon) -> String {
format!("factory/{}/{}", chain.sender_addr(), SUB_DENOM)
}

#[allow(clippy::type_complexity)]
fn setup_denom_ans(chain: Daemon) -> anyhow::Result<()> {
let deployment = AbstractClient::new(chain.clone())?;

// Create some liquidity
let denom_creation = MsgCreateDenom {
sender: chain.sender_addr().to_string(),
subdenom: SUB_DENOM.to_string(),
};
// Mint to myself
let denom_mint = MsgMint {
sender: chain.sender_addr().to_string(),
amount: Some(osmosis_std::types::cosmos::base::v1beta1::Coin {
denom: new_denom(&chain),
amount: "1000000000000".to_string(),
}),
mint_to_address: chain.sender_addr().to_string(),
};

// Create pool
let pool_create_msg = MsgCreateBalancerPool {
sender: chain.sender_addr().to_string(),
pool_params: Some(PoolParams {
swap_fee: "10000000000000000".to_string(),
exit_fee: "0".to_string(),
smooth_weight_change_params: None,
}),
pool_assets: vec![coin(100_000, OSMO), coin(100_000, new_denom(&chain))]
.iter()
.map(|c| PoolAsset {
token: Some(osmosis_std::types::cosmos::base::v1beta1::Coin {
denom: c.denom.to_owned(),
amount: format!("{}", c.amount),
}),
weight: "1000000".to_string(),
})
.collect(),
future_pool_governor: "".to_string(),
};

let response = chain.commit_any(
vec![
prost_types::Any {
type_url: MsgCreateDenom::TYPE_URL.to_string(),
value: denom_creation.encode_to_vec(),
},
prost_types::Any {
type_url: MsgMint::TYPE_URL.to_string(),
value: denom_mint.encode_to_vec(),
},
prost_types::Any {
type_url: MsgCreateBalancerPool::TYPE_URL.to_string(),
value: pool_create_msg.encode_to_vec(),
},
],
None,
)?;

let pool_id: u64 = response.get_events("pool_created")[0].get_attributes("pool_id")[0]
.value
.parse()?;

// We need to register some pairs and assets on the ans host contract
// Register OSMO and ATOM assets
deployment
.name_service()
.update_asset_addresses(
vec![
("osmo".to_string(), cw_asset::AssetInfoBase::native(OSMO)),
(
"new_denom".to_string(),
cw_asset::AssetInfoBase::native(new_denom(&chain)),
),
],
vec![],
)
.unwrap();

deployment
.name_service()
.update_dexes(vec![OSMOSIS.into()], vec![])
.unwrap();

deployment
.name_service()
.update_pools(
vec![(
PoolAddressBase::id(pool_id),
PoolMetadata::constant_product(
OSMOSIS,
vec!["osmo".to_string(), "new_denom".to_string()],
),
)],
vec![UniquePoolId::new(1)],
)
.unwrap();

Ok(())
}
fn main() -> anyhow::Result<()> {
dotenv::dotenv()?;
env_logger::init();

let chain = Daemon::builder(OSMO_5).build()?;
// setup_denom_ans(chain.clone())?;

let deployment = AbstractClient::new(chain.clone())?;

let namespace = Namespace::new("hackerhouse")?;
let account = deployment.fetch_or_build_account(namespace.clone(), |b| {
b.namespace(namespace.clone())
.install_adapter::<DexAdapter<Daemon>>()
})?;
let dex_adapter = account.application::<DexAdapter<Daemon>>()?;

RUNTIME.block_on(
chain
.sender()
.bank_send(&account.address()?, coins(1_000, OSMO)),
)?;

// swap 1_000 osmo to new_denom
let asset = AssetEntry::new("osmo");
let ask_asset = AssetEntry::new("new_denom");

let swap_value = 1_000u128;

let action = DexAnsAction::Swap {
offer_asset: AnsAsset::new(asset, swap_value),
ask_asset,
max_spread: Some(Decimal::percent(30)),
belief_price: Some(Decimal::percent(1)),
};

dex_adapter.module::<DexAdapter<_>>()?.ans_action(
OSMOSIS.into(),
action,
account.as_ref(),
deployment.name_service(),
)?;

Ok(())
}
Loading

0 comments on commit 67e77b1

Please sign in to comment.