Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
* develop:
  fix: binance wallet (#1382)
  • Loading branch information
trkhoi committed Apr 19, 2024
2 parents a4a802c + 5616b0d commit b182332
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 2 deletions.
23 changes: 21 additions & 2 deletions pkg/entities/wallet_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,18 +237,37 @@ func (e *Entity) FormatAsset(assets []response.BinanceUserAssetResponse) ([]resp
continue
}

assetValue, err := strconv.ParseFloat(asset.Free, 64)
assetFree, err := strconv.ParseFloat(asset.Free, 64)
if err != nil {
e.log.Error(err, "[entities.SumarizeBinanceAsset] Failed to parse asset value")
return nil, err
}

assetLocked, err := strconv.ParseFloat(asset.Locked, 64)
if err != nil {
e.log.Error(err, "[entities.SumarizeBinanceAsset] Failed to parse asset value")
return nil, err
}

assetValue := assetFree + assetLocked

btcValuation, err := strconv.ParseFloat(asset.BtcValuation, 64)
if err != nil {
e.log.Error(err, "[entities.SumarizeBinanceAsset] Failed to parse asset value")
return nil, err
}

usdBal := btcValuation * btcPrice["bitcoin"]
if usdBal <= 0 {
binancePrice, err := e.svc.Binance.GetPrice(asset.Asset + "USDT")
if err != nil {
e.log.Error(err, "[entities.SumarizeBinanceAsset] Failed to get binance price")
return nil, err
}

usdBal, _ = strconv.ParseFloat(binancePrice.Price, 64)
}

itm := response.WalletAssetData{
AssetBalance: assetValue,
Amount: util.FloatToString(fmt.Sprint(assetValue), 18),
Expand All @@ -257,7 +276,7 @@ func (e *Entity) FormatAsset(assets []response.BinanceUserAssetResponse) ([]resp
Decimal: 18,
Price: btcValuation * btcPrice["bitcoin"] / assetValue,
},
UsdBalance: btcValuation * btcPrice["bitcoin"],
UsdBalance: usdBal,
}

if asset.DetailLending != nil && asset.DetailLending.Amount != "0" {
Expand Down
5 changes: 5 additions & 0 deletions pkg/response/binance.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,3 +212,8 @@ type BinanceFuturePositionInfo struct {
PositionSide string `json:"positionSide"`
UpdateTime int64 `json:"updateTime"`
}

type BinanceApiTickerPriceResponse struct {
Symbol string `json:"symbol"`
Price string `json:"price"`
}
22 changes: 22 additions & 0 deletions pkg/service/binance/adapter/adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -312,3 +312,25 @@ func GetFutureAccountInfo(apiKey, apiSecret string) (fAccountBal []response.Bina

return fAccountBal, nil
}

func GetTickerPrice(symbol string) (price *response.BinanceApiTickerPriceResponse, err error) {
// http request
req, err := http.NewRequest("GET", url+"/api/v3/ticker/price?symbol="+symbol, nil)
if err != nil {
return nil, err
}

resp, err := do(req, "", 0)
if err != nil {
return nil, err
}
defer resp.Body.Close()

// decode response json
err = json.NewDecoder(resp.Body).Decode(&price)
if err != nil {
return nil, err
}

return price, nil
}
4 changes: 4 additions & 0 deletions pkg/service/binance/binance.go
Original file line number Diff line number Diff line change
Expand Up @@ -509,3 +509,7 @@ func (b *Binance) GetFutureAccountInfo(apiKey, apiSecret string) ([]response.Bin

return res, nil
}

func (b *Binance) GetPrice(symbol string) (*response.BinanceApiTickerPriceResponse, error) {
return badapter.GetTickerPrice(symbol)
}
1 change: 1 addition & 0 deletions pkg/service/binance/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,5 @@ type Service interface {
GetFutureAccountBalance(apiKey, apiSecret string) ([]response.BinanceFutureAccountBalance, error)
GetFutureAccount(apiKey, apiSecret string) (*response.BinanceFutureAccount, error)
GetFutureAccountInfo(apiKey, apiSecret string) ([]response.BinanceFuturePositionInfo, error)
GetPrice(symbol string) (*response.BinanceApiTickerPriceResponse, error)
}

0 comments on commit b182332

Please sign in to comment.