UNPKG

@berrywallet/core

Version:

Berrywallet main Core for work with common cryptocurrencies like Bitcoin, Ethereum, Dash, Litecoin

112 lines (111 loc) 4.81 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; } Object.defineProperty(exports, "__esModule", { value: true }); const lodash_1 = require("lodash"); const bignumber_js_1 = __importDefault(require("bignumber.js")); const etherscan_api_1 = __importDefault(require("etherscan-api")); const _1 = require("../../"); const network_client_1 = require("./network-client"); class EtherscanNetworkClient extends network_client_1.NetworkClient { constructor(coin, options) { super(coin, options); if (false === (coin instanceof _1.Coin.Defined.Ethereum)) { throw new Error('Invalid Coin. Just ETH Coin'); } const { network = null } = options; this.etherscanClient = etherscan_api_1.default.init(_1.Constants.ETHERSCAN_API_KEY, network); } getTx(txid) { return __awaiter(this, void 0, void 0, function* () { let response; try { response = this.etherscanClient.proxy.eth_getTransactionByHash(txid); } catch (error) { return undefined; } const tx = response.result; return { coin: this.coin.getUnit(), txid: tx.hash, blockHeight: new bignumber_js_1.default(tx.blockNumber).toNumber(), blockTime: +tx.timeStamp * 1000, scheme: _1.Coin.TransactionScheme.FROM_TO, value: new bignumber_js_1.default(tx.value).div(_1.Constants.WEI_PER_COIN).toString(), gasPrice: new bignumber_js_1.default(tx.gasPrice).div(_1.Constants.WEI_PER_COIN).toString(), to: tx.to, from: tx.from, gasLimit: tx.gas, data: tx.input, nonce: tx.nonce, }; }); } getBlock(blockHash) { throw new Error('Must be implement'); } getGasPrice() { return __awaiter(this, void 0, void 0, function* () { const res = yield this.etherscanClient.proxy.eth_gasPrice(); const estimateGasPrice = new bignumber_js_1.default(res.result).div(_1.Constants.WEI_PER_COIN); return { low: estimateGasPrice.div(2), standard: estimateGasPrice, high: estimateGasPrice.times(5), }; }); } estimateGas(address, value) { throw new Error('Can not get Value'); } broadCastTransaction(transaction) { return __awaiter(this, void 0, void 0, function* () { const response = yield this.etherscanClient.proxy .eth_sendRawTransaction(transaction.toBuffer().toString('hex')); return response.result; }); } getAddressTxs(address) { return __awaiter(this, void 0, void 0, function* () { let response; try { response = yield this.etherscanClient.account.txlist(address.toLowerCase(), 1, 'latest', 'asc'); } catch (error) { return []; } const txList = []; lodash_1.forEach(response.result, (tx) => { if (+tx.isError) return; const txData = { coin: this.coin.getUnit(), txid: tx.hash, blockHeight: new bignumber_js_1.default(tx.blockNumber).toNumber(), blockTime: +tx.timeStamp * 1000, scheme: _1.Coin.TransactionScheme.FROM_TO, value: new bignumber_js_1.default(tx.value).div(_1.Constants.WEI_PER_COIN).toString(), gasPrice: new bignumber_js_1.default(tx.gasPrice).div(_1.Constants.WEI_PER_COIN).toString(), to: tx.to, from: tx.from, gasLimit: tx.gas, data: tx.input, nonce: tx.nonce, }; txList.push(txData); }); return txList; }); } } exports.EtherscanNetworkClient = EtherscanNetworkClient;