getBalance

Return balance of a specific asset for the given account.

If the asset is omited from a request to MainNet, all asset and token balances will be returned.

Input Arguments

Parameter
Description

A list of Balance Request Objects, specifying which addresses, and which assets to query

network?: string

Network to submit this request to. If omitted, will default to network the wallet is currently set to.

BalanceRequest

Parameter
Description

address: string

Address to check balance(s)

assets?: string | string[]

Asset ID or script hash to check balance

fetchUTXO?: boolean

Fetches to UTXO data for NEO and/or GAS if attribute is 'true'

Success Response

Parameter
Description

This key is the actual address of the query eg. "AWSEU4BXpjGVdw9ajnFBXh8Rg8cgw9f3Zo"

BalanceResponse

Parameter
Description

assetID: string

ID of the given asset

symbol: string

Symbol of the given asset

amount: string

Double Value of the balance represented as a String

If fetch utxo's was turned on then the utxo array will be returned for the native assets NEO and GAS

UTXO

Parameter
Description

asset: string

Script hash of the native asset

createdAtBlock: int

Block number where this utxo was created

index: int

Output index of the UTXO relative to the txid in which it was created

txid: string

The transaction id of this UTXO

value: string

The double value of this UTXO represented as a String

Example

Request

neoline.getBalance({
  params: [
    {
      address: 'AUhp11NZfZEDKXWuSo5TPdGhnMx9wG2pdc',
      assets: ['GAS','MCT', '0xc56f33fc6ecfcd0c225c4ab356fee59390af8560be0e930faebe74a6daff7c9b', 'NEO']
    },{
      address: 'AWSEU4BXpjGVdw9ajnFBXh8Rg8cgw9f3Zo'
    }
  ],
  network: 'TestNet'
})
.then((results) => {
  Object.keys(results).forEach(address => {
    const balances = results[address];
    balances.forEach(balance => {
      const { assetID, symbol, amount } = balance

      console.log('Address: ' + address);
      console.log('Asset ID: ' + assetID);
      console.log('Asset symbol: ' + symbol);
      console.log('Amount: ' + amount);
    });
  });
})
.catch((error) => {
  const {type, description, data} = error;
  switch(type) {
    case 'NO_PROVIDER':
        console.log('No provider available.');
        break;
    case 'CONNECTION_DENIED':
        console.log('The user rejected the request to connect with your dApp');
        break;
    default:
        // Not an expected error object.  Just write the error to the console.
        console.error(error);
        break;
  }
});

Response

/* Single asset balance request sample */
// input
{
  params: {
    address: 'AWSEU4BXpjGVdw9ajnFBXh8Rg8cgw9f3Zo',
    assets: ['NEO']
  },
  network: 'TestNet'
}

// output
{
  AWSEU4BXpjGVdw9ajnFBXh8Rg8cgw9f3Zo: [
    {
      assetID: '0xc56f33fc6ecfcd0c225c4ab356fee59390af8560be0e930faebe74a6daff7c9b',
      symbol: 'NEO',
      amount: '6319'
    }
  ],
}

/* Single account balances request sample */
// input
{
  params: {
    address: 'AWSEU4BXpjGVdw9ajnFBXh8Rg8cgw9f3Zo',
  },
  network: 'TestNet'
}

// output
{
  AWSEU4BXpjGVdw9ajnFBXh8Rg8cgw9f3Zo: [
    {
      assetID: '0xc56f33fc6ecfcd0c225c4ab356fee59390af8560be0e930faebe74a6daff7c9b',
      symbol: 'NEO',
      amount: '6319'
    },
    {
      assetID: '0x602c79718b16e442de58778e148d0b1084e3b2dffd5de6b7b16cee7969282de7',
      symbol: 'GAS',
      amount: '2958.1094388'
    }
  ]
}

/* Multiple account balances request sample */
// input
{
  params: [
    {
      address: 'AUhp11NZfZEDKXWuSo5TPdGhnMx9wG2pdc'
    },
    {
      address: 'AWSEU4BXpjGVdw9ajnFBXh8Rg8cgw9f3Zo',
      asset: 'NEO'
    }
  ],
  network: 'TestNet'
}

// output
{
  AUhp11NZfZEDKXWuSo5TPdGhnMx9wG2pdc: [
    {
      assetID: '0x602c79718b16e442de58778e148d0b1084e3b2dffd5de6b7b16cee7969282de7',
      symbol: 'NEO',
      amount: '1'
    },
    {
      assetID: '0xc56f33fc6ecfcd0c225c4ab356fee59390af8560be0e930faebe74a6daff7c9b',
      symbol: 'GAS',
      amount: '1'
    },
  ],
  AWSEU4BXpjGVdw9ajnFBXh8Rg8cgw9f3Zo: [
    {
      assetID: '0xc56f33fc6ecfcd0c225c4ab356fee59390af8560be0e930faebe74a6daff7c9b',
      symbol: 'NEO',
      amount: '6319'
    },
    {
      assetID: '0x602c79718b16e442de58778e148d0b1084e3b2dffd5de6b7b16cee7969282de7',
      symbol: 'GAS',
      amount: '2958.1094388'
    }
  ]
}

最后更新于