This demo requires the following components:
Confirm that the necessary requirements are installed:
rustc --version
cargo --version
rustup target list --installed
docker --version
node --version
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.
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
.
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
}
}