Skip to content

Commit 29db04c

Browse files
committed
Make astroport builders chain agnostic..
1 parent e1d2d4a commit 29db04c

File tree

6 files changed

+219
-19
lines changed

6 files changed

+219
-19
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -107,15 +107,15 @@ Note that most `tx_*` helper functions expose a `.with_key(key: &str)` builder f
107107
* `.with_msg(msg: serde_json::Value)`
108108
* `.with_label(label: &str)`
109109
* Notable optional builder calls:
110-
* `.with_chain_name(chain_name: impl Into<String>)` - Should be one of `"osmosis" | "neutron"` or one of the registered chain names from `.with_chain`
110+
* `.with_chain(chain_name: impl Into<String>)` - Should be one of `"osmosis" | "neutron"` or one of the registered chain names from `.with_chain`
111111

112112
#### Tokens
113113

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

129129
#### Auctions
130130

examples/osmosis.rs

Lines changed: 189 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
1-
use localic_utils::{ConfigChainBuilder, TestContextBuilder, OSMOSIS_CHAIN_NAME};
1+
use cosmwasm_std::Decimal;
2+
use localic_utils::{
3+
types::contract::MinAmount, ConfigChainBuilder, TestContextBuilder, DEFAULT_KEY,
4+
OSMOSIS_CHAIN_NAME,
5+
};
26
use std::error::Error;
37

48
const ACC_0_ADDR: &str = "osmo1hj5fveer5cjtn4wd6wstzugjfdxzl0xpwhpz63";
9+
const LOCAL_CODE_ID_CACHE_PATH: &str = "code_id_cache_osmo.json";
10+
11+
const TEST_TOKEN_1_NAME: &str = "bruhtoken";
512

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

