UNPKG

@firefly-exchange/firefly-client

Version:

The Firefly Client Library allows traders to sign, create, retrieve and listen to orders on Firefly Exchange.

62 lines 3.27 kB
"use strict"; /** * Places a market order on exchange and listens to emitted events */ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); /* eslint-disable no-console */ const index_1 = require("../../index"); function main() { return __awaiter(this, void 0, void 0, function* () { // no gas fee is required to create order signature. const dummyAccountKey = "a182091b4d5a090b65d604e36f68629a692e3bf2aa864bd3f037854034cdd676"; const client = new index_1.FireflyClient(true, index_1.Networks.TESTNET_ARBITRUM, dummyAccountKey); // passing isTermAccepted = true for compliance and authorizarion // load/init contract addresses yield client.init(); client.addMarket(index_1.MARKET_SYMBOLS.ETH); const callback = ({ orderbook }) => { console.log(orderbook); // kill sockets in order to stop script client.sockets.close(); }; const connection_callback = () => __awaiter(this, void 0, void 0, function* () { // This callback will be invoked as soon as the socket connection is established // start listening to global market and local user events client.sockets.subscribeGlobalUpdatesBySymbol(index_1.MARKET_SYMBOLS.ETH); // start listening to local user events client.sockets.subscribeUserUpdateByToken(); // triggered when orderbook updates are received client.sockets.onOrderBookUpdate(callback); }); const disconnection_callback = () => __awaiter(this, void 0, void 0, function* () { console.log("Sockets disconnected, performing actions..."); }); // must specify connection_callback before opening the sockets below yield client.sockets.listen("connect", connection_callback); yield client.sockets.listen("disconnect", disconnection_callback); console.log("Making socket connection to firefly exchange"); client.sockets.open(); /** **** Placing an Order ***** */ // default leverage of account is set to 3 on firefly const leverage = yield client.getUserDefaultLeverage(index_1.MARKET_SYMBOLS.ETH); // post a market order yield client.postOrder({ symbol: index_1.MARKET_SYMBOLS.ETH, price: 0, quantity: 0.5, side: index_1.ORDER_SIDE.BUY, orderType: index_1.ORDER_TYPE.MARKET, leverage, }); }); } main().then().catch(console.warn); //# sourceMappingURL=2-orderbook-updates.js.map