Skip to content

Commit 9ad011b

Browse files
authored
Merge pull request #23 from vyomshm/feat/add-vault-methods
added vault create, modify and distribute methods
2 parents 0d1a189 + c1ba438 commit 9ad011b

4 files changed

Lines changed: 142 additions & 3 deletions

File tree

actions.go

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ type CancelAction struct {
2020
// CancelOrderWire has
2121
// See: https://github.com/hyperliquid-dex/hyperliquid-python-sdk/blob/f19056ca1b65cc15a019d92dffa9ada887b3d808/hyperliquid/exchange.py#L305-L310
2222
type CancelByCloidWire struct {
23-
Asset int `json:"asset" msgpack:"asset"`
23+
Asset int `json:"asset" msgpack:"asset"`
2424
ClientID string `json:"cloid" msgpack:"cloid"`
2525
}
2626

@@ -71,6 +71,29 @@ type VaultUsdTransferAction struct {
7171
Usd int `json:"usd" msgpack:"usd"`
7272
}
7373

74+
// CreateVaultAction represents create vault action
75+
type CreateVaultAction struct {
76+
Type string `json:"type" msgpack:"type"`
77+
Name string `json:"name" msgpack:"name"`
78+
Description string `json:"description" msgpack:"description"`
79+
InitialUsd int `json:"initialUsd" msgpack:"initialUsd"`
80+
}
81+
82+
// VaultModifyAction represents vault modify action
83+
type VaultModifyAction struct {
84+
Type string `json:"type" msgpack:"type"`
85+
VaultAddress string `json:"vaultAddress" msgpack:"vaultAddress"`
86+
AllowDeposits bool `json:"allowDeposits" msgpack:"allowDeposits"`
87+
AlwaysCloseOnWithdraw bool `json:"alwaysCloseOnWithdraw" msgpack:"alwaysCloseOnWithdraw"`
88+
}
89+
90+
// VaultDistributeAction represents vault distribute action
91+
type VaultDistributeAction struct {
92+
Type string `json:"type" msgpack:"type"`
93+
VaultAddress string `json:"vaultAddress" msgpack:"vaultAddress"`
94+
Usd int `json:"usd" msgpack:"usd"`
95+
}
96+
7497
// UpdateLeverageAction represents leverage update
7598
type UpdateLeverageAction struct {
7699
Type string `json:"type" msgpack:"type"`

exchange_others.go

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,116 @@ func (e *Exchange) VaultUsdTransfer(
317317
return &result, nil
318318
}
319319

320+
// CreateVault creates a new vault
321+
func (e *Exchange) CreateVault(
322+
name string,
323+
description string,
324+
initialUsd int,
325+
) (*CreateVaultResponse, error) {
326+
timestamp := time.Now().UnixMilli()
327+
328+
action := CreateVaultAction{
329+
Type: "createVault",
330+
Name: name,
331+
Description: description,
332+
InitialUsd: initialUsd,
333+
}
334+
335+
sig, err := SignL1Action(
336+
e.privateKey,
337+
action,
338+
"", // No vault address
339+
timestamp,
340+
e.expiresAfter,
341+
e.client.baseURL == MainnetAPIURL,
342+
)
343+
if err != nil {
344+
return nil, err
345+
}
346+
347+
resp, err := e.postAction(action, sig, timestamp)
348+
if err != nil {
349+
return nil, err
350+
}
351+
352+
var result CreateVaultResponse
353+
if err := json.Unmarshal(resp, &result); err != nil {
354+
return nil, err
355+
}
356+
return &result, nil
357+
}
358+
359+
func (e *Exchange) VaultModify(
360+
vaultAddress string,
361+
allowDeposits bool,
362+
alwaysCloseOnWithdraw bool,
363+
) (*TransferResponse, error) {
364+
timestamp := time.Now().UnixMilli()
365+
366+
action := VaultModifyAction{
367+
Type: "vaultModify",
368+
VaultAddress: vaultAddress,
369+
AllowDeposits: allowDeposits,
370+
AlwaysCloseOnWithdraw: alwaysCloseOnWithdraw,
371+
}
372+
373+
sig, err := SignL1Action(
374+
e.privateKey,
375+
action,
376+
"", // No vault address
377+
timestamp,
378+
e.expiresAfter,
379+
e.client.baseURL == MainnetAPIURL,
380+
)
381+
if err != nil {
382+
return nil, err
383+
}
384+
385+
resp, err := e.postAction(action, sig, timestamp)
386+
if err != nil {
387+
return nil, err
388+
}
389+
390+
var result TransferResponse
391+
if err := json.Unmarshal(resp, &result); err != nil {
392+
return nil, err
393+
}
394+
return &result, nil
395+
}
396+
397+
func (e *Exchange) VaultDistribute(vaultAddress string, usd int) (*TransferResponse, error) {
398+
timestamp := time.Now().UnixMilli()
399+
400+
action := VaultDistributeAction{
401+
Type: "vaultDistribute",
402+
VaultAddress: vaultAddress,
403+
Usd: usd,
404+
}
405+
406+
sig, err := SignL1Action(
407+
e.privateKey,
408+
action,
409+
"", // No vault address
410+
timestamp,
411+
e.expiresAfter,
412+
e.client.baseURL == MainnetAPIURL,
413+
)
414+
if err != nil {
415+
return nil, err
416+
}
417+
418+
resp, err := e.postAction(action, sig, timestamp)
419+
if err != nil {
420+
return nil, err
421+
}
422+
423+
var result TransferResponse
424+
if err := json.Unmarshal(resp, &result); err != nil {
425+
return nil, err
426+
}
427+
return &result, nil
428+
}
429+
320430
// UsdTransfer transfers USD to another address
321431
func (e *Exchange) UsdTransfer(amount float64, destination string) (*TransferResponse, error) {
322432
timestamp := time.Now().UnixMilli()

go.mod

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,15 @@ go 1.23.0
44

55
require (
66
github.com/ethereum/go-ethereum v1.16.2
7+
github.com/google/go-cmp v0.7.0
78
github.com/gorilla/websocket v1.5.3
89
github.com/joho/godotenv v1.5.1
910
github.com/mailru/easyjson v0.9.0
1011
github.com/sonirico/vago v0.8.1
1112
github.com/stretchr/testify v1.10.0
1213
github.com/valyala/fastjson v1.6.4
1314
github.com/vmihailenco/msgpack/v5 v5.4.1
15+
gopkg.in/dnaeon/go-vcr.v4 v4.0.4
1416
)
1517

1618
require (
@@ -22,7 +24,6 @@ require (
2224
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.4.0 // indirect
2325
github.com/ethereum/c-kzg-4844/v2 v2.1.1 // indirect
2426
github.com/ethereum/go-verkle v0.2.2 // indirect
25-
github.com/google/go-cmp v0.7.0 // indirect
2627
github.com/holiman/uint256 v1.3.2 // indirect
2728
github.com/josharian/intern v1.0.0 // indirect
2829
github.com/mattn/go-runewidth v0.0.16 // indirect
@@ -34,6 +35,5 @@ require (
3435
golang.org/x/crypto v0.40.0 // indirect
3536
golang.org/x/sync v0.16.0 // indirect
3637
golang.org/x/sys v0.34.0 // indirect
37-
gopkg.in/dnaeon/go-vcr.v4 v4.0.4 // indirect
3838
gopkg.in/yaml.v3 v3.0.1 // indirect
3939
)

types.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -343,6 +343,12 @@ type ApprovalResponse struct {
343343
Error string `json:"error,omitempty"`
344344
}
345345

346+
type CreateVaultResponse struct {
347+
Status string `json:"status"`
348+
Data string `json:"data,omitempty"`
349+
Error string `json:"error,omitempty"`
350+
}
351+
346352
type CreateSubAccountResponse struct {
347353
Status string `json:"status"`
348354
Data *SubAccount `json:"data,omitempty"`

0 commit comments

Comments
 (0)