Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update rewards apis #30

Merged
merged 1 commit into from
Sep 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

## Unreleased

## [0.0.8] - 2024-09-17

- StakingRewards's Date field is now a full timestamp
- StakingBalance's Date field is now a full timestamp
- Empty balance values default to 0

## [0.0.7] - 2024-09-12

- Adds headers to requests with SDK version & language
Expand Down
2 changes: 1 addition & 1 deletion pkg/auth/transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func (t *transport) RoundTrip(req *http.Request) (*http.Response, error) {
"Correlation-Context",
fmt.Sprintf(
"%s,%s",
fmt.Sprintf("%s=%s", "sdk_version", "0.0.7"),
fmt.Sprintf("%s=%s", "sdk_version", "0.0.8"),
fmt.Sprintf("%s=%s", "sdk_language", "go"),
),
)
Expand Down
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() != "" {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this because we are sometimes returning an empty value instead of a 0 value for a balance check?

Copy link
Contributor Author

@facoinbase facoinbase Sep 16, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Exactly

 unbondedBalance: 'Balance { amount: '' asset: 'Asset { networkId: '' assetId: '' contractAddress: '' decimals: '0' }' }

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AH makes sense, for solana its not a 0 value, but an nil one? If were just having the SDKs make nil -> 0, should we just return 0 from the API?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think making it nil on the api would be better

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
Loading