Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

autopilot formatting & timeouts #74

Merged
merged 3 commits into from
Jul 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 10 additions & 27 deletions contracts/covenant/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@
use cosmwasm_std::entry_point;
use cosmwasm_std::{
to_binary, Binary, CosmosMsg, Deps, DepsMut, Env, MessageInfo, Reply, Response, StdResult,
SubMsg, Uint128, WasmMsg,
SubMsg, WasmMsg,
};

use cw2::set_contract_version;
use cw_utils::parse_reply_instantiate_data;
use neutron_sdk::bindings::msg::IbcFee;

use crate::{
error::ContractError,
Expand All @@ -16,7 +15,7 @@ use crate::{
CLOCK_CODE, COVENANT_CLOCK_ADDR, COVENANT_DEPOSITOR_ADDR, COVENANT_HOLDER_ADDR,
COVENANT_LP_ADDR, COVENANT_LS_ADDR, DEPOSITOR_CODE, HOLDER_CODE, IBC_FEE, IBC_TIMEOUT,
LP_CODE, LS_CODE, POOL_ADDRESS, PRESET_CLOCK_FIELDS, PRESET_DEPOSITOR_FIELDS,
PRESET_HOLDER_FIELDS, PRESET_LP_FIELDS, PRESET_LS_FIELDS,
PRESET_HOLDER_FIELDS, PRESET_LP_FIELDS, PRESET_LS_FIELDS, TIMEOUTS,
},
};

