Skip to content

Lockup Contracts Delegation

Izek edited this page Feb 10, 2022 · 1 revision

Deposit and stake the tokens

Lockup contracts can stake their balance, regardless of their vesting schedule. You can proceed in two steps:

  • Check the lockup balance

  • Stake the balance

  • To know how many tokens you can stake, use the view method get_balance:

    near view $LOCKUP_ID get_balance ''

You should expect a result like:

near view meerkat.stakewars.testnet get_balance ''
View call: meerkat.stakewars.testnet.get_balance()
'100000499656128234500000000'

Where the resulting balance (in yoktoNEAR) is 100000499656128234500000000 or 100 $NEAR.

Heads Up

You have to leave 35 $NEAR in the lockup contract for the storage fees. If you have 100 tokens, you can stake up to 65 tokens. If you have 1000 tokens, you can stake up to 965 tokens.

You may want to npm i -g near-units to easily convert yoctoNEAR amounts to human-readable representations, and to go from human-typeable numbers to yoctoNEAR. Examples:

near-units -h 100000499656128234500000000 yN
near-units 965 N

On Linux/macOS/Unix, you can also use this directly in the following commands:

near call $LOCKUP_ID deposit_and_stake '{
   "amount": "'$(near-units 965N)'"
}' --accountId $ACCOUNT_ID --gas $(near-units 200Tgas)

You may want to set your desired amount-to-stake to a variable:

export AMOUNT=# your desired amount to stake

To stake the balance, use the call method deposit_and_stake:

near call $LOCKUP_ID deposit_and_stake '{
  "amount": "'$AMOUNT'"
}' --accountId $ACCOUNT_ID --gas 200000000000000

If using a Ledger:

near call $LOCKUP_ID deposit_and_stake '{
  "amount": "'$AMOUNT'"
}' --accountId $ACCOUNT_ID --gas 200000000000000 --useLedgerKey $HD_PATH 

Heads up

This sets the gas parameter to 200000000000000 gas units, or 200 Tgas (Teragas), as the near-cli default allocation of 30Tgas is too low.

You should expect a result like:

Scheduling a call: meerkat.stakewars.testnet.deposit_and_stake({"amount": "65000000000000000000000000"})
Receipts: BFJxgYMbPLpKy4jae3b5ZJfLuAmpcYCBG2oJniDgh1PA, 9sZSqdLvPXPzpRg9VLwaWjJaBUnXm42ntVCEK1UwmYTp, CMLdB4So6ZYPif8qq2s7zFvKw599HMBVcBfQg85AKNQm
    Log [meerkat.stakewars.testnet]: Depositing and staking 65000000000000000000000000 to the staking pool @zpool.pool.f863973.m0
Receipts: 3VpzWTG9Y2nbZmuJhmsub9bRLnwHCFtnW5NQqcMBWawF, 4BrEKDPmz3mAhnq5tTFYRQp8y9uRxGFwYffJFkPMdQ5P, HJPqqJDTajFfeEA8vFXTLDpxh4PYZCMJ2YLKpWVtwSMF
    Log [meerkat.stakewars.testnet]: Epoch 105: Contract received total rewards of 20383576173536987339742291104 tokens. New total staked balance is 104251712242552700026473040909. Total number of shares 77295581098329153455984430997
    Log [meerkat.stakewars.testnet]: Total rewards fee is 1511304065231935217653387665 stake shares.
    Log [meerkat.stakewars.testnet]: @meerkat.stakewars.testnet deposited 65000000000000000000000000. New unstaked balance is 65000000000000000000000000
    Log [meerkat.stakewars.testnet]: @meerkat.stakewars.testnet staking 64999999999999999999999999. Received 48193095953206307331949682 new staking shares. Total 1 unstaked balance and 48193095953206307331949682 staking shares
    Log [meerkat.stakewars.testnet]: Contract total staked balance is 104316712242552700026473040909. Total number of shares 77343774194282359763316380679
Receipt: FsJLXXEa8Jp8UNyiyG3XQhyHHSWzZ2bCjgWr8kPVjGmG
    Log [meerkat.stakewars.testnet]: The deposit and stake of 65000000000000000000000000 to @zpool.pool.f863973.m0 succeeded
Transaction Id AW9pFb5RjkCjsyu8ng56XVvckvd3drPBPtnqVo6bJhqh
To see the transaction in the transaction explorer, please open this url in your browser
https://explorer.testnet.near.org/transactions/AW9pFb5RjkCjsyu8ng56XVvckvd3drPBPtnqVo6bJhqh
true

The true statement at the end means the transaction was successful.

Unstake and withdraw your lockup tokens

NEAR Protocol automatically re-stakes all the rewards back to the staking pools, so your staked balance increases over time, accruing rewards.

Before starting, it's highly recommended to read the Lockup contracts documentation to understand which portion of the tokens is liquid, and which one is still locked even after the three epochs.

If you want to withdraw funds, you have to issue two separate commands:

  • Manually unstake tokens from the staking pool, and wait three epochs
  • Manually withdraw tokens back to your main account

Both these command require the amount in yoctoNEAR, which is the smallest unit of account for NEAR tokens. You can use the table below for the conversion, or install near units (npm i -g near-units) and use commands like near-units 1,000N

Staking Pool Delegation

Any funds that are not stored inside lockup contracts can be directly delegated to a staking pool by using the call method deposit_and_stake:

near call $POOL_ID deposit_and_stake '' --accountId $ACCOUNT_ID --amount 100

Or if using Ledger:

near call $POOL_ID deposit_and_stake '' --accountId $ACCOUNT_ID --useLedgerKey $HD_PATH --amount 100

You should expect a result like:

Scheduling a call: valeraverim.pool.f863973.m0.deposit_and_stake() with attached 100 NEAR
Receipts: FEfNuuCSttu7m4dKQPUSgpSFJSB86i6T2sYJWSv7PHPJ, GPYhZsyUuJgvr7gefxac4566DVQcyGs4wSFeydkmRX7D, 8hfcJuwsstnFQ1cgU5iUigvfrq1JcbaKURvKf5shaB4g
    Log [valeraverim.pool.f863973.m0]: Epoch 106: Contract received total rewards of 545371217890000000000 tokens. New total staked balance is 75030000959768421358700000000. Total number of shares 75029067713856889417103116457
    Log [valeraverim.pool.f863973.m0]: Total rewards fee is 54536443439735902364 stake shares.
    Log [valeraverim.pool.f863973.m0]: @meerkat.testnet deposited 100000000000000000000000000. New unstaked balance is 100000000000000000000000000
    Log [valeraverim.pool.f863973.m0]: @meerkat.testnet staking 99999999999999999999999999. Received 99998756169666008195607157 new staking shares. Total 1 unstaked balance and 99998756169666008195607157 staking shares
    Log [valeraverim.pool.f863973.m0]: Contract total staked balance is 75130000959768421358700000000. Total number of shares 75129066470026555425298723614
Transaction Id FDtzMmusJgFbryeVrdQyNvp6XU2xr11tecgqnnSsvyxv
To see the transaction in the transaction explorer, please open this url in your browser
https://explorer.testnet.near.org/transactions/FDtzMmusJgFbryeVrdQyNvp6XU2xr11tecgqnnSsvyxv
''

The empty string at the end, '', means the call was successful.

If you want to check your staking rewards, use the view method get_account:

near view $POOL_ID get_account '{"account_id": "'$ACCOUNT_ID'"}'

You should expect a result like:

View call: valeraverim.pool.f863973.m0.get_account({"account_id": "meerkat.testnet"})
{
  account_id: 'meerkat.testnet',
  unstaked_balance: '1',
  staked_balance: '100663740438210643632989745',
  can_withdraw: true
}

The staked balance in this example is 100663740438210643632989745, or 100.66 $NEAR (reminder: you can easily convert these using near-units -h 100663740438210643632989745yN).

A pool's rewards only compound if it has been "pinged", which means either having a direct action performed on it (like someone delegating or undelegating) or if the ping method is called on it once within a particular epoch. This is important to do if you run a pool and want your delegators to see updated reward balances.

Use the call method ping to re-calculate the rewards up to the previous epoch:

near call $POOL_ID ping '{}' --accountId $ACCOUNT_ID

Or if using a Ledger:

near call $POOL_ID ping '{}' --accountId $ACCOUNT_ID --useLedgerKey $HD_PATH

You should expect a result like:

Scheduling a call: valeraverim.pool.f863973.m0.ping({})
Transaction Id 4mTrz1hDBMTWZx251tX4M5CAo5j7LaxLiPtQrDvQgcZ9
To see the transaction in the transaction explorer, please open this url in your browser
https://explorer.testnet.near.org/transactions/4mTrz1hDBMTWZx251tX4M5CAo5j7LaxLiPtQrDvQgcZ9
''

The '' result means that your call was successful, and the get_account view method will provide updated results.

Astro App

AstroDAO App

Tech Stack

Architecture

Entities

DAOs

Users

Groups

Proposals

Bounties

Treasury

Storage

Processes

Transaction

Voting

Completing a Bounty

Lockup Contracts Delegation

Search

Tips and Tricks

Use Cases

Ecosystem

NEAR

Smart Contract

NEAR CLI

Gas

Bond

Clone this wiki locally