Tutorial: how to interact with the Astroport Staking contract using Terra.js

The Astroport Staking contract allows ASTRO holders to stake their tokens in exchange for xASTRO. This has two main utilities:

1. Swap Fees: a fee is charged on all Astroport trades in the respective native pool tokens. This fee is embedded in the Astroport smart contracts and is subject to adjustment by governance. A portion of swap fees goes to liquidity providers, another portion goes to ASTRO stakers:

Swap fees for each pool are split between LPs and ASTRO stakers according to the pool type:

For xASTRO holders, fees are used to buy ASTRO on the open market and are deposited into the xASTRO staking pool. Over time, each xASTRO becomes convertible for more ASTRO than was originally staked.

2. Governance: xASTRO holders will have the power to propose and make binding votes on smart contract parameter changes, smart contract upgrades, and treasury disbursements. In a future article, we will expand on governance and the Assembly contract.

For the fifth tutorial in this series, we will go over how to interact with the Astroport Staking contract (staking, unstaking, and querying) using Terra.js.

1. Prerequisites

Terra.js

For a step-by-step walkthrough on setting up Terra.js, refer to the third tutorial in this series. You should end up with something like this:

// required modules
const { LCDClient, MnemonicKey, MsgExecuteContract } = require('@terra-money/terra.js');
// connecting to terra blockchain
const terra = new LCDClient({
URL: 'https://pisco-lcd.terra.dev',
chainID: 'pisco-1',
});
// wallet information
const mk = new MnemonicKey({
mnemonic: ''
});
const wallet = terra.wallet(mk);

ASTRO Tokens

This tutorial assumes that you have ASTRO tokens to deposit into the Staking contract. To swap for ASTRO tokens, refer to our second tutorial in this series. Note that you should still use the Terra 2.0 setup mentioned above.

2. Set up

To complete this tutorial, you will need to interact with 3 Astroport contracts: the Staking contract itself in addition to the ASTRO and xASTRO contracts. For convenience, set up 3 variables with the addresses of each contract. Note that this tutorial uses the testnet addresses. For a full list of Astroport contract addresses, look here.

const staking_address = "terra19t8ffmz6q2rdm3rllyksd6sex6n650a4anttzzvz9mf8mqr4nkrq44cyu6";const astro_address = "terra167dsqkh2alurx997wmycw9ydkyu54gyswe3ygmrs4lwume3vmwks8ruqnv";const xastro_address = "terra1ctzthkc0nzseppqtqlwq9mjwy9gq8ht2534rtcj3yplerm06snmqfc5ucr";

3. Staking ASTRO

To deposit our ASTRO tokens into the Staking contract, we use the send function for the ASTRO token contract and specify the Staking address under the contract parameter. We also need to specify the amount of ASTRO to stake and include a Base64 encoded message (more on this next). Finally, the function is wrapped in an execute variable which contains our wallet information and the astro_address variable we created:

const execute = new MsgExecuteContract(
wallet.key.accAddress, // sender
astro_address, // contract address
{
"send": {
"contract": "terra19t8ffmz6q2rdm3rllyksd6sex6n650a4anttzzvz9mf8mqr4nkrq44cyu6",
"amount": "10000000",
"msg": "ewogICJlbnRlciI6IHt9Cn0="
}
}, // handle msg
);

The msg parameter contains an enter message to send to the Staking contract. You can use a Base64 encoder to complete this step:

To complete the call, we need an async function that creates and signs the transaction with our wallet. The msgs parameter takes in the execute variable we created above which contains our wallet information, our target contract address, and the ExecuteMsg (send) to call.

const start = async function () {
const executeTx = await wallet.createAndSignTx({
msgs: [execute]
})
.then(tx => terra.tx.broadcast(tx))
.then(result => {
console.log(result.txhash)
});
}
start();

Before we execute our call, we can use a wallet to quickly check our token balances:

We use the command line and node.js to execute the call and retrieve the transaction hash:

node index.js

Returns:

3775C58F9EB0C54CC83A7906EC62808319C87531ED5AD75CF2173E9A9FD4FE5B

