-
Notifications
You must be signed in to change notification settings - Fork 3.8k
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
base: main
Are you sure you want to change the base?
feat: add coin types to core #23346
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 |
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -9,6 +9,7 @@ | |||||||||||||||||||||||||||||||||
"strings" | ||||||||||||||||||||||||||||||||||
"unicode" | ||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
core "cosmossdk.io/core/coin" | ||||||||||||||||||||||||||||||||||
Check failure on line 12 in types/coin.go GitHub Actions / dependency-review
Check failure on line 12 in types/coin.go GitHub Actions / dependency-review
Check failure on line 12 in types/coin.go GitHub Actions / dependency-review
Check failure on line 12 in types/coin.go GitHub Actions / dependency-review
Check failure on line 12 in types/coin.go GitHub Actions / dependency-review
Check failure on line 12 in types/coin.go GitHub Actions / dependency-review
Check failure on line 12 in types/coin.go GitHub Actions / dependency-review
|
||||||||||||||||||||||||||||||||||
"cosmossdk.io/math" | ||||||||||||||||||||||||||||||||||
) | ||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
Suggested change
|
||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
// 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 { | ||||||||||||||||||||||||||||||||||
|
@@ -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 { | ||||||||||||||||||||||||||||||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,7 @@ package keeper | |
|
||
import ( | ||
"context" | ||
|
||
core "cosmossdk.io/core/coin" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add missing module dependency The Add the following to your go.mod file: + require cosmossdk.io/core v0.1.0
|
||
errorsmod "cosmossdk.io/errors" | ||
"cosmossdk.io/x/bank/types" | ||
|
||
|
@@ -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 | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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 )
🧰 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