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.
| Setting | Value |
|---|---|
| Chain ID | phoenix-1 |
| Address prefix | terra |
| HD path | m/44'/330'/0'/0/0 (coin type 330) |
| Fee denom | uluna |
| RPC | https://terra-rpc.publicnode.com |
| REST (LCD) | https://terra-rest.publicnode.com |
| Explorer | https://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
| Asset | Denom or address |
|---|---|
| LUNA | uluna |
| ASTRO (IBC from Neutron) | ibc/8D8A7F7253615E5F76CB6252A1E1BD921D5EDB7BBAAF8913FB1C77FF125D9995 |
| USDC (Noble) | ibc/2C962DAB9F57FE0921435426AE75196009FAA1981BF86991203C8411F8980FDB |
| USDC.inj | ibc/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
_18import { CosmWasmClient, SigningCosmWasmClient } from "@cosmjs/cosmwasm-stargate";_18import { DirectSecp256k1HdWallet } from "@cosmjs/proto-signing";_18import { stringToPath } from "@cosmjs/crypto";_18import { GasPrice } from "@cosmjs/stargate";_18_18const rpc = "https://terra-rpc.publicnode.com";_18_18// Queries_18const client = await CosmWasmClient.connect(rpc);_18_18// Transactions_18const wallet = await DirectSecp256k1HdWallet.fromMnemonic(process.env.MNEMONIC, {_18 prefix: "terra",_18 hdPaths: [stringToPath("m/44'/330'/0'/0/0")], // Terra's coin type_18});_18const 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
_4const router = "terra1txm8xcy8mete9e5zh5ktqr4jm4cq4ja8djttg3zkqll64x4ksq7qknaesf"; // Router 2.0.0_4_4const config = await client.queryContractSmart(router, { config: {} });_4console.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.
_19const [{ address: sender }] = await wallet.getAccounts();_19const pool = "terra1v3lqxl0eyte9x3nhdgcj8hwvjq76aupnnzz0yll8mxs5cckc29pqvg2scu"; // LUNA/USDC_19_19const 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_19console.log(result.transactionHash);
Terra's own SDK, feather.js, also works if you already use it; the contract messages are the same.