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

July 22, 2022
Technical
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:

  • Constant Product Pools: 0.3% Total Fee — 1/3 goes to ASTRO stakers
  • Stableswap Pools: 0.05% Total Fee — 1/2 goes to ASTRO stakers

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:

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.

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:

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.

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:

Returns:

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 sendfunction 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:

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:

Returns:

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:

Config

Returns the ASTRO and xASTRO contract addresses:

Response:

Total Shares

Returns the total amount of xASTRO tokens:

Response:

Total Deposit

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

Response:

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.


Previous post

There are no previous posts

<- Back to all posts
Next post

There are no next posts

<- Back to all posts