Skip to content

Commit 5d88441

Browse files
Merge pull request #6 from Logarithm-Labs/feature-add-deposits
Info API: Added GetDeposits method
2 parents 93688dc + 08a0212 commit 5d88441

File tree

3 files changed

+54
-0
lines changed

3 files changed

+54
-0
lines changed

hyperliquid/exchange_types.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,12 +191,19 @@ type FundingDelta struct {
191191
}
192192

193193
type Withdrawal struct {
194+
Time int64 `json:"time"`
194195
Hash string `json:"hash"`
195196
Amount float64 `json:"usdc"`
196197
Fee float64 `json:"fee"`
197198
Nonce int64 `json:"nonce"`
198199
}
199200

201+
type Deposit struct {
202+
Hash string `json:"hash,omitempty"`
203+
Time int64 `json:"time,omitempty"`
204+
Amount float64 `json:"usdc,omitempty"`
205+
}
206+
200207
type WithdrawAction struct {
201208
Type string `msgpack:"type" json:"type"`
202209
Destination string `msgpack:"destination" json:"destination"`

hyperliquid/info_service.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,7 @@ func (api *InfoAPI) GetWithdrawals(address string) (*[]Withdrawal, error) {
240240
for _, update := range *updates {
241241
if update.Delta.Type == "withdraw" {
242242
withrawal := Withdrawal{
243+
Time: update.Time,
243244
Hash: update.Hash,
244245
Amount: update.Delta.Usdc,
245246
Fee: update.Delta.Fee,
@@ -258,6 +259,35 @@ func (api *InfoAPI) GetAccountWithdrawals() (*[]Withdrawal, error) {
258259
return api.GetWithdrawals(api.AccountAddress())
259260
}
260261

262+
// Helper function to get the deposits of the given address
263+
// By default returns last 90 days
264+
func (api *InfoAPI) GetDeposits(address string) (*[]Deposit, error) {
265+
startTime, endTime := GetDefaultTimeRange()
266+
updates, err := api.GetNonFundingUpdates(address, startTime, endTime)
267+
if err != nil {
268+
return nil, err
269+
}
270+
var deposits []Deposit
271+
for _, update := range *updates {
272+
if update.Delta.Type == "deposit" {
273+
deposit := Deposit{
274+
Hash: update.Hash,
275+
Amount: update.Delta.Usdc,
276+
Time: update.Time,
277+
}
278+
deposits = append(deposits, deposit)
279+
}
280+
}
281+
return &deposits, nil
282+
}
283+
284+
// Helper function to get the deposits of the account address
285+
// The same as GetDeposits but user is set to the account address
286+
// Check AccountAddress() or SetAccountAddress() if there is a need to set the account address
287+
func (api *InfoAPI) GetAccountDeposits() (*[]Deposit, error) {
288+
return api.GetDeposits(api.AccountAddress())
289+
}
290+
261291
// Helper function to build a map of asset names to asset info
262292
// It is used to get the assetId for a given asset name
263293
func (api *InfoAPI) BuildMetaMap() (map[string]AssetInfo, error) {

hyperliquid/info_test.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,23 @@ func TestInfoAPI_GetAccountWithdrawals(t *testing.T) {
247247
t.Logf("GetAccountWithdrawals() = %v", res)
248248
}
249249

250+
func TestInfoAPI_GetAccountDeposits(t *testing.T) {
251+
api := GetInfoAPI()
252+
res, err := api.GetAccountDeposits()
253+
if err != nil {
254+
t.Errorf("GetAccountDeposits() error = %v", err)
255+
}
256+
if len(*res) == 0 {
257+
t.Errorf("GetAccountDeposits() len = %v, want > %v", res, 0)
258+
}
259+
for _, deposit := range *res {
260+
if deposit.Amount == 0 {
261+
t.Errorf("deposit.Amount = %v, want > %v", deposit.Amount, 0)
262+
}
263+
}
264+
t.Logf("GetAccountDeposits() = %v", res)
265+
}
266+
250267
func TestInfoAPI_GetMarketPx(t *testing.T) {
251268
api := GetInfoAPI()
252269
res, err := api.GetMartketPx("BTC")

0 commit comments

Comments
 (0)