Expand Down Expand Up @@ -47,33 +46,15 @@ pub fn instantiate(
CLOCK_CODE.save(deps.storage, &msg.preset_clock_fields.clock_code)?;

POOL_ADDRESS.save(deps.storage, &msg.pool_address)?;

PRESET_CLOCK_FIELDS.save(deps.storage, &msg.preset_clock_fields)?;
PRESET_LP_FIELDS.save(deps.storage, &msg.preset_lp_fields)?;
PRESET_LS_FIELDS.save(deps.storage, &msg.preset_ls_fields)?;
PRESET_DEPOSITOR_FIELDS.save(deps.storage, &msg.preset_depositor_fields)?;
PRESET_HOLDER_FIELDS.save(deps.storage, &msg.preset_holder_fields)?;

let ibc_timeout = if let Some(timeout) = msg.ibc_msg_transfer_timeout_timestamp {
timeout
} else {
DEFAULT_TIMEOUT_SECONDS
};
// 10 seconds
IBC_TIMEOUT.save(deps.storage, &ibc_timeout)?;
TIMEOUTS.save(deps.storage, &msg.timeouts)?;
IBC_FEE.save(
deps.storage,
&IbcFee {
recv_fee: vec![],
ack_fee: vec![cosmwasm_std::Coin {
denom: "untrn".to_string(),
amount: Uint128::new(1000u128),
}],
timeout_fee: vec![cosmwasm_std::Coin {
denom: "untrn".to_string(),
amount: Uint128::new(1000u128),
}],
},
&msg.preset_ibc_fee.to_ibc_fee(),
)?;

let clock_instantiate_tx = CosmosMsg::Wasm(WasmMsg::Instantiate {
Expand Down Expand Up @@ -199,14 +180,15 @@ pub fn handle_lp_reply(deps: DepsMut, env: Env, msg: Reply) -> Result<Response,
let clock_address = COVENANT_CLOCK_ADDR.load(deps.storage)?;
let code_id = LS_CODE.load(deps.storage)?;
let preset_ls_fields = PRESET_LS_FIELDS.load(deps.storage)?;
let ibc_timeout = IBC_TIMEOUT.load(deps.storage)?;
let ibc_fee = IBC_FEE.load(deps.storage)?;
let timeouts = TIMEOUTS.load(deps.storage)?;

let instantiate_msg = preset_ls_fields.clone().to_instantiate_msg(
clock_address.to_string(),
response.contract_address,
ibc_timeout,
ibc_fee,
timeouts.ica_timeout,
timeouts.ibc_transfer_timeout,
);

let ls_instantiate_tx = CosmosMsg::Wasm(WasmMsg::Instantiate {
Expand Down Expand Up @@ -242,16 +224,17 @@ pub fn handle_ls_reply(deps: DepsMut, env: Env, msg: Reply) -> Result<Response,
let lp_addr = COVENANT_LP_ADDR.load(deps.storage)?;
let code_id = DEPOSITOR_CODE.load(deps.storage)?;
let preset_depositor_fields = PRESET_DEPOSITOR_FIELDS.load(deps.storage)?;
let ibc_timeout = IBC_TIMEOUT.load(deps.storage)?;
let timeouts = TIMEOUTS.load(deps.storage)?;
let ibc_fee = IBC_FEE.load(deps.storage)?;

let instantiate_msg = preset_depositor_fields.clone().to_instantiate_msg(
"to be queried".to_string(),
clock_addr.to_string(),
response.contract_address,
lp_addr.to_string(),
ibc_timeout,
ibc_fee,
timeouts.ibc_transfer_timeout,
timeouts.ica_timeout,
);

let depositor_instantiate_tx = CosmosMsg::Wasm(WasmMsg::Instantiate {
Expand Down
27 changes: 21 additions & 6 deletions contracts/covenant/src/msg.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
use cosmwasm_schema::{cw_serde, QueryResponses};
use cosmwasm_std::{Addr, Coin};
use cosmwasm_std::{Addr, Uint128, Uint64};
use covenant_clock::msg::PresetClockFields;
use covenant_depositor::msg::PresetDepositorFields;
use covenant_holder::msg::PresetHolderFields;
use covenant_lp::msg::PresetLpFields;
use covenant_ls::msg::PresetLsFields;
use neutron_sdk::bindings::msg::IbcFee;

const NEUTRON_DENOM: &str = "untrn";

#[cw_serde]
pub struct InstantiateMsg {
pub label: String,
Expand All @@ -17,22 +19,35 @@ pub struct InstantiateMsg {
pub preset_holder_fields: PresetHolderFields,
pub pool_address: String,
pub ibc_msg_transfer_timeout_timestamp: Option<u64>,
// pub preset_ibc_fee: Option<PresetIbcFee>,
pub preset_ibc_fee: PresetIbcFee,
pub timeouts: Timeouts,
}

#[cw_serde]
pub struct Timeouts {
pub ica_timeout: Uint64,
pub ibc_transfer_timeout: Uint64,
}

#[cw_serde]
pub struct PresetIbcFee {
pub ack_fee: Coin,
pub timeout_fee: Coin,
pub ack_fee: Uint128,
pub timeout_fee: Uint128,
}

impl PresetIbcFee {
pub fn to_ibc_fee(self) -> IbcFee {
IbcFee {
// must be empty
recv_fee: vec![],
ack_fee: vec![self.ack_fee],
timeout_fee: vec![self.timeout_fee],
ack_fee: vec![cosmwasm_std::Coin {
denom: NEUTRON_DENOM.to_string(),
amount: self.ack_fee,
}],
timeout_fee: vec![cosmwasm_std::Coin {
denom: NEUTRON_DENOM.to_string(),
amount: self.timeout_fee,
}],
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions contracts/covenant/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ use cosmwasm_std::Addr;
use cw_storage_plus::Item;
use neutron_sdk::bindings::msg::IbcFee;

use crate::msg::Timeouts;

pub const LS_CODE: Item<u64> = Item::new("ls_code");
pub const LP_CODE: Item<u64> = Item::new("lp_code");
pub const DEPOSITOR_CODE: Item<u64> = Item::new("depositor_code");
Expand All @@ -11,6 +13,7 @@ pub const HOLDER_CODE: Item<u64> = Item::new("holder_code");
pub const POOL_ADDRESS: Item<String> = Item::new("pool_address");
pub const IBC_TIMEOUT: Item<u64> = Item::new("ibc_timeout");
pub const IBC_FEE: Item<IbcFee> = Item::new("ibc_fee");
pub const TIMEOUTS: Item<Timeouts> = Item::new("timeouts");

pub const PRESET_LS_FIELDS: Item<covenant_ls::msg::PresetLsFields> = Item::new("preset_ls_fields");
pub const PRESET_LP_FIELDS: Item<covenant_lp::msg::PresetLpFields> = Item::new("preset_lp_fields");
Expand Down
22 changes: 10 additions & 12 deletions contracts/covenant/src/suite_test/suite.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use cosmwasm_std::{Addr, Empty, Uint64};
use cosmwasm_std::{Addr, Empty, Uint64, Uint128};
use covenant_lp::msg::AssetData;
use cw_multi_test::{App, Contract, ContractWrapper, Executor};

use crate::msg::{InstantiateMsg, QueryMsg};
use crate::msg::{InstantiateMsg, QueryMsg, PresetIbcFee, Timeouts};

pub const CREATOR_ADDR: &str = "admin";
pub const TODO: &str = "replace";
Expand Down Expand Up @@ -99,16 +99,14 @@ impl Default for SuiteBuilder {
label: "covenant_contract".to_string(),
pool_address: TODO.to_string(),
ibc_msg_transfer_timeout_timestamp: None,
// preset_ibc_fee: PresetIbcFee {
// ack_fee: cosmwasm_std::Coin {
// denom: NEUTRON_DENOM.to_string(),
// amount: Uint128::new(1000u128),
// },
// timeout_fee: cosmwasm_std::Coin {
// denom: NEUTRON_DENOM.to_string(),
// amount: Uint128::new(1000u128),
// },
// },
preset_ibc_fee: PresetIbcFee {
ack_fee: Uint128::new(1000),
timeout_fee: Uint128::new(1000),
},
timeouts: Timeouts {
ica_timeout: Uint64::new(18000), // 5 hours
ibc_transfer_timeout: Uint64::new(120), // 2 minutes
},
},
}
}
Expand Down
60 changes: 29 additions & 31 deletions contracts/depositor/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use std::fmt::Error;
use cosmos_sdk_proto::cosmos::base::v1beta1::Coin;
use cosmos_sdk_proto::ibc::applications::transfer::v1::MsgTransfer;

use cosmos_sdk_proto::ibc::core::client::v1::Height;
#[cfg(not(feature = "library"))]
use cosmwasm_std::entry_point;
use cosmwasm_std::{
Expand All @@ -17,7 +16,7 @@ use neutron_sdk::interchain_queries::v045::new_register_transfers_query_msg;

use prost::Message;

use crate::msg::{ExecuteMsg, InstantiateMsg, MigrateMsg, OpenAckVersion, QueryMsg};
use crate::{msg::{ExecuteMsg, InstantiateMsg, MigrateMsg, OpenAckVersion, QueryMsg}, state::{IBC_TRANSFER_TIMEOUT, ICA_TIMEOUT}};
use neutron_sdk::{
bindings::{
msg::{MsgSubmitTxResponse, NeutronMsg},
Expand All @@ -33,7 +32,7 @@ use crate::state::{
save_reply_payload, save_sudo_payload, AcknowledgementResult, ContractState, SudoPayload,
ACKNOWLEDGEMENT_RESULTS, AUTOPILOT_FORMAT, CLOCK_ADDRESS, CONTRACT_STATE,
GAIA_NEUTRON_IBC_TRANSFER_CHANNEL_ID, GAIA_STRIDE_IBC_TRANSFER_CHANNEL_ID, IBC_FEE,
IBC_PORT_ID, IBC_TIMEOUT, ICA_ADDRESS, INTERCHAIN_ACCOUNTS, LS_ADDRESS, NATIVE_ATOM_RECEIVER,
IBC_PORT_ID, ICA_ADDRESS, INTERCHAIN_ACCOUNTS, LS_ADDRESS, NATIVE_ATOM_RECEIVER,
NEUTRON_GAIA_CONNECTION_ID, STRIDE_ATOM_RECEIVER, SUDO_PAYLOAD_REPLY_ID,
};

Expand Down Expand Up @@ -71,10 +70,11 @@ pub fn instantiate(
.save(deps.storage, &msg.gaia_stride_ibc_transfer_channel_id)?;
LS_ADDRESS.save(deps.storage, &msg.ls_address)?;
AUTOPILOT_FORMAT.save(deps.storage, &msg.autopilot_format)?;
IBC_TIMEOUT.save(deps.storage, &msg.ibc_timeout)?;
GAIA_STRIDE_IBC_TRANSFER_CHANNEL_ID
.save(deps.storage, &msg.gaia_stride_ibc_transfer_channel_id)?;
IBC_FEE.save(deps.storage, &msg.ibc_fee)?;
IBC_TRANSFER_TIMEOUT.save(deps.storage, &msg.ibc_transfer_timeout)?;
ICA_TIMEOUT.save(deps.storage, &msg.ica_timeout)?;

Ok(Response::default().add_attribute("method", "depositor_instantiate"))
}
Expand Down Expand Up @@ -127,7 +127,7 @@ fn try_tick(deps: ExecuteDeps, env: Env, info: MessageInfo) -> NeutronResult<Res

fn try_liquid_stake(
mut deps: ExecuteDeps,
_env: Env,
env: Env,
_info: MessageInfo,
_gaia_account_address: String,
) -> NeutronResult<Response<NeutronMsg>> {
Expand Down Expand Up @@ -158,8 +158,9 @@ fn try_liquid_stake(
let stride_receiver = STRIDE_ATOM_RECEIVER.load(deps.storage)?;
let gaia_stride_channel: String =
GAIA_STRIDE_IBC_TRANSFER_CHANNEL_ID.load(deps.storage)?;
let timeout = IBC_TIMEOUT.load(deps.storage)?;

let ibc_transfer_timeout = IBC_TRANSFER_TIMEOUT.load(deps.storage)?;
let ica_timeout = ICA_TIMEOUT.load(deps.storage)?;

let amount = stride_receiver.amount.to_string();
let st_ica = stride_ica_addr;

Expand All @@ -168,23 +169,19 @@ fn try_liquid_stake(
amount,
};

// let autopilot_receiver = AUTOPILOT_FORMAT
// .load(deps.storage)?
// .replace("{st_ica}", &st_ica);
// AUTOPILOT_FORMAT.save(deps.storage, &autopilot_receiver)?;
let autopilot_receiver = format!("{{\"autopilot\": {{\"receiver\": \"{st_ica}\",\"stakeibc\": {{\"stride_address\": \"{st_ica}\",\"action\": \"LiquidStake\"}}}}}}");
let autopilot_receiver = AUTOPILOT_FORMAT
.load(deps.storage)?
.replace("{st_ica}", &st_ica);
AUTOPILOT_FORMAT.save(deps.storage, &autopilot_receiver)?;

let stride_msg = MsgTransfer {
source_port: "transfer".to_string(),
source_channel: gaia_stride_channel,
token: Some(coin),
sender: address,
receiver: autopilot_receiver,
timeout_height: Some(Height {
revision_number: 3,
revision_height: 1000,
}),
timeout_timestamp: 0,
timeout_height: None,
timeout_timestamp: env.block.time.plus_seconds(ibc_transfer_timeout.u64()).nanos(),
};

// Serialize the Transfer message
Expand All @@ -204,7 +201,7 @@ fn try_liquid_stake(
INTERCHAIN_ACCOUNT_ID.to_string(),
vec![protobuf],
"".to_string(),
timeout,
ica_timeout.u64(),
fee,
);

Expand All @@ -219,8 +216,6 @@ fn try_liquid_stake(
},
)?;

// CONTRACT_STATE.save(deps.storage, &ContractState::LiquidStaked)?;

Ok(Response::default()
.add_attribute("method", "try_liquid_stake")
// .add_attribute("stride_submit_msg_hex", encode_hex(protobuf.value.as_slice()))
Expand All @@ -234,7 +229,7 @@ fn try_liquid_stake(

fn try_receive_atom_from_ica(
deps: ExecuteDeps,
_env: Env,
env: Env,
_info: MessageInfo,
_gaia_account_address: String,
) -> NeutronResult<Response<NeutronMsg>> {
Expand All @@ -247,7 +242,8 @@ fn try_receive_atom_from_ica(
let source_channel = GAIA_NEUTRON_IBC_TRANSFER_CHANNEL_ID.load(deps.storage)?;
let lp_receiver = NATIVE_ATOM_RECEIVER.load(deps.storage)?;
let amount = lp_receiver.amount.to_string();
let timeout = IBC_TIMEOUT.load(deps.storage)?;
let ibc_transfer_timeout = IBC_TRANSFER_TIMEOUT.load(deps.storage)?;
let ica_timeout = ICA_TIMEOUT.load(deps.storage)?;
let fee = IBC_FEE.load(deps.storage)?;

let coin = Coin {
Expand All @@ -261,11 +257,8 @@ fn try_receive_atom_from_ica(
token: Some(coin),
sender: address.clone(),
receiver: lp_receiver.address,
timeout_height: Some(Height {
revision_number: 2,
revision_height: 1300,
}),
timeout_timestamp: 0,
timeout_height: None,
timeout_timestamp: env.block.time.plus_seconds(ibc_transfer_timeout.u64()).nanos(),
};

// Serialize the Transfer message
Expand All @@ -285,7 +278,7 @@ fn try_receive_atom_from_ica(
INTERCHAIN_ACCOUNT_ID.to_string(),
vec![protobuf],
address,
timeout,
ica_timeout.u64(),
fee,
);

Expand Down Expand Up @@ -489,8 +482,9 @@ pub fn migrate(deps: DepsMut, _env: Env, msg: MigrateMsg) -> StdResult<Response>
gaia_stride_ibc_transfer_channel_id,
ls_address,
autopilot_format,
ibc_timeout,
ibc_fee,
ibc_transfer_timeout,
ica_timeout,
} => {
if let Some(clock_addr) = clock_addr {
CLOCK_ADDRESS.save(deps.storage, &deps.api.addr_validate(&clock_addr)?)?;
Expand Down Expand Up @@ -527,8 +521,12 @@ pub fn migrate(deps: DepsMut, _env: Env, msg: MigrateMsg) -> StdResult<Response>
AUTOPILOT_FORMAT.save(deps.storage, &autopilot_f)?;
}

if let Some(timeout) = ibc_timeout {
IBC_TIMEOUT.save(deps.storage, &timeout)?;
if let Some(timeout) = ibc_transfer_timeout {
IBC_TRANSFER_TIMEOUT.save(deps.storage, &timeout)?;
}

if let Some(timeout) = ica_timeout {
ICA_TIMEOUT.save(deps.storage, &timeout)?;
}

if let Some(fee) = ibc_fee {
Expand Down
Loading