add transfer function

This commit is contained in:
zebra 2021-08-07 14:50:12 +08:00
parent c6785be257
commit 2e1400d594
4 changed files with 79 additions and 4 deletions

View File

@ -141,8 +141,9 @@ func (e *Exchange) QueryAccount(ctx context.Context) (*types.Account, error) {
}
a := &types.Account{
MakerCommission: fixedpoint.NewFromFloat(resp.Result.MakerFee),
TakerCommission: fixedpoint.NewFromFloat(resp.Result.TakerFee),
MakerCommission: fixedpoint.NewFromFloat(resp.Result.MakerFee),
TakerCommission: fixedpoint.NewFromFloat(resp.Result.TakerFee),
TotalAccountValue: fixedpoint.NewFromFloat(resp.Result.TotalAccountValue),
}
balances, err := e.QueryAccountBalances(ctx)
@ -456,3 +457,20 @@ func (e *Exchange) QueryTicker(ctx context.Context, symbol string) (*types.Ticke
func (e *Exchange) QueryTickers(ctx context.Context, symbol ...string) (map[string]types.Ticker, error) {
panic("implement me")
}
func (e *Exchange) Transfer(ctx context.Context, coin string, size float64, destination string) (string, error) {
payload := TransferPayload{
Coin: coin,
Size: size,
Source: e.subAccount,
Destination: destination,
}
resp, err := e.newRest().Transfer(ctx, payload)
if err != nil {
return "", err
}
if !resp.Success {
return "", fmt.Errorf("ftx returns transfer failure")
}
return resp.Result.String(), nil
}

View File

@ -18,12 +18,47 @@ import (
"github.com/c9s/bbgo/pkg/util"
)
type transferRequest struct {
*restRequest
}
type TransferPayload struct {
Coin string
Size float64
Source string
Destination string
}
func (r *restRequest) Transfer(ctx context.Context, p TransferPayload) (transferResponse, error) {
resp, err := r.
Method("POST").
ReferenceURL("api/subaccounts/transfer").
Payloads(map[string]interface{}{
"coin": p.Coin,
"size": p.Size,
"source": p.Source,
"destination": p.Destination,
}).
DoAuthenticatedRequest(ctx)
if err != nil {
return transferResponse{}, err
}
var t transferResponse
if err := json.Unmarshal(resp.Body, &t); err != nil {
return transferResponse{}, fmt.Errorf("failed to unmarshal transfer response body to json: %w", err)
}
return t, nil
}
type restRequest struct {
*walletRequest
*orderRequest
*accountRequest
*marketRequest
*fillsRequest
*transferRequest
key, secret string
// Optional sub-account name

View File

@ -1,6 +1,7 @@
package ftx
import (
"fmt"
"strings"
"time"
@ -86,8 +87,9 @@ type accountResponse struct {
}
type account struct {
MakerFee float64 `json:"makerFee"`
TakerFee float64 `json:"takerFee"`
MakerFee float64 `json:"makerFee"`
TakerFee float64 `json:"takerFee"`
TotalAccountValue float64 `json:"totalAccountValue"`
}
type positionsResponse struct {
@ -368,3 +370,21 @@ type fill struct {
FeeCurrency string `json:"feeCurrency"`
Liquidity string `json:"liquidity"`
}
type transferResponse struct {
Success bool `json:"success"`
Result transfer `json:"result"`
}
type transfer struct {
Id uint `json:"id"`
Coin string `json:"coin"`
Size float64 `json:"size"`
Time string `json:"time"`
Notes string `json:"notes"`
Status string `json:"status"`
}
func (t *transfer) String() string {
return fmt.Sprintf("%+v", *t)
}

View File

@ -117,6 +117,8 @@ type Account struct {
TakerFeeRate fixedpoint.Value `json:"takerFeeRate,omitempty"`
AccountType string `json:"accountType,omitempty"`
TotalAccountValue fixedpoint.Value `json:"totalAccountValue,omitempty"`
balances BalanceMap
}