bbgo_origin/pkg/exchange/max/maxapi/reward.go

170 lines
3.8 KiB
Go
Raw Normal View History

2021-02-22 10:45:44 +00:00
package max
2022-04-20 08:58:42 +00:00
//go:generate -command GetRequest requestgen -method GET
//go:generate -command PostRequest requestgen -method POST
2021-02-22 10:45:44 +00:00
import (
"encoding/json"
"fmt"
"strings"
2021-02-23 01:37:06 +00:00
2022-04-20 08:58:42 +00:00
"github.com/c9s/requestgen"
2021-02-23 01:37:06 +00:00
"github.com/c9s/bbgo/pkg/fixedpoint"
"github.com/c9s/bbgo/pkg/types"
2021-02-22 10:45:44 +00:00
)
type RewardType string
const (
RewardAirdrop = RewardType("airdrop_reward")
RewardCommission = RewardType("commission")
RewardHolding = RewardType("holding_reward")
RewardMining = RewardType("mining_reward")
RewardTrading = RewardType("trading_reward")
2022-04-20 08:58:42 +00:00
RewardRedemption = RewardType("redemption_reward")
2021-02-22 10:45:44 +00:00
RewardVipRebate = RewardType("vip_rebate")
)
func ParseRewardType(s string) (RewardType, error) {
switch s {
case "airdrop_reward":
return RewardAirdrop, nil
case "commission":
return RewardCommission, nil
case "holding_reward":
return RewardHolding, nil
case "mining_reward":
return RewardMining, nil
case "trading_reward":
return RewardTrading, nil
case "vip_rebate":
return RewardVipRebate, nil
2022-04-20 08:58:42 +00:00
case "redemption_reward":
return RewardRedemption, nil
2021-02-22 10:45:44 +00:00
}
return RewardType(""), fmt.Errorf("unknown reward type: %s", s)
}
func (t *RewardType) UnmarshalJSON(o []byte) error {
var s string
var err = json.Unmarshal(o, &s)
if err != nil {
return err
}
rt, err := ParseRewardType(s)
if err != nil {
return err
}
*t = rt
return nil
}
func (t RewardType) RewardType() (types.RewardType, error) {
switch t {
case RewardAirdrop:
return types.RewardAirdrop, nil
case RewardCommission:
return types.RewardCommission, nil
case RewardHolding:
return types.RewardHolding, nil
case RewardMining:
return types.RewardMining, nil
case RewardTrading:
return types.RewardTrading, nil
case RewardVipRebate:
return types.RewardVipRebate, nil
}
return types.RewardType(""), fmt.Errorf("unknown reward type: %s", t)
}
2021-02-22 10:45:44 +00:00
type Reward struct {
// UUID here is more like SN, not the real UUID
2021-02-23 01:37:06 +00:00
UUID string `json:"uuid"`
Type RewardType `json:"type"`
Currency string `json:"currency"`
Amount fixedpoint.Value `json:"amount"`
State string `json:"state"`
Note string `json:"note"`
2021-02-22 10:45:44 +00:00
// Unix timestamp in seconds
2022-04-20 08:58:42 +00:00
CreatedAt types.Timestamp `json:"created_at"`
2021-02-22 10:45:44 +00:00
}
func (reward Reward) Reward() (*types.Reward, error) {
rt, err := reward.Type.RewardType()
if err != nil {
return nil, err
}
return &types.Reward{
UUID: reward.UUID,
Exchange: types.ExchangeMax,
Type: rt,
Currency: strings.ToUpper(reward.Currency),
Quantity: reward.Amount,
State: reward.State,
Note: reward.Note,
Spent: false,
2021-05-19 17:32:26 +00:00
CreatedAt: types.Time(reward.CreatedAt),
}, nil
}
2021-02-22 10:45:44 +00:00
type RewardService struct {
client requestgen.AuthenticatedAPIClient
2021-02-22 10:45:44 +00:00
}
2022-04-20 08:58:42 +00:00
func (s *RewardService) NewGetRewardsRequest() *GetRewardsRequest {
return &GetRewardsRequest{client: s.client}
2021-02-22 10:45:44 +00:00
}
2022-04-20 08:58:42 +00:00
func (s *RewardService) NewGetRewardsOfTypeRequest(pathType RewardType) *GetRewardsOfTypeRequest {
return &GetRewardsOfTypeRequest{client: s.client, pathType: &pathType}
2021-02-22 10:45:44 +00:00
}
2022-04-20 08:58:42 +00:00
//go:generate GetRequest -url "v2/rewards/:path_type" -type GetRewardsOfTypeRequest -responseType []Reward
type GetRewardsOfTypeRequest struct {
client requestgen.AuthenticatedAPIClient
2021-02-22 10:45:44 +00:00
2022-04-20 08:58:42 +00:00
pathType *RewardType `param:"path_type,slug"`
2021-02-22 10:45:44 +00:00
// From Unix-timestamp
2022-04-20 08:58:42 +00:00
from *int64 `param:"from"`
2021-02-22 10:45:44 +00:00
// To Unix-timestamp
2022-04-20 08:58:42 +00:00
to *int64 `param:"to"`
2021-02-22 10:45:44 +00:00
2022-04-20 08:58:42 +00:00
page *int64 `param:"page"`
limit *int64 `param:"limit"`
offset *int64 `param:"offset"`
2021-02-22 10:45:44 +00:00
}
2022-04-20 08:58:42 +00:00
//go:generate GetRequest -url "v2/rewards" -type GetRewardsRequest -responseType []Reward
type GetRewardsRequest struct {
client requestgen.AuthenticatedAPIClient
2021-02-22 10:45:44 +00:00
2022-04-20 08:58:42 +00:00
currency *string `param:"currency"`
2021-02-22 10:45:44 +00:00
2022-04-20 08:58:42 +00:00
// From Unix-timestamp
from *int64 `param:"from"`
2021-02-22 10:45:44 +00:00
2022-04-20 08:58:42 +00:00
// To Unix-timestamp
to *int64 `param:"to"`
2021-02-22 10:45:44 +00:00
2022-04-20 08:58:42 +00:00
page *int64 `param:"page"`
limit *int64 `param:"limit"`
offset *int64 `param:"offset"`
2021-02-22 10:45:44 +00:00
}