Skip to content

Commit

Permalink
added ability to voice to specify chain address length for proxies
Browse files Browse the repository at this point in the history
  • Loading branch information
NoahSaso committed May 26, 2024
1 parent adcbc3c commit 7304d5e
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 8 deletions.
38 changes: 30 additions & 8 deletions contracts/main/voice/src/contract.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#[cfg(not(feature = "library"))]
use cosmwasm_std::entry_point;
use cosmwasm_std::{
from_json, instantiate2_address, to_json_binary, to_json_vec, Binary, CodeInfoResponse,
ContractResult, Deps, DepsMut, Env, MessageInfo, Response, StdResult, SubMsg, SystemResult,
Uint64, WasmMsg,
from_json, instantiate2_address, to_json_binary, to_json_vec, Binary, CanonicalAddr,
CodeInfoResponse, ContractResult, Deps, DepsMut, Env, MessageInfo, Response, StdResult, SubMsg,
SystemResult, Uint64, WasmMsg,
};
use cw2::set_contract_version;

Expand All @@ -13,7 +13,9 @@ use polytone::ibc::{Msg, Packet};
use crate::error::ContractError;
use crate::ibc::{ACK_GAS_NEEDED, REPLY_FORWARD_DATA};
use crate::msg::{ExecuteMsg, InstantiateMsg, MigrateMsg, QueryMsg};
use crate::state::{BLOCK_MAX_GAS, PROXY_CODE_ID, SENDER_TO_PROXY};
use crate::state::{
SenderInfo, BLOCK_MAX_GAS, CONTRACT_ADDR_LEN, PROXY_CODE_ID, PROXY_TO_SENDER, SENDER_TO_PROXY,
};

const CONTRACT_NAME: &str = "crates.io:polytone-voice";
const CONTRACT_VERSION: &str = env!("CARGO_PKG_VERSION");
Expand All @@ -37,6 +39,7 @@ pub fn instantiate(

PROXY_CODE_ID.save(deps.storage, &msg.proxy_code_id.u64())?;
BLOCK_MAX_GAS.save(deps.storage, &msg.block_max_gas.u64())?;
CONTRACT_ADDR_LEN.save(deps.storage, &msg.contract_addr_len.unwrap_or(32))?;

Ok(Response::default()
.add_attribute("method", "instantiate")
Expand Down Expand Up @@ -104,17 +107,33 @@ pub fn execute(
let contract =
deps.api.addr_canonicalize(env.contract.address.as_str())?;
let code_id = PROXY_CODE_ID.load(deps.storage)?;
let addr_len = CONTRACT_ADDR_LEN.load(deps.storage)?;
let CodeInfoResponse { checksum, .. } =
deps.querier.query_wasm_code_info(code_id)?;
let salt = salt(&connection_id, &counterparty_port, &sender);
let proxy = deps.api.addr_humanize(&instantiate2_address(
&checksum, &contract, &salt,
)?)?;
let init2_addr_data: CanonicalAddr =
instantiate2_address(&checksum, &contract, &salt)?.to_vec()
[0..addr_len as usize]
.into();
let proxy = deps.api.addr_humanize(&init2_addr_data)?;
SENDER_TO_PROXY.save(
deps.storage,
(connection_id, counterparty_port, sender.clone()),
(
connection_id.clone(),
counterparty_port.clone(),
sender.clone(),
),
&proxy,
)?;
PROXY_TO_SENDER.save(
deps.storage,
proxy.clone(),
&SenderInfo {
connection_id,
remote_port: counterparty_port,
remote_sender: sender.clone(),
},
)?;
(
Some(WasmMsg::Instantiate2 {
admin: None,
Expand Down Expand Up @@ -171,6 +190,9 @@ pub fn query(deps: Deps, _env: Env, msg: QueryMsg) -> StdResult<Binary> {
match msg {
QueryMsg::BlockMaxGas => to_json_binary(&BLOCK_MAX_GAS.load(deps.storage)?),
QueryMsg::ProxyCodeId => to_json_binary(&PROXY_CODE_ID.load(deps.storage)?),
QueryMsg::SenderInfoForProxy { proxy } => {
to_json_binary(&PROXY_TO_SENDER.load(deps.storage, deps.api.addr_validate(&proxy)?)?)
}
}
}

Expand Down
8 changes: 8 additions & 0 deletions contracts/main/voice/src/msg.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
use cosmwasm_schema::{cw_serde, QueryResponses};
use cosmwasm_std::{Binary, Uint64};

use crate::state::SenderInfo;

#[cw_serde]
pub struct InstantiateMsg {
/// Code ID to use for instantiating proxy contracts.
pub proxy_code_id: Uint64,
/// The max gas allowed in a single block.
pub block_max_gas: Uint64,
/// The contract address length used by the chain. Defaults to 32. Some
/// chains use other lengths, such as Injective which uses 20.
pub contract_addr_len: Option<u8>,
}

#[cw_serde]
Expand Down Expand Up @@ -35,6 +40,9 @@ pub enum QueryMsg {
/// `"proxy_code_id"`.
#[returns(Uint64)]
ProxyCodeId,
/// Queries the sender information for a given proxy.
#[returns(SenderInfo)]
SenderInfoForProxy { proxy: String },
}

#[cw_serde]
Expand Down
14 changes: 14 additions & 0 deletions contracts/main/voice/src/state.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
use cosmwasm_schema::cw_serde;
use cosmwasm_std::Addr;
use cw_storage_plus::{Item, Map};

/// (connection_id, remote_port, remote_sender) -> proxy
pub(crate) const SENDER_TO_PROXY: Map<(String, String, String), Addr> = Map::new("c2p");

/// proxy -> { connection_id, remote_port, remote_sender }
pub(crate) const PROXY_TO_SENDER: Map<Addr, SenderInfo> = Map::new("p2c");

/// (channel_id) -> connection_id
pub(crate) const CHANNEL_TO_CONNECTION: Map<String, String> = Map::new("c2c");

Expand All @@ -12,3 +16,13 @@ pub(crate) const PROXY_CODE_ID: Item<u64> = Item::new("pci");

/// Max gas usable in a single block.
pub(crate) const BLOCK_MAX_GAS: Item<u64> = Item::new("bmg");

/// Contract address length used by the chain.
pub(crate) const CONTRACT_ADDR_LEN: Item<u8> = Item::new("cal");

#[cw_serde]
pub struct SenderInfo {
pub connection_id: String,
pub remote_port: String,
pub remote_sender: String,
}
1 change: 1 addition & 0 deletions contracts/main/voice/src/suite_tests/suite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ impl Default for SuiteBuilder {
instantiate: InstantiateMsg {
proxy_code_id: Uint64::new(9999),
block_max_gas: Uint64::new(110_000),
contract_addr_len: None,
},
}
}
Expand Down
1 change: 1 addition & 0 deletions packages/cw-orch-polytone/src/deploy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ impl<Chain: CwEnv> Deploy<Chain> for Polytone<Chain> {
&polytone_voice::msg::InstantiateMsg {
proxy_code_id: deployment.proxy.code_id()?.into(),
block_max_gas: MAX_BLOCK_GAS.into(),
contract_addr_len: None,
},
None,
None,
Expand Down

0 comments on commit 7304d5e

Please sign in to comment.