UNPKG

@gateway.fm/gtw-dvf-client-js

Version:

DVF client js lib with gateway.fm rpc endpoints

94 lines (74 loc) 2.38 kB
#!/usr/bin/env node /* DO NOT EDIT THIS FILE BY HAND! Examples are generated using helpers/buildExamples.js script. Check README.md for more details. */ const HDWalletProvider = require('@truffle/hdwallet-provider') const sw = require('starkware_crypto') const Web3 = require('web3') const DVF = require('../src/dvf') const envVars = require('./helpers/loadFromEnvOrConfig')( process.env.CONFIG_FILE_NAME ) const logExampleResult = require('./helpers/logExampleResult')(__filename) const ethPrivKey = envVars.ETH_PRIVATE_KEY // NOTE: you can also generate a new key using:` // const starkPrivKey = dvf.stark.createPrivateKey() const starkPrivKey = envVars.STARK_PRIVATE_KEY const rpcUrl = envVars.RPC_URL const provider = new HDWalletProvider(ethPrivKey, rpcUrl) const web3 = new Web3(provider) provider.engine.stop() const dvfConfig = { api: envVars.API_URL, dataApi: envVars.DATA_API_URL, useAuthHeader: true, apiKey: envVars.API_KEY // Add more variables to override default values } ;(async () => { const dvf = await DVF(web3, dvfConfig) const P = require('aigle') let order const orders = await dvf.getOrders() console.log('orders', orders) if (orders.length == 0) { console.log('submitting new order') // Submit an order to sell 0.1 ETH at a the price of 5000 USDT per ETH const symbol = 'ETH:USDT' const amount = -0.1 const price = 5000 const validFor = '0' const feeRate = '' order = await dvf.submitOrder({ symbol, amount, price, starkPrivateKey: starkPrivKey, validFor, // Optional feeRate, // Optional gid: '1', // Optional cid: 'mycid-cancel-example-' + Math.random().toString(36).substring(7), // Optional partnerId: 'P1' // Optional }) console.log('submitOrder response ->', order) while (true) { console.log('checking if order appears on the book...') if ((await dvf.getOrders()).find(o => o._id === order._id)) break await P.delay(1000) } } else { order = orders[0] } console.log('cancelling orderId', order._id) const response = await dvf.cancelOrder(order._id) // Alternative with cid : // const response = await dvf.cancelOrder({ cid: order.cid }) logExampleResult(response) })() .catch(error => { console.error(error) process.exit(1) })