Use our promisify

This commit is contained in:
Leonid Logvinov 2017-12-01 23:12:19 -06:00
parent 5673b42ec4
commit 78fb8d2bdc
No known key found for this signature in database
GPG Key ID: 0DD294BFDE8C95D4
4 changed files with 27 additions and 18 deletions

View File

@ -1,10 +1,9 @@
import BigNumber from 'bignumber.js'; import BigNumber from 'bignumber.js';
import promisify = require('es6-promisify');
import * as _ from 'lodash'; import * as _ from 'lodash';
import * as Web3 from 'web3'; import * as Web3 from 'web3';
import {Contract} from './contract';
import {Artifact, ArtifactContractName, TransactionReceipt, TxData, ZeroExError} from './types'; import {Artifact, ArtifactContractName, TransactionReceipt, TxData, ZeroExError} from './types';
import {promisify} from './utils/promisify';
interface RawLogEntry { interface RawLogEntry {
logIndex: string|null; logIndex: string|null;
@ -59,11 +58,11 @@ export class Web3Wrapper {
return _.includes(addresses, senderAddress); return _.includes(addresses, senderAddress);
} }
public async getNodeVersionAsync(): Promise<string> { public async getNodeVersionAsync(): Promise<string> {
const nodeVersion = await promisify(this.web3.version.getNode)(); const nodeVersion = await promisify<string>(this.web3.version.getNode)();
return nodeVersion; return nodeVersion;
} }
public async getTransactionReceiptAsync(txHash: string): Promise<TransactionReceipt> { public async getTransactionReceiptAsync(txHash: string): Promise<TransactionReceipt> {
const transactionReceipt = await promisify(this.web3.eth.getTransactionReceipt)(txHash); const transactionReceipt = await promisify<TransactionReceipt>(this.web3.eth.getTransactionReceipt)(txHash);
if (!_.isNull(transactionReceipt)) { if (!_.isNull(transactionReceipt)) {
transactionReceipt.status = this.normalizeTxReceiptStatus(transactionReceipt.status); transactionReceipt.status = this.normalizeTxReceiptStatus(transactionReceipt.status);
} }
@ -102,26 +101,26 @@ export class Web3Wrapper {
return balanceWei; return balanceWei;
} }
public async getBalanceInWeiAsync(owner: string): Promise<BigNumber> { public async getBalanceInWeiAsync(owner: string): Promise<BigNumber> {
let balanceInWei = await promisify(this.web3.eth.getBalance)(owner); let balanceInWei = await promisify<BigNumber>(this.web3.eth.getBalance)(owner);
balanceInWei = new BigNumber(balanceInWei); balanceInWei = new BigNumber(balanceInWei);
return balanceInWei; return balanceInWei;
} }
public async doesContractExistAtAddressAsync(address: string): Promise<boolean> { public async doesContractExistAtAddressAsync(address: string): Promise<boolean> {
const code = await promisify(this.web3.eth.getCode)(address); const code = await promisify<string>(this.web3.eth.getCode)(address);
// Regex matches 0x0, 0x00, 0x in order to accommodate poorly implemented clients // Regex matches 0x0, 0x00, 0x in order to accommodate poorly implemented clients
const codeIsEmpty = /^0x0{0,40}$/i.test(code); const codeIsEmpty = /^0x0{0,40}$/i.test(code);
return !codeIsEmpty; return !codeIsEmpty;
} }
public async signTransactionAsync(address: string, message: string): Promise<string> { public async signTransactionAsync(address: string, message: string): Promise<string> {
const signData = await promisify(this.web3.eth.sign)(address, message); const signData = await promisify<string>(this.web3.eth.sign)(address, message);
return signData; return signData;
} }
public async getBlockNumberAsync(): Promise<number> { public async getBlockNumberAsync(): Promise<number> {
const blockNumber = await promisify(this.web3.eth.getBlockNumber)(); const blockNumber = await promisify<number>(this.web3.eth.getBlockNumber)();
return blockNumber; return blockNumber;
} }
public async getBlockAsync(blockParam: string|Web3.BlockParam): Promise<Web3.BlockWithoutTransactionData> { public async getBlockAsync(blockParam: string|Web3.BlockParam): Promise<Web3.BlockWithoutTransactionData> {
const block = await promisify(this.web3.eth.getBlock)(blockParam); const block = await promisify<Web3.BlockWithoutTransactionData>(this.web3.eth.getBlock)(blockParam);
return block; return block;
} }
public async getBlockTimestampAsync(blockParam: string|Web3.BlockParam): Promise<number> { public async getBlockTimestampAsync(blockParam: string|Web3.BlockParam): Promise<number> {
@ -129,7 +128,7 @@ export class Web3Wrapper {
return timestamp; return timestamp;
} }
public async getAvailableAddressesAsync(): Promise<string[]> { public async getAvailableAddressesAsync(): Promise<string[]> {
const addresses: string[] = await promisify(this.web3.eth.getAccounts)(); const addresses = await promisify<string[]>(this.web3.eth.getAccounts)();
return addresses; return addresses;
} }
public async getLogsAsync(filter: Web3.FilterObject): Promise<Web3.LogEntry[]> { public async getLogsAsync(filter: Web3.FilterObject): Promise<Web3.LogEntry[]> {
@ -161,12 +160,12 @@ export class Web3Wrapper {
return web3ContractInstance; return web3ContractInstance;
} }
private async getNetworkAsync(): Promise<number> { private async getNetworkAsync(): Promise<number> {
const networkId = await promisify(this.web3.version.getNetwork)(); const networkId = await promisify<number>(this.web3.version.getNetwork)();
return networkId; return networkId;
} }
private async sendRawPayloadAsync<A>(payload: Web3.JSONRPCRequestPayload): Promise<A> { private async sendRawPayloadAsync<A>(payload: Web3.JSONRPCRequestPayload): Promise<A> {
const sendAsync = this.web3.currentProvider.sendAsync.bind(this.web3.currentProvider); const sendAsync = this.web3.currentProvider.sendAsync.bind(this.web3.currentProvider);
const response = await promisify(sendAsync)(payload); const response = await promisify<Web3.JSONRPCResponsePayload>(sendAsync)(payload);
const result = response.result; const result = response.result;
return result; return result;
} }

View File

@ -1,6 +1,5 @@
import BigNumber from 'bignumber.js'; import BigNumber from 'bignumber.js';
import * as chai from 'chai'; import * as chai from 'chai';
import promisify = require('es6-promisify');
import * as _ from 'lodash'; import * as _ from 'lodash';
import 'mocha'; import 'mocha';
import * as Sinon from 'sinon'; import * as Sinon from 'sinon';

View File

@ -1,6 +1,5 @@
import BigNumber from 'bignumber.js'; import BigNumber from 'bignumber.js';
import * as chai from 'chai'; import * as chai from 'chai';
import promisify = require('es6-promisify');
import 'mocha'; import 'mocha';
import * as Web3 from 'web3'; import * as Web3 from 'web3';
@ -19,6 +18,8 @@ import {
ZeroExError, ZeroExError,
} from '../src'; } from '../src';
import {BlockParamLiteral, DoneCallback} from '../src/types'; import {BlockParamLiteral, DoneCallback} from '../src/types';
import {promisify} from '../src/utils/promisify';
import {Web3Wrapper} from '../src/web3_wrapper';
import {BlockchainLifecycle} from './utils/blockchain_lifecycle'; import {BlockchainLifecycle} from './utils/blockchain_lifecycle';
import {chaiSetup} from './utils/chai_setup'; import {chaiSetup} from './utils/chai_setup';
@ -38,12 +39,14 @@ describe('TokenWrapper', () => {
let tokenUtils: TokenUtils; let tokenUtils: TokenUtils;
let coinbase: string; let coinbase: string;
let addressWithoutFunds: string; let addressWithoutFunds: string;
let web3Wrapper: Web3Wrapper;
const config = { const config = {
networkId: constants.TESTRPC_NETWORK_ID, networkId: constants.TESTRPC_NETWORK_ID,
}; };
before(async () => { before(async () => {
web3 = web3Factory.create(); web3 = web3Factory.create();
zeroEx = new ZeroEx(web3.currentProvider, config); zeroEx = new ZeroEx(web3.currentProvider, config);
web3Wrapper = new Web3Wrapper(web3.currentProvider, config.networkId);
userAddresses = await zeroEx.getAvailableAddressesAsync(); userAddresses = await zeroEx.getAvailableAddressesAsync();
tokens = await zeroEx.tokenRegistry.getTokensAsync(); tokens = await zeroEx.tokenRegistry.getTokensAsync();
tokenUtils = new TokenUtils(tokens); tokenUtils = new TokenUtils(tokens);
@ -237,8 +240,10 @@ describe('TokenWrapper', () => {
await zeroEx.token.setAllowanceAsync(zrx.address, coinbase, userWithNormalAllowance, transferAmount); await zeroEx.token.setAllowanceAsync(zrx.address, coinbase, userWithNormalAllowance, transferAmount);
await zeroEx.token.setUnlimitedAllowanceAsync(zrx.address, coinbase, userWithUnlimitedAllowance); await zeroEx.token.setUnlimitedAllowanceAsync(zrx.address, coinbase, userWithUnlimitedAllowance);
const initBalanceWithNormalAllowance = await promisify(web3.eth.getBalance)(userWithNormalAllowance); const initBalanceWithNormalAllowance = await web3Wrapper.getBalanceInWeiAsync(userWithNormalAllowance);
const initBalanceWithUnlimitedAllowance = await promisify(web3.eth.getBalance)(userWithUnlimitedAllowance); const initBalanceWithUnlimitedAllowance = await web3Wrapper.getBalanceInWeiAsync(
userWithUnlimitedAllowance,
);
await zeroEx.token.transferFromAsync( await zeroEx.token.transferFromAsync(
zrx.address, coinbase, userWithNormalAllowance, userWithNormalAllowance, transferAmount, zrx.address, coinbase, userWithNormalAllowance, userWithNormalAllowance, transferAmount,
@ -247,8 +252,10 @@ describe('TokenWrapper', () => {
zrx.address, coinbase, userWithUnlimitedAllowance, userWithUnlimitedAllowance, transferAmount, zrx.address, coinbase, userWithUnlimitedAllowance, userWithUnlimitedAllowance, transferAmount,
); );
const finalBalanceWithNormalAllowance = await promisify(web3.eth.getBalance)(userWithNormalAllowance); const finalBalanceWithNormalAllowance = await web3Wrapper.getBalanceInWeiAsync(userWithNormalAllowance);
const finalBalanceWithUnlimitedAllowance = await promisify(web3.eth.getBalance)(userWithUnlimitedAllowance); const finalBalanceWithUnlimitedAllowance = await web3Wrapper.getBalanceInWeiAsync(
userWithUnlimitedAllowance,
);
const normalGasCost = initBalanceWithNormalAllowance.minus(finalBalanceWithNormalAllowance); const normalGasCost = initBalanceWithNormalAllowance.minus(finalBalanceWithNormalAllowance);
const unlimitedGasCost = initBalanceWithUnlimitedAllowance.minus(finalBalanceWithUnlimitedAllowance); const unlimitedGasCost = initBalanceWithUnlimitedAllowance.minus(finalBalanceWithUnlimitedAllowance);

View File

@ -1,3 +1,7 @@
/**
* This file is auto-generated using https://github.com/0xProject/0x.js/tree/development/packages/typed-contracts. Don't edit directly.
* Templates can be found at https://github.com/0xProject/0x.js/tree/development/packages/typed-contracts-templates.
*/
import {BigNumber} from 'bignumber.js'; import {BigNumber} from 'bignumber.js';
import * as Web3 from 'web3'; import * as Web3 from 'web3';