26+
// Upload astroport contracts
27+
ctx.build_tx_upload_contracts().send_with_local_cache(
28+
"contracts_osmosis",
29+
OSMOSIS_CHAIN_NAME,
30+
LOCAL_CODE_ID_CACHE_PATH,
31+
)?;
32+
1933
// Create some tokens on osmosis
2034
ctx.build_tx_create_tokenfactory_token()
21-
.with_chain_name(OSMOSIS_CHAIN_NAME)
22-
.with_subdenom("bruhtoken")
35+
.with_chain(OSMOSIS_CHAIN_NAME)
36+
.with_subdenom(TEST_TOKEN_1_NAME)
2337
.send()?;
2438
let bruhtoken = ctx
2539
.get_tokenfactory_denom()
2640
.creator(ACC_0_ADDR)
27-
.subdenom("bruhtoken".into())
41+
.subdenom(TEST_TOKEN_1_NAME.into())
2842
.get();
2943
ctx.build_tx_mint_tokenfactory_token()
30-
.with_chain_name(OSMOSIS_CHAIN_NAME)
44+
.with_chain(OSMOSIS_CHAIN_NAME)
3145
.with_amount(10000000000000000000)
3246
.with_denom(&bruhtoken)
3347
.with_recipient_addr(ACC_0_ADDR)
@@ -55,5 +69,175 @@ fn main() -> Result<(), Box<dyn Error>> {
5569
.with_share_amount_out(1000000000000)
5670
.send()?;
5771

72+
// Deploy some astroport and valence contracts to osmosis
73+
// Deploy valence auctions
74+
ctx.build_tx_create_auctions_manager()
75+
.with_min_auction_amount(&[(
76+
&String::from("untrn"),
77+
MinAmount {
78+
send: "0".into(),
79+
start_auction: "0".into(),
80+
},
81+
)])
82+
.with_server_addr(ACC_0_ADDR)
83+
.with_chain(OSMOSIS_CHAIN_NAME)
84+
.send()?;
85+
ctx.build_tx_create_price_oracle()
86+
.with_chain(OSMOSIS_CHAIN_NAME)
87+
.send()?;
88+
ctx.build_tx_manual_oracle_price_update()
89+
.with_offer_asset("untrn")
90+
.with_ask_asset(bruhtoken.as_str())
91+
.with_price(Decimal::percent(10))
92+
.with_chain(OSMOSIS_CHAIN_NAME)
93+
.send()?;
94+
ctx.build_tx_update_auction_oracle()
95+
.with_chain(OSMOSIS_CHAIN_NAME)
96+
.send()?;
97+
98+
ctx.build_tx_mint_tokenfactory_token()
99+
.with_denom(bruhtoken.as_str())
100+
.with_amount(10000000000)
101+
.with_chain(OSMOSIS_CHAIN_NAME)
102+
.send()?;
103+
ctx.build_tx_mint_tokenfactory_token()
104+
.with_denom(bruhtoken.as_str())
105+
.with_amount(10000000000)
106+
.with_chain(OSMOSIS_CHAIN_NAME)
107+
.send()?;
108+
109+
ctx.build_tx_create_auction()
110+
.with_offer_asset("untrn")
111+
.with_ask_asset(bruhtoken.as_str())
112+
.with_amount_offer_asset(10000)
113+
.with_chain(OSMOSIS_CHAIN_NAME)
114+
.send()?;
115+
ctx.build_tx_create_auction()
116+
.with_offer_asset("untrn")
117+
.with_ask_asset(bruhtoken.as_str())
118+
.with_amount_offer_asset(10000)
119+
.with_chain(OSMOSIS_CHAIN_NAME)
120+
.send()?;
121+
122+
let _ = ctx
123+
.get_auction()
124+
.offer_asset("untrn")
125+
.ask_asset(
126+
&ctx.get_tokenfactory_denom()
127+
.creator(ACC_0_ADDR)
128+
.subdenom(TEST_TOKEN_1_NAME.to_owned())
129+
.get(),
130+
)
131+
.get_cw();
132+
let _ = ctx
133+
.get_auction()
134+
.offer_asset("untrn")
135+
.ask_asset(
136+
&ctx.get_tokenfactory_denom()
137+
.creator(ACC_0_ADDR)
138+
.subdenom(TEST_TOKEN_1_NAME.to_owned())
139+
.get(),
140+
)
141+
.get_cw();
142+
143+
ctx.build_tx_create_token_registry()
144+
.with_owner(ACC_0_ADDR)
145+
.with_chain(OSMOSIS_CHAIN_NAME)
146+
.send()?;
147+
ctx.build_tx_create_factory()
148+
.with_owner(ACC_0_ADDR)
149+
.with_chain(OSMOSIS_CHAIN_NAME)
150+
.send()?;
151+
ctx.build_tx_create_pool()
152+
.with_denom_a("untrn")
153+
.with_denom_b(bruhtoken.clone())
154+
.with_chain(OSMOSIS_CHAIN_NAME)
155+
.send()?;
156+
ctx.build_tx_create_pool()
157+
.with_denom_a("untrn")
158+
.with_denom_b(bruhtoken.clone())
159+
.with_chain(OSMOSIS_CHAIN_NAME)
160+
.send()?;
161+
162+
let pool = ctx
163+
.get_astro_pool()
164+
.denoms(
165+
"untrn".to_owned(),
166+
ctx.get_tokenfactory_denom()
167+
.creator(ACC_0_ADDR)
168+
.subdenom(TEST_TOKEN_1_NAME.to_owned())
169+
.get(),
170+
)
171+
.get_cw();
172+
173+
assert!(pool
174+
.query_value(&serde_json::json!({
175+
"pair": {}
176+
}))
177+
.get("data")
178+
.and_then(|data| data.get("asset_infos"))
179+
.is_some());
180+
181+
ctx.build_tx_fund_auction()
182+
.with_offer_asset("untrn")
183+
.with_ask_asset(bruhtoken.as_str())
184+
.with_amount_offer_asset(10000)
185+
.with_chain(OSMOSIS_CHAIN_NAME)
186+
.send()?;
187+
188+
ctx.build_tx_start_auction()
189+
.with_offer_asset("untrn")
190+
.with_ask_asset(bruhtoken.as_str())
191+
.with_end_block_delta(1000000)
192+
.with_chain(OSMOSIS_CHAIN_NAME)
193+
.send()?;
194+
195+
ctx.build_tx_fund_pool()
196+
.with_denom_a("untrn")
197+
.with_denom_b(bruhtoken)
198+
.with_amount_denom_a(10000)
199+
.with_amount_denom_b(10000)
200+
.with_slippage_tolerance(Decimal::percent(50))
201+
.with_liq_token_receiver(ACC_0_ADDR)
202+
.with_chain(OSMOSIS_CHAIN_NAME)
203+
.send()?;
204+
205+
let factory_contract_code_id = ctx
206+
.get_contract()
207+
.contract("astroport_whitelist")
208+
.get_cw()
209+
.code_id
210+
.unwrap();
211+
212+
// Instantiate a contract with a predictable address
213+
ctx.build_tx_instantiate2()
214+
.with_code_id(factory_contract_code_id)
215+
.with_msg(serde_json::json!({
216+
"admins": [],
217+
"mutable": false,
218+
}))
219+
.with_salt_hex_encoded(hex::encode("examplesalt").as_str())
220+
.with_label("test_contract")
221+
.with_flags("--gas 10000000")
222+
.send()
223+
.unwrap();
224+
225+
let addr = ctx
226+
.get_built_contract_address()
227+
.contract("cw1_whitelist")
228+
.creator(ACC_0_ADDR)
229+
.salt_hex_encoded(hex::encode("examplesalt").as_str())
230+
.get();
231+
232+
let mut cw = ctx.get_contract().contract("cw1_whitelist").get_cw();
233+
cw.contract_addr = Some(addr);
234+
235+
cw.execute(
236+
DEFAULT_KEY,
237+
&serde_json::json!({ "execute": { "msgs": [] } }).to_string(),
238+
"",
239+
)
240+
.unwrap();
241+
58242
Ok(())
59243
}

