HenryNguyen5 8fe664c171 Replace bignumber.js with bn.js (#319)
* Add definition file for bn.js

* Remove types-bn

* make isBN a static property

* progress commit -- swap out bignumber.js for bn.js

* Swap out bignumber for bn in vendor

* Change modn to number return

* Start to strip out units lib for a string manipulation based lib

* Convert codebase to only base units

* Get rid of useless component

* Handle only wei in values

* Use unit conversion in sidebar

* Automatically strip hex prefix, and  handle decimal edge case

* Handle base 16 wei in transactions

* Make a render callback component for dealing with unit conversion

* Switch contracts to use bn.js, and get transaction values from signedTx instead of state

* Get send transaction  working with bn.js

* Remove redundant hex stripping,  return base value of tokens

* Cleanup unit file

* Re-implement toFixed for strings

* Use formatNumber in codebase

* Cleanup code

* Undo package test changes

* Update snapshot and remove console logs

* Use TokenValue / Wei more consistently where applicable

* Add typing to deterministicWallets, fix confirmation modal, make UnitDisplay more flexible

* Clean up prop handling in UnitDisplay

* Change instanceof to typeof check, change boolean of displayBalance

* Fix tsc errors

* Fix token row displaying wrong decimals

* Fix deterministic modal token display

* Handle hex and non hex strings automatically in BN conversion

* Fix handling of strings and numbers for BN

* add web3 fixes & comments

* Display short balances on deterministic modals

* add more tests, fix rounding

* Add spacer to balance sidebar network name

* Fix tsc error
2017-11-12 11:45:52 -08:00

141 lines
3.4 KiB
TypeScript

import { Token } from 'config/data';
import { TransactionWithoutGas } from 'libs/messages';
import { Wei, TokenValue } from 'libs/units';
import { INode, TxObj } from '../INode';
import ERC20 from 'libs/erc20';
export default class Web3Node implements INode {
private web3: any;
constructor(web3: any) {
this.web3 = web3;
}
public sendCallRequest(txObj: TxObj): Promise<string> {
return new Promise((resolve, reject) => {
this.web3.eth.call(txObj, 'pending', (err, res) => {
if (err) {
return reject(err.message);
}
// web3 return string
resolve(res);
});
});
}
public getBalance(address: string): Promise<Wei> {
return new Promise((resolve, reject) => {
this.web3.eth.getBalance(address, (err, res) => {
if (err) {
return reject(err);
}
// web3 returns BigNumber
resolve(Wei(res.toString()));
});
});
}
public estimateGas(transaction: TransactionWithoutGas): Promise<Wei> {
return new Promise((resolve, reject) =>
this.web3.eth.estimateGas(
{
to: transaction.to,
data: transaction.data
},
(err, res) => {
if (err) {
return reject(err);
}
// web3 returns number
resolve(Wei(res));
}
)
);
}
public getTokenBalance(address: string, token: Token): Promise<TokenValue> {
return new Promise(resolve => {
this.web3.eth.call(
{
to: token.address,
data: ERC20.balanceOf(address)
},
'pending',
(err, res) => {
if (err) {
// TODO - Error handling
return resolve(TokenValue('0'));
}
// web3 returns string
resolve(TokenValue(res));
}
);
});
}
public getTokenBalances(
address: string,
tokens: Token[]
): Promise<TokenValue[]> {
return new Promise(resolve => {
const batch = this.web3.createBatch();
const totalCount = tokens.length;
const returnArr = new Array<TokenValue>(totalCount);
let finishCount = 0;
tokens.forEach((token, index) =>
batch.add(
this.web3.eth.call.request(
{
to: token.address,
data: ERC20.balanceOf(address)
},
'pending',
(err, res) => finish(index, err, res)
)
)
);
batch.execute();
function finish(index, err, res) {
if (err) {
// TODO - Error handling
returnArr[index] = TokenValue('0');
} else {
// web3 returns string
returnArr[index] = TokenValue(res);
}
finishCount++;
if (finishCount === totalCount) {
resolve(returnArr);
}
}
});
}
public getTransactionCount(address: string): Promise<string> {
return new Promise((resolve, reject) =>
this.web3.eth.getTransactionCount(address, 'pending', (err, txCount) => {
if (err) {
return reject(err);
}
// web3 returns number
resolve(txCount.toString());
})
);
}
public sendRawTx(signedTx: string): Promise<string> {
return new Promise((resolve, reject) =>
this.web3.eth.sendRawTransaction(signedTx, (err, txHash) => {
if (err) {
return reject(err);
}
// web3 return string
resolve(txHash);
})
);
}
}