Showing how to deploy a Terra smart contract using Chainlink Data Feeds

Overview

Chainlink Terra

Developing

Requirements

This demo requires the following components:

  • Rust:
    • rustup with cargo 1.44.1+.
    • rustc and cargo 1.44.1+.
    • Install the wasm32-unknown-unknown target for Rust: rustup target add wasm32-unknown-unknown
  • NodeJS 14 LTS or later.
  • Docker
  • A C compiler such as the one included in GCC.

Confirm that the necessary requirements are installed:

rustc --version
cargo --version
rustup target list --installed
docker --version
node --version

Compile a contract

Example contracts are available in the ./contracts directory of this repository. Make sure you can compile and run the contracts before making any changes. From the base of your cloned repository, run cargo wasm to produce a wasm build for each contract:

cargo wasm

This produces a .wasm build file in the ./target/wasm32-unknown-unknown/release/ directory for each contract.

Generating JSON Schema

While the Wasm calls (init, handle, query) accept JSON, this is not enough information to use it. We need to expose the schema for the expected messages to the clients. You can generate this schema by calling cargo schema, which will output four files in ./YOUR_CONTRACT/schema, corresponding to the three message types the contract accepts, as well as the internal State.

cd ./contracts/YOUR_CONTRACT && cargo schema

These files are in standard json-schema format, which should be usable by various client side tools, either to auto-generate codecs, or just to validate incoming json with regard to the defined schema.

Preparing the Wasm bytecode for production

Before we upload it to a chain, we need to ensure the smallest output size possible, as this will be included in the body of a transaction. We also want to have a reproducible build process, so third parties can verify that the uploaded Wasm code did indeed come from the claimed rust code.

To do this, use the following rust-optimizer (or to be specific in our case - workspace-optimizer), a docker image to produce an extremely small build output in a consistent manner. From the base of your cloned repository, run the workspace-optimizer container:

docker run --rm -v "$(pwd)":/code \
  --mount type=volume,source="$(basename "$(pwd)")_cache",target=/code/target \
  --mount type=volume,source=registry_cache,target=/usr/local/cargo/registry \
  cosmwasm/workspace-optimizer:0.11.4

This produces an ./artifacts directory in the root of the workspace. It contains the compiled and optimized WebAssembly output of the contracts along with a Sha256 checksum in ./artifacts/checksums.txt.

Deploying and Instantiating Price Consumer Contract, and Reading the Latest Price

This example uses the readLatestPrice.mjs script to deploy, instantiate, and read the Price Consumer Contract.

Install the Terra.js package, which supports several wallet functions in the script:

npm install @terra-money/terra.js

Export your wallet seed phrase to the TERRA_SEED environment variable. The script uses the seed phrase to determine the wallet key that you want to use to deploy and call the smart contract.

export TERRA_SEED="YOUR SEED PHRASE"

Run the script:

node ./scripts/readLatestPrice.mjs

After the script runs, the price consumer contract should be deployed and instantiated on the Terra testnet. The console prints the latest LUNA/USD price.

{
  round_data_response: {
    round_id: 39516,
    answer: '3706000000',
    started_at: 1634497952,
    updated_at: 1634497952,
    answered_in_round: 39516
  }
}
You might also like...
sblade or switchblade it's a multitool in one capable of doing simple analysis with any type of data, attempting to speed up ethical hacking activities

sblade or switchblade it's a multitool in one capable of doing simple analysis with any type of data, attempting to speed up ethical hacking activities

NSE is a rust cli binary and library for extracting real-time data from National Stock Exchange (India)

NSE Check out the sister projects NsePython and SaveKiteEnctoken which are Python & Javascript libraries to use the NSE and Zerodha APIs respectively

A dead-simple tool for working with data in Kafka
A dead-simple tool for working with data in Kafka

ktool - a tool for Kafka ktool is a dead-simple tool for working with data in Kafka: Copy partitions / topics to disk Replay messages Inspect message

dm-jitaux is a Rust-based JIT compiler using modified auxtools, dmasm and Inkwell LLVM wrapper for boosting Byond DM performance without any hassle!

dm-jitaux is a Rust-based JIT compiler using modified auxtools, dmasm and Inkwell LLVM wrapper for boosting Byond DM performance without any hassle (such as rewriting/refactroing your DM code).

A Simple, But amazing telegram bot, Made using the Rust language!

Telegram bot in Rust A fun Telegram bot made using Rust language.

Rust bindings to Cloudflare Worker KV Stores using wasm-bindgen and js-sys.

worker-kv Rust bindings to Cloudflare Worker KV Stores using wasm-bindgen and js-sys

