AI Network
  • What is AI Network
  • AIN Blockchain
    • Architecture
      • Design Principles
      • Event-driven Architectures
      • Blockchain Database
        • States
          • State Types
          • Operations
          • Predefined Structures
        • Rules and Owners
          • Rule Configs
          • Owner Configs
        • Functions
          • Built-in Functions
      • Instant Execution, and Eventual Consistency
      • Network ID and Chain ID
      • Transactions
        • Structure
        • Nonce
        • Read Concern
        • Propagation
      • Block Structure
      • Account and Keys
      • Consensus
      • Scalability
      • Apps
    • Developer Guide
      • Quick Start
      • AI Network Products
        • AI Network Worker
        • AI Network Insight
        • Testnet Server Node
        • Ainize Trigger
          • Project user
          • Project deployer
          • Why do we have to use Ainize Trigger?
      • Token Bridge
      • Trouble Shooting
    • Developer Reference
      • Blockchain API
        • JSON RPC API
        • Node Client API
      • Blockchain SDK
        • ain-js
        • ain-py
      • Validators
    • Staking
  • AIN DAO
    • What is AIN DAO
      • Runo (Run Your Node)
      • GPU Sponsorship Program
    • Onboarding & Participation
    • Governance
    • Tokenomics
      • AI Network Tokenomics
      • AINFT Tokenomics
  • AI Agents
    • AINFT
    • AINFT Factory
    • AINFT Projects
      • 🍳MiniEggs
      • 🦈Baby Shark
      • 🛸Soul Fiction
      • 🎻NFT Classics Society
    • Developer Reference
      • Ainft-Js
      • AINFT tutorial
        • Create AINFT object and Mint
        • Transfer AINFT
        • Set metadata of AINFT
        • Search and Retrieve AINFT
  • AIN Wallet
    • What is AIN Wallet?
    • AIN Wallet API
  • PROPOSAL DOCUMENTS
    • AIN Improvement Memos (AIMs)
    • AIN Improvement Proposals (AIPs)
Powered by GitBook
On this page
  • What is staking?
  • How does staking work?
  • How is this different from other blockchain?
  • How to stake?
  • Method 1: Using ain-js
  • Method 2: Using blockchain tools
  • How to decide the staking amount?

Was this helpful?

  1. AIN Blockchain

Staking

What is staking?

Staking is a way to lock your tokens to help the blockchain network or applications run smoothly. In return, you may earn rewards or contribute to the stability of the system. Think of it like putting your money in a savings account: you're helping the system grow, and sometimes you earn interest.

How does staking work?

In our system, staking works a bit differently depending on where you stake your tokens:

  1. Staking in the consensus app

  • What it does: AIN Blockchain adopts the PoS consensus algorithm. The staking amount in the 'consensus' app is used to create new blocks on the blockchain.

  • What you get: You earn rewards based on the blocks created.

  • Why it's important: It secures the blockchain and keeps it running smoothly.

  1. Staking in other apps

  • What it does: Your tokens provide the resources needed to write data to the blockchain.

  • What you get: Rewards depend on the app developer's decision. Some apps may offer rewards, while others may not.

  • Why it's important: It keeps the apps functional and contributes to the ecosystem.

How is this different from other blockchain?

  • In other blockchain, you lock your tokens to help the network and earn rewards like new tokens or fees.

  • In our blockchain, you can do more:

    1. Earn rewards: By staking in the consensus app, you earn rewards for block production.

    2. Support apps: By staking in other apps, you support their operations with rewards depending on the app developer's choice.

This gives you the choice to either focus on earning rewards or supporting the ecosystem.

How to stake?

Method 1: Using ain-js

  1. Install ain-js Make sure you have Node.js and ain-js installed.

$ npm install @ainblockchain/ain-js
  1. Stake your tokens

Use the following example code to connect to the blockchain and stake tokens:

stake_app.js
const Ain = require('@ainblockchain/ain-js').default;

const ain = new Ain('https://testnet-api.ainetwork.ai', 'wss://testnet-event.ainetwork.ai', 0); // testnet

const address = ain.wallet.addAndSetDefaultAccount('YOUR_PRIVATE_KEY');

const appName = 'YOUR_APP_NAME'; // or 'consensus
const appPath = `/apps/${appName}`;
const amount = 10; // amount of tokens to stake

