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: add coin types to core #23346

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
10 changes: 10 additions & 0 deletions core/coin/coin.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package coin

import "math/big"

type Coin struct {
Denom string
Amount big.Int
}

type Coins []Coin
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ require (

// TODO remove after all modules have their own go.mods
replace (
cosmossdk.io/core => ./core
cosmossdk.io/x/bank => ./x/bank
cosmossdk.io/x/staking => ./x/staking
)
Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ cosmossdk.io/api v0.8.0 h1:E5Xifxu/3mPTLF79si9fyq4rR0wagubeVNmOz5duTUo=
cosmossdk.io/api v0.8.0/go.mod h1:hgJ83P0ZUu0rS1SZoVM6abk6ADOkiM259BVVlYtAPP0=
cosmossdk.io/collections v1.0.0 h1:YCYIe/pIMtc1iLDD0OrVdfWCnIkpwdy7k9NSQpaR5mg=
cosmossdk.io/collections v1.0.0/go.mod h1:mFfLxnYT1fV+B3Lx9GLap1qxmffIPqQCND4xBExerps=
cosmossdk.io/core v1.0.0 h1:e7XBbISOytLBOXMVwpRPixThXqEkeLGlg8no/qpgS8U=
cosmossdk.io/core v1.0.0/go.mod h1:mKIp3RkoEmtqdEdFHxHwWAULRe+79gfdOvmArrLDbDc=
cosmossdk.io/core/testing v0.0.1 h1:gYCTaftcRrz+HoNXmK7r9KgbG1jgBJ8pNzm/Pa/erFQ=
cosmossdk.io/core/testing v0.0.1/go.mod h1:2VDNz/25qtxgPa0+j8LW5e8Ev/xObqoJA7QuJS9/wIQ=
cosmossdk.io/depinject v1.1.0 h1:wLan7LG35VM7Yo6ov0jId3RHWCGRhe8E8bsuARorl5E=
Expand Down
1 change: 1 addition & 0 deletions simapp/v2/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,7 @@ replace (
cosmossdk.io/x/slashing => ../../x/slashing
cosmossdk.io/x/staking => ../../x/staking
cosmossdk.io/x/upgrade => ../../x/upgrade
cosmossdk.io/core => ../../core
)

// Below are the long-lived replace of the SimApp
Expand Down
21 changes: 21 additions & 0 deletions types/coin.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"strings"
"unicode"

core "cosmossdk.io/core/coin"

Check failure on line 12 in types/coin.go

View workflow job for this annotation

GitHub Actions / dependency-review

no required module provides package cosmossdk.io/core/coin; to add it:

Check failure on line 12 in types/coin.go

View workflow job for this annotation

GitHub Actions / dependency-review

no required module provides package cosmossdk.io/core/coin; to add it:

Check failure on line 12 in types/coin.go

View workflow job for this annotation

GitHub Actions / dependency-review

no required module provides package cosmossdk.io/core/coin; to add it:

Check failure on line 12 in types/coin.go

View workflow job for this annotation

GitHub Actions / dependency-review

no required module provides package cosmossdk.io/core/coin; to add it:

Check failure on line 12 in types/coin.go

View workflow job for this annotation

GitHub Actions / dependency-review

no required module provides package cosmossdk.io/core/coin; to add it:

Check failure on line 12 in types/coin.go

View workflow job for this annotation

GitHub Actions / dependency-review

no required module provides package cosmossdk.io/core/coin; to add it:

Check failure on line 12 in types/coin.go

View workflow job for this annotation

GitHub Actions / dependency-review

no required module provides package cosmossdk.io/core/coin; to add it:

Check failure on line 12 in types/coin.go

View workflow job for this annotation

GitHub Actions / dependency-review

no required module provides package cosmossdk.io/core/coin; to add it:
Copy link
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue

Add core module dependency to go.mod

The import of cosmossdk.io/core/coin requires adding the module to go.mod. This is currently causing build failures.

Add the following to your go.mod file:

require (
+    cosmossdk.io/core v1.0.0
)

Committable suggestion skipped: line range outside the PR's diff.

🧰 Tools
🪛 GitHub Check: dependency-review

[failure] 12-12:
no required module provides package cosmossdk.io/core/coin; to add it:


[failure] 12-12:
no required module provides package cosmossdk.io/core/coin; to add it:


[failure] 12-12:
no required module provides package cosmossdk.io/core/coin; to add it:


[failure] 12-12:
no required module provides package cosmossdk.io/core/coin; to add it:


[failure] 12-12:
no required module provides package cosmossdk.io/core/coin; to add it:


[failure] 12-12:
no required module provides package cosmossdk.io/core/coin; to add it:


[failure] 12-12:
no required module provides package cosmossdk.io/core/coin; to add it:


[failure] 12-12:
no required module provides package cosmossdk.io/core/coin; to add it:

🪛 GitHub Actions: Dependency Review

[error] 12-12: Missing required module: no required module provides package cosmossdk.io/core/coin

🪛 GitHub Actions: CodeQL

[error] 12-12: Missing required module dependency: package cosmossdk.io/core/coin is not provided by any module

🪛 GitHub Actions: Tests / Code Coverage

[error] 12-12: Missing required module: no required module provides package cosmossdk.io/core/coin

"cosmossdk.io/math"
)

