-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from consenlabs/feat-support-multiple-provider-url
Feat support multiple provider url
- Loading branch information
Showing
7 changed files
with
46 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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', | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
}) | ||
}) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') | ||
} |