Skip to content

Commit

Permalink
Update rewards apis
Browse files Browse the repository at this point in the history
Align with api changes
- StakingRewards's Date field is now a full timestamp
- StakingBalance's Date field is now a full timestamp
  • Loading branch information
facoinbase committed Sep 16, 2024
1 parent a5cd988 commit 0dc54f3
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 8 deletions.
12 changes: 9 additions & 3 deletions pkg/coinbase/balance.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,15 @@ func newBalanceFromModel(model *client.Balance) (*Balance, error) {
if err != nil {
return nil, err
}
amount, ok := new(big.Int).SetString(model.Amount, 10)
if !ok {
return nil, fmt.Errorf("failed to parse amount: %s", model.Amount)

amount := big.NewInt(0)

if model.GetAmount() != "" {
a, ok := new(big.Int).SetString(model.Amount, 10)
if !ok {
return nil, fmt.Errorf("failed to parse amount: %s", model.Amount)
}
amount = a
}

return &Balance{
Expand Down
21 changes: 21 additions & 0 deletions pkg/coinbase/balance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,27 @@ func TestBalance_String(t *testing.T) {
}(),
want: "Balance { amount: '100' asset: 'Asset { networkId: 'networkId' assetId: 'assetId' contractAddress: 'contractAddress' decimals: '18' }' }",
},
{
name: "empty amount",
b: func() *Balance {
dec := int32(18)
contractAddress := "contractAddress"
balance, err := newBalanceFromModel(&client.Balance{
Asset: client.Asset{
NetworkId: "networkId",
AssetId: "assetId",
ContractAddress: &contractAddress,
Decimals: &dec,
},
Amount: "",
})
if err != nil {
t.Fatal(err)
}
return balance
}(),
want: "Balance { amount: '0' asset: 'Asset { networkId: 'networkId' assetId: 'assetId' contractAddress: 'contractAddress' decimals: '18' }' }",
},
}
for _, tt := range tests {
assert.Equal(t, tt.want, tt.b.String())
Expand Down
2 changes: 1 addition & 1 deletion pkg/coinbase/staking_balance.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func (c *Client) ListHistoricalStakingBalances(
}

func newStakingBalanceFromModel(m *client.StakingBalance) (*StakingBalance, error) {
date, err := time.Parse("2006-01-02", m.Date)
date, err := time.Parse(timestampFormat, m.Date)
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/coinbase/staking_balance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func TestListHistoricalStakingBalances(t *testing.T) {
Data: []client.StakingBalance{
{
ParticipantType: "participantType",
Date: parsedTime.Format("2006-01-02"),
Date: parsedTime.Format(timestampFormat),
BondedStake: client.Balance{
Amount: "123",
Asset: client.Asset{
Expand Down
2 changes: 1 addition & 1 deletion pkg/coinbase/staking_reward.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ func (s StakingReward) Amount() (*big.Float, error) {

// Date returns the date of the staking reward.
func (s StakingReward) Date() (time.Time, error) {
parsedDate, err := time.Parse(time.DateOnly, s.model.GetDate())
parsedDate, err := time.Parse(timestampFormat, s.model.GetDate())
if err != nil {
return time.Time{}, fmt.Errorf("invalid date found: %s", s.model.GetDate())
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/coinbase/staking_reward_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -261,13 +261,13 @@ func TestAmount(t *testing.T) {
func TestDate(t *testing.T) {
stakingReward := StakingReward{
model: api.StakingReward{
Date: "2024-08-10",
Date: "2024-08-10T00:00:11Z",
},
}

resp, err := stakingReward.Date()
assert.NoError(t, err, "error should be nil")
assert.Equal(t, "2024-08-10", resp.Format("2006-01-02"))
assert.Equal(t, "2024-08-10T00:00:11Z", resp.Format(timestampFormat))
}

func mockFetchAsset(t *testing.T, assetsAPI *mocks.AssetsAPI, statusCode int) {
Expand Down
2 changes: 2 additions & 0 deletions pkg/coinbase/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ const (
StakingOperationModePartial = "partial"
StakingOperationModeDefault = "default"
StakingOperationModeNative = "native"

timestampFormat = "2006-01-02T15:04:05Z"
)

func normalizeNetwork(network string) string {
Expand Down

0 comments on commit 0dc54f3

Please sign in to comment.