<commit_before>import * as Web3 from 'web3'
import {getTransactionsFromRange, checkAllBalances} from './utility'
import BigNumber from 'bignumber.js';
import {AddressManager, Block, EthereumClient, EthereumTransaction} from "./types";

export interface Web3EthereumClientConfig {
  http: string
  sweepAddress?: string
}

export class Web3EthereumClient implements EthereumClient {
  private web3

  constructor(ethereumConfig: Web3EthereumClientConfig) {
    this.web3 = new Web3()
    this.web3.setProvider(new this.web3.providers.HttpProvider(ethereumConfig.http))
  }

  getWeb3() {
    return this.web3
  }

  getTransaction(txid) {
    return new Promise((resolve, reject) => {
      this.web3.eth.getTransaction(txid, (err, block) => {
        if (err) {
          console.error('Error querying transaction', txid, 'with message', err.message)
          reject(new Error(err));
        }
        else {
          resolve(block)
        }
      })
    })
  }

  getCoinbase() {
    return Promise.resolve(this.web3.eth.coinbase)
  }

  toWei(amount: number) {
    return this.web3.toWei(amount)
  }

  fromWei(amount: number) {
    return new BigNumber(amount).dividedBy(1000000000000000000).toString();
  }

  createAddress(): Promise<string> {
    return new Promise((resolve, reject) => {
      // if (!this.web3.isConnected()) {
      //   reject(new Error("Cannot create address, not connected to client."))
      // }

      this.web3.personal.newAccount((err, result) => {
        if (err)
          reject(new Error("Error creating address: " + err.message))
        else {
          console.log('Created new address', result)
          resolve(result)
        }
      })
    })
  }

  getAccounts(): Promise<string[]> {
    return new Promise((resolve, reject) => {
      this.web3.eth.getAccounts((err, result) => {
        if (err)
          reject(new Error("Error getting accounts: " + err.message))
        else
          resolve(result)
      })
    })
  }

  getBalance(address: string): Promise<any> {
    return new Promise((resolve, reject) => {
      this.web3.eth.getBalance(address, (err, result) => {
        if (err)
          reject(new Error("Error getting balance: " + err.message))
        else
          resolve(result)
      })
    })
  }

  unlockAccount(address: string) {
    return new Promise((resolve, reject) => {
      try {
        this.web3.personal.unlockAccount(address, (err, result) => {
          if (err)
            reject(new Error("Error unlocking account: " + err.message))
          else
            resolve(result)
        })
      }
      catch(error) {
        reject(new Error("Error unlocking account: " + address + '.  ' + error.message))
      }
    })
  }

  send(from: string | object, to?: string, amount?: string): Promise<EthereumTransaction> {
    const transaction = from && typeof from === 'object'
      ? from as any
      : {from: from, to: to, value: amount, gas: 21000}

    if (!transaction.from)
      throw Error("Ethereum transaction.from cannot be empty.")

    if (!transaction.to)
      throw Error("Ethereum transaction.to cannot be empty.")

    if (transaction.from === '')
      transaction.from = this.web3.eth.coinbase

    const original = Object.assign({}, transaction)
    transaction.value = transaction.value.toString()
    return this.unlockAccount(transaction.from)<commit_msg>Fix gasPrice issue that was causing failed tx<commit_after>import * as Web3 from 'web3'
import {getTransactionsFromRange, checkAllBalances} from './utility'
import BigNumber from 'bignumber.js';
import {AddressManager, Block, EthereumClient, EthereumTransaction} from "./types";

export interface Web3EthereumClientConfig {
  http: string
  sweepAddress?: string
}

export class Web3EthereumClient implements EthereumClient {
  private web3

  constructor(ethereumConfig: Web3EthereumClientConfig) {
    this.web3 = new Web3()
    this.web3.setProvider(new this.web3.providers.HttpProvider(ethereumConfig.http))
  }

  getWeb3() {
    return this.web3
  }

  getTransaction(txid) {
    return new Promise((resolve, reject) => {
      this.web3.eth.getTransaction(txid, (err, block) => {
        if (err) {
          console.error('Error querying transaction', txid, 'with message', err.message)
          reject(new Error(err));
        }
        else {
          resolve(block)
        }
      })
    })
  }

  getCoinbase() {
    return Promise.resolve(this.web3.eth.coinbase)
  }

  toWei(amount: number) {
    return this.web3.toWei(amount)
  }

  fromWei(amount: number) {
    return new BigNumber(amount).dividedBy(1000000000000000000).toString();
  }

  createAddress(): Promise<string> {
    return new Promise((resolve, reject) => {
      // if (!this.web3.isConnected()) {
      //   reject(new Error("Cannot create address, not connected to client."))
      // }

      this.web3.personal.newAccount((err, result) => {
        if (err)
          reject(new Error("Error creating address: " + err.message))
        else {
          console.log('Created new address', result)
          resolve(result)
        }
      })
    })
  }

  getAccounts(): Promise<string[]> {
    return new Promise((resolve, reject) => {
      this.web3.eth.getAccounts((err, result) => {
        if (err)
          reject(new Error("Error getting accounts: " + err.message))
        else
          resolve(result)
      })
    })
  }

  getBalance(address: string): Promise<any> {
    return new Promise((resolve, reject) => {
      this.web3.eth.getBalance(address, (err, result) => {
        if (err)
          reject(new Error("Error getting balance: " + err.message))
        else
          resolve(result)
      })
    })
  }

  unlockAccount(address: string) {
    return new Promise((resolve, reject) => {
      try {
        this.web3.personal.unlockAccount(address, (err, result) => {
          if (err)
            reject(new Error("Error unlocking account: " + err.message))
          else
            resolve(result)
        })
      }
      catch(error) {
        reject(new Error("Error unlocking account: " + address + '.  ' + error.message))
      }
    })
  }

  send(from: string | object, to?: string, amount?: string): Promise<EthereumTransaction> {
    const transaction = from && typeof from === 'object'
      ? from as any
      : {from: from, to: to, value: amount, gas: 21000}

    if (!transaction.from)
      throw Error("Ethereum transaction.from cannot be empty.")

    if (!transaction.to)
      throw Error("Ethereum transaction.to cannot be empty.")

    if (transaction.from === '')
      transaction.from = this.web3.eth.coinbase

    const original = Object.assign({}, transaction)
    transaction.value = transaction.value.toString()
    transaction.gasPrice = this.web3.eth.toWei(transaction.gasPrice, 'gwei')
    return this.unlockAccount(transaction.from)
{'fraction_non_alphanumeric': 0.13023187983082982, 'fraction_numerical': 0.013271109814787808, 'mean_word_length': 2.6576, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 31, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3477868', 'n_tokens_mistral': 2098, 'n_tokens_neox': 2006, 'n_words': 461}