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

1. Prerequisites

Terra.js

// 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);

Provide liquidity

2. Depositing LP tokens in the Generator contract

Retrieving the LP token contract address

// astro-luna addr
const contract_address = 'terra1udsua9w6jljwxwgwsegvt6v657rg3ayfvemupnes7lrggd28s0wq7g8azm'
// query config paramsconst query = terra.wasm.contractQuery(
contract_address,
{
"pair": {}
} // query msg
).then(result => { console.log(result) });
node index.js
{
asset_infos: [ { token: [Object] }, { native_token: [Object] } ],

contract_addr: 'terra1udsua9w6jljwxwgwsegvt6v657rg3ayfvemupnes7lrggd28s0wq7g8azm',

liquidity_token: 'terra1886vn036tc9e7ejx8pe4nkhts3gwpdfegwc4n3u77n0q76fjdthqarl8uc',
pair_type: { xyk: {} }
}

Adding LP tokens to your wallet

Depositing LP tokens

// astro-luna LP token
const contract_address = 'terra1886vn036tc9e7ejx8pe4nkhts3gwpdfegwc4n3u77n0q76fjdthqarl8uc'
const execute = new MsgExecuteContract(
wallet.key.accAddress, // sender
contract_address, // contract account address
{
"send": {
// generator addr
"contract": "terra1gc4d4v82vjgkz0ag28lrmlxx3tf6sq69tmaujjpe7jwmnqakkx0qm28j2l",
"amount": "3075518",
"msg": "ewogICJkZXBvc2l0Ijoge30KfQ=="
}
}, // 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();
node index.js
54A3403484898F7A4B23CC11AE244AF224C8C7A8754BA842ED4D5F5643A9077C

Auto Staking

3. Withdrawing LP tokens/rewards

Withdrawing LP tokens from the Generator contract

// generator testnet address
const contract_address = 'terra1gc4d4v82vjgkz0ag28lrmlxx3tf6sq69tmaujjpe7jwmnqakkx0qm28j2l'
// withdraw function
const execute = new MsgExecuteContract(
wallet.key.accAddress, // sender
contract_address, // contract account address
{
"withdraw": {
"lp_token": "terra1886vn036tc9e7ejx8pe4nkhts3gwpdfegwc4n3u77n0q76fjdthqarl8uc",
"amount": "3075518"
}
},
);const start = async function () {
const executeTx = await wallet.createAndSignTx({
msgs: [execute]
})
.then(tx => terra.tx.broadcast(tx))
.then(result => {
console.log(result.txhash)
});
}start();
node index.js
A4278F0C5DEE7BE6C95DF12BADEC730AA42DBAE5D3BDD8D404AF591C810FBF16

Congrats! You’ve completed the Generator contract tutorial!

DISCLAIMER

--

--

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

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