Code Monkey home page Code Monkey logo

ethjs-query's Introduction

ethjs-query


A simple module for querying the Ethereum RPC layer.

Install

npm install --save ethjs-query

Usage

const BN = require('bn.js');
const HttpProvider = require('ethjs-provider-http');
const Eth = require('ethjs-query');
const eth = new Eth(new HttpProvider('http://localhost:8545'));

eth.getBalance('0x407d73d8a49eeb85d32cf465507dd71d507100c1', cb);

// result null <BN ...>

eth.sendTransaction({
  from: '0x407d73d8a49eeb85d32cf465507dd71d507100c1',
  to: '0x987d73d8a49eeb85d32cf462207dd71d50710033',
  value: new BN('6500000'),
  gas: 3000000,
  data: '0x',
}).then(cb).catch(cb);

// result null 0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470

About

A simple Ethereum RPC module for querying data from an Ethereum node such as a geth (go-etherem), parity (rust-ethereum) or TestRPC (local js-ethereum).

This module supports all Ethereum RPC methods and is designed completely to specification.

Amorphic Data Formatting

ethjs-query uses the ethjs-format module to format incoming and outgoing RPC data payloads. The primary formatting task is numbers. Number values can be inputed as: BigNumber, BN, string, hex or actual numbers. Because the blockchain does not support decimal or negative numbers, any kind of decimal or negative number will cause an error return. All received number values are returned as BN.js object instances.

Read more about the formatting layer here: ethjs-format

Async Only

All methods are async only, requiring either a callback or promise.

Error handling

Error handling is done through function callbacks or promised catches.

Debugging Options

ethjs-query comes equip with a full debug options for all data inputs and outputs.

const HttpProvider = require('ethjs-provider-http');
const Eth = require('ethjs-query');
const eth = new Eth(new HttpProvider('http://localhost:8545'), { debug: true, logger: console, jsonSpace: 0 });

eth.accounts(cb);

/* result
[ethjs-query 2016-11-27T19:37:54.917Z] attempting method accounts with params [null]
[ethjs-query 2016-11-27T19:37:54.917Z] [method 'accounts'] callback provided: true
[ethjs-query 2016-11-27T19:37:54.917Z] [method 'accounts'] attempting input formatting of 0 inputs
[ethjs-query 2016-11-27T19:37:54.917Z] [method 'accounts'] formatted inputs: []
[ethjs-query 2016-11-27T19:37:54.917Z] [method 'accounts'] attempting query with formatted inputs...
[ethjs-query 2016-11-27T19:37:54.919Z] [method 'accounts'] callback success, attempting formatting of raw outputs: ["0xb88643569c19d05dc67b960f91d9d696eebf808e","0xf...]
[ethjs-query 2016-11-27T19:37:54.919Z] [method 'accounts'] formatted outputs: ["0xb88643569c19d05dc67b960f91d9d696eebf808e","0xf...]
*/

Supported Methods

ethjs-query supports all Ethereum specified RPC methods.

const HttpProvider = require('ethjs-provider-http');
const Eth = require('ethjs-query');
const eth = new Eth(new HttpProvider('http://localhost:8545'));

eth.protocolVersion(cb);

// ....

Contributing

Please help better the ecosystem by submitting issues and pull requests to ethjs-query. We need all the help we can get to build the absolute best linting standards and utilities. We follow the AirBNB linting standard and the unix philosophy.

Guides

You'll find more detailed information on using ethjs-query and tailoring it to your needs in our guides:

  • User guide - Usage, configuration, FAQ and complementary tools.
  • Developer guide - Contributing to ethjs-query and writing your own code and coverage.

Help out

There is always a lot of work to do, and will have many rules to maintain. So please help out in any way that you can:

  • Create, enhance, and debug ethjs rules (see our guide to "Working on rules").
  • Improve documentation.
  • Chime in on any open issue or pull request.
  • Open new issues about your ideas for making ethjs-query better, and pull requests to show us how your idea works.
  • Add new tests to absolutely anything.
  • Create or contribute to ecosystem tools, like modules for encoding or contracts.
  • Spread the word.

Please consult our Code of Conduct docs before helping out.

We communicate via issues and pull requests.

Important documents

Licence

This project is licensed under the MIT license, Copyright (c) 2016 Nick Dodson. For more information see LICENSE.md.

The MIT License

Copyright (c) 2016 Nick Dodson. nickdodson.com

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

ethjs-query's People

Contributors

