How to Delegate NEAR Tokens through the Command Line

Cryptium Labs
Cryptium Labs
Published in
6 min readSep 24, 2020

Network Updates

As announced recently, the NEAR network has started transitioning from Phase 0 to Phase I on Thursday, the 24th of September 2020, which entails the progression from Proof-of-Authority to Proof-of-Stake and the NEAR Foundation ceasing to operate validators on the network (more details on this article by Stefano Pepe). Additionally, the token holders will have the ability to stake on the network and earn a rewards. Nonetheless, inflationary rewards and transfers will only be enabled after a successful governance vote (more details on this article by Erik Trautman).

A meerkat relaxing — by GoodFon

How to Stake on NEAR with the NEAR-CLI

At the time of writing, the only means to trustless delegate on the NEAR network is through the near-cli. This article provides a straightforward how-to delegate your tokens on NEAR guide using the command line.

Before getting started

  1. We really recommend to take a look at the Hitchhiker’s Guide to the NEAR to learn about the basics on consensus, proof-of-stake and other basic information.
  2. On this guide:
$ this is an example command➜ this is an example command line output or message

3. The example commands below are on MacOS, there might be slight variations if you are on a different OS and that command line support might be limited to OS and Linux systems.

Setting Up the NEAR-CLI on Your Device

Installing Node.js

  1. You can find the latest version of Node.js here. Make sure that you are installing at least version 10, if not later.
  2. Once installed, verify the installation and version by running:
$ node -v
➜ v14.9.0

Installing near-cli

  1. The near-cli can be found in this repository (check that it’s from the NEAR team GitHub organisation).
  2. To install the near-cli run the command. This can take a bit and the command line output looks similar to the following:
$ npm install -g near-cli
➜ ⸨░░░░░░░░░░░░░░░░░░⸩ ⠼ fetchMetadata: sill resolveWithNewModule @ledgerhq/hw-tr
[...]+ near-cli@1.1.0
added 302 packages from 218 contributors in 36.581s

3. Verify the installation by running near, which will display information about usage:

$ near➜ Usage: near <command> [options]Commands:
near create-account <accountId> create a new developer account (subaccount of the masterAccount, ex: app.alice.test)
near state <accountId> view account state
near delete <accountId> <beneficiaryId> delete an account and transfer funds to beneficiary account.
near keys <accountId> view account public keys
near tx-status <hash> lookup transaction status by hash
near deploy [accountId] [wasmFile] [initFunction] [initArgs] [initGas] [initDeposit] deploy your smart contract
near dev-deploy [wasmFile]
[...]

You’re ready to roll!

On NEAR Lockup Contracts & Staking Pools

Before playing with the near-cli, there are two fundamental smart contracts that NEAR token holders should be aware of: the lockup and staking pool smart contracts.

At this stage your NEAR tokens are managed by your lockup smart contract, unless you have acquired your NEAR tokens via an exchange. The lockup smart contract handles for instance the vesting schedule of your NEAR tokens, conditions of which might vary depending on under what terms you have acquired your tokens. Nonetheless, independent to the vesting schedule of your lockup contract, you can still stake your NEAR tokens to a staking pool (operated by NEAR validators).

You can find the list of active staking pool operators on the NEAR explorer, for example Cryptium Labs’ staking pool is:

cryptium.poolv1.near Staking 1,200,032.50435 Ⓝ

Interacting with Your Lockup Contract

Your LOCKUP_ID

Depending on how you have contributed to the NEAR token sales, by now you should’ve been contacted by the NEAR Foundation or e.g. CoinList about generating your NEAR contribution accounts. The details of the process might differ depending on how you have contributed, but grosso modo it looks like the following:

  1. You have generated a private and public key using a hardware wallet (Ledger and its NEAR app), locally (file-based keys), or even via a 2FA authentication with your email or phone number.
  2. You have been contacted by the NEAR Foundation or CoinList and asked to provide your public key (NEVER share your private key!).
  3. If you have made the contribution through CoinList, you were given the option of picking a human-readable OWNER_ID (just like DNS), if you have made the contribution through the NEAR Foundation, you have most likely been assigned a OWNER_ID (but manageable only by you through your private key).
  4. After the 24th of September 2020, you should receive your LOCKUP_ID from the NEAR Foundation and CoinList (make sure you keep track of their official communication channels).

