@ambitlabs/hyperliquid-sdk
Advanced tools
Comparing version 0.0.1-beta4 to 0.0.1-beta5
import { Wallet } from 'ethers'; | ||
import { ExchangeEndpoint, SpotDeployAction } from './ExchangeEndpoint'; | ||
import { ExchangeEndpoint, HyperliquidAction, SpotDeployAction } from './ExchangeEndpoint'; | ||
import { AllMidsResponse, ClearingHouseStateResponse, InfoEndpoint, SpotMetaResponse, VaultDetailsResponse } from './InfoEndpoint'; | ||
@@ -45,7 +45,7 @@ import { Price } from './Price'; | ||
export declare class HyperliquidSDK { | ||
readonly wallet: Wallet; | ||
readonly environment: HyperliquidEnvironment; | ||
readonly exchange: ExchangeEndpoint; | ||
readonly wallet: Wallet | undefined; | ||
readonly exchange: ExchangeEndpoint | undefined; | ||
readonly info: InfoEndpoint; | ||
constructor(wallet: Wallet, environment: HyperliquidEnvironment); | ||
constructor(environment: HyperliquidEnvironment, wallet: Wallet | undefined); | ||
fetchSpotMeta(): Promise<SpotMetaResponse>; | ||
@@ -63,4 +63,4 @@ fetchSpotTokens(): Promise<Record<string, { | ||
fetchMids(): Promise<AllMidsResponse>; | ||
fetchClearinghouseState(): Promise<ClearingHouseStateResponse>; | ||
fetchSpotClearinghouseState(): Promise<Record<string, { | ||
fetchClearinghouseState(user?: string | undefined): Promise<ClearingHouseStateResponse>; | ||
fetchSpotClearinghouseState(user?: string | undefined): Promise<Record<string, { | ||
coin: string; | ||
@@ -73,2 +73,3 @@ token: number; | ||
fetchVaultDetails(options: VaultDetailsOptions): Promise<VaultDetailsResponse>; | ||
executeOrThrow<TResponse>(action: HyperliquidAction): Promise<TResponse>; | ||
spotDeployer(): { | ||
@@ -75,0 +76,0 @@ registerToken2: (options: RegisterToken2Options) => Promise<number>; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.HyperliquidSDK = void 0; | ||
const ethers_1 = require("ethers"); | ||
const Signer_1 = require("./Signer"); | ||
@@ -9,11 +10,11 @@ const ExchangeEndpoint_1 = require("./ExchangeEndpoint"); | ||
class HyperliquidSDK { | ||
environment; | ||
wallet; | ||
environment; | ||
exchange; | ||
info; | ||
constructor(wallet, environment) { | ||
constructor(environment, wallet) { | ||
this.environment = environment; | ||
this.wallet = wallet; | ||
this.environment = environment; | ||
const endpoint = constants_1.HYPERLIQUID_API_URL[this.environment]; | ||
this.exchange = new ExchangeEndpoint_1.ExchangeEndpoint(new Signer_1.Signer(wallet, environment), environment, endpoint); | ||
this.exchange = wallet ? new ExchangeEndpoint_1.ExchangeEndpoint(new Signer_1.Signer(wallet, environment), environment, endpoint) : undefined; | ||
this.info = new InfoEndpoint_1.InfoEndpoint(endpoint); | ||
@@ -36,13 +37,13 @@ } | ||
} | ||
async fetchClearinghouseState() { | ||
async fetchClearinghouseState(user = undefined) { | ||
const response = await this.info.fetch({ | ||
type: 'clearinghouseState', | ||
user: this.wallet.address, | ||
user: user ?? (this.wallet ? this.wallet.address : ethers_1.ZeroAddress), | ||
}); | ||
return response; | ||
} | ||
async fetchSpotClearinghouseState() { | ||
async fetchSpotClearinghouseState(user = undefined) { | ||
const response = await this.info.fetch({ | ||
type: 'spotClearinghouseState', | ||
user: this.wallet.address, | ||
user: user ?? (this.wallet ? this.wallet.address : ethers_1.ZeroAddress), | ||
}); | ||
@@ -60,9 +61,15 @@ return response.balances.reduce((previous, current) => { | ||
vaultAddress: options.vault, | ||
user: options.user ?? this.wallet.address, | ||
user: options.user ?? (this.wallet ? this.wallet.address : ethers_1.ZeroAddress), | ||
}); | ||
} | ||
async executeOrThrow(action) { | ||
if (this.exchange === undefined) { | ||
throw Error(); | ||
} | ||
return this.throwOnError(this.exchange.execute(action)); | ||
} | ||
spotDeployer() { | ||
return { | ||
registerToken2: async (options) => { | ||
const response = await this.throwOnError(this.exchange.execute({ | ||
const response = await this.executeOrThrow({ | ||
type: 'spotDeploy', | ||
@@ -72,7 +79,7 @@ registerToken2: { | ||
}, | ||
})); | ||
}); | ||
return response.data; | ||
}, | ||
userGenesis: (options) => { | ||
return this.throwOnError(this.exchange.execute({ | ||
return this.executeOrThrow({ | ||
type: 'spotDeploy', | ||
@@ -82,6 +89,6 @@ userGenesis: { | ||
}, | ||
})); | ||
}); | ||
}, | ||
registerSpot: async (options) => { | ||
const response = await this.throwOnError(this.exchange.execute({ | ||
const response = await this.executeOrThrow({ | ||
type: 'spotDeploy', | ||
@@ -91,7 +98,7 @@ registerSpot: { | ||
}, | ||
})); | ||
}); | ||
return response.data; | ||
}, | ||
registerHyperliquidity: async (options) => { | ||
const response = await this.throwOnError(this.exchange.execute({ | ||
const response = await this.executeOrThrow({ | ||
type: 'spotDeploy', | ||
@@ -103,7 +110,7 @@ registerHyperliquidity: { | ||
}, | ||
})); | ||
}); | ||
console.log(response); | ||
}, | ||
genesis: async (options) => { | ||
const response = await this.throwOnError(this.exchange.execute({ | ||
const response = await this.executeOrThrow({ | ||
type: 'spotDeploy', | ||
@@ -113,7 +120,7 @@ genesis: { | ||
}, | ||
})); | ||
}); | ||
console.log(response); | ||
}, | ||
setEvmContract: (options) => { | ||
return this.throwOnError(this.exchange.execute({ | ||
return this.executeOrThrow({ | ||
type: 'spotDeploy', | ||
@@ -124,3 +131,3 @@ setEvmContract: { | ||
}, | ||
})); | ||
}); | ||
}, | ||
@@ -130,6 +137,6 @@ }; | ||
spotSend(options) { | ||
return this.throwOnError(this.exchange.execute({ | ||
return this.executeOrThrow({ | ||
type: 'spotSend', | ||
...options, | ||
})); | ||
}); | ||
} | ||
@@ -150,7 +157,7 @@ async marketOrder(options) { | ||
}; | ||
const response = await this.throwOnError(this.exchange.execute({ | ||
const response = await this.executeOrThrow({ | ||
type: 'order', | ||
orders: [order], | ||
grouping: 'na', | ||
})); | ||
}); | ||
return response.data.statuses[0]; | ||
@@ -157,0 +164,0 @@ } |
import { Wallet } from 'ethers'; | ||
import { ExchangeEndpoint, SpotDeployAction } from './ExchangeEndpoint'; | ||
import { ExchangeEndpoint, HyperliquidAction, SpotDeployAction } from './ExchangeEndpoint'; | ||
import { AllMidsResponse, ClearingHouseStateResponse, InfoEndpoint, SpotMetaResponse, VaultDetailsResponse } from './InfoEndpoint'; | ||
@@ -45,7 +45,7 @@ import { Price } from './Price'; | ||
export declare class HyperliquidSDK { | ||
readonly wallet: Wallet; | ||
readonly environment: HyperliquidEnvironment; | ||
readonly exchange: ExchangeEndpoint; | ||
readonly wallet: Wallet | undefined; | ||
readonly exchange: ExchangeEndpoint | undefined; | ||
readonly info: InfoEndpoint; | ||
constructor(wallet: Wallet, environment: HyperliquidEnvironment); | ||
constructor(environment: HyperliquidEnvironment, wallet: Wallet | undefined); | ||
fetchSpotMeta(): Promise<SpotMetaResponse>; | ||
@@ -63,4 +63,4 @@ fetchSpotTokens(): Promise<Record<string, { | ||
fetchMids(): Promise<AllMidsResponse>; | ||
fetchClearinghouseState(): Promise<ClearingHouseStateResponse>; | ||
fetchSpotClearinghouseState(): Promise<Record<string, { | ||
fetchClearinghouseState(user?: string | undefined): Promise<ClearingHouseStateResponse>; | ||
fetchSpotClearinghouseState(user?: string | undefined): Promise<Record<string, { | ||
coin: string; | ||
@@ -73,2 +73,3 @@ token: number; | ||
fetchVaultDetails(options: VaultDetailsOptions): Promise<VaultDetailsResponse>; | ||
executeOrThrow<TResponse>(action: HyperliquidAction): Promise<TResponse>; | ||
spotDeployer(): { | ||
@@ -75,0 +76,0 @@ registerToken2: (options: RegisterToken2Options) => Promise<number>; |
@@ -0,1 +1,2 @@ | ||
import { ZeroAddress } from 'ethers'; | ||
import { Signer } from './Signer'; | ||
@@ -6,11 +7,11 @@ import { ExchangeEndpoint, } from './ExchangeEndpoint'; | ||
export class HyperliquidSDK { | ||
environment; | ||
wallet; | ||
environment; | ||
exchange; | ||
info; | ||
constructor(wallet, environment) { | ||
constructor(environment, wallet) { | ||
this.environment = environment; | ||
this.wallet = wallet; | ||
this.environment = environment; | ||
const endpoint = HYPERLIQUID_API_URL[this.environment]; | ||
this.exchange = new ExchangeEndpoint(new Signer(wallet, environment), environment, endpoint); | ||
this.exchange = wallet ? new ExchangeEndpoint(new Signer(wallet, environment), environment, endpoint) : undefined; | ||
this.info = new InfoEndpoint(endpoint); | ||
@@ -33,13 +34,13 @@ } | ||
} | ||
async fetchClearinghouseState() { | ||
async fetchClearinghouseState(user = undefined) { | ||
const response = await this.info.fetch({ | ||
type: 'clearinghouseState', | ||
user: this.wallet.address, | ||
user: user ?? (this.wallet ? this.wallet.address : ZeroAddress), | ||
}); | ||
return response; | ||
} | ||
async fetchSpotClearinghouseState() { | ||
async fetchSpotClearinghouseState(user = undefined) { | ||
const response = await this.info.fetch({ | ||
type: 'spotClearinghouseState', | ||
user: this.wallet.address, | ||
user: user ?? (this.wallet ? this.wallet.address : ZeroAddress), | ||
}); | ||
@@ -57,9 +58,15 @@ return response.balances.reduce((previous, current) => { | ||
vaultAddress: options.vault, | ||
user: options.user ?? this.wallet.address, | ||
user: options.user ?? (this.wallet ? this.wallet.address : ZeroAddress), | ||
}); | ||
} | ||
async executeOrThrow(action) { | ||
if (this.exchange === undefined) { | ||
throw Error(); | ||
} | ||
return this.throwOnError(this.exchange.execute(action)); | ||
} | ||
spotDeployer() { | ||
return { | ||
registerToken2: async (options) => { | ||
const response = await this.throwOnError(this.exchange.execute({ | ||
const response = await this.executeOrThrow({ | ||
type: 'spotDeploy', | ||
@@ -69,7 +76,7 @@ registerToken2: { | ||
}, | ||
})); | ||
}); | ||
return response.data; | ||
}, | ||
userGenesis: (options) => { | ||
return this.throwOnError(this.exchange.execute({ | ||
return this.executeOrThrow({ | ||
type: 'spotDeploy', | ||
@@ -79,6 +86,6 @@ userGenesis: { | ||
}, | ||
})); | ||
}); | ||
}, | ||
registerSpot: async (options) => { | ||
const response = await this.throwOnError(this.exchange.execute({ | ||
const response = await this.executeOrThrow({ | ||
type: 'spotDeploy', | ||
@@ -88,7 +95,7 @@ registerSpot: { | ||
}, | ||
})); | ||
}); | ||
return response.data; | ||
}, | ||
registerHyperliquidity: async (options) => { | ||
const response = await this.throwOnError(this.exchange.execute({ | ||
const response = await this.executeOrThrow({ | ||
type: 'spotDeploy', | ||
@@ -100,7 +107,7 @@ registerHyperliquidity: { | ||
}, | ||
})); | ||
}); | ||
console.log(response); | ||
}, | ||
genesis: async (options) => { | ||
const response = await this.throwOnError(this.exchange.execute({ | ||
const response = await this.executeOrThrow({ | ||
type: 'spotDeploy', | ||
@@ -110,7 +117,7 @@ genesis: { | ||
}, | ||
})); | ||
}); | ||
console.log(response); | ||
}, | ||
setEvmContract: (options) => { | ||
return this.throwOnError(this.exchange.execute({ | ||
return this.executeOrThrow({ | ||
type: 'spotDeploy', | ||
@@ -121,3 +128,3 @@ setEvmContract: { | ||
}, | ||
})); | ||
}); | ||
}, | ||
@@ -127,6 +134,6 @@ }; | ||
spotSend(options) { | ||
return this.throwOnError(this.exchange.execute({ | ||
return this.executeOrThrow({ | ||
type: 'spotSend', | ||
...options, | ||
})); | ||
}); | ||
} | ||
@@ -147,7 +154,7 @@ async marketOrder(options) { | ||
}; | ||
const response = await this.throwOnError(this.exchange.execute({ | ||
const response = await this.executeOrThrow({ | ||
type: 'order', | ||
orders: [order], | ||
grouping: 'na', | ||
})); | ||
}); | ||
return response.data.statuses[0]; | ||
@@ -154,0 +161,0 @@ } |
{ | ||
"name": "@ambitlabs/hyperliquid-sdk", | ||
"version": "0.0.1-beta4", | ||
"version": "0.0.1-beta5", | ||
"types": "./dist/esm/index.d.ts", | ||
@@ -5,0 +5,0 @@ "module": "./dist/esm/index.js", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
91930
1684