Tutorial: how to stake LP tokens in the Astroport Generator contract and withdraw rewards using Terra.js

July 15, 2022
Technical
Tutorial: how to stake LP tokens in the Astroport Generator contract and withdraw rewards using Terra.js

One of the issues that liquidity providers (LPs) face is the decision of which platform to provide liquidity to, as the rewards might differ in amount or value. Astroport addresses this problem with special, proxy-based smart contracts that enable simultaneous “dual farming” of ASTRO tokens and governance tokens coming from another community seeking to incentivize LPs.

Rather than depositing Astroport LP tokens directly in third-party staking contracts, LPs can deposit their tokens into the Astroport Generator contract. With the use of proxy contracts, the Generator contract forward LP tokens into relevant third-party staking contracts. Therefore, LPs end up accruing two sets of rewards (both ASTRO and the third-party protocol tokens).

In a previous tutorial, we walked through providing liquidity to an Astroport pool, which gives us LP tokens in return. In this tutorial, we will go over how to deposit your LP tokens in the Astroport Generator contract to receive additional ASTRO or dual rewards.

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:

Provide liquidity

This tutorial assumes that you have LP tokens to stake in the Generator contract. To get a hold of LP tokens, refer to our first tutorial on providing liquidity. Note that you should still use the Terra 2.0 setup mentioned above.

2. Depositing LP tokens in the Generator contract

Retrieving the LP token contract address

Before we deposit LP tokens into the Astroport Generator contract, we need to know the corresponding LP token contract address. For this example, we will be using the ASTRO-LUNA contract to retrieve the LP token address for the pair.

For convenience, set up a “contract_address” variable with the address of the contract you are currently working on. For our example, we will begin with the ASTRO-LUNA testnet address and eventually switch to the LP contract for the pair. For a full list of Astroport contract addresses, look here.

If you are missing the contract address for a specific Astroport pair, refer to our first tutorial (section on searching for Astroport pairs using the factory contract.)

Once you have the contract address for a pair, you can retrieve the associated LP token contract address by calling the “pair” query.

Returns:

Adding LP tokens to your wallet

If you are using a web extension wallet, you can add the LP token contract address to the list of tokens on your wallet. As a CW-20 token, an Astroport LP token can be swapped, staked, or sent to another address.

If you have provided liquidity, you should see an active LP token balance. When you deposit your LP tokens into the Astroport Generator contract, your LP token balance should decrease.

Depositing LP tokens

Now that we have the LP token contract address, we can deposit our tokens into the Generator contract and start earning ASTRO or dual rewards.

For this tutorial, we are done with the ASTRO LUNA contract and need to switch our “contract_address” variable to the corresponding LP token contract address.

To deposit our LP tokens into the Generator contract, we use the “send” function on the LP token contract and specify the Generator address under the “contract” parameter. We need to also specify the amount and include a Base64 encoded message (more on this next). Finally, the function is wrapped in an execute variable which also contains our wallet information and the contract we are working with:

The “msg” parameter contains a “deposit” message that’s required for our call to execute properly. You can use any 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, the “contract_address” we are working with, and the ExecuteMsg (“send”) to call.

To execute this call and retrieve the transaction hash, we use the command line and node.js.

Returns:

Congrats! You’ve staked your LP tokens in the Astroport Generator contract and will now start earning ASTRO or dual rewards. We can use the transaction hash that’s returned to get more information about our transaction using a Terra block explorer like finder.terra.money


You can select “Show Logs” to expand the transaction details. The “wasm” section will display the transfer of LP tokens to the Generator contract.

Auto Staking

If you want to provide liquidity and automatically stake all of your LP tokens with the Generator contract, you can do so in one transaction.

A guide for this has already been covered in our first tutorial on providing liquidity (under optional parameters).

3. Withdrawing LP tokens/rewards

Withdrawing LP tokens from the Generator contract

If we want to withdraw our LP tokens, we now have to do this in two steps. First, we need to withdraw them from the Generator contract. Second, we need to withdraw them from the liquidity pool itself. This tutorial will only cover withdrawing LP tokens in the first case.

Since we will be working with the Generator contract next, we will need to change our “contract_address” variable accordingly.

To withdraw LP tokens staked in the Generator contract and claim outstanding rewards, we call the “withdraw” function on the Generator contract. The “withdraw” function takes in the contract address of our LP token (in our case, the ASTRO-LUNA LP token address) and the amount of LP tokens we want to withdraw from the Generator contract.

An address can also claim rewards without unstaking their LP tokens from the Generator contract by specifying the amount as 0 (you don’t withdraw any LP tokens, only claim rewards).

To execute this call and retrieve the transaction hash, we use the command line and node.js.

Returns:

Congrats! You’ve withdrawn your LP tokens from the Generator contract and have claimed staking rewards. We can use the transaction hash that’s returned to get more information about our transaction using a Terra block explorer like finder.terra.money

You can select “Show Logs” to expand the transaction details. The “wasm” section will display the withdrawal of LP tokens from the Generator contract.

Congrats! You’ve completed the Generator contract tutorial!

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