Skip to content

Commit

Permalink
Make astroport builders chain agnostic..
Browse files Browse the repository at this point in the history
  • Loading branch information
dowlandaiello committed Sep 6, 2024
1 parent e1d2d4a commit 29db04c
Show file tree
Hide file tree
Showing 6 changed files with 219 additions and 19 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,15 +107,15 @@ Note that most `tx_*` helper functions expose a `.with_key(key: &str)` builder f
* `.with_msg(msg: serde_json::Value)`
* `.with_label(label: &str)`
* Notable optional builder calls:
* `.with_chain_name(chain_name: impl Into<String>)` - Should be one of `"osmosis" | "neutron"` or one of the registered chain names from `.with_chain`
* `.with_chain(chain_name: impl Into<String>)` - Should be one of `"osmosis" | "neutron"` or one of the registered chain names from `.with_chain`

#### Tokens

* `.build_tx_create_tokenfactory_token` - Creates a tokenfactory token from `acc0` on Neutron by default.
* Required builder calls:
* `.with_subdenom(subdenom: &str)`
* Notable optional builder calls:
* `.with_chain_name(chain_name: impl Into<String>)` - Should be one of `"osmosis" | "neutron" | "stride"` or one of the registered chain names from `.with_chain`
* `.with_chain(chain_name: impl Into<String>)` - Should be one of `"osmosis" | "neutron" | "stride"` or one of the registered chain names from `.with_chain`
* `.get_tokenfactory_denom(key: &str, subdenom: &str)` - Gets the tokenfactory denom of a tokenfactory token given its subdenom and key
* `.build_tx_mint_tokenfactory_token` - Mints a tokenfactory token from `acc0` on Neutron by default.
* Required builder calls
Expand All @@ -124,7 +124,7 @@ Note that most `tx_*` helper functions expose a `.with_key(key: &str)` builder f
* Required builder calls for osmosis
* `.with_recipient_addr(addr: &str)` - Specifies a recipient of the minted tokens on Osmosis. This builder call does nothing on Neutron.
* Notable optional builder calls:
* `.with_chain_name(chain_name: impl Into<String>)` - Specifies which chain to mint the tokens on. See previous notes about chain names.
* `.with_chain(chain_name: impl Into<String>)` - Specifies which chain to mint the tokens on. See previous notes about chain names.

#### Auctions

Expand Down
194 changes: 189 additions & 5 deletions examples/osmosis.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
use localic_utils::{ConfigChainBuilder, TestContextBuilder, OSMOSIS_CHAIN_NAME};
use cosmwasm_std::Decimal;
use localic_utils::{
types::contract::MinAmount, ConfigChainBuilder, TestContextBuilder, DEFAULT_KEY,
OSMOSIS_CHAIN_NAME,
};
use std::error::Error;

const ACC_0_ADDR: &str = "osmo1hj5fveer5cjtn4wd6wstzugjfdxzl0xpwhpz63";
const LOCAL_CODE_ID_CACHE_PATH: &str = "code_id_cache_osmo.json";

const TEST_TOKEN_1_NAME: &str = "bruhtoken";

