Skip to content

Commit

Permalink
add example oracle & marketmap query contracts
Browse files Browse the repository at this point in the history
  • Loading branch information
quasisamurai committed Jun 3, 2024
1 parent ab2fa67 commit 7a3e7c9
Show file tree
Hide file tree
Showing 16 changed files with 439 additions and 0 deletions.
6 changes: 6 additions & 0 deletions contracts/marketmap/.cargo/config
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[alias]
wasm = "build --release --target wasm32-unknown-unknown"
wasm-debug = "build --target wasm32-unknown-unknown"
unit-test = "test --lib --features backtraces"
integration-test = "test --test integration"
schema = "run --example schema"
44 changes: 44 additions & 0 deletions contracts/marketmap/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
[package]
name = "marketmap"
version = "0.1.0"
edition = "2021"


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"]

[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"]
library = []

[dependencies]
cosmwasm-std = "1.3.1"
cw2 = "1.1.0"
schemars = "0.8.10"
serde = { version = "1.0.180", default-features = false, features = ["derive"] }
neutron-sdk = { path = "../../packages/neutron-sdk", default-features = false }


[dev-dependencies]
cosmwasm-schema = { version = "1.3.1", default-features = false }
3 changes: 3 additions & 0 deletions contracts/marketmap/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# MarketMap

The example contract shows how interact with MarketMap module.
30 changes: 30 additions & 0 deletions contracts/marketmap/examples/schema.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Copyright 2022 Neutron
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use std::env::current_dir;
use std::fs::create_dir_all;

use cosmwasm_schema::{export_schema, remove_schemas, schema_for};
use marketmap::contract::InstantiateMsg;
use neutron_sdk::bindings::marketmap::query::MarketMapQuery;

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!(InstantiateMsg), &out_dir);
export_schema(&schema_for!(MarketMapQuery), &out_dir);
}
5 changes: 5 additions & 0 deletions contracts/marketmap/schema/instantiate_msg.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "InstantiateMsg",
"type": "object"
}
43 changes: 43 additions & 0 deletions contracts/marketmap/schema/marketmap_query.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "MarketmapQuery",
"oneOf": [
{
"description": "Parameters queries the parameters of the module.",
"type": "object",
"required": [
"params"
],
"properties": {
"params": {
"type": "object"
}
},
"additionalProperties": false
},
{
"type": "object",
"required": [
"get_last_updated_request"
],
"properties": {
"get_last_updated_request": {
"type": "object"
}
},
"additionalProperties": false
},
{
"type": "object",
"required": [
"get_market_map_request"
],
"properties": {
"get_market_map_request": {
"type": "object"
}
},
"additionalProperties": false
}
]
}
71 changes: 71 additions & 0 deletions contracts/marketmap/src/contract.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
use cosmwasm_std::{
entry_point, to_json_binary, Binary, Deps, DepsMut, Env, MessageInfo, Response, StdResult,
};
use cw2::set_contract_version;
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
pub struct InstantiateMsg {}

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq)]
#[serde(rename_all = "snake_case")]
pub enum ExecuteMsg {}

use neutron_sdk::bindings::marketmap::query::MarketResponse;
use neutron_sdk::bindings::{
marketmap::query::{LastUpdatedResponse, MarketMapQuery, MarketMapResponse, ParamsResponse},
msg::NeutronMsg,
query::NeutronQuery,
};

const CONTRACT_NAME: &str = concat!("crates.io:neutron-contracts__", env!("CARGO_PKG_NAME"));
const CONTRACT_VERSION: &str = env!("CARGO_PKG_VERSION");

#[entry_point]
pub fn instantiate(
deps: DepsMut,
_env: Env,
_info: MessageInfo,
_msg: InstantiateMsg,
) -> StdResult<Response> {
deps.api.debug("WASMDEBUG: instantiate");
set_contract_version(deps.storage, CONTRACT_NAME, CONTRACT_VERSION)?;
Ok(Response::default())
}

#[entry_point]
pub fn execute(
_deps: DepsMut,
_env: Env,
_info: MessageInfo,
_msg: ExecuteMsg,
) -> StdResult<Response<NeutronMsg>> {
Ok(Default::default())
}

