-
Notifications
You must be signed in to change notification settings - Fork 618
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
Add func convert token to coin ibc #6584
Changes from 5 commits
a1c1677
fdc8113
7f6e451
2907999
cf41fd4
786ddf0
6a6a4e4
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 | ||||
---|---|---|---|---|---|---|
|
@@ -3,6 +3,7 @@ package types | |||||
import ( | ||||||
errorsmod "cosmossdk.io/errors" | ||||||
sdkmath "cosmossdk.io/math" | ||||||
sdk "github.com/cosmos/cosmos-sdk/types" | ||||||
) | ||||||
|
||||||
// Tokens is a slice of Tokens | ||||||
|
@@ -25,3 +26,19 @@ func (t Token) Validate() error { | |||||
|
||||||
return nil | ||||||
} | ||||||
|
||||||
// ToCoin converts a Token to an sdk.Coin. | ||||||
// | ||||||
// It takes a Token as a parameter and returns an sdk.Coin and an error. The function | ||||||
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.
Suggested change
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. tweaked this slightly more, feel free to do a suggestion if you feel its needed |
||||||
// parses the Amount field of the Token into an sdkmath.Int and creates a new | ||||||
// sdk.Coin with the IBCDenom of the Token's Denom field and the parsed Amount. | ||||||
// If the Amount cannot be parsed, an error is returned with a wrapped error message. | ||||||
func (t Token) ToCoin() (sdk.Coin, error) { | ||||||
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. It would be nice to have a couple of tests for this just so we have it covered and so that any changes in the future to this function would cover any issues. 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. @gjermundgaraba I just add unit test.
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. dont think we need to add cases where the parsing fails. The amount for each token is validated during initial transfer. Just covering the error case with the test case you added seems perfectly fine to me. 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. @DimitrisJim I got it. Thank you! |
||||||
transferAmount, ok := sdkmath.NewIntFromString(t.Amount) | ||||||
if !ok { | ||||||
return sdk.Coin{}, errorsmod.Wrapf(ErrInvalidAmount, "unable to parse transfer amount (%s) into math.Int", transferAmount) | ||||||
} | ||||||
|
||||||
coin := sdk.NewCoin(t.Denom.IBCDenom(), transferAmount) | ||||||
return coin, nil | ||||||
} |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -4,6 +4,8 @@ import ( | |||||
"fmt" | ||||||
"testing" | ||||||
|
||||||
sdkmath "cosmossdk.io/math" | ||||||
sdk "github.com/cosmos/cosmos-sdk/types" | ||||||
"github.com/stretchr/testify/require" | ||||||
) | ||||||
|
||||||
|
@@ -149,3 +151,50 @@ func TestValidate(t *testing.T) { | |||||
}) | ||||||
} | ||||||
} | ||||||
|
||||||
func TestToCoin(t *testing.T) { | ||||||
testCases := []struct { | ||||||
name string | ||||||
token Token | ||||||
coins sdk.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.
Suggested change
|
||||||
expError error | ||||||
}{ | ||||||
{ | ||||||
"success: convert token to coin", | ||||||
Token{ | ||||||
Denom: Denom{ | ||||||
Base: denom, | ||||||
Trace: []Trace{}, | ||||||
}, | ||||||
Amount: amount, | ||||||
}, | ||||||
sdk.NewCoin(denom, sdkmath.NewInt(100)), | ||||||
nil, | ||||||
}, | ||||||
{ | ||||||
"failure: invalid amount string", | ||||||
Token{ | ||||||
Denom: Denom{ | ||||||
Base: denom, | ||||||
Trace: []Trace{}, | ||||||
}, | ||||||
Amount: "value", | ||||||
}, | ||||||
sdk.Coin{}, | ||||||
ErrInvalidAmount, | ||||||
}, | ||||||
} | ||||||
|
||||||
for _, tc := range testCases { | ||||||
t.Run(tc.name, func(t *testing.T) { | ||||||
coin, err := tc.token.ToCoin() | ||||||
expPass := tc.expError == nil | ||||||
if expPass { | ||||||
require.NoError(t, err, tc.name) | ||||||
require.Equal(t, tc.coins, coin, tc.name) | ||||||
} else { | ||||||
require.ErrorContains(t, err, tc.expError.Error(), tc.name) | ||||||
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. You could also do here: require.Equal(t, tc.coins, coin, tc.name) To check that the coin returned in the failure case is empty. Which means that you could basically do: require.Equal(t, tc.expCoin, coin, tc.name)
if expPass {
require.NoError(t, err, tc.name)
} else {
require.ErrorContains(t, err, tc.expError.Error(), tc.name)
} 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. Thank you! |
||||||
} | ||||||
}) | ||||||
} | ||||||
} |
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.
ah, we modify the trace here so the hashed denom ends up different (ditto if not Source), we would need to swap out the coins Denomination to have this working 😞 we should do manual conversion here and ooth use
ToCoin
inforwarding.go
(which I think should be possible)Can take care of fixing these!