From 5827449919f03c8a83bc4b3d5d1252c67de5dd99 Mon Sep 17 00:00:00 2001 From: Xaber Date: Tue, 6 Aug 2019 10:56:19 +0800 Subject: [PATCH 1/3] optimize logs --- src/utils/stomp.ts | 2 +- src/utils/tracker.ts | 14 +++++++++++--- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/src/utils/stomp.ts b/src/utils/stomp.ts index af5a390..5d576bb 100644 --- a/src/utils/stomp.ts +++ b/src/utils/stomp.ts @@ -58,7 +58,7 @@ export default class StompForExchange { this.connected = true this.resetTriedTimes() }, - async (error) => { + (error) => { tracker.captureEvent({ message: 'disconnect', level: Sentry.Severity.Warning, diff --git a/src/utils/tracker.ts b/src/utils/tracker.ts index bbbf906..99fcec4 100644 --- a/src/utils/tracker.ts +++ b/src/utils/tracker.ts @@ -1,6 +1,7 @@ import * as Sentry from '@sentry/node' let enabled = false +let isProd = false export const initSentry = ({ SENTRY_DSN, NODE_ENV }) => { Sentry.init({ dsn: SENTRY_DSN, environment: NODE_ENV ? NODE_ENV.toLowerCase() : 'development' }) @@ -8,6 +9,7 @@ export const initSentry = ({ SENTRY_DSN, NODE_ENV }) => { export const init = ({ SENTRY_DSN, NODE_ENV }) => { enabled = SENTRY_DSN && SENTRY_DSN.indexOf('https') !== -1 && NODE_ENV && ['DEVELOPMENT', 'STAGING', 'PRODUCTION'].includes(NODE_ENV) + isProd = 'PRODUCTION' === NODE_ENV if (enabled) { initSentry({ SENTRY_DSN, NODE_ENV }) } @@ -16,7 +18,9 @@ export const init = ({ SENTRY_DSN, NODE_ENV }) => { export const captureMessage = (message: string, level?: Sentry.Severity) => { if (enabled) { Sentry.captureMessage(message, level) - } else { + } + + if (!enabled || (enabled && isProd)) { console.log(message, level) } } @@ -24,7 +28,9 @@ export const captureMessage = (message: string, level?: Sentry.Severity) => { export const captureException = (error: Error) => { if (enabled) { Sentry.captureException(error) - } else { + } + + if (!enabled || (enabled && isProd)) { console.log(error) } } @@ -32,7 +38,9 @@ export const captureException = (error: Error) => { export const captureEvent = (options: Sentry.SentryEvent) => { if (enabled) { Sentry.captureEvent(options) - } else { + } + + if (!enabled || (enabled && isProd)) { console.log(options) } } From 3aeebe0dfd31d1f32eed0ede331b0c2dd469872c Mon Sep 17 00:00:00 2001 From: Xaber Date: Thu, 15 Aug 2019 11:03:48 +0800 Subject: [PATCH 2/3] feat: support multiple provider url --- src/types/index.ts | 2 +- src/utils/ethereum.ts | 28 ++++++++++++---------------- src/utils/web3.ts | 23 +++++++++++++++++++---- 3 files changed, 32 insertions(+), 21 deletions(-) diff --git a/src/types/index.ts b/src/types/index.ts index 78f0469..83356c4 100644 --- a/src/types/index.ts +++ b/src/types/index.ts @@ -21,7 +21,7 @@ export interface Wallet { export interface ConfigForStart { EXCHANGE_URL: string WEBSOCKET_URL: string - PROVIDER_URL: string + PROVIDER_URL: string | string[] WALLET_ADDRESS: string USE_KEYSTORE?: boolean diff --git a/src/utils/ethereum.ts b/src/utils/ethereum.ts index 921006f..3da8a85 100644 --- a/src/utils/ethereum.ts +++ b/src/utils/ethereum.ts @@ -1,25 +1,21 @@ import { BigNumber } from '0x.js' import { toBN } from './math' -import { getweb3 } from './web3' +import { web3RequestWrap } from './web3' import { addressWithout0x } from './address' import { leftPadWith0 } from './helper' -export const getEthBalance = (address) => { - const web3 = getweb3() - return web3.eth.getBalance(address).then(toBN) -} - export const getTokenBalance = ({ address, contractAddress }): Promise => { - const web3 = getweb3() - return new Promise((resolve, reject) => { - web3.eth.call({ - to: contractAddress, - data: `0x70a08231${leftPadWith0(addressWithout0x(address), 64)}`, - }, (err, res) => { - if (err) { - return reject(err) - } - resolve(toBN(res === '0x' ? 0 : res)) + return web3RequestWrap((web3) => { + return new Promise((resolve, reject) => { + web3.eth.call({ + to: contractAddress, + data: `0x70a08231${leftPadWith0(addressWithout0x(address), 64)}`, + }, (err, res) => { + if (err) { + return reject(err) + } + resolve(toBN(res === '0x' ? 0 : res)) + }) }) }) } \ No newline at end of file diff --git a/src/utils/web3.ts b/src/utils/web3.ts index 91c1701..ea91a2f 100644 --- a/src/utils/web3.ts +++ b/src/utils/web3.ts @@ -1,13 +1,28 @@ import * as Web3Export from 'web3' +import * as _ from 'lodash' +import { BigNumber } from '0x.js' import { config } from '../config' const Web3 = Web3Export.default ? Web3Export.default : Web3Export let web3 = null -export const getweb3 = () => { - if (!web3) { - web3 = new Web3(new Web3.providers.HttpProvider(config.PROVIDER_URL)) +type Handler = (web3: any) => Promise + +export const web3RequestWrap = async (handler: Handler) => { + const urls = _.isArray(config.PROVIDER_URL) ? config.PROVIDER_URL : [config.PROVIDER_URL] + let error = null + + for (let url of urls) { + try { + if (!web3 || error) { + web3 = new Web3(new Web3.providers.HttpProvider(url)) + } + return await handler(web3) + } catch (e) { + error = e + } } - return web3 + + throw error ? error : new Error('unknown web3 error') } \ No newline at end of file From 1400e92394f0fa62ae8f635cd30ffc023d3cfaa2 Mon Sep 17 00:00:00 2001 From: Xaber Date: Thu, 15 Aug 2019 11:12:45 +0800 Subject: [PATCH 3/3] feat: update version to 0.3.0 --- package.json | 2 +- src/router/version.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index 21aa4cd..2b378f9 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "tokenlon-mmsk", - "version": "0.2.8", + "version": "0.3.0", "description": "", "main": "lib/index.js", "types": "src/globals.d.ts", diff --git a/src/router/version.ts b/src/router/version.ts index c3fbe9c..bb9e923 100644 --- a/src/router/version.ts +++ b/src/router/version.ts @@ -1,6 +1,6 @@ export const version = (ctx) => { ctx.body = { result: true, - version: '0.2.8', + version: '0.3.0', } } \ No newline at end of file