kumavis avatar silentcicero avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

ethjs-query's Issues

Formatting issue

Hello,

I have been trying to make the following code work in order to build a node.js server proxy to interact with ethereum.
It seemed to work just fine, displaying what appeared to be a valid transaction hash. However I was not able to have my transactions registered into the blockchain. I was able to reproduce this behaviour on both Ubuntu and Windows with Node 6.10.0 and npm 3.10.10. Also It behaved the same whether I was using a local node or a remote one (ropsten furia).
When running a local node I noticed it hadn't the same view of the transaction as the js application. It displays a transaction hash like :
0x39acd6e906255a46ff186088f0fb84193d71de4f05bc39f5941195267fa6be7d whereas my node sees it as : 39acd6e906255a46ff186088f0fb84193d71de4f05bc39f5941195267fa6be7d. Same problem with the 'to address'. My local node sees it in the format : "&2824dd13d388ed0df7ae1b5697b7b66e737e021a" instead of '0x2824dd13d388ed0df7ae1b5697b7b66e737e021a' which is the format my local node sees succesful transactions sent by example via web3.
I believe it is the root of the problem whoever I can't tellewhich of the packages is responsible for this discrepancy. Thank you for your help and please find my code below :``

`
const HttpProvider = require('ethjs-provider-http')'
const Eth = require('ethjs-query')
const eth = new Eth(new HttpProvider('http://localhost:8545'),{ debug: true, logger: console, jsonSpace: 0 });
//const eth = new Eth(new HttpProvider('https://ropsten.infura.io/'))
const sign = require('ethjs-signer').sign

address = '0x4129c6A02caB77DeEdBDC221904a3dc1ab7cAAfE'
dump = '2824dD13D388ed0Df7Ae1b5697b7b66E737E021a'
const privateKey = '0x600998f685993f3fc4e82fbc4593e9c854139e57b42756ef62260a5b8168a2c1'

eth.getBalance(address).then((result) => {
console.log(result)
}).catch(function () {
console.log("Promise Rejected");
});
eth.sendRawTransaction(sign({
from: address,
to: dump,
value: 10,
gasPrice:20000000000,
gas: 2000000,
nonce: 589786,
}, privateKey)).then((txHash) => {
console.log('Transaction Hash', txHash)
}).catch(function () {
console.log("Promise Rejected");
});

`

Add Type Definitions File

ethjs-query

Before opening a new issue, please take a moment to review our community guidelines to make the contribution process easy and effective for everyone involved.

Before opening a new issue, you may find an answer in already closed issues:
https://github.com/ethjs/ethjs-query/issues?q=is%3Aissue+is%3Aclosed

Issue Type

Description

Adding a type definition file will be heavily beneficial for libraries using this and re-writing their codebase to TypeScript.

Example:
https://github.com/BboyAkers/nonce-tracker

Currently re-writing MetaMask/nounce-tracker to TypeScript and EthQuery is labeled having an any type.

Steps to reproduce

