Skip to content

Commit

Permalink
feat: stargate-poc
Browse files Browse the repository at this point in the history
  • Loading branch information
ratik committed Dec 10, 2023
1 parent a97d020 commit e1c199c
Show file tree
Hide file tree
Showing 10 changed files with 197 additions and 3 deletions.
24 changes: 24 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 8 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 2 additions & 0 deletions contracts/stargate-poc/.cargo/config
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[alias]
schema = "run --bin lido-stargate-poc-schema"
47 changes: 47 additions & 0 deletions contracts/stargate-poc/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
[package]
authors = ["Sergey Ratiashvili <[email protected]>"]
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"
1 change: 1 addition & 0 deletions contracts/stargate-poc/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# LIDO Interchain interceptor
15 changes: 15 additions & 0 deletions contracts/stargate-poc/src/bin/lido-stargate-poc-schema.rs
Original file line number Diff line number Diff line change
@@ -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
}
}
79 changes: 79 additions & 0 deletions contracts/stargate-poc/src/contract.rs
Original file line number Diff line number Diff line change
@@ -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<Response> {
Ok(Response::default().add_attribute("method", "instantiate"))
}

#[cfg_attr(not(feature = "library"), entry_point)]
pub fn query(deps: Deps, env: Env, msg: QueryMsg) -> NeutronResult<Binary> {
match msg {
QueryMsg::Trace { hash } => query_trace(deps, env, hash),
}
}

fn query_trace(deps: Deps, _env: Env, hash: String) -> NeutronResult<Binary> {
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<u8>,
) -> StdResult<String> {
let raw = to_json_vec::<QueryRequest<Empty>>(&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<Response> {
deps.api.debug("WASMDEBUG: migrate");
Ok(Response::default())
}
3 changes: 3 additions & 0 deletions contracts/stargate-poc/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pub mod contract;
pub mod msg;
pub mod state;
10 changes: 10 additions & 0 deletions contracts/stargate-poc/src/msg.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
use cosmwasm_schema::cw_serde;

#[cw_serde]
pub struct InstantiateMsg {}

#[cw_serde]
pub enum ExecuteMsg {}

#[cw_serde]
pub struct MigrateMsg {}
8 changes: 8 additions & 0 deletions contracts/stargate-poc/src/state.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
use cosmwasm_schema::{cw_serde, QueryResponses};

#[cw_serde]
#[derive(QueryResponses)]
pub enum QueryMsg {
#[returns(String)]
Trace { hash: String },
}

0 comments on commit e1c199c

Please sign in to comment.