- Install dependencies:
npm install
- Copy the example environment file and provide your HGraph API key:
cp .env.example .env # Edit .env and replace <YOUR_API_KEY> with your key
Compile the TypeScript source into the dist
directory:
npm run build
Execute the compiled code directly:
node dist/index.js
# or
npm run start
When successful you should see output similar to:
consensus_timestamp was about <seconds> seconds ago
HGRAPH_API_KEY
– your API key used by the GraphQL client. This value is loaded from.env
at runtime.
src/index.ts
demonstrates subscribing to the latest transaction using
@hgraph.io/sdk
:
const LatestTransactionSubscription = `
subscription LatestTransaction {
transaction(limit: 1, order_by: {consensus_timestamp: desc}) {
consensus_timestamp
}
}`
const subscription = client.subscribe(LatestTransactionSubscription, {
next: ({ data }) => {
const { consensus_timestamp } = data.transaction[0]
const diff = secondsSinceConsensus(consensus_timestamp, BigInt(Date.now()))
console.log(`consensus_timestamp was about ${diff} seconds ago`)
}
})
If you encounter connection errors ensure the API key is valid and that your network allows outbound connections to HGraph.
src/contractType.ts
shows how to determine if a contract address implements the ERC-20 or ERC-721 interface using ethers. The exported detectContractType
function returns "erc20"
, "erc721"
, or "unknown"
.
The file also contains showContractTypeDemo
which downloads token lists from the
Hiero Mirror Node Explorer
repository and prints the detected type of a couple of sample addresses.
Run the demo with:
npm run demo:contractType
Run the following command to build the project and execute a script demonstrating every function in the ERC20 and ERC721 wrappers:
npm run start:wrappers
The script src/ercWrappersDemo.ts
shows how to read token information and invoke transfer functions using an ethers Wallet
.