Last active
October 8, 2020 15:28
-
-
Save RiccardoBiosas/51494ed310ab20ef97855c00f980a0f9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const { Api, Utils } = require('@opiumteam/oex-js') | |
const { signMessage } = Utils | |
class Example { | |
constructor (publicKey, privateKey) { | |
this.api = new Api() | |
this.publicKey = publicKey | |
this.privateKey = privateKey | |
} | |
async login () { | |
try { | |
const loginData = await this.api.getAuthLoginData() | |
this.api.signature = signMessage({ | |
data: loginData, | |
privateKey: Buffer.from(this.privateKey, 'hex') | |
}) | |
/* | |
authenticated public key that will be used to make calls to protected endpoints | |
*/ | |
this.api.authAddress = this.publicKey | |
} catch (e) { | |
console.error(e) | |
} | |
} | |
// fetch list of tickers | |
async fetchTickers (query) { | |
try { | |
const tickers = await this.api.tickersGetAll(query) | |
return tickers | |
} catch (e) { | |
console.error(e) | |
} | |
} | |
// fetch information about a specific ticker | |
async fetchTicker (hash) { | |
try { | |
const tickersData = await this.api.tickersGetData(hash) | |
return tickersData | |
} catch (e) { | |
console.error(e) | |
} | |
} | |
// submit order | |
async postOrder (orderBody) { | |
try { | |
const orderToSign = await this.api.postOrderForm(orderBody) | |
return orderToSign | |
} catch (e) { | |
console.error(e) | |
} | |
} | |
// submit eip712 signature for submitted orders | |
async signOrder (signedOrder) { | |
try { | |
const signedOrdersId = await this.api.postOrderSign(signedOrder) | |
return signedOrdersId | |
} catch (e) { | |
console.error(e) | |
} | |
} | |
} | |
const start = async () => { | |
const myExample = new Example('your public key...', 'your private key...') | |
try { | |
await myExample.login() | |
/* | |
Specify type of tickers that you want to fetch (i.e: synthetic, future, swap) | |
*/ | |
const tickersQuery = { | |
type: 'swap' | |
} | |
const tickers = await myExample.fetchTickers(tickersQuery) | |
const tickerHash = tickers[0].hash | |
/* | |
Fetch information associated to the ticker with the given hash | |
(i.e: oracleId, token, endTime etc.) | |
*/ | |
const tickerData = await myExample.fetchTicker(tickerHash) | |
/* | |
submit order | |
*/ | |
const orderBody = { | |
action: 'BID', | |
price: 1, | |
ticker: tickerHash, | |
quantity: 1, | |
expiresAt: 0, | |
currency: tickerData[0].token | |
} | |
const orderToSign = await myExample.postOrder(orderBody) | |
/* | |
sign eip712 submitted orders' signatures | |
*/ | |
const signature = signMessage({ | |
data: orderToSign[0].orderToSign, | |
privateKey: Buffer.from('your private key...', 'hex') | |
}) | |
/* | |
sign submitted orders | |
*/ | |
const signedOrderBody = [ | |
{ | |
id: orderToSign[0].id, | |
signature: signature | |
} | |
] | |
const signedOrdersId = await myExample.signOrder(signedOrderBody) | |
} catch (e) { | |
console.error(e) | |
} | |
} | |
start() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment