Skip to content

Commit

Permalink
refactor get credit type from batch
Browse files Browse the repository at this point in the history
  • Loading branch information
aaronc committed Feb 28, 2024
1 parent b29c249 commit e7aa7be
Show file tree
Hide file tree
Showing 9 changed files with 36 additions and 38 deletions.
2 changes: 1 addition & 1 deletion x/ecocredit/base/keeper/msg_cancel.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func (k Keeper) Cancel(ctx context.Context, req *types.MsgCancel) (*types.MsgCan
if err != nil {
return nil, sdkerrors.ErrInvalidRequest.Wrapf("could not get batch with denom %s: %s", credit.BatchDenom, err.Error())
}
creditType, err := utils.GetCreditTypeFromBatchDenom(ctx, k.stateStore, batch.Denom)
creditType, err := utils.GetCreditTypeFromBatchDenom(ctx, k.stateStore, batch)
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion x/ecocredit/base/keeper/msg_mint_batch_credits.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func (k Keeper) MintBatchCredits(ctx context.Context, req *types.MsgMintBatchCre
return nil, err
}

ct, err := utils.GetCreditTypeFromBatchDenom(ctx, k.stateStore, batch.Denom)
ct, err := utils.GetCreditTypeFromBatchDenom(ctx, k.stateStore, batch)
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion x/ecocredit/base/keeper/msg_retire.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func (k Keeper) Retire(ctx context.Context, req *types.MsgRetire) (*types.MsgRet
if err != nil {
return nil, sdkerrors.ErrInvalidRequest.Wrapf("could not get batch with denom %s: %s", credit.BatchDenom, err.Error())
}
creditType, err := utils.GetCreditTypeFromBatchDenom(ctx, k.stateStore, batch.Denom)
creditType, err := utils.GetCreditTypeFromBatchDenom(ctx, k.stateStore, batch)
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion x/ecocredit/base/keeper/msg_send.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func (k Keeper) Send(ctx context.Context, req *types.MsgSend) (*types.MsgSendRes
}

// get credit type precision
creditType, err := utils.GetCreditTypeFromBatchDenom(ctx, k.stateStore, batch.Denom)
creditType, err := utils.GetCreditTypeFromBatchDenom(ctx, k.stateStore, batch)
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion x/ecocredit/marketplace/keeper/msg_buy_direct.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func (k Keeper) BuyDirect(ctx context.Context, req *types.MsgBuyDirect) (*types.
if err != nil {
return nil, err
}
ct, err := utils.GetCreditTypeFromBatchDenom(ctx, k.baseStore, batch.Denom)
ct, err := utils.GetCreditTypeFromBatchDenom(ctx, k.baseStore, batch)
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion x/ecocredit/marketplace/keeper/msg_sell.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func (k Keeper) Sell(ctx context.Context, req *types.MsgSell) (*types.MsgSellRes
)
}

creditType, err := utils.GetCreditTypeFromBatchDenom(ctx, k.baseStore, batch.Denom)
creditType, err := utils.GetCreditTypeFromBatchDenom(ctx, k.baseStore, batch)
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion x/ecocredit/marketplace/keeper/msg_update_sell_orders.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ func (k Keeper) getCreditTypeFromBatchKey(ctx context.Context, key uint64) (*bas
if err != nil {
return nil, err
}
creditType, err := utils.GetCreditTypeFromBatchDenom(ctx, k.baseStore, batch.Denom)
creditType, err := utils.GetCreditTypeFromBatchDenom(ctx, k.baseStore, batch)
if err != nil {
return nil, err
}
Expand Down
48 changes: 24 additions & 24 deletions x/ecocredit/server/tests/features/bridge.feature
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ Feature: Bridge Integration
And expect event bridge receive with values
"""
{
"project_id": "C01-001",
"batch_denom": "C01-001-20200101-20210101-001",
"project_id": "P001",
"batch_denom": "C01-P001-20200101-20210101-001",
"amount": "100",
"origin_tx": {
"id": "0x0000000000000000000000000000000000000000000000000000000000000001",
Expand All @@ -79,7 +79,7 @@ Feature: Bridge Integration
And expect project with properties
"""
{
"id": "C01-001",
"id": "P001",
"metadata": "regen:13toVfvC2YxrrfSXWB5h2BGHiXZURsKxWUz72uDRDSPMCrYPguGUXSC.rdf",
"jurisdiction": "US-WA",
"reference_id": "VCS-001"
Expand All @@ -89,22 +89,22 @@ Feature: Bridge Integration
And expect credit batch with properties
"""
{
"denom": "C01-001-20200101-20210101-001",
"denom": "C01-P001-20200101-20210101-001",
"metadata": "regen:13toVgf5aZqSVSeJQv562xkkeoe3rr3bJWa29PHVKVf77VAkVMcDvVd.rdf",
"start_date": "2020-01-01T00:00:00Z",
"end_date": "2021-01-01T00:00:00Z",
"open": true
}
"""
And expect batch supply with batch denom "C01-001-20200101-20210101-001"
And expect batch supply with batch denom "C01-P001-20200101-20210101-001"
"""
{
"tradable_amount": "100",
"retired_amount": "0",
"cancelled_amount": "0"
}
"""
And expect batch balance with address "regen1s3x2yhc4qf59gf53hwsnhkh7gqa3eryxwj8p42" and batch denom "C01-001-20200101-20210101-001"
And expect batch balance with address "regen1s3x2yhc4qf59gf53hwsnhkh7gqa3eryxwj8p42" and batch denom "C01-P001-20200101-20210101-001"
"""
{
"tradable_amount": "100",
Expand Down Expand Up @@ -174,8 +174,8 @@ Feature: Bridge Integration
And expect event bridge receive with values
"""
{
"project_id": "C01-001",
"batch_denom": "C01-001-20200101-20210101-001",
"project_id": "P001",
"batch_denom": "C01-P001-20200101-20210101-001",
"amount": "100",
"origin_tx": {
"id": "0x0000000000000000000000000000000000000000000000000000000000000002",
Expand All @@ -186,7 +186,7 @@ Feature: Bridge Integration
"""
And expect total projects "1"
And expect total credit batches "1"
And expect batch balance with address "regen1s3x2yhc4qf59gf53hwsnhkh7gqa3eryxwj8p42" and batch denom "C01-001-20200101-20210101-001"
And expect batch balance with address "regen1s3x2yhc4qf59gf53hwsnhkh7gqa3eryxwj8p42" and batch denom "C01-P001-20200101-20210101-001"
"""
{
"tradable_amount": "200",
Expand Down Expand Up @@ -225,8 +225,8 @@ Feature: Bridge Integration
And expect event bridge receive with values
"""
{
"project_id": "C01-001",
"batch_denom": "C01-001-20200101-20210101-002",
"project_id": "P001",
"batch_denom": "C01-P001-20200101-20210101-002",
"amount": "100",
"origin_tx": {
"id": "0x0000000000000000000000000000000000000000000000000000000000000003",
Expand All @@ -240,22 +240,22 @@ Feature: Bridge Integration
And expect credit batch with properties
"""
{
"denom": "C01-001-20200101-20210101-002",
"denom": "C01-P001-20200101-20210101-002",
"metadata": "regen:13toVgf5aZqSVSeJQv562xkkeoe3rr3bJWa29PHVKVf77VAkVMcDvVd.rdf",
"start_date": "2020-01-01T00:00:00Z",
"end_date": "2021-01-01T00:00:00Z",
"open": true
}
"""
And expect batch supply with batch denom "C01-001-20200101-20210101-002"
And expect batch supply with batch denom "C01-P001-20200101-20210101-002"
"""
{
"tradable_amount": "100",
"retired_amount": "0",
"cancelled_amount": "0"
}
"""
And expect batch balance with address "regen1s3x2yhc4qf59gf53hwsnhkh7gqa3eryxwj8p42" and batch denom "C01-001-20200101-20210101-002"
And expect batch balance with address "regen1s3x2yhc4qf59gf53hwsnhkh7gqa3eryxwj8p42" and batch denom "C01-P001-20200101-20210101-002"
"""
{
"tradable_amount": "100",
Expand Down Expand Up @@ -294,8 +294,8 @@ Feature: Bridge Integration
And expect event bridge receive with values
"""
{
"project_id": "C01-001",
"batch_denom": "C01-001-20200101-20210101-002",
"project_id": "P001",
"batch_denom": "C01-P001-20200101-20210101-002",
"amount": "100",
"origin_tx": {
"id": "0x0000000000000000000000000000000000000000000000000000000000000004",
Expand All @@ -306,15 +306,15 @@ Feature: Bridge Integration
"""
And expect total projects "1"
And expect total credit batches "2"
And expect batch supply with batch denom "C01-001-20200101-20210101-002"
And expect batch supply with batch denom "C01-P001-20200101-20210101-002"
"""
{
"tradable_amount": "200",
"retired_amount": "0",
"cancelled_amount": "0"
}
"""
And expect batch balance with address "regen1s3x2yhc4qf59gf53hwsnhkh7gqa3eryxwj8p42" and batch denom "C01-001-20200101-20210101-002"
And expect batch balance with address "regen1s3x2yhc4qf59gf53hwsnhkh7gqa3eryxwj8p42" and batch denom "C01-P001-20200101-20210101-002"
"""
{
"tradable_amount": "200",
Expand Down Expand Up @@ -353,8 +353,8 @@ Feature: Bridge Integration
And expect event bridge receive with values
"""
{
"project_id": "C01-002",
"batch_denom": "C01-002-20200101-20210101-001",
"project_id": "P002",
"batch_denom": "C01-P002-20200101-20210101-001",
"amount": "100",
"origin_tx": {
"id": "0x0000000000000000000000000000000000000000000000000000000000000005",
Expand Down Expand Up @@ -401,7 +401,7 @@ Feature: Bridge Integration
"recipient": "0x1000000000000000000000000000000000000000",
"credits": [
{
"batch_denom": "C01-001-20200101-20210101-001",
"batch_denom": "C01-P001-20200101-20210101-001",
"amount": "200"
}
]
Expand All @@ -416,18 +416,18 @@ Feature: Bridge Integration
"contract": "0x0000000000000000000000000000000000000001",
"amount": "200",
"owner": "regen1s3x2yhc4qf59gf53hwsnhkh7gqa3eryxwj8p42",
"batch_denom": "C01-001-20200101-20210101-001"
"batch_denom": "C01-P001-20200101-20210101-001"
}
"""
And expect batch supply with batch denom "C01-001-20200101-20210101-001"
And expect batch supply with batch denom "C01-P001-20200101-20210101-001"
"""
{
"tradable_amount": "0",
"retired_amount": "0",
"cancelled_amount": "200"
}
"""
And expect batch balance with address "regen1s3x2yhc4qf59gf53hwsnhkh7gqa3eryxwj8p42" and batch denom "C01-001-20200101-20210101-001"
And expect batch balance with address "regen1s3x2yhc4qf59gf53hwsnhkh7gqa3eryxwj8p42" and batch denom "C01-P001-20200101-20210101-001"
"""
{
"tradable_amount": "0",
Expand Down
12 changes: 5 additions & 7 deletions x/ecocredit/server/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,19 @@ import (

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

api "github.com/regen-network/regen-ledger/api/v2/regen/ecocredit/v1"
"github.com/regen-network/regen-ledger/types/v2/math"
"github.com/regen-network/regen-ledger/x/ecocredit/v3/base"
)

// GetCreditTypeFromBatchDenom extracts the classID from a batch denom string, then retrieves it from the params.
func GetCreditTypeFromBatchDenom(ctx context.Context, store api.StateStore, denom string) (*api.CreditType, error) {
classID := base.GetClassIDFromBatchDenom(denom)
classInfo, err := store.ClassTable().GetById(ctx, classID)
func GetCreditTypeFromBatchDenom(ctx context.Context, store api.StateStore, batch *api.Batch) (*api.CreditType, error) {
cls, err := store.ClassTable().Get(ctx, batch.ClassKey)
if err != nil {
return nil, sdkerrors.ErrInvalidRequest.Wrapf("could not get class with ID %s: %s", classID, err.Error())
return nil, err
}
return store.CreditTypeTable().Get(ctx, classInfo.CreditTypeAbbrev)

return store.CreditTypeTable().Get(ctx, cls.CreditTypeAbbrev)
}

// GetNonNegativeFixedDecs takes an arbitrary amount of decimal strings, and returns their corresponding fixed decimals
Expand Down

0 comments on commit e7aa7be

Please sign in to comment.