Orion Trading SDK
Attention! For the correct work of the SDK, you need to update the package to the latest version and follow updated instructions below.
Installation
npm install @orionprotocol/orion-trading-sdk
Methods with parameters per module
Module Chain
getWalletBalance(ticker)
Parameter | Type | Required | Description |
---|
ticker | string | no | empty ticker field return balance for all tokens |
@return token balance on wallet (uint)
Module OrionAggregator
createOrder({...params})
Parameter | Type | Required | Description |
---|
fromCurrency | string | yes | token symbol |
toCurrency | string | yes | token symbol |
side | string | yes | 'buy' or 'sell' |
price | number | yes | any number |
amount | number | yes | any number |
priceDeviation | number | yes | it's percents, 0 < priceDeviation < 50 |
needWithdraw | boolean | no | false by default, feature in process |
chainPrices | object | no | |
chainPrices is optional (use it if you already knew prices):
Parameter | Type | Required | Description |
---|
gasWei | string | yes | gas price in wei |
baseAsset | string/number | yes | aka 'fromCurrency' |
networkAsset | string/number | yes | |
feeAsset | string/number | yes | |
@return prepared and signed order
sendOrder(order, isCreateInternalOrder)
Parameter | Type | Required | Description |
---|
order | object | yes | Order object from createOrder() |
isCreateInternalOrder | boolean | no | Execution only in the internal order book; false by default |
@return orderId
cancelOrder(orderId)
Parameter | Type | Required | Description |
---|
orderId | string | yes | |
@return orderId of cancelled order
getOrderById(orderId, owner)
Parameter | Type | Required | Description |
---|
orderId | string | yes | |
owner | string | no | by default owner address is a current wallet address |
@return order with requested id
getTradeHistory({...options})
Parameter | Type | Required | Description |
---|
baseAsset | string | no | token address |
quoteAsset | string | no | token address |
startTime | number | no | |
endTime | number | no | |
limit | number | no | default 1000 |
@return list of orders
Module Exchange
getContractBalance(ticker)
Parameter | Type | Required | Description |
---|
ticker | string | no | empty ticker field return balance for all tokens |
@return token balance on smart contract (bignumber)
deposit(token, amount)
Parameter | Type | Required | Description |
---|
token | string | yes | |
amount | string | yes | |
@return transaction hash
withdraw(token, amount)
Parameter | Type | Required | Description |
---|
token | string | yes | |
amount | string | yes | |
@return transaction hash
Module WS
priceFeedAll()
@return subscriber for all tickers price feed
priceFeedTicker(ticker)
Parameter | Type | Required | Description |
---|
ticker | string | yes | |
@return subscriber for specific ticker price feed
orderBooks(pair)
Parameter | Type | Required | Description |
---|
pair | string | yes | |
@return subscriber for orderbooks
How to use
Important note! you should always wrap your async functions in a try-catch block, so you could handle errors in a right way.
try {
} catch(error) {
}
First step: Create base Chain instance
import { Chain, Constants } from '@orionprotocol/orion-trading-sdk'
const privateKey = 'your_private_key'
const networkParams = Constants.NETWORK.TEST.BSC
try {
const chain = new Chain(privateKey, networkParams)
await chain.init()
} catch (error) {
}
In examples below we hide try-catch blocks, because they are wrappers. But you should always use them.
Now you ready to go.
Examples
(previous steps are required)
Get wallet balance:
const walletBalance = await chain.getWalletBalance('ORN')
const walletBalanceSummary = await chain.getWalletBalance()
For further operations, network tokens are required to pay for transactions, as well as tokens for deposit / withdrawal / exchange.
Deposit token:
import { Exchange } from '@orionprotocol/orion-trading-sdk'
const exchange = new Exchange(chain)
const deposit = await exchange.deposit('ORN', '10')
Get smart contract balance:
const contractBalance = await exchange.getContractBalance('ORN')
const contractBalanceSummary = await exchange.getContractBalance()
Withdraw token:
const withdraw = await exchange.withdraw('ORN', '10')
Work with OrionAggregator:
Creating, sending, canceling orders and getting info
import { OrionAggregator } from '@orionprotocol/orion-trading-sdk'
orionAggregator = new OrionAggregator(chain)
Create, sign and send order to OrionAggregator:
const order = {
fromCurrency: 'ORN',
toCurrency: 'DAI',
feeCurrency: 'ORN',
side: 'sell',
price: 12,
amount: 10,
priceDeviation: 1,
chainPrices: {
networkAsset: 57,
baseAsset: 1,
feeAsset: 1,
gasWei: '10000000000'
}
}
const signedOrder = await orionAggregator.createOrder(order)
const sentOrderResponse = await orionAggregator.sendOrder(signedOrder)
Cancel order:
const orderCancelation = await orionAggregator.cancelOrder(sentOrderResponse.orderId)
Get orders history/status:
const history = await orionAggregator.getTradeHistory()
const order = await orionAggregator.getOrderById(sentOrderResponse.orderId)
Websockets
Create WS instance:
import { WS, Constants } from '@orionprotocol/orion-trading-sdk'
const wsUrl = Constants.ORION_WS.TEST.BSC
const ws = new WS(wsUrl)
To subscribe to the price feed:
const subscriberForAll = ws.priceFeedAll()
const subscriberForTicker = ws.priceFeedTicker('ORN-USDT')
subscriberForAll.on('message', (message) => {
});
subscriberForTicker.on('message', (message) => {
});
subscriberForAll.close()
subscriberForTicker.close()
To subscribe to the orderbooks:
const subscriberForOrderbooks = ws.orderBooks('ORN-USDT')
subscriberForOrderbooks.on('message', (message) => {
});
subscriberForOrderbooks.close()
Testing
To run the tests, follow these steps. You must have at least node v10 installed.
Clone repository
git clone https://github.com/orionprotocol/trading-sdk
Move into the trading-sdk working directory
cd trading-sdk/
Install dependencies
npm install
Run tests
npm run test
You should see output with all test passed
Resources