Crates.io | terra-bindings |
lib.rs | terra-bindings |
version | 1.0.0 |
source | src |
created_at | 2020-07-06 07:39:51.233447 |
updated_at | 2020-07-06 09:31:55.166857 |
description | Bindings for CosmWasm contracts to call into custom modules of Terra Core |
homepage | |
repository | https://github.com/terra-project/terra-cosmwasm-bindings |
max_upload_size | |
id | 261860 |
size | 24,723 |
This crate provides Terra-specific bindings to enable your CosmWasm smart contracts to interact with the Terra blockchain by exposing messages and queriers that can be emitted and used from within your contract.
Add the following to your smart contract's Cargo.toml
:
[dependencies]
terra_cosmwasm = { version = "1.0" }
Currently, the Terra bindings include:
MsgSwap
MsgSwapSend
In order to use the query functions enabled by the bindings, create a TerraQuerier
instance within your contract logic -- in either init()
, handle()
, or query()
entrypoints. You can access all the enabled queries through this object.
// src/contract.rs
use cosmwasm_std::Coin;
use terra_bindings::TerraQuerier;
...
// handler
pub fn try_something<S: Storage, A: Api, Q: Querier>(
deps: &mut Extern<S, A, Q>,
env: Env,
offer: &Coin
) -> StdResult<HandleResponse> {
let querier = TerraQuerier::new(&deps.querier);
let swap_rate: Coin = querier.query_swap(offer.clone(), "uusd")?;
let tax_cap: Uint128 = querier.query_tax_cap("")?;
let tax_rate: Uint128 = querier.query_tax_rate()?;
...
}
NOTE: The Terra bindings do not cover messages that have already been implemented by the CosmWasm team, such as staking-related messages and fundamental ones like MsgSend
.
You may want your contract to perform messages such as MsgSwap
and MsgSwapSend
operations at the end of its execution. To do this, create a message using the predefined functions:
create_swap_msg
create_swap_send_msg
And add it to the vector of messages
in your HandleResponse
before you return Ok
.
use cosmwasm_std::CosmosMsg;
use terra_cosmwasm::{create_swap_msg, TerraMsgWrapper};
...
pub fn try_something<S: Storage, A: Api, Q: Querier>(
deps: &mut Extern<S, A, Q>,
env: Env,
offer: &Coin
) -> StdResult<HandleResponse<TerraMsgWrapper>> {
...
let msg: CosmosMsg<TerraMsgWrapper> = create_swap_msg(contract_addr, offer_coin, ask_denom);
let res = HandleResponse {
messages: vec![msg],
log: vec![],
data: None
};
Ok(res)
}