Open WebSocket Connection
In order to use the eosfinex API a websocket connection needs to be established.
To connect with the eosfinex API, create a Sunbeam instance with the following options:
- Provide your EOS private key(s) to the
JsSignatureProvider
. This is needed to sign transactions. - Set the public url in
opts.urls.pub
towss://api.bitfinex.com/ws/2
- Set the private url in
opts.urls.priv
towss://api.eosfinex.com/ws/
- Set the eosfinex http endpoint in
opts.eos.httpEndpoint
tohttps://api.eosfinex.com
- Set the exchange contract in
opts.eos.exchangeContract
toeosfinexeos1
- Set your EOS account name -- of which you have provided the private key -- in
opts.eos.auth.keys.account
NOTE
Transactions signed with
owner
key won't be accepted. Please, useactive
key and set the permission in the configuration accodringly.
const Sunbeam = require('sunbeam')
const { Api, JsonRpc } = require('eosjs')
const { JsSignatureProvider } = require('eosjs/dist/eosjs-jssig')
const fetch = require('node-fetch')
const { TextDecoder, TextEncoder } = require('util')
const keys = ['EOS_PRIVATE_KEY'] // replace this with your account’s private key
const signatureProvider = new JsSignatureProvider(keys)
const httpEndpoint = 'https://api.eosfinex.com'
const rpc = new JsonRpc(httpEndpoint, { fetch })
const api = new Api({
rpc,
signatureProvider,
textDecoder: new TextDecoder(),
textEncoder: new TextEncoder()
})
const client = {
rpc,
api
}
// setup sunbeam
const opts = {
urls: {
pub: 'wss://api.bitfinex.com/ws/2',
priv: 'wss://api.eosfinex.com/ws/'
},
eos: {
httpEndpoint, // Used to get metadata for signing transactions
exchangeContract: 'eosfinexeos1',
auth: {
keys: {
account: "EOS_ACCOUNT_NAME", // replace this with your account’s name
permission: "active"
},
}
}
}
const ws = new Sunbeam(client, opts)
ws.on('open', () => {
// ready to trade!
})
ws.open()
// https://github.com/websockets/ws
const WebSocket = require('ws');
const ws = new WebSocket('wss://api.eosfinex.com/ws/');
ws.on('open', () => {
ws.send('something');
});
ws.on('message', (msg) {
console.log(msg);
});
import (
"code.google.com/p/go.net/websocket"
)
ws, err := websocket.Dial("wss://api.eosfinex.com/ws/", "", "http://localhost/")
if err != nil {
return err
}
Once the websocket connection is open, we can proceed to interact with the API:
- Retrieve orders
- Retrieve trades
- Retrieve wallets
- Create a new order
- Cancel an order
To retrieve orders, trades and wallets updates, you need to subscribe to their channels.
Updated almost 4 years ago
What’s Next