ain.db
  .ref(`/staking/${appName}/${address}/0/stake/${Date.now()}/value`)
  .setValue({
    value: amount,
    nonce: -1,
  })
  .then((res) => {
    console.log('tx_hash:', res.tx_hash);
    console.log('code:', res.result.code); // 0: success
  });
  1. Confirm the transaction

Method 2: Using blockchain tools

If you prefer a simpler, non-programming method, you can use the pre-built script available in our repository.

  1. Download the repository

$ git clone https://github.com/ainblockchain/ain-blockchain.git
$ cd ain-blockchain/tools/staking
  1. Set up the environment Make sure you have Node.js installed. Install any dependencies by running:

$ npm install
$ node sendStakeTx.js <BLOCKCHAIN_API_ENDPOINT> <CHAIN_ID> <APP_NAME> <STAKING_AMOUNT> <ACCOUNT_TYPE> [<KEYSTORE_FILE_PATH>]
$ node sendStakeTx.js https://testnet-api.ainetwork.ai 0 test_app 100 private_key
$ node sendStakeTx.js https://testnet-api.ainetwork.ai 0 test_app 100 mnemonic
  • Replace the placeholders with your own values:

  • <BLOCKCHAIN_API_ENDPOINT>: The endpoint of the blockchain API. (e.g. testnet: 'https://testnet-api.ainetwork.ai', mainnet: 'https://mainnet-api.ainetwork.ai')

  • <CHAIN_ID>: The ID of the chain. (0: testnet, 1: mainnet)

  • <APP_NAME>: The name of the app.

  • <STAKING_AMOUNT>: The amount of tokens to stake.

  • <ACCOUNT_TYPE>: The type of the account: private_key, mnemonic, keystore

  • <KEYSTORE_FILE_PATH>: The path to the keystore file.

  1. Confirm the transaction

How to decide the staking amount?

To determine the right staking amount for your app, consider the following key points:

  1. Understand resource needs

  • Your app requires two budgets:

    • Bandwidth Budget: Calculated as the number of the DB write operations needed to execute the transaction.

    • State Budget: Based on the memory used in the blockchain's state tree.

  • The more resources your app uses, the higher the staking amount required.

  1. Dynamic allocation

  • The amount of state budget your app receives depends on the percentage of total staking your app contributes to the systam.

  • This allocation is dynamic and adjusts as the total staking changes across all apps.

  • Run the getSateUsage method to check:

    • Your app's current state usage.

    • Available resources based on your app's staking amount. (in the free tier, you can only use 5% of the total state budget)

  • Example command: Request:

curl https://testnet-api.ainetwork.ai/json-rpc -X POST -H "Content-Type: application/json" -d '{
  "jsonrpc": "2.0",
  "id": 1,
  "method": "ain_getStateUsage",
  "params": {
    "protoVer": "1.1.3",
    "app_name": "YOUR_APP_NAME"
  }
}'

Response:

{
  "jsonrpc": "2.0",
  "id": 1,
  "result": {
    "result": {
      "usage": {
        "tree_height": 6,
        "tree_size": 11,
        "tree_bytes": 2114,
        "tree_max_siblings": 5
      },
      "available": {
        "tree_height": 30,
        "tree_bytes": 12291542508.778091,
        "tree_size": 76822140.67986308
      },
      "staking": {
        "app": 50500000,
        "total": 10168575.540000014,
        "unstakeable": 50500000
      }
    },
    "protoVer": "1.1.3"
  }
}

Interpret the response:

  • State Usage (usage)

    • tree_bytes: Your app is currently using 2114 bytes of state budget.

  • Available State (available)

    • tree_bytes: You can use up to 12291542508.778091 bytes of state budget.

  • Staking Details (staking)

    • app: Your app has staked 50500000 tokens.

    • total: The total staking amount in the system is 10168575.540000014 tokens.

  1. How to decide staking

  • If usage.tree_byte is close to available.tree_bytes, your app is nearing its limit.

  • Increase your staking amount of app to get more state budget.

  • Keep monitoring the state usage to ensure your app has enough resources.

PreviousValidatorsNextWhat is AIN DAO

Last updated 4 months ago

Was this helpful?

In our system, there are two main methods to stake your tokens: using or . Both methods are straightforward and easy to use.

Run the staking script Use the provided script to stake your tokens:

Use : Get State Usage

ain-js
blockchain tools
sendStakeTx.js
JSON RPC API