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

feat: backport basket proto updates #772

Merged
merged 17 commits into from
Feb 15, 2022
Merged
17 changes: 17 additions & 0 deletions x/ecocredit/basket/msg_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,3 +101,20 @@ func validateDateCriteria(d *DateCriteria) error {
}
return nil
}

// MsgCreateDenom formats denom and display denom:
// * denom: eco.<m.Exponent><m.CreditTypeAbbrev>.<m.Name>
// * display denom: eco.<m.Exponent><m.CreditTypeAbbrev>.<m.Name>
// Returns error if MsgCrete.Exponent is not supported
func MsgCreateDenom(m *MsgCreate) (string, string, error) {
Copy link
Member

Choose a reason for hiding this comment

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

reminder to refrain from adding interface methods to proto types 😉

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

It's not a method - it's a function.

Copy link
Member

Choose a reason for hiding this comment

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

ah you're right... read it wrong 👍

const basketDenomPrefix = "eco."
denomPrefix, err := ecocredit.ExponentToPrefix(m.Exponent)
if err != nil {
return "", "", err
}

denomTail := m.CreditTypeAbbrev + "." + m.Name
displayDenomName := basketDenomPrefix + denomTail //
denom := basketDenomPrefix + denomPrefix + denomTail // eco.<credit-class>.<name>
return denom, displayDenomName, nil
}
8 changes: 1 addition & 7 deletions x/ecocredit/server/basket/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ import (
"github.com/regen-network/regen-ledger/x/ecocredit/basket"
)

const basketDenomPrefix = "eco."

// Create is an RPC to handle basket.MsgCreate
func (k Keeper) Create(ctx context.Context, msg *basket.MsgCreate) (*basket.MsgCreateResponse, error) {
rgCtx := types.UnwrapSDKContext(ctx)
Expand All @@ -34,15 +32,11 @@ func (k Keeper) Create(ctx context.Context, msg *basket.MsgCreate) (*basket.MsgC
if err = validateCreditType(ctx, k.ecocreditKeeper, msg.CreditTypeAbbrev, msg.Exponent); err != nil {
return nil, err
}
denomPrefix, err := ecocredit.ExponentToPrefix(msg.Exponent)
denom, displayDenomName, err := basket.MsgCreateDenom(msg)
if err != nil {
return nil, err
}

denomTail := msg.CreditTypeAbbrev + "." + msg.Name
displayDenomName := basketDenomPrefix + denomTail // eco.<exponent><credit-type-abbrev>.<name>
denom := basketDenomPrefix + denomPrefix + denomTail // eco.<credit-class>.<name>

id, err := k.stateStore.BasketStore().InsertReturningID(ctx, &basketv1.Basket{
BasketDenom: denom,
DisableAutoRetire: msg.DisableAutoRetire,
Expand Down
18 changes: 11 additions & 7 deletions x/ecocredit/server/basket/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/golang/mock/gomock"
"github.com/regen-network/regen-ledger/x/ecocredit/basket"
baskettypes "github.com/regen-network/regen-ledger/x/ecocredit/basket"
"gotest.tools/v3/assert"
)
Expand Down Expand Up @@ -75,8 +76,16 @@ func TestDuplicateDenom(t *testing.T) {
t.Parallel()
s := setupBase(t)

mc := baskettypes.MsgCreate{
Curator: s.addr.String(),
CreditTypeAbbrev: "C",
Exponent: 6,
Name: "foo",
}
denom, _, err := basket.MsgCreateDenom(&mc)
assert.NilError(t, err)
assert.NilError(t, s.stateStore.BasketStore().Insert(s.ctx,
&basketv1.Basket{BasketDenom: "eco.foo"},
&basketv1.Basket{BasketDenom: denom},
))

s.ecocreditKeeper.EXPECT().GetCreateBasketFee(gomock.Any()).Return(nil) // nil fee
Expand All @@ -86,12 +95,7 @@ func TestDuplicateDenom(t *testing.T) {
{Abbreviation: "B", Precision: 3}, {Abbreviation: "C", Precision: 6},
}}, nil,
)
_, err := s.k.Create(s.ctx, &baskettypes.MsgCreate{
Curator: s.addr.String(),
CreditTypeAbbrev: "C",
Exponent: 6,
Name: "foo",
})
_, err = s.k.Create(s.ctx, &mc)
assert.ErrorContains(t, err, "unique")
}

Expand Down