Skip to main content

Terra

Overview​

Terra (chain ID phoenix-1) is one of the two chains Astroport runs on. The tutorials use CosmJS, which works with Terra once you set the address prefix and HD path below. Terra Classic is not supported.

SettingValue
Chain IDphoenix-1
Address prefixterra
HD pathm/44'/330'/0'/0/0 (coin type 330)
Fee denomuluna
RPChttps://terra-rpc.publicnode.com
REST (LCD)https://terra-rest.publicnode.com
Explorerhttps://terrasco.pe/mainnet/address/<address>

The endpoints are public and run by a third party. For production, run your own node or pick a provider from the Cosmos chain registry.

Assets on Terra​

AssetDenom or address
LUNAuluna
ASTRO (IBC from Neutron)ibc/8D8A7F7253615E5F76CB6252A1E1BD921D5EDB7BBAAF8913FB1C77FF125D9995
USDC (Noble)ibc/2C962DAB9F57FE0921435426AE75196009FAA1981BF86991203C8411F8980FDB
USDC.injibc/E8481AD838C31D4FC12A504B10F9B4E2F830F8818D2735C2FFC707579B5FA60B
yASTRO (CW20)terra1fe8uj96z7f4jfsnkrdxhchxdhdtx8alr9zjqze76hm2ga6tpxjjq5rdjw7

ASTRO on Terra is the IBC denom above. The old CW20 ASTRO (terra1nsuqsk6kh58ulczatwev87ttq2z6r3pusulg9r24mfj2fvtzd4uq3exn26) is legacy, and converting it to IBC ASTRO no longer works. yASTRO is Terra's ASTRO staking token: see yASTRO.

Connecting with CosmJS​


_18
import { CosmWasmClient, SigningCosmWasmClient } from "@cosmjs/cosmwasm-stargate";
_18
import { DirectSecp256k1HdWallet } from "@cosmjs/proto-signing";
_18
import { stringToPath } from "@cosmjs/crypto";
_18
import { GasPrice } from "@cosmjs/stargate";
_18
_18
const rpc = "https://terra-rpc.publicnode.com";
_18
_18
// Queries
_18
const client = await CosmWasmClient.connect(rpc);
_18
_18
// Transactions
_18
const wallet = await DirectSecp256k1HdWallet.fromMnemonic(process.env.MNEMONIC, {
_18
prefix: "terra",
_18
hdPaths: [stringToPath("m/44'/330'/0'/0/0")], // Terra's coin type
_18
});
_18
const signingClient = await SigningCosmWasmClient.connectWithSigner(rpc, wallet, {
_18
gasPrice: GasPrice.fromString("0.015uluna"),
_18
});

Without the hdPaths option, CosmJS derives the Cosmos Hub address (coin type 118) from your mnemonic, which is not the address Terra wallets show.

Querying a Contract​


_4
const router = "terra1txm8xcy8mete9e5zh5ktqr4jm4cq4ja8djttg3zkqll64x4ksq7qknaesf"; // Router 2.0.0
_4
_4
const config = await client.queryContractSmart(router, { config: {} });
_4
console.log(config); // { astroport_factory: "terra14x9fr055x5hvr48hzy2t4q7kvjvfttsvxusa4xsdcy702mnzsvuqprer8r" }

Executing a Message​

This swaps 1 LUNA for USDC in the LUNA/USDC pool. See Swapping for how to set belief_price and max_spread.


_19
const [{ address: sender }] = await wallet.getAccounts();
_19
const pool = "terra1v3lqxl0eyte9x3nhdgcj8hwvjq76aupnnzz0yll8mxs5cckc29pqvg2scu"; // LUNA/USDC
_19
_19
const result = await signingClient.execute(
_19
sender,
_19
pool,
_19
{
_19
swap: {
_19
offer_asset: { info: { native_token: { denom: "uluna" } }, amount: "1000000" },
_19
belief_price: beliefPrice, // offer / return from a simulation, as a decimal string
_19
max_spread: "0.01",
_19
},
_19
},
_19
"auto",
_19
"",
_19
[{ denom: "uluna", amount: "1000000" }] // the offer asset
_19
);
_19
_19
console.log(result.transactionHash);

Terra's own SDK, feather.js, also works if you already use it; the contract messages are the same.