-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathevm_example.ts
More file actions
118 lines (98 loc) · 4.06 KB
/
evm_example.ts
File metadata and controls
118 lines (98 loc) · 4.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#!/usr/bin/env npx ts-node
// @ts-nocheck
/**
* HyperEVM API Example - Interact with Hyperliquid's EVM chain.
*
* This example shows how to query the Hyperliquid EVM chain (chain ID 999 mainnet, 998 testnet).
*
* Requirements:
* npm install hyperliquid-sdk
*
* Usage:
* export ENDPOINT="https://your-endpoint.example.com/TOKEN"
* npx ts-node evm_example.ts
*/
import { HyperliquidSDK } from '@quicknode/hyperliquid-sdk';
// Get endpoint from environment
const ENDPOINT = process.env.ENDPOINT;
if (!ENDPOINT) {
console.error("Error: Set ENDPOINT environment variable");
console.error(" export ENDPOINT='https://your-endpoint.example.com/TOKEN'");
process.exit(1);
}
async function main() {
console.log("Hyperliquid EVM API Example");
console.log("=".repeat(50));
console.log(`Endpoint: ${ENDPOINT.slice(0, 50)}...`);
console.log();
// Create SDK client
const sdk = new HyperliquidSDK(ENDPOINT);
// ==========================================================================
// Chain Info
// ==========================================================================
console.log("Chain Info");
console.log("-".repeat(30));
// Get chain ID
const chainId = await sdk.evm.chainId();
console.log(`Chain ID: ${chainId}`);
console.log(`Network: ${chainId === 999 ? 'Mainnet' : chainId === 998 ? 'Testnet' : 'Unknown'}`);
// Get latest block number
const blockNum = await sdk.evm.blockNumber();
console.log(`Latest block: ${blockNum}`);
// Get gas price
const gasPrice = await sdk.evm.gasPrice();
const gasGwei = Number(gasPrice) / 1e9;
console.log(`Gas price: ${gasGwei.toFixed(2)} Gwei`);
console.log();
// ==========================================================================
// Account Balance
// ==========================================================================
console.log("Account Balance");
console.log("-".repeat(30));
// Example address - replace with your address
const address = "0x0000000000000000000000000000000000000000";
const balanceWei = await sdk.evm.getBalance(address);
const balanceEth = Number(balanceWei) / 1e18;
console.log(`Address: ${address}`);
console.log(`Balance: ${balanceEth.toFixed(6)} HYPE`);
console.log();
// ==========================================================================
// Block Data
// ==========================================================================
console.log("Block Data");
console.log("-".repeat(30));
// Get latest block
const block = await sdk.evm.getBlockByNumber(blockNum);
if (block) {
const b = block as Record<string, unknown>;
console.log(`Block ${blockNum}:`);
console.log(` Hash: ${String(b.hash || '?').slice(0, 20)}...`);
console.log(` Parent: ${String(b.parentHash || '?').slice(0, 20)}...`);
console.log(` Timestamp: ${b.timestamp || '?'}`);
console.log(` Gas Used: ${parseInt(String(b.gasUsed || '0x0'), 16).toLocaleString()}`);
const txs = b.transactions as unknown[] || [];
console.log(` Transactions: ${txs.length}`);
}
console.log();
// ==========================================================================
// Transaction Count
// ==========================================================================
console.log("Transaction Count");
console.log("-".repeat(30));
const txCount = await sdk.evm.getTransactionCount(address);
console.log(`Nonce for ${address.slice(0, 10)}...: ${txCount}`);
console.log();
// ==========================================================================
// Smart Contract Call (Example: ERC20 balanceOf)
// ==========================================================================
console.log("Smart Contract Call");
console.log("-".repeat(30));
// Example: Read a contract (this is just a demonstration)
// In real usage, you'd use actual contract addresses and proper ABI encoding
console.log(" (Contract call example would go here)");
console.log(" Use sdk.evm.call() with proper contract address and data");
console.log();
console.log("=".repeat(50));
console.log("Done!");
}
main().catch(console.error);