Java Script API

In order for a software application to be able to interact with the Hash Ahead blockchain (eg: read blockchain data or send transaction information to the network), the software must be connected to a Hash Ahead node.

For this purpose, each Hash Ahead client implements the JSON-RPC specification, so applications can rely on a uniform set of endpoints.

If you want to connect to a Hash Ahead node with JavaScript, you can use native JavaScript, but there are some handy libraries in the ecosystem that make this easier. Through these libraries, developers can write intuitive and easy to understand or even a single line of code to initialize the interaction with Ethereum (using JSON RPC requests behind the scenes).

Running a node requires a connection to the Hash Ahead software -- the Hash Ahead client. If your node is not on your local machine (for example, your node is running on an AWS instance), please update the IP address in the tutorial accordingly. See our page on running nodes for more information.

Why use a library?

These libraries reduce the complexity of interacting directly with a Hash Ahead node. They also provide utility functions (example: converting HAH to Gwei), so as a developer you can spend less time dealing with the intricacies of the Hash Ahead client and more time working on your application's unique features.

Library Function

Connect to the Hash Ahead node

Using providers, these libraries allow you to connect to Hash Ahead and read its data, either via JSON-RPC or Metamask.

For example:

// A Web3Provider wraps a standard Web3 provider, which is
// what MetaMask injects as window.ethereum into each page
const provider = new ethers.providers.Web3Provider(window.ethereum)

// The MetaMask plugin also allows signing transactions to
// send ether and pay to change state within the blockchain.
// For this we need the account signer...
const signer = provider.getSigner()

Web3js example:

Once set up, you will be able to query the blockchain for the following:

  • Block number

  • Fuel estimate

  • Smart contract events

  • Network id

and more...

Wallet Function

These libraries provide you with the functionality to create wallets, manage keys, and sign transactions.

An example in HAH is provided here:

Once set up you'll be able to:

  • create accounts

  • send transactions

  • sign transactions

and more...

Interact with smart contract functions

JavaScript client libraries allow your application to call smart contract functions by reading the Application Binary Interface (ABI) of a compiled contract.

The ABI essentially explains the contract's functions in a JSON format and allows you to use it like a normal JavaScript object.

So the following Solidity contract:\

Would result in the following JSON:

This means you can:

  • Send a transaction to the smart contract and execute its method

  • Call to estimate the gas a method execution will take when executed in the EVM

  • Deploy a contract

And more...

\

Last updated