src/types/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,5 @@ pub mod config;
55
pub mod contract;
66

77
pub mod ibc;
8+
9+
pub mod osmosis;

src/utils/fs.rs

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ use std::{
1414
/// A tx uploading contract artifacts.
1515
pub struct UploadContractsTxBuilder<'a> {
1616
key: Option<&'a str>,
17+
chain: &'a str,
1718
test_ctx: &'a mut TestContext,
1819
}
1920

@@ -24,11 +25,18 @@ impl<'a> UploadContractsTxBuilder<'a> {
2425
self
2526
}
2627

28+
pub fn with_chain(&mut self, chain: &'a str) -> &mut Self {
29+
self.chain = chain;
30+
31+
self
32+
}
33+
2734
/// Sends the transaction.
2835
pub fn send(&mut self) -> Result<(), Error> {
2936
self.test_ctx.tx_upload_contracts(
3037
self.key
3138
.ok_or(Error::MissingBuilderParam(String::from("key")))?,
39+
self.chain,
3240
)
3341
}
3442

@@ -53,11 +61,12 @@ impl TestContext {
5361
pub fn build_tx_upload_contracts(&mut self) -> UploadContractsTxBuilder {
5462
UploadContractsTxBuilder {
5563
key: Some(DEFAULT_KEY),
64+
chain: NEUTRON_CHAIN_NAME,
5665
test_ctx: self,
5766
}
5867
}
5968

60-
fn tx_upload_contracts(&mut self, key: &str) -> Result<(), Error> {
69+
fn tx_upload_contracts(&mut self, key: &str, chain: &str) -> Result<(), Error> {
6170
fs::read_dir(&self.artifacts_dir)?
6271
.filter_map(|dir_ent| dir_ent.ok())
6372
.filter(|dir_ent| {
@@ -67,19 +76,17 @@ impl TestContext {
6776
.map(fs::canonicalize)
6877
.try_for_each(|maybe_abs_path| {
6978
let path = maybe_abs_path?;
70-
let neutron_local_chain = self.get_mut_chain(NEUTRON_CHAIN_NAME);
79+
let chain = self.get_mut_chain(chain);
7180

72-
let mut cw = CosmWasm::new(&neutron_local_chain.rb);
81+
let mut cw = CosmWasm::new(&chain.rb);
7382

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

7685
let id = path
7786
.file_stem()
7887
.and_then(|stem| stem.to_str())
7988
.ok_or(Error::Misc(String::from("failed to format file path")))?;
80-
neutron_local_chain
81-
.contract_codes
82-
.insert(id.to_string(), code_id);
89+
chain.contract_codes.insert(id.to_string(), code_id);
8390

8491
Ok(())
8592
})

src/utils/setup/astroport.rs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
use super::super::{
22
super::{
3-
error::Error, DEFAULT_KEY, FACTORY_NAME, NEUTRON_CHAIN_ADMIN_ADDR, NEUTRON_CHAIN_NAME,
4-
PAIR_NAME, STABLE_PAIR_NAME, TOKEN_NAME, TOKEN_REGISTRY_NAME, WHITELIST_NAME,
3+
error::Error, CW1_WHITELIST_NAME, DEFAULT_KEY, FACTORY_NAME, FACTORY_ON_OSMOSIS_NAME,
4+
NEUTRON_CHAIN_ADMIN_ADDR, NEUTRON_CHAIN_NAME, OSMOSIS_CHAIN_NAME, PAIR_NAME,
5+
STABLE_PAIR_NAME, TOKEN_NAME, TOKEN_REGISTRY_NAME, WHITELIST_NAME,
56
},
67
test_context::TestContext,
78
};
@@ -26,6 +27,12 @@ impl<'a> CreateTokenRegistryTxBuilder<'a> {
2627
self
2728
}
2829

30+
pub fn with_chain(&mut self, chain: &'a str) -> &mut Self {
31+
self.chain = chain;
32+
33+
self
34+
}
35+
2936
pub fn with_owner(&mut self, owner: impl Into<String>) -> &mut Self {
3037
self.owner = Some(owner.into());
3138

@@ -258,7 +265,7 @@ impl TestContext {
258265
key: &str,
259266
factory_owner: impl Into<String>,
260267
) -> Result<(), Error> {
261-
let neutron = self.get_chain(NEUTRON_CHAIN_NAME);
268+
let chain = self.get_chain(chain_name);
262269

263270
let pair_xyk_code_id =
264271
neutron

src/utils/setup/tokens.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ impl<'a> CreateTokenFactoryTokenTxBuilder<'a> {
1818
self
1919
}
2020

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

2424
self
@@ -61,7 +61,7 @@ impl<'a> MintTokenFactoryTokenTxBuilder<'a> {
6161
self
6262
}
6363

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

6767
self

0 commit comments

Comments
 (0)