Verifying Control of Your Lockup Contract with NEAR-CLI

Once you have your LOCKUP_ID, you can verify that you in fact have control over the contract and its funds via the near-cli:

$ near view <LOCKUP_ID> get_owner_account_id ''

Which should display <OWNER_ID>, for example meerkat:

➜ meerkat.mainnet

Checking the Amount of Tokens on Your Lockup Contract with NEAR-CLI

To check how many tokens you have available to stake, run the command:

$ near view <LOCKUP_ID> get_balance ''
➜ View call: meerkat.mainnet.get_balance() '100000499656128234500000000'
# ~100 $NEAR

Note that the unit above Yocto (10^-24).

Ready to stake!

Staking NEAR with Your Lockup Contract

  1. To stake your NEAR, first we need to set the staking pool of your lockup contract (note that you can only set one staking pool at a time per lockup contract):
$ near call <LOCKUP_ID> select_staking_pool '{"staking_pool_account_id": "<POOL_ID>"}' --accountId <OWNER_ID>

For this command, you will need three pieces of information:

  • LOCKUP_ID (what you have received from the NEAR Foundation or CoinList)
  • POOL_ID for example cryptium.poolv1.near
  • OWNER_ID such as meerkat.mainnet

The resulting command line output should look like:

➜ near call meerkat.mainnet select_staking_pool ‘{“staking_pool_account_id”: “cryptium.poolv1.near”}’ — accountId meerkat.mainnet Scheduling a call: meerkat.mainnet.select_staking_pool({“staking_pool_account_id”: “cryptium.poolv1.near”}) Receipts: 
[...]
true

2. Before staking, remember that you have to always leave at least 35 $near in the lockup contract for the storage fees.

3. To stake, use the following command:

$ near call <LOCKUP_ID> deposit_and_stake '{"amount": "<AMOUNT>"}' --accountId <OWNER_ID>

Where <AMOUNT> is in Yocto (10^-24) and where you leave at least 35 $NEAR in the lockup account for storage fees. Using the example above, we’ll be staking 65000000000000000000000000. When successfully staked, the console should output something similar to:

➜ Scheduling a call: meerkat.mainnet.deposit_and_stake({"amount": "65000000000000000000000000"})[...]true

4. The true statement at the end indicates that the operation was successful and now you have staked your tokens on NEAR.

5. The rewards that are generated on NEAR are automatically staked.

6. As staking on NEAR is trustless (the validator has no access to your rewards) and only the owner can manage the tokens, you can check from time to time how many rewards you have accumulated with the following command:

$ near call <LOCK_ID> refresh_staking_pool_balance '' --accountId <OWNER_ID>Log [meerkat.mainnet]: The current total balance on the staking pool is 65000000000000000000000000

Staking Non-Lockup NEAR Tokens

In the near future, you might get a hold on NEAR tokens that are not allocated through lockup contracts (for example via exchanges). In order to stake those, use the following command:

$ near call <POOL_ID> deposit_and_stake '' --accountId <YOUR OWNER_ID> --amount 100

As these tokens are not managed by a lockup smart contract, LOCKUP_ID is not applicable here. After staking, you can check the status and rewards by:

$ near view <POOL_ID> get_account '{"account_id": "<OWNER_ID>"}'

Where POOL_ID is for example cryptium.poolv1.near:

➜ View call: cryptium.poolv1.near.get_account({"account_id": "meerkat.mainnet"}){   
account_id: 'meerkat.mainnet',
unstaked_balance: '1',
staked_balance: '100663740438210643632989745',
can_withdraw: true
}

NEAR x Cryptium Labs

Cryptium Labs has participated in all major testnets including StakeWars and is currently on the active validator set of NEAR’s mainnet (see cryptium.poolv1.near). Additionally, we are a founding member of the NEAR Validator Advisory Board (NVAB). Just like with the existing networks, Cryptium Labs service fees on NEAR will be competitive with the market. Soon you will be able to find all the information relevant to NEAR stakeholders on Cryptium Labs NEAR portal.

Follow us on Twitter & Medium to stay up-to-date with NEAR staking

--

--

Cryptium Labs
Cryptium Labs

Security-Oriented Staking from the Swiss Alps. Currently securing Tezos, Cosmos, Kusama. More on https://cryptium.ch