Skip to content

Commit

Permalink
Add nativedex governance proposals to DEX test
Browse files Browse the repository at this point in the history
  • Loading branch information
ChristianBorst committed Apr 19, 2024
1 parent 80c60ac commit 56b79b8
Show file tree
Hide file tree
Showing 4 changed files with 576 additions and 24 deletions.
7 changes: 7 additions & 0 deletions integration_tests/test_runner/src/bootstrapping.rs
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,7 @@ pub struct DexAddresses {
pub query: EthAddress,
pub impact: EthAddress,
pub policy: EthAddress,
pub upgrade: EthAddress,
}

/// Parses the DEX contract addresses from the file created
Expand All @@ -290,6 +291,7 @@ pub fn parse_dex_contract_addresses() -> DexAddresses {
let mut query: EthAddress = EthAddress::default();
let mut impact: EthAddress = EthAddress::default();
let mut policy: EthAddress = EthAddress::default();
let mut upgrade: EthAddress = EthAddress::default();
for line in output.lines() {
if line.contains("CrocSwapDex deployed at Address -") {
let address_string = line.split('-').last().unwrap();
Expand All @@ -307,13 +309,18 @@ pub fn parse_dex_contract_addresses() -> DexAddresses {
let address_string = line.split('-').last().unwrap();
policy = address_string.trim().parse().unwrap();
info!("found policy address it is {}", address_string);
} else if line.contains("ColdPathUpgrade deployed at Address -") {
let address_string = line.split('-').last().unwrap();
upgrade = address_string.trim().parse().unwrap();
info!("found upgrade address it is {}", address_string);
}
}
DexAddresses {
dex,
query,
impact,
policy,
upgrade,
}
}

Expand Down
97 changes: 95 additions & 2 deletions integration_tests/test_runner/src/dex_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,24 @@ use web30::{

use crate::utils::OPERATION_TIMEOUT;

// Callpath Indices
pub const BOOT_PATH: u16 = 0;
pub const HOT_PROXY: u16 = 1;
pub const WARM_PATH: u16 = 2;
pub const COLD_PATH: u16 = 3;
pub const LONG_PATH: u16 = 4;
pub const MICRO_PATHS: u16 = 5;
pub const KNOCKOUT_LIQ_PATH: u16 = 7;
pub const KNOCKOUT_FLAG_PATH: u16 = 3500;
pub const SAFE_MODE_PATH: u16 = 9999;

lazy_static! {
pub static ref MIN_TICK: Int256 = Int256::from(-665454i64);
pub static ref MAX_TICK: Int256 = Int256::from(831818i64);
pub static ref MIN_PRICE: Uint256 = Uint256::from(65538u32);
pub static ref MAX_PRICE: Uint256 = Uint256::from(21267430153580247136652501917186561137u128);
}

// ABI result parsing notes:
// a struct with static fields is returned like a static tuple, (static fields have no tail)
// enc(X1, ..., Xk) = head(X(1)) ... head(X(k)) tail(X(1)) ... tail(X(k)) = enc(X1) ... enc(Xk)
Expand Down Expand Up @@ -464,6 +482,7 @@ pub async fn croc_query_conc_rewards(
})
}

/// Specifies a swap() call on the DEX contract
#[derive(Debug, Clone)]
pub struct SwapArgs {
pub base: EthAddress,
Expand Down Expand Up @@ -524,11 +543,14 @@ pub async fn dex_swap(
web30.wait_for_transaction(txhash, timeout, None).await
}

/// Specifies a userCmd call to be made on the DEX contract
#[derive(Debug, Clone)]
pub struct UserCmdArgs {
pub callpath: u16,
pub cmd: Vec<AbiToken>,
}

/// Calls any userCmd on the DEX contract
pub async fn dex_user_cmd(
web30: &Web3,
dex_contract: EthAddress,
Expand All @@ -555,11 +577,16 @@ pub async fn dex_user_cmd(
.await?;
web30.wait_for_transaction(txhash, timeout, None).await
}

/// Specifies a protocolCmd call to be made on the DEX contract
/// If CrocPolicy is set up then this call will fail
#[derive(Debug, Clone)]
pub struct ProtocolCmdArgs {
pub callpath: u16,
pub cmd: Vec<AbiToken>,
pub sudo: bool,
}
/// Calls any protocolCmd on the DEX contract
pub async fn dex_protocol_cmd(
web30: &Web3,
dex_contract: EthAddress,
Expand All @@ -573,8 +600,8 @@ pub async fn dex_protocol_cmd(

let cmd = clarity::abi::encode_tokens(&cmd_args.cmd);
let payload = clarity::abi::encode_call(
"protocolCmd(uint16,bytes)",
&[cmd_args.callpath.into(), cmd.into()],
"protocolCmd(uint16,bytes,bool)",
&[cmd_args.callpath.into(), cmd.into(), cmd_args.sudo.into()],
)?;
let native_in = native_in.unwrap_or(0u8.into());
let txhash = web30
Expand All @@ -586,3 +613,69 @@ pub async fn dex_protocol_cmd(
.await?;
web30.wait_for_transaction(txhash, timeout, None).await
}

/// Conveniently wraps dex_protocol_cmd() to invoke an authority transfer, useful when setting up CrocPolicy
pub async fn dex_authority_transfer(
web30: &Web3,
dex_contract: EthAddress,
new_auth: EthAddress,
wallet: PrivateKey,
timeout: Option<Duration>,
) -> Result<TransactionResponse, Web3Error> {
let code: Uint256 = 20u8.into();

// ABI: authorityTransfer (uint8 cmd_code, address auth)
let cmd_args = vec![code.into(), new_auth.into()];

dex_protocol_cmd(
web30,
dex_contract,
wallet,
ProtocolCmdArgs {
callpath: COLD_PATH,
cmd: cmd_args,
sudo: true,
},
None,
timeout,
)
.await
}

pub async fn dex_query_safe_mode(
web30: &Web3,
dex_contract: EthAddress,
caller: Option<EthAddress>,
) -> Result<bool, Web3Error> {
// ABI: bool internal inSafeMode_
let caller = caller.unwrap_or(dex_contract);
let payload = clarity::abi::encode_call("safeMode()", &[])?;

let query_res = web30
.simulate_transaction(
TransactionRequest::quick_tx(caller, dex_contract, payload),
None,
)
.await?;

Ok(query_res[31] > 0u8)
}

pub async fn dex_query_authority(
web30: &Web3,
dex_contract: EthAddress,
caller: Option<EthAddress>,
) -> Result<EthAddress, Web3Error> {
// ABI: bool internal inSafeMode_
let caller = caller.unwrap_or(dex_contract);
let payload = clarity::abi::encode_call("authority()", &[])?;

let query_res = web30
.simulate_transaction(
TransactionRequest::quick_tx(caller, dex_contract, payload),
None,
)
.await?;

Ok(EthAddress::from_slice(&query_res[12..32])?)
}
Loading

0 comments on commit 56b79b8

Please sign in to comment.