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

fix(types): return nil duration and timestamp (backport #1292) #1294

Merged
merged 1 commit into from
Jul 23, 2022
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 @@ -7,6 +7,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased v4.0]

### types

#### Fixed

- [#1292](https://github.com/regen-network/regen-ledger/pull/1292) Fix nil timestamp and duration conversion

### x/ecocredit

#### Added
Expand Down
6 changes: 3 additions & 3 deletions types/timestamp.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
// GogoToProtobufTimestamp converts a gogo timestamp to a protobuf timestamp.
func GogoToProtobufTimestamp(ts *gogotypes.Timestamp) *timestamppb.Timestamp {
if ts == nil {
return &timestamppb.Timestamp{}
return nil
}
return &timestamppb.Timestamp{
Seconds: ts.Seconds,
Expand All @@ -24,7 +24,7 @@ func GogoToProtobufTimestamp(ts *gogotypes.Timestamp) *timestamppb.Timestamp {
// ProtobufToGogoTimestamp converts a protobuf timestamp to a gogo timestamp.
func ProtobufToGogoTimestamp(ts *timestamppb.Timestamp) *gogotypes.Timestamp {
if ts == nil {
return &gogotypes.Timestamp{}
return nil
}
return &gogotypes.Timestamp{
Seconds: ts.Seconds,
Expand All @@ -35,7 +35,7 @@ func ProtobufToGogoTimestamp(ts *timestamppb.Timestamp) *gogotypes.Timestamp {
// GogoToProtobufDuration converts a gogo duration to a protobuf duration.
func GogoToProtobufDuration(d *gogotypes.Duration) *durationpb.Duration {
if d == nil {
return &durationpb.Duration{}
return nil
}
return &durationpb.Duration{
Seconds: d.Seconds,
Expand Down
6 changes: 3 additions & 3 deletions types/timestamp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func TestGogoToProtobufDuration(t *testing.T) {
{
name: "nil case",
args: args{d: nil},
want: &durationpb.Duration{},
want: nil,
},
}
for _, tt := range tests {
Expand Down Expand Up @@ -59,7 +59,7 @@ func TestGogoToProtobufTimestamp(t *testing.T) {
{
name: "nil",
args: args{ts: nil},
want: &timestamppb.Timestamp{},
want: nil,
},
}
for _, tt := range tests {
Expand Down Expand Up @@ -88,7 +88,7 @@ func TestProtobufToGogoTimestamp(t *testing.T) {
{
name: "nil",
args: args{ts: nil},
want: &gogotypes.Timestamp{},
want: nil,
},
}
for _, tt := range tests {
Expand Down
75 changes: 48 additions & 27 deletions x/ecocredit/server/marketplace/query_sell_order_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ package marketplace
import (
"testing"

"gotest.tools/v3/assert"
"github.com/stretchr/testify/require"
"google.golang.org/protobuf/types/known/timestamppb"

"github.com/cosmos/cosmos-sdk/orm/types/ormerrors"

Expand All @@ -18,34 +19,54 @@ func TestQuery_SellOrder(t *testing.T) {
s := setupBase(t, 1)
s.testSellSetup(batchDenom, ask.Denom, ask.Denom[1:], "C01", start, end, creditType)

// make a sell order
order := api.SellOrder{
Seller: s.addrs[0],
BatchKey: 1,
Quantity: "15.32",
MarketId: 1,
AskAmount: "100",
DisableAutoRetire: false,
Expiration: nil,
Maker: false,
expiration, err := types.ParseDate("expiration", "2030-01-01")
require.NoError(s.t, err)

// make a sell order (with expiration)
order1 := api.SellOrder{
Seller: s.addrs[0],
BatchKey: 1,
Quantity: "15.32",
MarketId: 1,
AskAmount: "100",
Expiration: timestamppb.New(expiration),
}
id1, err := s.marketStore.SellOrderTable().InsertReturningID(s.ctx, &order1)
require.NoError(t, err)

// make a sell order (no expiration)
order2 := api.SellOrder{
Seller: s.addrs[0],
BatchKey: 1,
Quantity: "15.32",
MarketId: 1,
AskAmount: "100",
}
id, err := s.marketStore.SellOrderTable().InsertReturningID(s.ctx, &order)
assert.NilError(t, err)

var gogoOrder marketplace.SellOrder
assert.NilError(t, ormutil.PulsarToGogoSlow(&order, &gogoOrder))

res, err := s.k.SellOrder(s.ctx, &marketplace.QuerySellOrderRequest{SellOrderId: id})
assert.NilError(t, err)
assert.Equal(t, s.addrs[0].String(), res.SellOrder.Seller)
assert.Equal(t, batchDenom, res.SellOrder.BatchDenom)
assert.Equal(t, order.Quantity, res.SellOrder.Quantity)
assert.Equal(t, ask.Denom, res.SellOrder.AskDenom)
assert.Equal(t, order.AskAmount, res.SellOrder.AskAmount)
assert.Equal(t, order.DisableAutoRetire, res.SellOrder.DisableAutoRetire)
assert.DeepEqual(t, types.ProtobufToGogoTimestamp(order.Expiration), res.SellOrder.Expiration)
id2, err := s.marketStore.SellOrderTable().InsertReturningID(s.ctx, &order2)
require.NoError(t, err)

var gogoOrder1 marketplace.SellOrder
require.NoError(t, ormutil.PulsarToGogoSlow(&order1, &gogoOrder1))

var gogoOrder2 marketplace.SellOrder
require.NoError(t, ormutil.PulsarToGogoSlow(&order2, &gogoOrder2))

res1, err := s.k.SellOrder(s.ctx, &marketplace.QuerySellOrderRequest{SellOrderId: id1})
require.NoError(t, err)
require.Equal(t, s.addrs[0].String(), res1.SellOrder.Seller)
require.Equal(t, batchDenom, res1.SellOrder.BatchDenom)
require.Equal(t, order1.Quantity, res1.SellOrder.Quantity)
require.Equal(t, ask.Denom, res1.SellOrder.AskDenom)
require.Equal(t, order1.AskAmount, res1.SellOrder.AskAmount)
require.Equal(t, order1.DisableAutoRetire, res1.SellOrder.DisableAutoRetire)
require.Equal(t, types.ProtobufToGogoTimestamp(order1.Expiration), res1.SellOrder.Expiration)

res2, err := s.k.SellOrder(s.ctx, &marketplace.QuerySellOrderRequest{SellOrderId: id2})
require.NoError(t, err)
require.True(t, res2.SellOrder.Expiration.Equal(nil))
require.Equal(t, types.ProtobufToGogoTimestamp(order2.Expiration), res2.SellOrder.Expiration)

// invalid order id should fail
_, err = s.k.SellOrder(s.ctx, &marketplace.QuerySellOrderRequest{SellOrderId: 404})
assert.ErrorContains(t, err, ormerrors.NotFound.Error())
require.ErrorContains(t, err, ormerrors.NotFound.Error())
}