From 35917d27b93fbbde98f26cef96c71a0f58c76255 Mon Sep 17 00:00:00 2001 From: HAOYUatHZ <37070449+HAOYUatHZ@users.noreply.github.com> Date: Wed, 15 Sep 2021 21:47:07 +0800 Subject: [PATCH] style: format js codes (#332) --- examples/js/RESTClient.ts | 6 ++--- examples/js/bots/mm_external_price_bot.ts | 18 +++++++++------ examples/js/bots/run_bots.ts | 10 ++++---- examples/js/bots/utils.ts | 14 ++++++------ examples/js/client.ts | 28 +++++++++++------------ examples/js/kafka_client.ts | 6 ++--- examples/js/multi_market.ts | 8 +++---- examples/js/print_orders.ts | 2 +- examples/js/put_batch_orders.ts | 14 ++++++------ examples/js/set_markets.ts | 14 ++++++------ examples/js/stress.ts | 2 +- examples/js/tick.ts | 12 +++++----- examples/js/trade.ts | 6 ++--- examples/js/transfer.ts | 2 +- examples/js/util.ts | 6 ++--- 15 files changed, 76 insertions(+), 72 deletions(-) diff --git a/examples/js/RESTClient.ts b/examples/js/RESTClient.ts index c0f46198..28cbf396 100644 --- a/examples/js/RESTClient.ts +++ b/examples/js/RESTClient.ts @@ -16,7 +16,7 @@ class RESTClient { console.log("using REST API server: ", server); this.client = axios.create({ baseURL: server, - timeout: 1000 + timeout: 1000, }); } @@ -27,7 +27,7 @@ class RESTClient { console.log("error:", resp.data); return null; } - let userInfo = (resp.data as unknown) as UserInfo; + let userInfo = resp.data as unknown as UserInfo; //console.log('raw', resp.data, 'result', userInfo); return userInfo; } @@ -44,7 +44,7 @@ class RESTClient { } ) { let resp = await this.client.get(`/internal_txs/${user_id}`, { - params: _.pickBy(params, _.identity) + params: _.pickBy(params, _.identity), }); if (resp.status === 200) { return resp.data; diff --git a/examples/js/bots/mm_external_price_bot.ts b/examples/js/bots/mm_external_price_bot.ts index f2c34f53..7875052e 100644 --- a/examples/js/bots/mm_external_price_bot.ts +++ b/examples/js/bots/mm_external_price_bot.ts @@ -4,7 +4,7 @@ import { ORDER_SIDE_BID, ORDER_SIDE_ASK, ORDER_TYPE_LIMIT, - VERBOSE + VERBOSE, } from "../config"; class PriceBotParams {} class MMByPriceBot { @@ -41,8 +41,12 @@ class MMByPriceBot { // run every second async tick(balance, oldOrders): Promise<{ reset; orders }> { const VERBOSE = this.verbose; - const oldAskOrder = oldOrders.orders.find(elem => elem.order_side == "ASK"); - const oldBidOrder = oldOrders.orders.find(elem => elem.order_side == "BID"); + const oldAskOrder = oldOrders.orders.find( + (elem) => elem.order_side == "ASK" + ); + const oldBidOrder = oldOrders.orders.find( + (elem) => elem.order_side == "BID" + ); // put a big buy order and a big sell order //const price = await getPriceOfCoin(baseCoin, 5); @@ -99,7 +103,7 @@ class MMByPriceBot { } return { reset: false, - orders: [] + orders: [], }; } //lastAskPrice = askPrice; @@ -112,7 +116,7 @@ class MMByPriceBot { order_side: ORDER_SIDE_BID, order_type: ORDER_TYPE_LIMIT, amount: bidAmount, - price: bidPrice + price: bidPrice, }; const ask_order = { user_id: this.user_id, @@ -120,11 +124,11 @@ class MMByPriceBot { order_side: ORDER_SIDE_ASK, order_type: ORDER_TYPE_LIMIT, amount: askAmount, - price: askPrice + price: askPrice, }; return { reset: true, - orders: [bid_order, ask_order] + orders: [bid_order, ask_order], }; } handleTrade(trade) {} diff --git a/examples/js/bots/run_bots.ts b/examples/js/bots/run_bots.ts index 547e745e..fa90e926 100644 --- a/examples/js/bots/run_bots.ts +++ b/examples/js/bots/run_bots.ts @@ -5,14 +5,14 @@ import { defaultRESTClient, RESTClient } from "../RESTClient"; import { defaultClient as defaultGrpcClient, Client as grpcClient, - defaultClient + defaultClient, } from "../client"; import { sleep, depositAssets, getPriceOfCoin } from "../util"; import { ORDER_SIDE_BID, ORDER_SIDE_ASK, ORDER_TYPE_LIMIT, - VERBOSE + VERBOSE, } from "../config"; import { estimateMarketOrderSell, @@ -20,7 +20,7 @@ import { execMarketOrderAsLimit_Sell, execMarketOrderAsLimit_Buy, rebalance, - printBalance + printBalance, } from "./utils"; import { executeOrders } from "./executor"; async function initUser(): Promise { @@ -40,7 +40,7 @@ async function initUser(): Promise { let resp = await defaultGrpcClient.registerUser({ user_id: 0, // discard in server side l1_address: acc.ethAddr, - l2_pubkey: acc.bjjPubKey + l2_pubkey: acc.bjjPubKey, }); const t = Date.now(); console.log("register resp", resp); @@ -79,7 +79,7 @@ async function main() { null, VERBOSE ); - bot.priceFn = async function(coin: string) { + bot.priceFn = async function (coin: string) { return await getPriceOfCoin(coin, 5, "coinstats"); }; let count = 0; diff --git a/examples/js/bots/utils.ts b/examples/js/bots/utils.ts index 8e2b6c8b..00d993b9 100644 --- a/examples/js/bots/utils.ts +++ b/examples/js/bots/utils.ts @@ -3,14 +3,14 @@ import { defaultRESTClient, RESTClient } from "../RESTClient"; import { defaultClient as defaultGrpcClient, Client as grpcClient, - defaultClient + defaultClient, } from "../client"; import { sleep, depositAssets, getPriceOfCoin } from "../util"; import { ORDER_SIDE_BID, ORDER_SIDE_ASK, ORDER_TYPE_LIMIT, - VERBOSE + VERBOSE, } from "../config"; // TODO: add a similar function using quoteAmount. "i want to sell some eth to get 5000 usdt" @@ -42,7 +42,7 @@ async function estimateMarketOrderSell( quote: quoteAcc, avgPrice: quoteAcc / baseAcc, bestPrice, - worstPrice + worstPrice, }; //console.log("estimateMarketOrderSell:", estimateResult); return estimateResult; @@ -77,7 +77,7 @@ async function estimateMarketOrderBuy( quote: quoteAcc, avgPrice: quoteAcc / tradeAmount, bestPrice, - worstPrice + worstPrice, }; //console.log("estimateMarketOrderBuy:", estimateResult); return estimateResult; @@ -182,13 +182,13 @@ async function printBalance(user_id, baseCoin, quoteCoin, market) { console.log("------- BALANCE1:", { quote: allQuote, base: res.quote, - total: allQuote + res.quote + total: allQuote + res.quote, }); console.log("------- BALANCE2:", { quote: allQuote, base: allBase * externalPrice, total: allQuote + allBase * externalPrice, - totalInB: allQuote / externalPrice + allBase + totalInB: allQuote / externalPrice + allBase, }); } @@ -198,5 +198,5 @@ export { execMarketOrderAsLimit_Sell, execMarketOrderAsLimit_Buy, rebalance, - printBalance + printBalance, }; diff --git a/examples/js/client.ts b/examples/js/client.ts index 1d22564c..3488979c 100644 --- a/examples/js/client.ts +++ b/examples/js/client.ts @@ -5,7 +5,7 @@ import { ORDER_SIDE_BID, ORDER_SIDE_ASK, ORDER_TYPE_LIMIT, - VERBOSE + VERBOSE, } from "./config"; const file = "../../orchestra/proto/exchange/matchengine.proto"; @@ -14,7 +14,7 @@ const load = { longs: String, enums: String, defaults: true, - oneofs: true + oneofs: true, }; function fullPrec(d, p): Decimal { @@ -57,7 +57,7 @@ class Client { const allBalances = ( await this.client.BalanceQuery({ user_id: user_id, assets: [asset] }) ).balances; - const balance = allBalances.find(item => item.asset_id == asset); + const balance = allBalances.find((item) => item.asset_id == asset); let available = new Decimal(balance.available); let frozen = new Decimal(balance.frozen); let total = available.add(frozen); @@ -76,7 +76,7 @@ class Client { business_id, delta, detail: JSON.stringify(detail), - signature: "" + signature: "", }); } roundOrderInput(market, amount, price) { @@ -130,7 +130,7 @@ class Client { tokenSell, tokenBuy, totalSell, - totalBuy + totalBuy, }); signature = this.accounts.get(user_id).signHashPacked(orderInput.hash()); } @@ -143,7 +143,7 @@ class Client { price: priceRounded, taker_fee, maker_fee, - signature + signature, }; } async orderPut( @@ -183,7 +183,7 @@ class Client { amount, price, taker_fee, - maker_fee + maker_fee, } = o; order_reqs.push( await this.createOrder( @@ -201,7 +201,7 @@ class Client { return await this.client.batchOrderPut({ market, reset, - orders: order_reqs + orders: order_reqs, }); } @@ -233,7 +233,7 @@ class Client { } let resp = (await this.client.MarketSummary({ markets })).market_summaries; if (typeof req === "string") { - return resp.find(item => item.name === req); + return resp.find((item) => item.name === req); } return resp; } @@ -265,7 +265,7 @@ class Client { amount: delta, from, from_nonce: nonce, - to + to, }); signature = this.accounts.get(user_id).signHashPacked(tx.hash()); } @@ -275,7 +275,7 @@ class Client { asset, delta, memo, - signature + signature, }; } @@ -287,7 +287,7 @@ class Client { token_id: this.assets.get(asset).inner_id, amount: delta, nonce: 0, - old_balance: 0 // TODO: Update `old_balance` with precision. + old_balance: 0, // TODO: Update `old_balance` with precision. }); signature = this.accounts.get(account_id).signHashPacked(tx.hash()); } @@ -298,7 +298,7 @@ class Client { business_id, delta: -delta, detail: JSON.stringify(detail), - signature: signature + signature: signature, }; } @@ -340,7 +340,7 @@ class Client { return await this.client.RegisterUser({ user_id: user.id || user.user_id, // legacy reasons l1_address: user.l1_address, - l2_pubkey: user.l2_pubkey + l2_pubkey: user.l2_pubkey, }); } } diff --git a/examples/js/kafka_client.ts b/examples/js/kafka_client.ts index 813a4e55..43a6b44b 100644 --- a/examples/js/kafka_client.ts +++ b/examples/js/kafka_client.ts @@ -12,7 +12,7 @@ export class KafkaConsumer { const brokers = process.env.KAFKA_BROKERS; const kafka = new Kafka.Kafka({ brokers: (brokers || "127.0.0.1:9092").split(","), - logLevel: Kafka.logLevel.WARN + logLevel: Kafka.logLevel.WARN, }); const consumer = kafka.consumer({ groupId: "test-group" }); this.consumer = consumer; @@ -31,11 +31,11 @@ export class KafkaConsumer { partition, offset: message.offset, key: message.key.toString(), - value: message.value.toString() + value: message.value.toString(), }); } this.messages.get(topic).push(message.value.toString()); - } + }, }); } Reset() { diff --git a/examples/js/multi_market.ts b/examples/js/multi_market.ts index b8794d4d..14a5b95d 100644 --- a/examples/js/multi_market.ts +++ b/examples/js/multi_market.ts @@ -19,7 +19,7 @@ async function initAccounts() { await client.client.RegisterUser({ userId, l1_address: acc.ethAddr, - l2_pubkey: acc.bjjPubKey + l2_pubkey: acc.bjjPubKey, }); } @@ -32,10 +32,10 @@ async function orderTest() { "ETH_USDT", "LINK_USDT", "MATIC_USDT", - "UNI_USDT" + "UNI_USDT", ]); let orders = await Promise.all( - markets.map(market => + markets.map((market) => client .orderPut( userId, @@ -47,7 +47,7 @@ async function orderTest() { fee, fee ) - .then(o => [market, o.id]) + .then((o) => [market, o.id]) ) ); console.log(orders); diff --git a/examples/js/print_orders.ts b/examples/js/print_orders.ts index c665e20d..c1509d8c 100644 --- a/examples/js/print_orders.ts +++ b/examples/js/print_orders.ts @@ -32,7 +32,7 @@ async function main() { assert.equal(ticker.quote_volume, 4.4); } } -main().catch(function(e) { +main().catch(function (e) { console.log(e); process.exit(1); //throw e; diff --git a/examples/js/put_batch_orders.ts b/examples/js/put_batch_orders.ts index 36e04fe4..3d341e90 100644 --- a/examples/js/put_batch_orders.ts +++ b/examples/js/put_batch_orders.ts @@ -27,7 +27,7 @@ async function registerAccounts() { await client.client.RegisterUser({ user_id, l1_address: acc.ethAddr, - l2_pubkey: acc.bjjPubKey + l2_pubkey: acc.bjjPubKey, }); } } @@ -67,7 +67,7 @@ async function putOrdersTest() { amount: "1", price: "1", taker_fee: fee, - maker_fee: fee + maker_fee: fee, }, { user_id: botsIds[1], @@ -77,8 +77,8 @@ async function putOrdersTest() { amount: "1", price: "1", taker_fee: fee, - maker_fee: fee - } + maker_fee: fee, + }, ]); const newOrderNum1 = await openOrderNum(userId1); @@ -110,7 +110,7 @@ async function putAndResetOrdersTest() { amount: "1", price: "1", taker_fee: fee, - maker_fee: fee + maker_fee: fee, }, { user_id: botsIds[1], @@ -120,8 +120,8 @@ async function putAndResetOrdersTest() { amount: "1", price: "1", taker_fee: fee, - maker_fee: fee - } + maker_fee: fee, + }, ]); const newOrderNum1 = await openOrderNum(userId1); diff --git a/examples/js/set_markets.ts b/examples/js/set_markets.ts index 4267e6e7..a566b018 100644 --- a/examples/js/set_markets.ts +++ b/examples/js/set_markets.ts @@ -6,7 +6,7 @@ const isCI = !!process.env.GITHUB_ACTIONS; const new_asset = { assets: [{ name: "BTC", prec_save: 4, prec_show: 4 }], - not_reload: true + not_reload: true, }; const new_market1 = { @@ -15,13 +15,13 @@ const new_market1 = { base: { name: "BTC", prec: 2 }, quote: { name: "USDC", prec: 4 }, fee_prec: 2, - min_amount: 0.01 + min_amount: 0.01, }, asset_quote: { name: "USDC", prec_save: 6, - prec_show: 6 - } + prec_show: 6, + }, }; const new_market2 = { @@ -30,8 +30,8 @@ const new_market2 = { base: { name: "BTC", prec: 2 }, quote: { name: "USDT", prec: 4 }, fee_prec: 2, - min_amount: 0.01 - } + min_amount: 0.01, + }, }; async function main() { const server = process.env.API_ENDPOINT || "0.0.0.0:8765"; @@ -79,7 +79,7 @@ async function main() { assert.equal(markets.length, 3); } } -main().catch(function(e) { +main().catch(function (e) { console.log(e); process.exit(1); //throw e; diff --git a/examples/js/stress.ts b/examples/js/stress.ts index c5daaf0d..9b24c2b7 100644 --- a/examples/js/stress.ts +++ b/examples/js/stress.ts @@ -7,7 +7,7 @@ import { putRandOrder, sleep, decimalAdd, - decimalEqual + decimalEqual, } from "./util"; async function stressTest({ parallel, interval, repeat }) { diff --git a/examples/js/tick.ts b/examples/js/tick.ts index 44aa0a63..7e9f324c 100644 --- a/examples/js/tick.ts +++ b/examples/js/tick.ts @@ -7,7 +7,7 @@ import { getRandomFloatAroundNormal, getRandomElem, depositAssets, - getPriceOfCoin + getPriceOfCoin, } from "./util"; import { Account } from "fluidex.js"; import { getTestAccount } from "./accounts"; @@ -40,7 +40,7 @@ async function registerAccounts() { await client.registerUser({ user_id, l1_address: acc.ethAddr, - l2_pubkey: acc.bjjPubKey + l2_pubkey: acc.bjjPubKey, }); } } @@ -111,19 +111,19 @@ async function withdrawTest() { console.log("withdrawTest BEGIN"); await client.withdraw(botsIds[0], "USDT", "withdraw", businessId(), 100, { - key0: "value0" + key0: "value0", }); await client.withdraw(botsIds[1], "USDT", "withdraw", businessId(), 100, { - key1: "value1" + key1: "value1", }); await client.withdraw(botsIds[2], "USDT", "withdraw", businessId(), 100, { - key2: "value2" + key2: "value2", }); await client.withdraw(botsIds[3], "USDT", "withdraw", businessId(), 100, { - key3: "value3" + key3: "value3", }); console.log("withdrawTest END"); diff --git a/examples/js/trade.ts b/examples/js/trade.ts index fc26a15d..37874579 100644 --- a/examples/js/trade.ts +++ b/examples/js/trade.ts @@ -7,7 +7,7 @@ import { ORDER_SIDE_BID, ORDER_SIDE_ASK, ORDER_TYPE_MARKET, - ORDER_TYPE_LIMIT + ORDER_TYPE_LIMIT, } from "./config"; // dotenv import { getTestAccount } from "./accounts"; import { defaultClient as client } from "./client"; @@ -36,7 +36,7 @@ async function initAccounts() { await client.client.RegisterUser({ user_id, l1_address: acc.ethAddr, - l2_pubkey: acc.bjjPubKey + l2_pubkey: acc.bjjPubKey, }); } } @@ -93,7 +93,7 @@ async function orderTest() { const depth = await client.orderDepth(market, 100, /*not merge*/ "0"); assert.deepEqual(depth, { asks: [], - bids: [{ price: "1.10", amount: "10.0000" }] + bids: [{ price: "1.10", amount: "10.0000" }], }); await client.orderCancel(askUser, market, 1); diff --git a/examples/js/transfer.ts b/examples/js/transfer.ts index 9864329f..4258c833 100644 --- a/examples/js/transfer.ts +++ b/examples/js/transfer.ts @@ -21,7 +21,7 @@ async function registerUsers() { await client.registerUser({ id: i, l1_address: "l1_address_" + i, - l2_pubkey: "l2_pubkey_" + i + l2_pubkey: "l2_pubkey_" + i, }); console.log("register user", i); } diff --git a/examples/js/util.ts b/examples/js/util.ts index 0388444e..229f56ec 100644 --- a/examples/js/util.ts +++ b/examples/js/util.ts @@ -5,7 +5,7 @@ import { ORDER_SIDE_ASK, ORDER_TYPE_MARKET, ORDER_TYPE_LIMIT, - VERBOSE + VERBOSE, } from "./config"; // dotenv import { defaultClient as client } from "./client"; @@ -48,7 +48,7 @@ export async function depositAssets(assets: object, userId: number) { for (const [asset, amount] of Object.entries(assets)) { console.log("deposit", amount, asset); await client.balanceUpdate(userId, asset, "deposit", depositId(), amount, { - key: "value" + key: "value", }); } } @@ -98,7 +98,7 @@ export async function putRandOrder(userId, market) { } export function sleep(ms) { - return new Promise(resolve => setTimeout(resolve, ms)); + return new Promise((resolve) => setTimeout(resolve, ms)); } let pricesCache = new Map();