Skip to content

Commit

Permalink
Merge pull request #11 from republicprotocol/type-fix
Browse files Browse the repository at this point in the history
bn.js and Web3 typings fix
  • Loading branch information
jazg authored Nov 29, 2018
2 parents 7180673 + 1a32455 commit 1df89cc
Show file tree
Hide file tree
Showing 25 changed files with 684 additions and 150 deletions.
566 changes: 553 additions & 13 deletions package-lock.json

Large diffs are not rendered by default.

6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
"build:dev": "cross-env NODE_ENV=development webpack --mode development --display-error-details --progress --color",
"build:prod": "npm run clean && cross-env NODE_ENV=production webpack --mode production --progress --color",
"prepare": "# npm run lint && npm run build:dev",
"test": "source .env; ./node_modules/ts-mocha/bin/ts-mocha --timeout 60000 --paths -p ./tsconfig.json ./test/*.spec.ts ./test/**/*.spec.ts",
"test": "source .env; ./node_modules/ts-mocha/bin/ts-mocha --timeout 180000 --paths -p ./tsconfig.json ./test/*.spec.ts ./test/**/*.spec.ts",
"clean": "rm -rf dist"
},
"devDependencies": {
Expand All @@ -35,6 +35,7 @@
"@types/chai-as-promised": "7.1.0",
"@types/expect": "^1.20.3",
"@types/mocha": "^5.2.5",
"@types/web3": "^1.0.12",
"awesome-typescript-loader": "5.2.1",
"babel-loader": "8.0.2",
"babel-plugin-transform-es2015-modules-simple-commonjs": "^0.3.0",
Expand All @@ -61,6 +62,7 @@
"webpack-node-externals": "1.7.2"
},
"dependencies": {
"@types/bn.js": "^4.11.3",
"@types/bs58": "3.0.30",
"@types/detect-node": "^2.0.0",
"@types/node-fetch": "2.1.2",
Expand All @@ -78,7 +80,7 @@
"node-persist": "^3.0.1",
"node-rsa": "1.0.1",
"truffle-contract": "4.0.0-next.0",
"web3": "1.0.0-beta.35",
"web3": "1.0.0-beta.36",
"web3-provider-engine": "^14.1.0"
}
}
10 changes: 6 additions & 4 deletions src/contracts/bindings/darknode_registry.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
// tslint:disable

import { BN } from "bn.js";
import { Log, PromiEvent, Provider, TransactionReceipt, Tx } from "web3/types";
import { Tx } from "web3/eth/types";
import { Provider } from "web3/providers";
import PromiEvent from "web3/promiEvent";
import { TransactionReceipt, EventLog } from "web3/types";

export interface Transaction { receipt: TransactionReceipt; tx: string; logs: Log[]; }
interface Transaction { receipt: TransactionReceipt; tx: string; logs: EventLog[]; }

type BigNumber = string | number | BN;
type BigNumber = string;

