Skip to content

Commit

Permalink
Merge pull request #6 from consenlabs/feat-support-multiple-provider-url
Browse files Browse the repository at this point in the history
Feat support multiple provider url
  • Loading branch information
Xaber20110202 authored Aug 15, 2019
2 parents 3db0420 + 1400e92 commit 648c192
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 27 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
2 changes: 1 addition & 1 deletion src/router/version.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export const version = (ctx) => {
ctx.body = {
result: true,
version: '0.2.8',
version: '0.3.0',
}
}
2 changes: 1 addition & 1 deletion src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
28 changes: 12 additions & 16 deletions src/utils/ethereum.ts
Original file line number Diff line number Diff line change
@@ -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<BigNumber> => {
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))
})
})
})
}
2 changes: 1 addition & 1 deletion src/utils/stomp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ export default class StompForExchange {
this.connected = true
this.resetTriedTimes()
},
async (error) => {
(error) => {
tracker.captureEvent({
message: 'disconnect',
level: Sentry.Severity.Warning,
Expand Down
14 changes: 11 additions & 3 deletions src/utils/tracker.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
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' })
}

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 })
}
Expand All @@ -16,23 +18,29 @@ 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)
}
}

export const captureException = (error: Error) => {
if (enabled) {
Sentry.captureException(error)
} else {
}

if (!enabled || (enabled && isProd)) {
console.log(error)
}
}

export const captureEvent = (options: Sentry.SentryEvent) => {
if (enabled) {
Sentry.captureEvent(options)
} else {
}

if (!enabled || (enabled && isProd)) {
console.log(options)
}
}
Expand Down
23 changes: 19 additions & 4 deletions src/utils/web3.ts
Original file line number Diff line number Diff line change
@@ -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<BigNumber>

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')
}

0 comments on commit 648c192

Please sign in to comment.