From e1c199cf14a11bb05ebff2f3936e3c084235eb6d Mon Sep 17 00:00:00 2001 From: Sergey Ratiashvili Date: Sun, 10 Dec 2023 09:11:21 +0100 Subject: [PATCH 1/2] feat: stargate-poc --- Cargo.lock | 24 ++++++ Cargo.toml | 11 ++- contracts/stargate-poc/.cargo/config | 2 + contracts/stargate-poc/Cargo.toml | 47 +++++++++++ contracts/stargate-poc/README.md | 1 + .../src/bin/lido-stargate-poc-schema.rs | 15 ++++ contracts/stargate-poc/src/contract.rs | 79 +++++++++++++++++++ contracts/stargate-poc/src/lib.rs | 3 + contracts/stargate-poc/src/msg.rs | 10 +++ contracts/stargate-poc/src/state.rs | 8 ++ 10 files changed, 197 insertions(+), 3 deletions(-) create mode 100644 contracts/stargate-poc/.cargo/config create mode 100644 contracts/stargate-poc/Cargo.toml create mode 100644 contracts/stargate-poc/README.md create mode 100644 contracts/stargate-poc/src/bin/lido-stargate-poc-schema.rs create mode 100644 contracts/stargate-poc/src/contract.rs create mode 100644 contracts/stargate-poc/src/lib.rs create mode 100644 contracts/stargate-poc/src/msg.rs create mode 100644 contracts/stargate-poc/src/state.rs diff --git a/Cargo.lock b/Cargo.lock index 5aaa0620..06a27dac 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -672,6 +672,30 @@ dependencies = [ "thiserror", ] +[[package]] +name = "lido-stargate-poc" +version = "1.0.0" +dependencies = [ + "base64", + "cosmos-sdk-proto", + "cosmwasm-schema", + "cosmwasm-std", + "cosmwasm-storage", + "cw-multi-test 0.19.0", + "cw-storage-plus", + "cw2", + "cw20", + "lido-interchain-interceptor-base", + "neutron-sdk", + "prost", + "prost-types", + "protobuf", + "schemars", + "serde", + "serde-json-wasm 1.0.0", + "tendermint-proto", +] + [[package]] name = "neutron-sdk" version = "0.7.0" diff --git a/Cargo.toml b/Cargo.toml index 8a2c0bbe..75d1550a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,9 +1,14 @@ [workspace] -members = ["contracts/interchain-interceptor", "contracts/interchain-interceptor-authz", "packages/interchain-interceptor-base"] +members = [ + "contracts/interchain-interceptor", + "contracts/interchain-interceptor-authz", + "contracts/stargate-poc", + "packages/interchain-interceptor-base", +] [workspace.dependencies] -cw-ownable = "0.5.1" -thiserror = "1.0.50" +cw-ownable = "0.5.1" +thiserror = "1.0.50" [profile.release] rpath = false diff --git a/contracts/stargate-poc/.cargo/config b/contracts/stargate-poc/.cargo/config new file mode 100644 index 00000000..772c5c74 --- /dev/null +++ b/contracts/stargate-poc/.cargo/config @@ -0,0 +1,2 @@ +[alias] +schema = "run --bin lido-stargate-poc-schema" diff --git a/contracts/stargate-poc/Cargo.toml b/contracts/stargate-poc/Cargo.toml new file mode 100644 index 00000000..d067b1f7 --- /dev/null +++ b/contracts/stargate-poc/Cargo.toml @@ -0,0 +1,47 @@ +[package] +authors = ["Sergey Ratiashvili "] +description = "Contract to check stargate features" +edition = "2021" +name = "lido-stargate-poc" +version = "1.0.0" + +exclude = [ + # Those files are rust-optimizer artifacts. You might want to commit them for convenience but they should not be part of the source code publication. + "contract.wasm", + "hash.txt", +] + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[lib] +crate-type = ["cdylib", "rlib"] + +[features] +# for more explicit tests, cargo test --features=backtraces +backtraces = ["cosmwasm-std/backtraces"] +# use library feature to disable all instantiate/execute/query exports +library = [] + +[dependencies] +base64 = "0.21.2" +cosmos-sdk-proto = { version = "0.20.0", default-features = false } +neutron-sdk = { git = "https://github.com/neutron-org/neutron-sdk", branch = "sdk/47" } +prost = "0.12.1" +prost-types = "0.12.1" +protobuf = "3.2.0" +tendermint-proto = "0.34.0" + +cosmwasm-schema = { version = "1.3.1" } +cosmwasm-std = { version = "1.2.1" } +cw-storage-plus = { version = "1.0.1" } +cw2 = { version = "1.0.1" } +cw20 = { version = "1.0.1" } +schemars = "0.8" +serde = { version = "1.0.127", default-features = false, features = ["derive"] } +serde-json-wasm = { version = "1.0.0" } + +lido-interchain-interceptor-base = { path = "../../packages/interchain-interceptor-base", default-features = false } + +[dev-dependencies] +cosmwasm-storage = { version = "1.0" } +cw-multi-test = "0.19.0" diff --git a/contracts/stargate-poc/README.md b/contracts/stargate-poc/README.md new file mode 100644 index 00000000..4ac41e2e --- /dev/null +++ b/contracts/stargate-poc/README.md @@ -0,0 +1 @@ +# LIDO Interchain interceptor \ No newline at end of file diff --git a/contracts/stargate-poc/src/bin/lido-stargate-poc-schema.rs b/contracts/stargate-poc/src/bin/lido-stargate-poc-schema.rs new file mode 100644 index 00000000..6b984dcd --- /dev/null +++ b/contracts/stargate-poc/src/bin/lido-stargate-poc-schema.rs @@ -0,0 +1,15 @@ +use cosmwasm_schema::write_api; + +use lido_stargate_poc::{ + msg::{ExecuteMsg, InstantiateMsg, MigrateMsg}, + state::QueryMsg, +}; + +fn main() { + write_api! { + instantiate: InstantiateMsg, + query: QueryMsg, + execute: ExecuteMsg, + migrate: MigrateMsg + } +} diff --git a/contracts/stargate-poc/src/contract.rs b/contracts/stargate-poc/src/contract.rs new file mode 100644 index 00000000..d76cf2e0 --- /dev/null +++ b/contracts/stargate-poc/src/contract.rs @@ -0,0 +1,79 @@ +use cosmos_sdk_proto::ibc; +use cosmwasm_std::{ + entry_point, to_json_binary, to_json_vec, ContractResult, Deps, Empty, QueryRequest, StdError, + SystemResult, +}; +use cosmwasm_std::{Binary, DepsMut, Env, MessageInfo, Response, StdResult}; +use neutron_sdk::NeutronResult; +use prost::Message; +use std::str::from_utf8; + +use crate::msg::{InstantiateMsg, MigrateMsg}; +use crate::state::QueryMsg; + +#[cfg_attr(not(feature = "library"), entry_point)] +pub fn instantiate( + _deps: DepsMut, + _env: Env, + _info: MessageInfo, + _msg: InstantiateMsg, +) -> NeutronResult { + Ok(Response::default().add_attribute("method", "instantiate")) +} + +#[cfg_attr(not(feature = "library"), entry_point)] +pub fn query(deps: Deps, env: Env, msg: QueryMsg) -> NeutronResult { + match msg { + QueryMsg::Trace { hash } => query_trace(deps, env, hash), + } +} + +fn query_trace(deps: Deps, _env: Env, hash: String) -> NeutronResult { + let msg = ibc::applications::transfer::v1::QueryDenomTraceRequest { hash }; + let resp = make_stargate_query( + deps, + "/ibc.applications.transfer.v1.Query/DenomTrace".to_string(), + msg.encode_to_vec(), + )?; + + Ok(to_json_binary(&resp)?) +} + +pub fn make_stargate_query( + deps: Deps, + path: String, + encoded_query_data: Vec, +) -> StdResult { + let raw = to_json_vec::>(&QueryRequest::Stargate { + path, + data: encoded_query_data.into(), + }) + .map_err(|serialize_err| { + StdError::generic_err(format!("Serializing QueryRequest: {}", serialize_err)) + })?; + match deps.querier.raw_query(&raw) { + SystemResult::Err(system_err) => Err(StdError::generic_err(format!( + "Querier system error: {}", + system_err + ))), + SystemResult::Ok(ContractResult::Err(contract_err)) => Err(StdError::generic_err(format!( + "Querier contract error: {}", + contract_err + ))), + // response(value) is base64 encoded bytes + SystemResult::Ok(ContractResult::Ok(value)) => { + let str = value.to_base64(); + deps.api + .debug(format!("WASMDEBUG: make_stargate_query: {:?}", str).as_str()); + from_utf8(value.as_slice()) + .map(|s| s.to_string()) + .map_err(|_e| StdError::generic_err("Unable to encode from utf8")) + } + } +} + +#[cfg_attr(not(feature = "library"), entry_point)] +pub fn migrate(deps: DepsMut, _env: Env, _msg: MigrateMsg) -> StdResult { + deps.api.debug("WASMDEBUG: migrate"); + Ok(Response::default()) +} diff --git a/contracts/stargate-poc/src/lib.rs b/contracts/stargate-poc/src/lib.rs new file mode 100644 index 00000000..4934c19d --- /dev/null +++ b/contracts/stargate-poc/src/lib.rs @@ -0,0 +1,3 @@ +pub mod contract; +pub mod msg; +pub mod state; diff --git a/contracts/stargate-poc/src/msg.rs b/contracts/stargate-poc/src/msg.rs new file mode 100644 index 00000000..a6422988 --- /dev/null +++ b/contracts/stargate-poc/src/msg.rs @@ -0,0 +1,10 @@ +use cosmwasm_schema::cw_serde; + +#[cw_serde] +pub struct InstantiateMsg {} + +#[cw_serde] +pub enum ExecuteMsg {} + +#[cw_serde] +pub struct MigrateMsg {} diff --git a/contracts/stargate-poc/src/state.rs b/contracts/stargate-poc/src/state.rs new file mode 100644 index 00000000..c8d79100 --- /dev/null +++ b/contracts/stargate-poc/src/state.rs @@ -0,0 +1,8 @@ +use cosmwasm_schema::{cw_serde, QueryResponses}; + +#[cw_serde] +#[derive(QueryResponses)] +pub enum QueryMsg { + #[returns(String)] + Trace { hash: String }, +} From 3806daccb86af443ccfde4779d6ac9f3f40aaab7 Mon Sep 17 00:00:00 2001 From: Sergey Ratiashvili Date: Tue, 12 Dec 2023 13:23:45 +0100 Subject: [PATCH 2/2] chore: doc --- contracts/stargate-poc/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/contracts/stargate-poc/README.md b/contracts/stargate-poc/README.md index 4ac41e2e..0f1a0175 100644 --- a/contracts/stargate-poc/README.md +++ b/contracts/stargate-poc/README.md @@ -1 +1 @@ -# LIDO Interchain interceptor \ No newline at end of file +# LIDO Stargate query POC \ No newline at end of file