-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add master package for balance calculation
- Loading branch information
Showing
6 changed files
with
150 additions
and
80 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,9 @@ | ||
go 1.22 | ||
go 1.22.0 | ||
|
||
use ( | ||
./binance | ||
./cmd/ccy-cli | ||
./master | ||
./max | ||
./okx | ||
) |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
package master | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/dasbd72/asset-management/binance" | ||
"github.com/dasbd72/asset-management/max" | ||
"github.com/dasbd72/asset-management/okx" | ||
) | ||
|
||
type ( | ||
Balance struct { | ||
Usdt float64 | ||
Twd float64 | ||
} | ||
) | ||
|
||
func (c *Client) GetBalance(ctx context.Context) (*Balance, error) { | ||
var ( | ||
totalBalanceUsdt float64 | ||
totalBalanceTwd float64 | ||
) | ||
funcs := []func() error{ | ||
func() error { | ||
sum := 0.0 | ||
wallet, err := c.binanceClient.GetUserWalletBalance(ctx) | ||
if err != nil { | ||
return err | ||
} | ||
for _, w := range *wallet { | ||
sum += w.Balance.Float64() | ||
} | ||
|
||
averagePrice, err := c.binanceClient.GetAveragePrice(ctx, binance.NewGetAveragePriceRequest("BTCUSDT")) | ||
if err != nil { | ||
return err | ||
} | ||
btcPrice := averagePrice.Price.Float64() | ||
|
||
totalBalanceUsdt += sum * btcPrice | ||
return nil | ||
}, | ||
func() error { | ||
sum := 0.0 | ||
wallet, err := c.okxClient.GetBalance(ctx, okx.NewGetBalanceRequest()) | ||
if err != nil { | ||
return err | ||
} | ||
for _, w := range wallet.Balances { | ||
sum += w.TotalEq.Float64() | ||
} | ||
funding, err := c.okxClient.GetFundingBalances(ctx, okx.NewGetFundingBalancesRequest()) | ||
if err != nil { | ||
return err | ||
} | ||
for _, f := range funding.Balances { | ||
sum += f.Bal.Float64() | ||
} | ||
savings, err := c.okxClient.GetSavingBalance(ctx, okx.NewGetSavingBalanceRequest()) | ||
if err != nil { | ||
return err | ||
} | ||
for _, s := range savings.Balances { | ||
price := 1.0 | ||
if s.Ccy != "USDT" { | ||
ticker, err := c.okxClient.GetTicker(ctx, okx.NewGetTickerRequest(s.Ccy+"-USDT")) | ||
if err != nil { | ||
return err | ||
} | ||
price = ticker.Tickers[0].Last.Float64() | ||
} | ||
sum += s.Amt.Float64() * price | ||
} | ||
|
||
totalBalanceUsdt += sum | ||
return nil | ||
}, | ||
func() error { | ||
usdtToTWD, err := max.GetUsdtToTWD() | ||
if err != nil { | ||
return err | ||
} | ||
totalBalanceTwd = totalBalanceUsdt * usdtToTWD | ||
return nil | ||
}, | ||
} | ||
for _, f := range funcs { | ||
if err := f(); err != nil { | ||
return nil, err | ||
} | ||
} | ||
return &Balance{ | ||
Usdt: totalBalanceUsdt, | ||
Twd: totalBalanceTwd, | ||
}, nil | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package master | ||
|
||
import ( | ||
"github.com/dasbd72/asset-management/binance" | ||
"github.com/dasbd72/asset-management/okx" | ||
) | ||
|
||
type Client struct { | ||
binanceClient *binance.Client | ||
okxClient *okx.Client | ||
} | ||
|
||
func NewClient(binanceClient *binance.Client, okxClient *okx.Client) *Client { | ||
return &Client{ | ||
binanceClient: binanceClient, | ||
okxClient: okxClient, | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
module github.com/dasbd72/asset-management/master | ||
|
||
go 1.22.0 | ||
|
||
require ( | ||
github.com/dasbd72/asset-management/binance v0.0.0-20240219071459-322ce6b28840 | ||
github.com/dasbd72/asset-management/max v0.0.0-20240219071459-322ce6b28840 | ||
github.com/dasbd72/asset-management/okx v0.0.0-20240219071459-322ce6b28840 | ||
) |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
github.com/dasbd72/asset-management/binance v0.0.0-20240219071459-322ce6b28840 h1:LErZfCFuHhDtbF3Jw0D6DdqwCDuO83/avVp5AqVA4S8= | ||
github.com/dasbd72/asset-management/binance v0.0.0-20240219071459-322ce6b28840/go.mod h1:7MWjdfOOk6OwTja1SeVFRkyhHB4OkJqoloL9eTDwDUU= | ||
github.com/dasbd72/asset-management/max v0.0.0-20240219071459-322ce6b28840 h1:I3b7yD8vASGD6tvm+6II1SlV81VYWRsHa/EDEjqcJQI= | ||
github.com/dasbd72/asset-management/max v0.0.0-20240219071459-322ce6b28840/go.mod h1:nQOAzru+GswuHXPTz2Y4KNxMtryD2FxppkqkHkRQopo= | ||
github.com/dasbd72/asset-management/okx v0.0.0-20240219071459-322ce6b28840 h1:Gi7JQtwjfO7cAcFn8r6i4ipAqfYK000yWiwFSueTwgY= | ||
github.com/dasbd72/asset-management/okx v0.0.0-20240219071459-322ce6b28840/go.mod h1:pqzDe3U2v2iB61vcAPfQL6KQ0BHBwwWYydZGGVOLbgw= | ||
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI= | ||
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= |