Congrats! You’ve staked your ASTRO tokens in the Staking contract and will start accruing swap fees. We can check our wallet again to confirm our token balance has changed:

We can also use the hash that’s returned to get more information about our transaction using a Terra block explorer like terrasco.pe (for mainnet) and finder.terra.money:

We can select “Show Logs” to expand the transaction details. The “wasm” section will display the transfer of ASTRO tokens to the Staking contract and the minting of xASTRO in exchange:

4. Unstaking xASTRO

To withdraw our ASTRO tokens from the Staking contract, we use the send function for the xASTRO token contract and specify the Staking address under the contract parameter. Note that we are calling the xASTRO contract for unstaking and the ASTRO contract for staking. We also need to specify the amount of xASTRO to stake and include a Base64 encoded message (more on this next). Finally, the function is wrapped in an execute variable which contains our wallet information and the xastro_address variable we created:

const execute = new MsgExecuteContract(
wallet.key.accAddress, // sender
xastro_address, // contract address
{
"send": {
"contract": "terra19t8ffmz6q2rdm3rllyksd6sex6n650a4anttzzvz9mf8mqr4nkrq44cyu6",
"amount": "10000000",
"msg": "ewogICJsZWF2ZSI6IHt9Cn0"
}
}, // handle msg
);
const start = async function () {
const executeTx = await wallet.createAndSignTx({
msgs: [execute]
})
.then(tx => terra.tx.broadcast(tx))
.then(result => {
console.log(result.txhash)
});
}
start();

The msg parameter contains a leave message to send to the Staking contract. You can use a Base64 encoder to complete this step:

We use the command line and node.js to execute the call and retrieve the transaction hash:

node index.js

Returns:

1EA7076D517E17A4932F3C9DFF766C90B2261A2560A4EEB4D4BAB8C56DA6A488

Congrats! You’ve staked your ASTRO tokens in the Staking contract and will start accruing swap fees. We can check our wallet again to confirm our token balance has changed:

We can also use the hash that’s returned to get more information about our transaction using a Terra block explorer like terrasco.pe (for mainnet) and finder.terra.money:

We can select “Show Logs” to expand the transaction details. The “wasm” section will display the burning of xASTRO and transfer of ASTRO from the Staking contract:

5. Querying the Staking contract

All query messages for the Staking contract are described below. A custom struct is defined for each query response. Queries are executed in the command line using node.js:

node index.js

Config

Returns the ASTRO and xASTRO contract addresses:

// query config paramsconst query = terra.wasm.contractQuery(
staking_address,
{
"config": {}
} // query msg
).then(result => { console.log(result) });

Response:

{
deposit_token_addr: 'terra167dsqkh2alurx997wmycw9ydkyu54gyswe3ygmrs4lwume3vmwks8ruqnv',
share_token_addr: 'terra1ctzthkc0nzseppqtqlwq9mjwy9gq8ht2534rtcj3yplerm06snmqfc5ucr'
}

Total Shares

Returns the total amount of xASTRO tokens:

// query config paramsconst query = terra.wasm.contractQuery(
staking_address,
{
"total_shares": {}
} // query msg
).then(result => { console.log(result) });

Response:

871578645933

Total Deposit

Returns the total amount of ASTRO deposits in the staking contract:

// query config paramsconst query = terra.wasm.contractQuery(
staking_address,
{
"total_deposit": {}
} // query msg
).then(result => { console.log(result) });

Response:

871578645933

6. Congrats! You have completed the tutorial on the Astroport Staking contract!

For more information regarding Astroport smart contracts, visit the Astroport docs.

Follow Astroport on Twitter and subscribe to the Astroport email newsletter to get the latest alerts from the mothership.

DISCLAIMER

Any mention of third-party protocols is not an endorsement. As always, DYOR. This article does not constitute investment advice. Before interacting with Astroport, review the project disclaimers here.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Astroport

Astroport

5.9K Followers

An automated, decentralised exchange protocol on the Terra blockchain. http://astroport.fi