Expand All @@ -30,6 +31,13 @@
return coin
}

func NewCoreCoin(coin core.Coin) Coin {
return Coin{
Denom: coin.Denom,
Amount: math.NewIntFromBigInt(&coin.Amount),
}
}
Comment on lines +34 to +39
Copy link
Contributor

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Add validation in NewCoreCoin

The function directly converts core.Coin to Coin without validation. Consider validating the denomination and amount to maintain data integrity.

Apply this diff to add validation:

 func NewCoreCoin(coin core.Coin) Coin {
-    return Coin{
+    c := Coin{
         Denom:  coin.Denom,
         Amount: math.NewIntFromBigInt(&coin.Amount),
     }
+    if err := c.Validate(); err != nil {
+        panic(err)
+    }
+    return c
 }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
func NewCoreCoin(coin core.Coin) Coin {
return Coin{
Denom: coin.Denom,
Amount: math.NewIntFromBigInt(&coin.Amount),
}
}
func NewCoreCoin(coin core.Coin) Coin {
c := Coin{
Denom: coin.Denom,
Amount: math.NewIntFromBigInt(&coin.Amount),
}
if err := c.Validate(); err != nil {
panic(err)
}
return c
}


// NewInt64Coin returns a new coin with a denomination and amount. It will panic
// if the amount is negative.
func NewInt64Coin(denom string, amount int64) Coin {
Expand Down Expand Up @@ -201,6 +209,19 @@
return newCoins
}

func NewCoreCoins(coins core.Coins) Coins {
var res Coins

for _, coin := range coins {
res = append(res, Coin{
Denom: coin.Denom,
Amount: math.NewIntFromBigInt(&coin.Amount),
})
}

return NewCoins(res...)
}

func sanitizeCoins(coins []Coin) Coins {
newCoins := removeZeroCoins(coins)
if len(newCoins) == 0 {
Expand Down
2 changes: 2 additions & 0 deletions x/bank/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -169,3 +169,5 @@ replace github.com/cosmos/cosmos-sdk => ../../.

// TODO remove post spinning out all modules
replace cosmossdk.io/x/staking => ../staking

replace cosmossdk.io/core => ../../core
13 changes: 7 additions & 6 deletions x/bank/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

"cosmossdk.io/core/address"
"cosmossdk.io/core/appmodule"
core "cosmossdk.io/core/coin"
"cosmossdk.io/core/event"
errorsmod "cosmossdk.io/errors"
"cosmossdk.io/math"
Expand Down Expand Up @@ -39,9 +40,9 @@ type Keeper interface {
GetAllDenomMetaData(ctx context.Context) []types.Metadata
IterateAllDenomMetaData(ctx context.Context, cb func(types.Metadata) bool)

SendCoinsFromModuleToAccount(ctx context.Context, senderModule string, recipientAddr sdk.AccAddress, amt sdk.Coins) error
SendCoinsFromModuleToModule(ctx context.Context, senderModule, recipientModule string, amt sdk.Coins) error
SendCoinsFromAccountToModule(ctx context.Context, senderAddr sdk.AccAddress, recipientModule string, amt sdk.Coins) error
SendCoinsFromModuleToAccount(ctx context.Context, senderModule string, recipientAddr sdk.AccAddress, amt core.Coins) error
SendCoinsFromModuleToModule(ctx context.Context, senderModule, recipientModule string, amt core.Coins) error
SendCoinsFromAccountToModule(ctx context.Context, senderAddr sdk.AccAddress, recipientModule string, amt core.Coins) error
DelegateCoinsFromAccountToModule(ctx context.Context, senderAddr sdk.AccAddress, recipientModule string, amt sdk.Coins) error
UndelegateCoinsFromModuleToAccount(ctx context.Context, senderModule string, recipientAddr sdk.AccAddress, amt sdk.Coins) error
MintCoins(ctx context.Context, moduleName string, amt sdk.Coins) error
Expand Down Expand Up @@ -251,7 +252,7 @@ func (k BaseKeeper) SetDenomMetaData(ctx context.Context, denomMetaData types.Me
// An error is returned if the module account does not exist or if
// the recipient address is black-listed or if sending the tokens fails.
func (k BaseKeeper) SendCoinsFromModuleToAccount(
ctx context.Context, senderModule string, recipientAddr sdk.AccAddress, amt sdk.Coins,
ctx context.Context, senderModule string, recipientAddr sdk.AccAddress, amt core.Coins,
) error {
senderAddr := k.ak.GetModuleAddress(senderModule)
if senderAddr == nil {
Expand All @@ -268,7 +269,7 @@ func (k BaseKeeper) SendCoinsFromModuleToAccount(
// SendCoinsFromModuleToModule transfers coins from a ModuleAccount to another.
// An error is returned if either module accounts does not exist.
func (k BaseKeeper) SendCoinsFromModuleToModule(
ctx context.Context, senderModule, recipientModule string, amt sdk.Coins,
ctx context.Context, senderModule, recipientModule string, amt core.Coins,
) error {
senderAddr := k.ak.GetModuleAddress(senderModule)
if senderAddr == nil {
Expand All @@ -286,7 +287,7 @@ func (k BaseKeeper) SendCoinsFromModuleToModule(
// SendCoinsFromAccountToModule transfers coins from an AccAddress to a ModuleAccount.
// An error is returned if the module account does not exist.
func (k BaseKeeper) SendCoinsFromAccountToModule(
ctx context.Context, senderAddr sdk.AccAddress, recipientModule string, amt sdk.Coins,
ctx context.Context, senderAddr sdk.AccAddress, recipientModule string, amt core.Coins,
) error {
recipientAcc := k.ak.GetModuleAccount(ctx, recipientModule)
if recipientAcc == nil {
Expand Down
12 changes: 10 additions & 2 deletions x/bank/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package keeper

import (
"context"

core "cosmossdk.io/core/coin"
Copy link
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue

Add missing module dependency

The cosmossdk.io/core/coin module is not available in the module requirements.

Add the following to your go.mod file:

+ require cosmossdk.io/core v0.1.0

Committable suggestion skipped: line range outside the PR's diff.

errorsmod "cosmossdk.io/errors"
"cosmossdk.io/x/bank/types"

Expand Down Expand Up @@ -57,7 +57,15 @@ func (k msgServer) Send(ctx context.Context, msg *types.MsgSend) (*types.MsgSend
return nil, errorsmod.Wrapf(sdkerrors.ErrUnauthorized, "%s is not allowed to receive funds", msg.ToAddress)
}

err = k.SendCoins(ctx, from, to, msg.Amount)
var coreAmount core.Coins
for _, coin := range msg.Amount {
coreAmount = append(coreAmount, core.Coin{
Denom: coin.Denom,
Amount: *coin.Amount.BigInt(),
})
}

err = k.SendCoins(ctx, from, to, coreAmount)
if err != nil {
return nil, err
}
Expand Down
7 changes: 5 additions & 2 deletions x/bank/keeper/send.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

"cosmossdk.io/collections"
"cosmossdk.io/core/appmodule"
core "cosmossdk.io/core/coin"
"cosmossdk.io/core/event"
errorsmod "cosmossdk.io/errors"
"cosmossdk.io/math"
Expand All @@ -26,7 +27,7 @@ type SendKeeper interface {
ClearSendRestriction()

InputOutputCoins(ctx context.Context, input types.Input, outputs []types.Output) error
SendCoins(ctx context.Context, fromAddr, toAddr sdk.AccAddress, amt sdk.Coins) error
SendCoins(ctx context.Context, fromAddr, toAddr sdk.AccAddress, amt core.Coins) error

GetParams(ctx context.Context) types.Params
SetParams(ctx context.Context, params types.Params) error
Expand Down Expand Up @@ -197,7 +198,9 @@ func (k BaseSendKeeper) InputOutputCoins(ctx context.Context, input types.Input,

// SendCoins transfers amt coins from a sending account to a receiving account.
// An error is returned upon failure.
func (k BaseSendKeeper) SendCoins(ctx context.Context, fromAddr, toAddr sdk.AccAddress, amt sdk.Coins) error {
func (k BaseSendKeeper) SendCoins(ctx context.Context, fromAddr, toAddr sdk.AccAddress, rawAmt core.Coins) error {
amt := sdk.NewCoreCoins(rawAmt)

if !amt.IsValid() {
return errorsmod.Wrap(sdkerrors.ErrInvalidCoins, amt.String())
}
Expand Down
Loading