convert and parse binance margin account structure

This commit is contained in:
c9s 2021-02-28 14:58:43 +08:00
parent 32c2780b16
commit 3e616c5fac
3 changed files with 52 additions and 2 deletions

View File

@ -62,6 +62,34 @@ func toGlobalIsolatedMarginAccount(account *binance.IsolatedMarginAccount) *type
}
}
func toGlobalMarginUserAssets(userAssets []binance.UserAsset) (retAssets []types.MarginUserAsset) {
for _, asset := range userAssets {
retAssets = append(retAssets, types.MarginUserAsset{
Asset: asset.Asset,
Borrowed: fixedpoint.MustNewFromString(asset.Borrowed),
Free: fixedpoint.MustNewFromString(asset.Free),
Interest: fixedpoint.MustNewFromString(asset.Interest),
Locked: fixedpoint.MustNewFromString(asset.Locked),
NetAsset: fixedpoint.MustNewFromString(asset.NetAsset),
})
}
return retAssets
}
func toGlobalMarginAccount(account *binance.MarginAccount) *types.MarginAccount {
return &types.MarginAccount{
BorrowEnabled: account.BorrowEnabled,
MarginLevel: fixedpoint.MustNewFromString(account.MarginLevel),
TotalAssetOfBTC: fixedpoint.MustNewFromString(account.TotalAssetOfBTC),
TotalLiabilityOfBTC: fixedpoint.MustNewFromString(account.TotalLiabilityOfBTC),
TotalNetAssetOfBTC: fixedpoint.MustNewFromString(account.TotalNetAssetOfBTC),
TradeEnabled: account.TradeEnabled,
TransferEnabled: account.TransferEnabled,
UserAssets: toGlobalMarginUserAssets(account.UserAssets),
}
}
func toGlobalTicker(stats *binance.PriceChangeStats) types.Ticker {
return types.Ticker{
Volume: util.MustParseFloat(stats.Volume),

View File

@ -170,13 +170,13 @@ func (e *Exchange) NewStream() types.Stream {
return stream
}
func (e *Exchange) QueryMarginAccount(ctx context.Context) (*binance.MarginAccount, error) {
func (e *Exchange) QueryMarginAccount(ctx context.Context) (*types.MarginAccount, error) {
account, err := e.Client.NewGetMarginAccountService().Do(ctx)
if err != nil {
return nil, err
}
return account, nil
return toGlobalMarginAccount(account), nil
}
func (e *Exchange) QueryIsolatedMarginAccount(ctx context.Context, symbols ...string) (*types.IsolatedMarginAccount, error) {

View File

@ -29,6 +29,28 @@ func (e *MarginSettings) UseIsolatedMargin(symbol string) {
e.IsolatedMarginSymbol = symbol
}
// MarginAccount is for the cross margin account
type MarginAccount struct {
BorrowEnabled bool `json:"borrowEnabled"`
MarginLevel fixedpoint.Value `json:"marginLevel"`
TotalAssetOfBTC fixedpoint.Value `json:"totalAssetOfBtc"`
TotalLiabilityOfBTC fixedpoint.Value `json:"totalLiabilityOfBtc"`
TotalNetAssetOfBTC fixedpoint.Value `json:"totalNetAssetOfBtc"`
TradeEnabled bool `json:"tradeEnabled"`
TransferEnabled bool `json:"transferEnabled"`
UserAssets []MarginUserAsset `json:"userAssets"./examples/binance-margin`
}
// MarginUserAsset define user assets of margin account
type MarginUserAsset struct {
Asset string `json:"asset"`
Borrowed fixedpoint.Value `json:"borrowed"`
Free fixedpoint.Value `json:"free"`
Interest fixedpoint.Value `json:"interest"`
Locked fixedpoint.Value `json:"locked"`
NetAsset fixedpoint.Value `json:"netAsset"`
}
// IsolatedMarginAccount defines isolated user assets of margin account
type IsolatedMarginAccount struct {
TotalAssetOfBTC fixedpoint.Value `json:"totalAssetOfBtc"`