Private swaps for Secret Network using a private entropy pool & differential privacy.

WIP SecretSwap: Anon Edition Private swaps for Secret Network! Uses private entropy pool for differential privacy when reporting pools sizes. Swap amo

A template for kick starting a Cloudflare worker project using workers-rs.

Getting Started A template for kick starting a Cloudflare worker project using workers-rs. This template is designed for compiling Rust to WebAssembly

twilight-interactions is a set of macros and utilities to work with Discord Interactions using twilight.

Twilight interactions twilight-interactions is a set of macros and utilities to work with Discord Interactions using twilight. Note: This crate is not

Comments
  • account sequence mismatch, expected 1, got 0: incorrect account sequence

    account sequence mismatch, expected 1, got 0: incorrect account sequence

    I've followed the steps in the readme, and I'm running into this:

    node ./scripts/readLatestPrice.mjs
    
    Deploying Price Consumer Contract
    [{"events":[{"type":"message","attributes":[{"key":"action","value":"/terra.wasm.v1beta1.MsgStoreCode"},{"key":"module","value":"wasm"}]},{"type":"store_code","attributes":[{"key":"sender","value":"terra1v38va9ujp5zmwsyf7n0d0vyngggavhwvn2p694"},{"key":"code_id","value":"20658"}]}]}]
    instatiating contract
    account sequence mismatch, expected 1, got 0: incorrect account sequence
    SyntaxError: Unexpected token a in JSON at position 0
        at JSON.parse (<anonymous>)
        at extractContractAddress (file:///Users/patrick/code/chainlink-terra-feeds-demo/scripts/readLatestPrice.mjs:100:23)
        at instantiate (file:///Users/patrick/code/chainlink-terra-feeds-demo/scripts/readLatestPrice.mjs:86:12)
        at processTicksAndRejections (internal/process/task_queues.js:95:5)
        at async run (file:///Users/patrick/code/chainlink-terra-feeds-demo/scripts/readLatestPrice.mjs:40:24)
    
    bug 
    opened by PatrickAlphaC 0
Owner
SmartContract
SmartContract
Terra development suite

rover Terra development suite Planned features Usage Commands Planned features Includes a starter smart contract, can be cw-template or similar. Has a

Jason Stallings 3 Nov 9, 2021
Terra rust bot

terra-rust-bot ⚠️ You will need to provide your seed phrase to let the bot create and sign transactions. ⚠️ Terra-rust-bot will not save your seed phr

Philipp 32 Oct 19, 2022
MeiliSearch is a powerful, fast, open-source, easy to use and deploy search engine

MeiliSearch is a powerful, fast, open-source, easy to use and deploy search engine. Both searching and indexing are highly customizable. Features such as typo-tolerance, filters, and synonyms are provided out-of-the-box. For more information about features go to our documentation.

MeiliSearch 31.6k Dec 30, 2022
A GitHub Action to automatically build and deploy your mdbook project.

?? deploy-mdbook The deploy-mdbook action allows you to easily build and deploy your mdBook project to GitHub Pages. See action.yml for configuration

null 27 Oct 24, 2022
This contract implements simple vote for the best coffe in indonesia using near protocol.

vote-coffe-near Description This contract implements simple vote for the best coffe in indonesia using near protocol. Contract in contract/src/lib.rs

RickhySis25 1 Nov 15, 2021
Uindex is a data store, for data that can be parsed as sentences in some context-free language.

Uindex - Universal index Uindex is a data store, for data that can be parsed as sentences in some context-free language.

Enrique Pérez Arnaud 3 Jul 20, 2021
A library to access BGPKIT Broker API and enable searching for BGP data archive files over time from public available data sources.

BGPKIT Broker BGPKIT Broker is a online data API service that allows users to search for publicly available BGP archive files by time, collector, proj

BGPKIT 10 Nov 30, 2022
RusQTTbom takes weather data from the Bureau of Meteorology (BOM) and publishes that data via MQTT messages.

RusQTTbom RusQTTbom collects weather data from the Bureau of Meterology (BOM) then publishes said data locally via MQTT messages. BOM weather data is

Athenars 7 Jan 13, 2023
A tool using binrw to read FFXIV data files

last-legend-dob A tool using binrw to read FFXIV data files. Mainly made to harvest the music from the game files for personal consumption, since it t

Octavia Togami 2 Oct 10, 2022
An (unofficial) Rust library for querying db-ip.com data

db_ip An (unofficial) library for querying db-ip.com CSV databases in safe Rust. This library is not affiliated with or endorsed by db-ip.com. Be advi

Finn Bear 4 Dec 2, 2022