Created
June 19, 2025 07:26
-
-
Save sennett-lau/6b050cf4f4e465553d71cf188e3c9b41 to your computer and use it in GitHub Desktop.
@nktkas/hyperliquid Usage sample
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * Creating Info API and Exchange API client | |
| * Use `isTestnet: true` option for Testnet | |
| */ | |
| import { HttpTransport, InfoClient, ExchangeClient } from "@nktkas/hyperliquid"; | |
| import { privateKeyToAccount } from "viem/accounts"; | |
| const MASTER_PRIVATE_KEY = '' | |
| const main = async () => { | |
| // include `{ isTestnet: true }` for testnet | |
| const transport = new HttpTransport({ isTestnet: true }) | |
| // Public API, no wallet signing is required | |
| const infoClient = new InfoClient({ | |
| transport, | |
| }) | |
| // Exchange API require a wallet for signing, `{ isTestnet: true }` for testnet | |
| const wallet = privateKeyToAccount(`0x${MASTER_PRIVATE_KEY}`); | |
| const exchClient = new ExchangeClient({ | |
| transport, | |
| wallet, | |
| isTestnet: true, | |
| }) | |
| } | |
| main().then(() => { | |
| console.log('done') | |
| }).catch((error) => { | |
| console.error(error) | |
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * Using Info API to retrieve user portfolio | |
| */ | |
| import { HttpTransport, InfoClient } from "@nktkas/hyperliquid"; | |
| const TARGET_WALLET_ADDRESS = '' | |
| const main = async () => { | |
| const transport = new HttpTransport({ isTestnet: true }) | |
| const infoClient = new InfoClient({ | |
| transport, | |
| }) | |
| const res = await infoClient.portfolio({ | |
| user: TARGET_WALLET_ADDRESS, | |
| }) | |
| console.log(JSON.stringify(res, null, 2)) | |
| } | |
| main().then(() => { | |
| console.log('done') | |
| }).catch((error) => { | |
| console.error(error) | |
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * Using the master account wallet to revoke another address's API Wallet access | |
| */ | |
| import { HttpTransport, ExchangeClient } from "@nktkas/hyperliquid"; | |
| import { privateKeyToAccount } from "viem/accounts"; | |
| const MASTER_PRIVATE_KEY = '' | |
| const NULL_ADDRESS = '0x0000000000000000000000000000000000000000' | |
| const main = async () => { | |
| const wallet = privateKeyToAccount(`0x${MASTER_PRIVATE_KEY}`); | |
| const transport = new HttpTransport({ isTestnet: true }) | |
| const exchClient = new ExchangeClient({ | |
| transport, | |
| wallet, | |
| isTestnet: true, | |
| }) | |
| const agentAddress = NULL_ADDRESS | |
| const agentName = 'Agent 1' // authorized agent name | |
| await exchClient.approveAgent({ | |
| agentAddress, | |
| agentName, | |
| }) | |
| } | |
| main().then(() => { | |
| console.log('done') | |
| }).catch((error) => { | |
| console.error(error) | |
| }) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment