Search
Chainlink Data Feeds are the quickest way to connect your smart contracts to the real-world market prices of assets. For example, one use for data feeds is to enable smart contracts to retrieve the latest pricing data of an asset in a single call.
This guide applies specifically to using data feeds on the Terra network. To get the full list of Chainlink Data Feeds running on the Terra Bombay testnet, see the Terra Feeds page.
Select quality data feeds
Be aware of the quality of the data that you use. Learn more about making responsible data quality decisions.
This guide demonstrates the following tasks:
This guide requires the following tools:
Confirm that the necessary requirements are installed:
rustc --version
cargo --version
rustup target list --installed
docker --version
node --version
You need a Terra Station wallet with testnet LUNA to complete the steps in this guide. If you don't already have a Terra wallet, create one to use for this guide.
After you complete these steps, you can see the LUNA balance for your wallet on the testnet.
Use git clone
to clone the example code:
git clone https://github.com/smartcontractkit/chainlink-terra-feeds-demo.git && cd ./chainlink-terra-feeds-demo
The example data feed consumer contract is in .contracts/consumer
.
From the base of the cloned repository, run cargo wasm
to
produce the ./target/wasm32-unknown-unknown/release/consumer.wasm
build file:
cargo wasm
From the base of the cloned repository, run the workspace-optimizer
Rust optimizer to ensure the smallest output size and create a reproducible build process. The optimizer runs in a Docker container and creates an /artifacts
directory with the compiled and optimized output of the contract:
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
Install the Terra.js package.
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"
From the base of the cloned repository, use NodeJS to run the readLatestPrice.mjs
to deploy, instantiate, and execute the contract.
node ./scripts/readLatestPrice.mjs
If the script is successful, it prints the current price from the of LUNA/USD feed.
{
round_data_response: {
round_id: 39516,
answer: '3706000000',
started_at: 1634497952,
updated_at: 1634497952,
answered_in_round: 39516
}
}
If the script is not successful, check to make sure that you compiled the contracts correctly, that your wallet is funded, and that you entered the correct seed phrase. You can also check the Terra Bombay Testnet Status to make sure that the testnet is available.
You can view the Rust code and scripts for this example on GitHub. See the chainlink-terra-feeds-demo repository. The example code has a few main components:
./artifacts/price_consumer.wasm
bytecode file.readLatestPrice.mjs
scriptThe ./scripts/readLatestPrice.mjs
script completes each of the steps to deploy, instantiate, and execute the price_consumer.wasm
compiled smart contract. The run()
function deploys and instantiates the price_consumer.wasm
smart contract. Then, the script uses an LCDClient
object from Terra.js to call the latest_round_data
function on the aggregator contract and retrieve the price data.
async function run() {
console.log("Deploying Price Consumer Contract");
const consmCodeId = await upload(CONSUMER_PATH);
await sleep(12000);
console.log("instatiating contract");
const consmAddress = await instantiate(consmCodeId, {"feed": "terra185esv8hg3ddn85fkwgznskf95k0th9ryvegeak"})
await sleep(12000);
console.log("reading contract");
const result = await terra.wasm.contractQuery(consmAddress, { "latest_round_data": {} } )
console.log(result);
}