-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Simplify, implement receiver contract example
- Loading branch information
1 parent
b4e7d14
commit 737233a
Showing
18 changed files
with
317 additions
and
149 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,3 @@ | ||
use schemars::JsonSchema; | ||
use serde::{Deserialize, Serialize}; | ||
|
||
use cosmwasm_std::{IbcPacketAckMsg, Timestamp}; | ||
use cw_storage_plus::{Item, Map}; | ||
use cw_storage_plus::Item; | ||
|
||
pub const PENDING: Item<String> = Item::new("pending"); | ||
|
||
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)] | ||
pub struct IbcQueryResultResponse { | ||
/// last block balance was updated (0 is never) | ||
pub last_update_time: Timestamp, | ||
pub response: IbcPacketAckMsg, | ||
} | ||
pub const LATEST_QUERIES: Map<&str, IbcQueryResultResponse> = Map::new("queries"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
[alias] | ||
wasm = "build --release --target wasm32-unknown-unknown" | ||
wasm-debug = "build --target wasm32-unknown-unknown" | ||
unit-test = "test --lib" | ||
schema = "run --example schema" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
[package] | ||
name = "cw-ibc-query-receiver" | ||
version = "0.1.0" | ||
authors = ["Ethan Frey <[email protected]>", "Jake Hartnell <[email protected]>"] | ||
edition = "2021" | ||
publish = false | ||
license = "Apache-2.0" | ||
|
||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
||
[lib] | ||
crate-type = ["cdylib", "rlib"] | ||
|
||
[profile.release] | ||
opt-level = 3 | ||
debug = false | ||
rpath = false | ||
lto = true | ||
debug-assertions = false | ||
codegen-units = 1 | ||
panic = 'abort' | ||
incremental = false | ||
overflow-checks = true | ||
|
||
[features] | ||
# for quicker tests, cargo test --lib | ||
# for more explicit tests, cargo test --features=backtraces | ||
backtraces = ["cosmwasm-std/backtraces"] | ||
|
||
[dependencies] | ||
cw-ibc-query = { path = "../../packages/cw-ibc-query"} | ||
cosmwasm-std = { version = "1.0.0", features = ["iterator", "ibc3"] } | ||
cw-storage-plus = { version = "0.13.4" } | ||
cw-utils = { version = "0.13.4" } | ||
cw1-whitelist = { version = "0.13.4", features = ["library"]} | ||
schemars = "0.8.1" | ||
serde = { version = "1.0.103", default-features = false, features = ["derive"] } | ||
thiserror = { version = "1.0.23" } | ||
|
||
[dev-dependencies] | ||
cosmwasm-schema = { version = "1.0.0" } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# CosmWasm IBC Query Receiver | ||
|
||
An example contract illustrating how to recieve and store the result of an IBC query using `ReceiveIbcResponseMsg`. | ||
|
||
## Workflow | ||
|
||
Requires `cw-ibc-queries` contract to be deployed on the same chain from which it will recieve query results. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
use std::env::current_dir; | ||
use std::fs::create_dir_all; | ||
|
||
use cosmwasm_schema::{export_schema, remove_schemas, schema_for}; | ||
|
||
use cw_ibc_query_receiver::{ | ||
msg::{ExecuteMsg, InstantiateMsg, QueryMsg}, | ||
state::IbcQueryResultResponse, | ||
}; | ||
|
||
fn main() { | ||
let mut out_dir = current_dir().unwrap(); | ||
out_dir.push("schema"); | ||
create_dir_all(&out_dir).unwrap(); | ||
remove_schemas(&out_dir).unwrap(); | ||
|
||
export_schema(&schema_for!(ExecuteMsg), &out_dir); | ||
export_schema(&schema_for!(InstantiateMsg), &out_dir); | ||
export_schema(&schema_for!(QueryMsg), &out_dir); | ||
export_schema(&schema_for!(IbcQueryResultResponse), &out_dir); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
use cosmwasm_std::{ | ||
entry_point, to_binary, Deps, DepsMut, Env, IbcPacketAckMsg, MessageInfo, QueryResponse, | ||
Response, StdResult, | ||
}; | ||
use cw_ibc_query::ReceiveIbcResponseMsg; | ||
|
||
use crate::error::ContractError; | ||
use crate::msg::{ExecuteMsg, InstantiateMsg, QueryMsg}; | ||
use crate::state::{IbcQueryResultResponse, LATEST_QUERIES}; | ||
|
||
#[entry_point] | ||
pub fn instantiate( | ||
_deps: DepsMut, | ||
_env: Env, | ||
_info: MessageInfo, | ||
_msg: InstantiateMsg, | ||
) -> StdResult<Response> { | ||
// Do nothing for now | ||
Ok(Response::new()) | ||
} | ||
|
||
#[entry_point] | ||
pub fn execute( | ||
deps: DepsMut, | ||
env: Env, | ||
info: MessageInfo, | ||
msg: ExecuteMsg, | ||
) -> Result<Response, ContractError> { | ||
cw_utils::nonpayable(&info)?; | ||
match msg { | ||
ExecuteMsg::ReceiveIbcResponse(ReceiveIbcResponseMsg { msg }) => { | ||
execute_receive(deps, env, info, msg) | ||
} | ||
} | ||
} | ||
|
||
pub fn execute_receive( | ||
deps: DepsMut, | ||
env: Env, | ||
_info: MessageInfo, | ||
msg: IbcPacketAckMsg, | ||
) -> Result<Response, ContractError> { | ||
// which local channel was this packet send from | ||
let channel_id = msg.original_packet.src.channel_id.clone(); | ||
// store IBC response for later querying from the smart contract?? | ||
LATEST_QUERIES.save( | ||
deps.storage, | ||
&channel_id, | ||
&IbcQueryResultResponse { | ||
last_update_time: env.block.time, | ||
response: msg, | ||
}, | ||
)?; | ||
Ok(Response::default()) | ||
} | ||
|
||
#[entry_point] | ||
pub fn query(deps: Deps, _env: Env, msg: QueryMsg) -> StdResult<QueryResponse> { | ||
match msg { | ||
QueryMsg::LatestQueryResult { channel_id } => { | ||
to_binary(&query_latest_ibc_query_result(deps, channel_id)?) | ||
} | ||
} | ||
} | ||
|
||
fn query_latest_ibc_query_result( | ||
deps: Deps, | ||
channel_id: String, | ||
) -> StdResult<IbcQueryResultResponse> { | ||
let results = LATEST_QUERIES.load(deps.storage, &channel_id)?; | ||
Ok(results) | ||
} |
Oops, something went wrong.