#[entry_point]
pub fn query(deps: Deps<NeutronQuery>, env: Env, msg: MarketMapQuery) -> StdResult<Binary> {
query_marketmap(deps, env, msg)
}

fn query_marketmap(deps: Deps<NeutronQuery>, _env: Env, msg: MarketMapQuery) -> StdResult<Binary> {
match msg {
MarketMapQuery::Params { .. } => {
let query_response: ParamsResponse = deps.querier.query(&msg.into())?;
to_json_binary(&query_response)
}
MarketMapQuery::LastUpdated { .. } => {
let query_response: LastUpdatedResponse = deps.querier.query(&msg.into())?;
to_json_binary(&query_response)
}
MarketMapQuery::MarketMap { .. } => {
let query_response: MarketMapResponse = deps.querier.query(&msg.into())?;
to_json_binary(&query_response)
}
MarketMapQuery::Market { .. } => {
let query_response: MarketResponse = deps.querier.query(&msg.into())?;
to_json_binary(&query_response)
}
}
}
1 change: 1 addition & 0 deletions contracts/marketmap/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod contract;
6 changes: 6 additions & 0 deletions contracts/oracle/.cargo/config
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[alias]
wasm = "build --release --target wasm32-unknown-unknown"
wasm-debug = "build --target wasm32-unknown-unknown"
unit-test = "test --lib --features backtraces"
integration-test = "test --test integration"
schema = "run --example schema"
43 changes: 43 additions & 0 deletions contracts/oracle/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
[package]
name = "oracle"
version = "0.1.0"
edition = "2021"


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"]

[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"]
library = []

[dependencies]
cosmwasm-std = "1.3.1"
cw2 = "1.1.0"
schemars = "0.8.10"
serde = { version = "1.0.180", default-features = false, features = ["derive"] }
neutron-sdk = { path = "../../packages/neutron-sdk", default-features = false }

[dev-dependencies]
cosmwasm-schema = { version = "1.3.1", default-features = false }
4 changes: 4 additions & 0 deletions contracts/oracle/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Oracle

The example contract shows how to interact with Oracle module

30 changes: 30 additions & 0 deletions contracts/oracle/examples/schema.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Copyright 2022 Neutron
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use std::env::current_dir;
use std::fs::create_dir_all;

use cosmwasm_schema::{export_schema, remove_schemas, schema_for};
use neutron_sdk::bindings::oracle::query::OracleQuery;
use oracle::contract::InstantiateMsg;

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!(InstantiateMsg), &out_dir);
export_schema(&schema_for!(OracleQuery), &out_dir);
}
5 changes: 5 additions & 0 deletions contracts/oracle/schema/instantiate_msg.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "InstantiateMsg",
"type": "object"
}
79 changes: 79 additions & 0 deletions contracts/oracle/schema/oracle_query.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "OracleQuery",
"oneOf": [
{
"description": "Parameters queries the parameters of the module.",
"type": "object",
"required": [
"get_all_currency_pairs"
],
"properties": {
"get_all_currency_pairs": {
"type": "object"
}
},
"additionalProperties": false
},
{
"type": "object",
"required": [
"get_price_request"
],
"properties": {
"get_price_request": {
"type": "object",
"required": [
"currency_pair"
],
"properties": {
"currency_pair": {
"$ref": "#/definitions/CurrencyPair"
}
}
}
},
"additionalProperties": false
},
{
"type": "object",
"required": [
"get_prices_request"
],
"properties": {
"get_prices_request": {
"type": "object",
"required": [
"currency_pair_ids"
],
"properties": {
"currency_pair_ids": {
"type": "array",
"items": {
"type": "string"
}
}
}
}
},
"additionalProperties": false
}
],
"definitions": {
"CurrencyPair": {
"type": "object",
"required": [
"base",
"quote"
],
"properties": {
"base": {
"type": "string"
},
"quote": {
"type": "string"
}
}
}
}
}
Loading

0 comments on commit 7a3e7c9

Please sign in to comment.