Code Monkey home page Code Monkey logo

terra-cosmwasm's Introduction

Terra Bindings for CosmWasm

​ 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. ​

Installation

​ Add the following to your smart contract's Cargo.toml: ​

[dependencies]
terra-cosmwasm = { version = "1.2" }

Contents

​ Currently, the Terra bindings include: ​

  • Query support for:
    • Market
      • swap rate between 2 currencies at market price
    • Treasury
      • current tax cap for a denomination
      • current tax rate
    • Oracle
      • exchange rates for the given base_denom / quote_denoms ​
  • Messages
    • MsgSwap
    • MsgSwapSend

Usage

Querying

​ 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_cosmwasm::{ TerraQuerier, SwapResponse, TaxRateResponse, TaxCapResponse, ExchangeRatesResponse };
​
...
​
// 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: SwapResponse = querier.query_swap(offer.clone(), "uusd")?;
    let tax_cap: TaxCapResponse = querier.query_tax_cap("usdr")?;
    let tax_rate: TaxRateResponse = querier.query_tax_rate()?;
    let exchange_rates: ExchangeRatesResponse = querier.query_exchange_rates("uusd", vec!["uluna", "ukrw"])?;
    ...
}

Creating Messages

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)
}

terra-cosmwasm's People

Contributors

yun-yeo avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.