export interface DarknodeRegistryContract {
numDarknodesNextEpoch(options?: Tx): Promise<BigNumber>;
Expand Down
10 changes: 6 additions & 4 deletions src/contracts/bindings/erc20.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
// tslint:disable

import { BN } from "bn.js";
import { Log, PromiEvent, Provider, TransactionReceipt, Tx } from "web3/types";
import { Tx } from "web3/eth/types";
import { Provider } from "web3/providers";
import PromiEvent from "web3/promiEvent";
import { TransactionReceipt, EventLog } from "web3/types";

export interface Transaction { receipt: TransactionReceipt; tx: string; logs: Log[]; }
interface Transaction { receipt: TransactionReceipt; tx: string; logs: EventLog[]; }

type BigNumber = string | number | BN;
type BigNumber = string;

export interface ERC20Contract {
totalSupply(options?: Tx): Promise<BigNumber>;
Expand Down
10 changes: 6 additions & 4 deletions src/contracts/bindings/orderbook.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
// tslint:disable

import { BN } from "bn.js";
import { Log, PromiEvent, Provider, TransactionReceipt, Tx } from "web3/types";
import { Tx } from "web3/eth/types";
import { Provider } from "web3/providers";
import PromiEvent from "web3/promiEvent";
import { TransactionReceipt, EventLog } from "web3/types";

export interface Transaction { receipt: TransactionReceipt; tx: string; logs: Log[]; }
interface Transaction { receipt: TransactionReceipt; tx: string; logs: EventLog[]; }

type BigNumber = string | number | BN;
type BigNumber = string;

export interface OrderbookContract {
renounceOwnership(options?: Tx): PromiEvent<Transaction>;
Expand Down
10 changes: 6 additions & 4 deletions src/contracts/bindings/ren_ex_balances.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
// tslint:disable

import { BN } from "bn.js";
import { Log, PromiEvent, Provider, TransactionReceipt, Tx } from "web3/types";
import { Tx } from "web3/eth/types";
import { Provider } from "web3/providers";
import PromiEvent from "web3/promiEvent";
import { TransactionReceipt, EventLog } from "web3/types";

export interface Transaction { receipt: TransactionReceipt; tx: string; logs: Log[]; }
interface Transaction { receipt: TransactionReceipt; tx: string; logs: EventLog[]; }

type BigNumber = string | number | BN;
type BigNumber = string;

export interface RenExBalancesContract {
brokerVerifierContract(options?: Tx): Promise<string>;
Expand Down
10 changes: 6 additions & 4 deletions src/contracts/bindings/ren_ex_settlement.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
// tslint:disable

import { BN } from "bn.js";
import { Log, PromiEvent, Provider, TransactionReceipt, Tx } from "web3/types";
import { Tx } from "web3/eth/types";
import { Provider } from "web3/providers";
import PromiEvent from "web3/promiEvent";
import { TransactionReceipt, EventLog } from "web3/types";

export interface Transaction { receipt: TransactionReceipt; tx: string; logs: Log[]; }
interface Transaction { receipt: TransactionReceipt; tx: string; logs: EventLog[]; }

type BigNumber = string | number | BN;
type BigNumber = string;

export interface RenExSettlementContract {
orderStatus(index_0: string, options?: Tx): Promise<BigNumber>;
Expand Down
10 changes: 6 additions & 4 deletions src/contracts/bindings/ren_ex_tokens.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
// tslint:disable

import { BN } from "bn.js";
import { Log, PromiEvent, Provider, TransactionReceipt, Tx } from "web3/types";
import { Tx } from "web3/eth/types";
import { Provider } from "web3/providers";
import PromiEvent from "web3/promiEvent";
import { TransactionReceipt, EventLog } from "web3/types";

export interface Transaction { receipt: TransactionReceipt; tx: string; logs: Log[]; }
interface Transaction { receipt: TransactionReceipt; tx: string; logs: EventLog[]; }

type BigNumber = string | number | BN;
type BigNumber = string;

export interface RenExTokensContract {
renounceOwnership(options?: Tx): PromiEvent<Transaction>;
Expand Down
10 changes: 6 additions & 4 deletions src/contracts/bindings/wyre.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
// tslint:disable

import { BN } from "bn.js";
import { Log, PromiEvent, TransactionReceipt, Tx, Provider } from "web3/types";
import { Tx } from "web3/eth/types";
import { Provider } from "web3/providers";
import PromiEvent from "web3/promiEvent";
import { TransactionReceipt, EventLog } from "web3/types";

export interface Transaction { receipt: TransactionReceipt; tx: string; logs: Log[]; }
interface Transaction { receipt: TransactionReceipt; tx: string; logs: EventLog[]; }

type BigNumber = string | number | BN;
type BigNumber = string;

export interface WyreContract {
approve(_to: string, _tokenId: BigNumber, options?: Tx): PromiEvent<Transaction>;
Expand Down
2 changes: 1 addition & 1 deletion src/contracts/contracts.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const contract = require("./truffle-contract/index.js");

import { Provider } from "web3/types";
import { Provider } from "web3/providers";

// Bindings
import { DarknodeRegistryArtifact } from "./bindings/darknode_registry";
Expand Down
2 changes: 1 addition & 1 deletion src/declarations/bignumber.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as BigNumberLib from "bignumber.js";
import { BN } from "bn.js";
import BN from "bn.js";

/**
* The existing typings does not let you convert BN into BigNumber.
Expand Down
73 changes: 0 additions & 73 deletions src/declarations/bn.d.ts

This file was deleted.

5 changes: 3 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import BigNumber from "bignumber.js";
import Web3 from "web3";

import { BN } from "bn.js";
import BN from "bn.js";
import PromiEvent from "web3/promiEvent";

import { PromiEvent, Provider } from "web3/types";
import { Provider } from "web3/providers";

import LocalStorage from "./storage/localStorage";

Expand Down
2 changes: 1 addition & 1 deletion src/lib/balances.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import BigNumber from "bignumber.js";
import { BN } from "bn.js";
import BN from "bn.js";

import { ErrNumericalPrecision } from "./errors";
// import { Token, TokenDetails } from "./market";
Expand Down
16 changes: 8 additions & 8 deletions src/lib/ingress.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const NodeRSA = require("node-rsa") as { new(): NodeRSAType };

import Web3 from "web3";

import { BN } from "bn.js";
import BN from "bn.js";
import { List, Map } from "immutable";

import * as shamir from "./shamir";
Expand Down Expand Up @@ -236,10 +236,10 @@ export async function requestWithdrawalSignature(ingressURL: string, address: st
return new EncodedData(resp.data.signature, Encodings.BASE64);
}

async function ordersBatch(orderbook: OrderbookContract, offset: number, limit: number): Promise<List<[OrderID, OrderStatus, string]>> {
async function ordersBatch(web3: Web3, orderbook: OrderbookContract, offset: number, limit: number): Promise<List<[OrderID, OrderStatus, string]>> {
let orders;
try {
orders = await orderbook.getOrders(offset, limit);
orders = await orderbook.getOrders(web3.utils.toHex(offset), web3.utils.toHex(limit));
} catch (error) {
console.error(`Failed to get call getOrders in ordersBatch`);
throw error;
Expand All @@ -256,7 +256,7 @@ async function ordersBatch(orderbook: OrderbookContract, offset: number, limit:
return ordersList;
}

export async function getOrders(orderbook: OrderbookContract, startIn?: number, limitIn?: number): Promise<List<[OrderID, OrderStatus, string]>> {
export async function getOrders(web3: Web3, orderbook: OrderbookContract, startIn?: number, limitIn?: number): Promise<List<[OrderID, OrderStatus, string]>> {
let orderCount;
try {
orderCount = new BN(await orderbook.ordersCount()).toNumber();
Expand Down Expand Up @@ -288,7 +288,7 @@ export async function getOrders(orderbook: OrderbookContract, startIn?: number,
batchLimit = Math.min(batchLimit, stop - start);

// Retrieve batch of orders and increment start
const batch = await ordersBatch(orderbook, start, batchLimit);
const batch = await ordersBatch(web3, orderbook, start, batchLimit);
ordersList = ordersList.concat(batch).toList();
start += batchLimit;
}
Expand Down Expand Up @@ -496,8 +496,8 @@ export function encryptForDarknode(darknodeKey: NodeRSAType | null, share: shami
* When the {start} value is not the NULL address, it is always returned as the
* first entry so it should not be re-added to the list of all darknodes.
*/
async function getAllDarknodes(darknodeRegistryContract: DarknodeRegistryContract): Promise<string[]> {
const batchSize = 10;
async function getAllDarknodes(web3: Web3, darknodeRegistryContract: DarknodeRegistryContract): Promise<string[]> {
const batchSize = web3.utils.toHex(10);

const allDarknodes = [];
let lastDarknode = NULL;
Expand All @@ -514,7 +514,7 @@ async function getAllDarknodes(darknodeRegistryContract: DarknodeRegistryContrac
* Calculate pod arrangement based on current epoch
*/
async function getPods(web3: Web3, darknodeRegistryContract: DarknodeRegistryContract, simpleConsole: SimpleConsole): Promise<List<Pod>> {
const darknodes = await getAllDarknodes(darknodeRegistryContract);
const darknodes = await getAllDarknodes(web3, darknodeRegistryContract);
const minimumPodSize = new BN(await darknodeRegistryContract.minimumPodSize()).toNumber();
simpleConsole.log(`Using minimum pod size ${minimumPodSize}`);
const epoch = await darknodeRegistryContract.currentEpoch();
Expand Down
2 changes: 1 addition & 1 deletion src/lib/order.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import BigNumber from "bignumber.js";
import { BN } from "bn.js";
import BN from "bn.js";

import { OrderStatus } from "../index";
import { ErrUnknownOrderStatus } from "./errors";
Expand Down
2 changes: 1 addition & 1 deletion src/lib/shamir.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { BN } from "bn.js";
import BN from "bn.js";
import crypto from "crypto";
import { List } from "immutable";

Expand Down
2 changes: 1 addition & 1 deletion src/lib/tokens.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import RenExSDK from "../index";

import { BigNumber } from "bignumber.js";
import { BN } from "bn.js";
import BN from "bn.js";
import { Token, TokenCode, TokenDetails } from "../types";

export function toSmallestUnit(amount: BigNumber, tokenDetails: TokenDetails): BigNumber {
Expand Down
14 changes: 8 additions & 6 deletions src/methods/balanceActionMethods.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import BigNumber from "bignumber.js";

import { BN } from "bn.js";
import { PromiEvent } from "web3/types";
import BN from "bn.js";
import PromiEvent from "web3/promiEvent";

import RenExSDK from "../index";
import { BalanceAction, BalanceActionType, NumberInput, Token, TokenCode, TokenDetails, Transaction, TransactionStatus } from "../types";
Expand Down Expand Up @@ -96,7 +96,7 @@ export const deposit = async (

const { txHash, promiEvent } = await onTxHash(sdk._contracts.renExBalances.deposit(
tokenDetails.address,
valueBN,
sdk.getWeb3().utils.toHex(valueBN),
{ value: valueBN.toString(), from: address, gasPrice },
));

Expand Down Expand Up @@ -131,12 +131,14 @@ export const deposit = async (
// if there are any pending deposits for the same token
const allowance = new BN(await tokenContract.allowance(address, sdk._contracts.renExBalances.address, { from: address, gasPrice }));
if (allowance.lt(valueBN)) {
await onTxHash(tokenContract.approve(sdk._contracts.renExBalances.address, valueBN, { from: address, gasPrice }));
await onTxHash(tokenContract.approve(sdk._contracts.renExBalances.address,
sdk.getWeb3().utils.toHex(valueBN),
{ from: address, gasPrice }));
}

const { txHash, promiEvent } = await onTxHash(sdk._contracts.renExBalances.deposit(
tokenDetails.address,
valueBN,
sdk.getWeb3().utils.toHex(valueBN),
{
// Manually set gas limit since gas estimation won't work
// if the ethereum node hasn't seen the previous transaction
Expand Down Expand Up @@ -226,7 +228,7 @@ export const withdraw = async (

const { txHash, promiEvent } = await onTxHash(sdk._contracts.renExBalances.withdraw(
tokenDetails.address,
valueBN,
sdk.getWeb3().utils.toHex(valueBN),
signature.toHex(),
{ from: address, gasPrice, /* nonce: balanceAction.nonce */ },
));
Expand Down
Loading

0 comments on commit 1df89cc

Please sign in to comment.