file-force
74 строки · 1.7 Кб
1/*
2* Copyright (C) 2017 Igor Konovalov
3*
4* Licensed under the Apache License, Version 2.0 (the "License");
5* you may not use this file except in compliance with the License.
6* You may obtain a copy of the License at
7*
8* http://www.apache.org/licenses/LICENSE-2.0
9*
10* Unless required by applicable law or agreed to in writing, software
11* distributed under the License is distributed on an "AS IS" BASIS,
12* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13* See the License for the specific language governing permissions and
14* limitations under the License.
15*/
16
17const Web3 = require('web3');18
19class Ethereum {20
21constructor(config) {22this._web3 = new Web3();23this.web3.setProvider(new this.web3.providers.HttpProvider(config.eth.api));24this._eth = this.web3.eth;25}26
27get web3() {28return this._web3;29}30
31get eth() {32return this._eth;33}34
35get lastBlock() {36return this.eth.getBlock('latest')37}38
39listAccounts() {40return this.eth.accounts;41}42
43coinbase() {44return this.eth.coinbase45}46
47getBalance(account, units = 'ether') {48return this.web3.fromWei(this.eth.getBalance(account), units)49}50
51txCount(address) {52return this.eth.getTransactionCount(address);53}54
55txCountWithPending(address) {56return this.eth.getTransactionCount(address, 'pending')57}58
59get gasPrice() {60return this.eth.gasPrice;61}62
63get defaultAccount() {64return this.eth.defaultAccount;65}66
67sendRawTransaction(tx) {68let txHash = this.eth.sendRawTransaction(tx);69return txHash;70}71
72}
73
74module.exports = Ethereum;