skubakdj c9c147db52 Wallet Decrypt - Web3 (MetaMask / Mist) (#303)
* add support for web3, disabled, and hidden in node dropdown header

* add web3 node config actions

* add web3 wallet actions

* add web3 node support

* add web3 wallet & web3 wallet ui selection

* add web3 wallet & config sagas

* add web3 transaction support to SendTransaction tab

* add web3 node check & reset to redux store

* remove comments from Web3.tsx

* update comment

* correct spacing display issue in Web3 component

* convert getTransactionCount response to string

* disable web3 wallets in offline mode

* implement sendCallRequest method on Web3 node

* remove unused vars

* make typescript happy

* convert wallet constants to enum & apply to wallet action files

* update wallet reducer to use TypeKeys enum

* remove unnecessary console log

* remove unnecessary await

* make token balance math more readable

* use NewTabLink in Web3.tsx, allow NewTabLink to accept className

* move web3.ts to non-deterministic folder

* update imports & method names, implement message signing

* add web3 wallet export

* use bufferToHex
2017-11-09 19:30:20 -08:00

141 lines
3.5 KiB
TypeScript

import Big, { BigNumber } from 'bignumber.js';
import { Token } from 'config/data';
import { TransactionWithoutGas } from 'libs/messages';
import { Wei } 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);
}
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);
}
resolve(new Wei(res.toString()));
});
});
}
public estimateGas(transaction: TransactionWithoutGas): Promise<BigNumber> {
return new Promise((resolve, reject) =>
this.web3.eth.estimateGas(
{
to: transaction.to,
data: transaction.data
},
(err, res) => {
if (err) {
return reject(err);
}
resolve(new Big(res.toString()));
}
)
);
}
public getTokenBalance(address: string, token: Token): Promise<BigNumber> {
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(new Big(0));
}
const bigResult = new Big(res.toString());
const bigTokenBase = new Big(10).pow(token.decimal);
resolve(bigResult.div(bigTokenBase));
}
);
});
}
public getTokenBalances(
address: string,
tokens: Token[]
): Promise<BigNumber[]> {
return new Promise(resolve => {
const batch = this.web3.createBatch();
const totalCount = tokens.length;
const returnArr = new Array<BigNumber>(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(token, index, err, res)
)
)
);
batch.execute();
function finish(token, index, err, res) {
if (err) {
// TODO - Error handling
returnArr[index] = new Big(0);
} else {
returnArr[index] = new Big(res.toString()).div(
new Big(10).pow(token.decimal)
);
}
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);
}
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);
}
resolve(txHash);
})
);
}
}