Node API

Description on interact directly with node api

Alcor Exchange provides an API for the UI view reasons charts/lates deals/orderbook. etc. But any intereation with blockchain, such as new order or order cancel, we have to work with blockchain now api directly, here we will provide some examples.

We will use EosJS lib to interact with node api. And will use WAX blockchain as example.

Submit new order.

// Code not tested yet, and provided for explanation reason

import fetch from 'node-fetch'
import { Api, JsonRpc, RpcError, JsSignatureProvider } from 'eosjs'

const rpc = new JsonRpc('https://wax.greymass.com', { fetch })
const signatureProvider = new JsSignatureProvider(['private key of order owner'])
const api = new Api({ rpc, signatureProvider, textDecoder: new TextDecoder(), textEncoder: new TextEncoder() });

// Place buy limit order, buying 1000 TLM for 10 WAX
const objTrans = [{
  account: 'eosio', // token contract
  name: 'transfer',
  authorization: [{
    actor: 'useraaaaaaaa', // account placing order (owner)
    permission: 'active',
  }],
  data: {
    from: 'useraaaaaaaa',
    to: 'alcordexmain',
    quantity: `10.0000 WAX`,
    memo: `1000.0000 TLM@alien.worlds`
  }
}]

// Result of transaction
const r = await api.transact(objTrans, { blocksBehind: 3, expireSeconds: 30})

Get order from contract table.

// Code not tested yet, and provided for explanation reason

import fetch from 'node-fetch'
import { Api, JsonRpc, RpcError } from 'eosjs'

const rpc = new JsonRpc('https://wax.greymass.com', { fetch })

// Get buy orderbook from conract table

const { rows } = await rpc.get_table_rows({
  code: 'alcordexmain',
  table: 'buyorder',
  limit: 1000,
  scope: 29, // Market id from /api/markets
  key_type: 'i128', // we are using it for getting order sorted by price.
  index_position: 2
})

Last updated