/// Demonstrates using localic-utils for neutron.
fn main() -> Result<(), Box<dyn Error>> {
Expand All @@ -16,18 +23,25 @@ fn main() -> Result<(), Box<dyn Error>> {
.with_chain(ConfigChainBuilder::default_osmosis().build()?)
.build()?;

// Upload astroport contracts
ctx.build_tx_upload_contracts().send_with_local_cache(
"contracts_osmosis",
OSMOSIS_CHAIN_NAME,
LOCAL_CODE_ID_CACHE_PATH,
)?;

// Create some tokens on osmosis
ctx.build_tx_create_tokenfactory_token()
.with_chain_name(OSMOSIS_CHAIN_NAME)
.with_subdenom("bruhtoken")
.with_chain(OSMOSIS_CHAIN_NAME)
.with_subdenom(TEST_TOKEN_1_NAME)
.send()?;
let bruhtoken = ctx
.get_tokenfactory_denom()
.creator(ACC_0_ADDR)
.subdenom("bruhtoken".into())
.subdenom(TEST_TOKEN_1_NAME.into())
.get();
ctx.build_tx_mint_tokenfactory_token()
.with_chain_name(OSMOSIS_CHAIN_NAME)
.with_chain(OSMOSIS_CHAIN_NAME)
.with_amount(10000000000000000000)
.with_denom(&bruhtoken)
.with_recipient_addr(ACC_0_ADDR)
Expand Down Expand Up @@ -55,5 +69,175 @@ fn main() -> Result<(), Box<dyn Error>> {
.with_share_amount_out(1000000000000)
.send()?;

// Deploy some astroport and valence contracts to osmosis
// Deploy valence auctions
ctx.build_tx_create_auctions_manager()
.with_min_auction_amount(&[(
&String::from("untrn"),
MinAmount {
send: "0".into(),
start_auction: "0".into(),
},
)])
.with_server_addr(ACC_0_ADDR)
.with_chain(OSMOSIS_CHAIN_NAME)
.send()?;
ctx.build_tx_create_price_oracle()
.with_chain(OSMOSIS_CHAIN_NAME)
.send()?;
ctx.build_tx_manual_oracle_price_update()
.with_offer_asset("untrn")
.with_ask_asset(bruhtoken.as_str())
.with_price(Decimal::percent(10))
.with_chain(OSMOSIS_CHAIN_NAME)
.send()?;
ctx.build_tx_update_auction_oracle()
.with_chain(OSMOSIS_CHAIN_NAME)
.send()?;

ctx.build_tx_mint_tokenfactory_token()
.with_denom(bruhtoken.as_str())
.with_amount(10000000000)
.with_chain(OSMOSIS_CHAIN_NAME)
.send()?;
ctx.build_tx_mint_tokenfactory_token()
.with_denom(bruhtoken.as_str())
.with_amount(10000000000)
.with_chain(OSMOSIS_CHAIN_NAME)
.send()?;

ctx.build_tx_create_auction()
.with_offer_asset("untrn")
.with_ask_asset(bruhtoken.as_str())
.with_amount_offer_asset(10000)
.with_chain(OSMOSIS_CHAIN_NAME)
.send()?;
ctx.build_tx_create_auction()
.with_offer_asset("untrn")
.with_ask_asset(bruhtoken.as_str())
.with_amount_offer_asset(10000)
.with_chain(OSMOSIS_CHAIN_NAME)
.send()?;

let _ = ctx
.get_auction()
.offer_asset("untrn")
.ask_asset(
&ctx.get_tokenfactory_denom()
.creator(ACC_0_ADDR)
.subdenom(TEST_TOKEN_1_NAME.to_owned())
.get(),
)
.get_cw();
let _ = ctx
.get_auction()
.offer_asset("untrn")
.ask_asset(
&ctx.get_tokenfactory_denom()
.creator(ACC_0_ADDR)
.subdenom(TEST_TOKEN_1_NAME.to_owned())
.get(),
)
.get_cw();

ctx.build_tx_create_token_registry()
.with_owner(ACC_0_ADDR)
.with_chain(OSMOSIS_CHAIN_NAME)
.send()?;
ctx.build_tx_create_factory()
.with_owner(ACC_0_ADDR)
.with_chain(OSMOSIS_CHAIN_NAME)
.send()?;
ctx.build_tx_create_pool()
.with_denom_a("untrn")
.with_denom_b(bruhtoken.clone())
.with_chain(OSMOSIS_CHAIN_NAME)
.send()?;
ctx.build_tx_create_pool()
.with_denom_a("untrn")
.with_denom_b(bruhtoken.clone())
.with_chain(OSMOSIS_CHAIN_NAME)
.send()?;

let pool = ctx
.get_astro_pool()
.denoms(
"untrn".to_owned(),
ctx.get_tokenfactory_denom()
.creator(ACC_0_ADDR)
.subdenom(TEST_TOKEN_1_NAME.to_owned())
.get(),
)
.get_cw();

assert!(pool
.query_value(&serde_json::json!({
"pair": {}
}))
.get("data")
.and_then(|data| data.get("asset_infos"))
.is_some());

ctx.build_tx_fund_auction()
.with_offer_asset("untrn")
.with_ask_asset(bruhtoken.as_str())
.with_amount_offer_asset(10000)
.with_chain(OSMOSIS_CHAIN_NAME)
.send()?;

ctx.build_tx_start_auction()
.with_offer_asset("untrn")
.with_ask_asset(bruhtoken.as_str())
.with_end_block_delta(1000000)
.with_chain(OSMOSIS_CHAIN_NAME)
.send()?;

ctx.build_tx_fund_pool()
.with_denom_a("untrn")
.with_denom_b(bruhtoken)
.with_amount_denom_a(10000)
.with_amount_denom_b(10000)
.with_slippage_tolerance(Decimal::percent(50))
.with_liq_token_receiver(ACC_0_ADDR)
.with_chain(OSMOSIS_CHAIN_NAME)
.send()?;

let factory_contract_code_id = ctx
.get_contract()
.contract("astroport_whitelist")
.get_cw()
.code_id
.unwrap();

// Instantiate a contract with a predictable address
ctx.build_tx_instantiate2()
.with_code_id(factory_contract_code_id)
.with_msg(serde_json::json!({
"admins": [],
"mutable": false,
}))
.with_salt_hex_encoded(hex::encode("examplesalt").as_str())
.with_label("test_contract")
.with_flags("--gas 10000000")
.send()
.unwrap();

let addr = ctx
.get_built_contract_address()
.contract("cw1_whitelist")
.creator(ACC_0_ADDR)
.salt_hex_encoded(hex::encode("examplesalt").as_str())
.get();

let mut cw = ctx.get_contract().contract("cw1_whitelist").get_cw();
cw.contract_addr = Some(addr);

cw.execute(
DEFAULT_KEY,
&serde_json::json!({ "execute": { "msgs": [] } }).to_string(),
"",
)
.unwrap();

Ok(())
}
2 changes: 2 additions & 0 deletions src/types/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@ pub mod config;
pub mod contract;

pub mod ibc;

pub mod osmosis;
19 changes: 13 additions & 6 deletions src/utils/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use std::{
/// A tx uploading contract artifacts.
pub struct UploadContractsTxBuilder<'a> {
key: Option<&'a str>,
chain: &'a str,
test_ctx: &'a mut TestContext,
}

Expand All @@ -24,11 +25,18 @@ impl<'a> UploadContractsTxBuilder<'a> {
self
}

pub fn with_chain(&mut self, chain: &'a str) -> &mut Self {
self.chain = chain;

self
}

/// Sends the transaction.
pub fn send(&mut self) -> Result<(), Error> {
self.test_ctx.tx_upload_contracts(
self.key
.ok_or(Error::MissingBuilderParam(String::from("key")))?,
self.chain,
)
}

Expand All @@ -53,11 +61,12 @@ impl TestContext {
pub fn build_tx_upload_contracts(&mut self) -> UploadContractsTxBuilder {
UploadContractsTxBuilder {
key: Some(DEFAULT_KEY),
chain: NEUTRON_CHAIN_NAME,
test_ctx: self,
}
}

fn tx_upload_contracts(&mut self, key: &str) -> Result<(), Error> {
fn tx_upload_contracts(&mut self, key: &str, chain: &str) -> Result<(), Error> {
fs::read_dir(&self.artifacts_dir)?
.filter_map(|dir_ent| dir_ent.ok())
.filter(|dir_ent| {
Expand All @@ -67,19 +76,17 @@ impl TestContext {
.map(fs::canonicalize)
.try_for_each(|maybe_abs_path| {
let path = maybe_abs_path?;
let neutron_local_chain = self.get_mut_chain(NEUTRON_CHAIN_NAME);
let chain = self.get_mut_chain(chain);

let mut cw = CosmWasm::new(&neutron_local_chain.rb);
let mut cw = CosmWasm::new(&chain.rb);

let code_id = cw.store(key, &path)?;

let id = path
.file_stem()
.and_then(|stem| stem.to_str())
.ok_or(Error::Misc(String::from("failed to format file path")))?;
neutron_local_chain
.contract_codes
.insert(id.to_string(), code_id);
chain.contract_codes.insert(id.to_string(), code_id);

Ok(())
})
Expand Down
13 changes: 10 additions & 3 deletions src/utils/setup/astroport.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use super::super::{
super::{
error::Error, DEFAULT_KEY, FACTORY_NAME, NEUTRON_CHAIN_ADMIN_ADDR, NEUTRON_CHAIN_NAME,
PAIR_NAME, STABLE_PAIR_NAME, TOKEN_NAME, TOKEN_REGISTRY_NAME, WHITELIST_NAME,
error::Error, CW1_WHITELIST_NAME, DEFAULT_KEY, FACTORY_NAME, FACTORY_ON_OSMOSIS_NAME,
NEUTRON_CHAIN_ADMIN_ADDR, NEUTRON_CHAIN_NAME, OSMOSIS_CHAIN_NAME, PAIR_NAME,
STABLE_PAIR_NAME, TOKEN_NAME, TOKEN_REGISTRY_NAME, WHITELIST_NAME,
},
test_context::TestContext,
};
Expand All @@ -26,6 +27,12 @@ impl<'a> CreateTokenRegistryTxBuilder<'a> {
self
}

pub fn with_chain(&mut self, chain: &'a str) -> &mut Self {
self.chain = chain;

self
}

pub fn with_owner(&mut self, owner: impl Into<String>) -> &mut Self {
self.owner = Some(owner.into());

Expand Down Expand Up @@ -258,7 +265,7 @@ impl TestContext {
key: &str,
factory_owner: impl Into<String>,
) -> Result<(), Error> {
let neutron = self.get_chain(NEUTRON_CHAIN_NAME);
let chain = self.get_chain(chain_name);

let pair_xyk_code_id =
neutron
Expand Down
4 changes: 2 additions & 2 deletions src/utils/setup/tokens.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ impl<'a> CreateTokenFactoryTokenTxBuilder<'a> {
self
}

pub fn with_chain_name(&mut self, chain_name: impl Into<String>) -> &mut Self {
pub fn with_chain(&mut self, chain_name: impl Into<String>) -> &mut Self {
self.chain_name = Some(chain_name.into());

self
Expand Down Expand Up @@ -61,7 +61,7 @@ impl<'a> MintTokenFactoryTokenTxBuilder<'a> {
self
}

pub fn with_chain_name(&mut self, chain_name: impl Into<String>) -> &mut Self {
pub fn with_chain(&mut self, chain_name: impl Into<String>) -> &mut Self {
self.chain_name = Some(chain_name.into());

self
Expand Down

0 comments on commit 29db04c

Please sign in to comment.