(Add link to a demo on https://jsfiddle.net or similar if possible)

Versions

  • Node/NPM:
  • Browser:

[ethjs-query] while formatting outputs in 'getTransactionByHash'

Issue Type

  • Bug

Description

Error: [ethjs-format] hex string '0x0xdce7150b95ec4677ec22b851106cd8964a37158b43' must be an alphanumeric 42 utf8 byte hex

I'm working with an older version of ethjs-query in an old project, lastly, it was working fine. But now it's throwing the following error while checking for the pending transactions. Although the transaction is successful, but it's unable to get its status.

"[ethjs-query] while formatting outputs from RPC '{"blockHash":"0x9970e674ae287c3fdb03b28813d7ed5a82dfa1036cbc5704525e2e45827aead9","blockNumber":"0x169a923","from":"xdce7150b95ec4677ec22b851106cd8964a37158b43","gas":"0x5208","gasPrice":"0x3b9aca00","hash":"0x49ff9aea452d64a0456fab70e2aae52b5e3264b934f269b34f590b84259b2c43","input":"0x","nonce":"0x1","to":"xdc73a72fa27979c7d6568a2c72587046b41323f2a9","transactionIndex":"0x0","value":"0xd02ab486cedc0000","v":"0x8a","r":"0x6c62ba8d38a8ddf563ac507a0251071616a57c740bb840117d7e11460ab3dc62","s":"0x5eb0da414fcc0cfea0de1c48dca191b19db7a12763a7d1cb229996677b4211ba"}' for method 'getTransactionByHash' Error: [ethjs-format] hex string '0x0xdce7150b95ec4677ec22b851106cd8964a37158b43' must be an alphanumeric 42 utf8 byte hex (chars: a-fA-F) string, is 46 bytes"

Versions

  • Node/NPM: 8.1.0
  • Browser: Chrome

cb is not defined

ethjs-query

Description

hello , when i use the eth.getBalance('0x04D43d123C04884177b55E4c70A50F348fE82df5',cb)
i get the following error , is there something wrong with my code ?

(node:15526) UnhandledPromiseRejectionWarning: ReferenceError: cb is not defined
    at getbalance (/home/linfang/workspace/uport/ethr-did/src/main.js:22:81)
    at Object.<anonymous> (/home/linfang/workspace/uport/ethr-did/src/main.js:26:1)
    at Module._compile (internal/modules/cjs/loader.js:778:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)
    at Module.load (internal/modules/cjs/loader.js:653:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
    at Function.Module._load (internal/modules/cjs/loader.js:585:3)
    at Function.Module.runMain (internal/modules/cjs/loader.js:831:12)
    at startup (internal/bootstrap/node.js:283:19)
    at bootstrapNodeJSCore (internal/bootstrap/node.js:622:3)
(node:15526) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:15526) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

here is my code :

let HttpProvider = require('ethjs-provider-http')
const Eth = require('ethjs-query');

const provider = new HttpProvider('http://localhost:7545')
const eth = new Eth( new HttpProvider('http://localhost:7545'));

async function getbalance() {
  let value = await eth.getBalance('0x04D43d123C04884177b55E4c70A50F348fE82df5',cb)
  console.log(value)
}
getbalance()

when i changed the getBalsnce with removing the cb: eth.getBalance('0x04D43d123C04884177b55E4c70A50F348fE82df5',),

it returned

<BN: 56b060b1d14f70000>
(node:15984) [DEP0079] DeprecationWarning: Custom inspection function on Objects via .inspect() is deprecated

acctually, the balance of the account is 99.95 ETH

how can I get the right balance ?
thank you very much ~

Bug - newFilter result and getFilterChanges params incompatible

const filterId = await eth.newFilter({ address: contractAddress, topics: [targetTopic], fromBlock: blockNumber, toBlock: 'latest' })
const filterChanges = await eth.getFilterChanges(filterId)

eth.newFilter returns a BN
eth.getFilterChanges converts BN to hex but does not use 'quantity' format

<BN: 1> becomes 0x1 instead of 0x01

suggestion, do either or both:

  • fix hex format in getFilterChanges
  • just return hex string from newFilter

Unable to deserialize event from ABI for usage in getLogs call

Hi Guys - Love the library. We are using it in several places as we continue to develop our decentralized reward and loyalty system. One thing we do is use you to call getLogs in order to query for events on the blockchain, which is how we store achievements on chain. However, try as we might we could not successfully serialize our event from the ABI such that it could be passed to your getLogs call and we had to resort to Web3 as you can see below:

let getAchievements = (address) => {
	//using web3 to format the options correctly
	let event = pointTokenContractInstanceWeb3.Award({ _to: address }, { fromBlock: config.blockFrom, toBlock: 'latest' });
	eth.getLogs(event.options).then(result => {
		result.map(r => {
			//console.log(r);
			console.log(new BN(util.stripHexPrefix(r.topics[3])).toNumber());
		});
	});
};

It would be really cool to use your stuff only and not have to pull in Web3. The ABI created from your code works great for everything else. Any ideas?

RPC Error: Error: [ethjs-query] while formatting outputs from RPC '{"value":{"code":-32000,"message":"oversized data"}}'

ethjs-query

Before opening a new issue, please take a moment to review our community guidelines to make the contribution process easy and effective for everyone involved.

Before opening a new issue, you may find an answer in already closed issues:
https://github.com/ethjs/ethjs-query/issues?q=is%3Aissue+is%3Aclosed

Issue Type

Description

Hi, I'm using truffle and testing a new application of mine which involves large text to be sent to a smart contract. I don't know if this is the right place to ask because maybe my issue is because of some other limit I'm not aware of, but hopefully you can point me in the right direction. I get the error above when approving a transaction with large data

Steps to reproduce

(Add link to a demo on https://jsfiddle.net or similar if possible)

Versions

  • Node/NPM: 6.14.8
  • Browser: firefox

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.