-
Notifications
You must be signed in to change notification settings - Fork 3.6k
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
REST bank transfers fail due to encoding. #6536
Merged
Merged
Changes from all commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
3cde1e4
test
jgimeno 923f70a
refactor
jgimeno fe402e3
fix problem with rest endpoint for getting tx
jgimeno eeb1cd3
Make test pass.
jgimeno b01b40a
add authclient codec
jgimeno d1d1671
Remove config from default
jgimeno 24c4d80
Use tx generator for api.
jgimeno 1947662
Merge branch 'master' into jonathan/bank-accounts-transfer-test
jgimeno 85ce955
Add changelog
jgimeno 51342d7
Merge branch 'jonathan/bank-accounts-transfer-test' of github.com:cos…
jgimeno 46c7f9a
Put changelog entry on top of bugs.
jgimeno 2ca3a34
Merge branch 'master' into jonathan/bank-accounts-transfer-test
jgimeno 4015e17
rename imports
jgimeno daf47f2
create post request method
jgimeno 7d7b229
generate post method 2
jgimeno ed16572
remove useless check
jgimeno 769cd24
Use go errors instead of Sdk Errors.
jgimeno 7b808ef
Change Changelog message.
jgimeno f28267a
Remove comment.
jgimeno 75ad103
Add nolint for post request.
jgimeno 1bd7137
Merge branch 'master' into jonathan/bank-accounts-transfer-test
alexanderbez b2d917e
Merge branch 'master' into jonathan/bank-accounts-transfer-test
jgimeno File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
package rest_test | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/cosmos/cosmos-sdk/simapp" | ||
"github.com/cosmos/cosmos-sdk/testutil" | ||
"github.com/cosmos/cosmos-sdk/types" | ||
"github.com/cosmos/cosmos-sdk/types/errors" | ||
"github.com/cosmos/cosmos-sdk/types/rest" | ||
authclient "github.com/cosmos/cosmos-sdk/x/auth/client" | ||
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" | ||
bankrest "github.com/cosmos/cosmos-sdk/x/bank/client/rest" | ||
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types" | ||
) | ||
|
||
func (s *IntegrationTestSuite) TestCoinSend() { | ||
encodingConfig := simapp.MakeEncodingConfig() | ||
authclient.Codec = encodingConfig.Marshaler | ||
|
||
val := s.network.Validators[0] | ||
|
||
account, err := getAccountInfo(val) | ||
s.Require().NoError(err) | ||
|
||
sendReq := generateSendReq( | ||
account, | ||
types.Coins{types.NewCoin(s.cfg.BondDenom, types.TokensFromConsensusPower(1))}, | ||
) | ||
|
||
stdTx, err := submitSendReq(val, sendReq) | ||
s.Require().NoError(err) | ||
|
||
s.Require().Nil(stdTx.Signatures) | ||
s.Require().Equal([]types.Msg{ | ||
&banktypes.MsgSend{ | ||
FromAddress: account.GetAddress(), | ||
ToAddress: account.GetAddress(), | ||
Amount: sendReq.Amount, | ||
}, | ||
}, stdTx.GetMsgs()) | ||
} | ||
|
||
func submitSendReq(val *testutil.Validator, req bankrest.SendReq) (authtypes.StdTx, error) { | ||
url := fmt.Sprintf("%s/bank/accounts/%s/transfers", val.APIAddress, val.Address) | ||
|
||
bz, err := val.ClientCtx.JSONMarshaler.MarshalJSON(req) | ||
if err != nil { | ||
return authtypes.StdTx{}, errors.Wrap(err, "error encoding SendReq to json") | ||
} | ||
|
||
res, err := rest.PostRequest(url, "application/json", bz) | ||
if err != nil { | ||
return authtypes.StdTx{}, err | ||
} | ||
|
||
var tx authtypes.StdTx | ||
err = val.ClientCtx.JSONMarshaler.UnmarshalJSON(res, &tx) | ||
if err != nil { | ||
return authtypes.StdTx{}, errors.Wrap(err, "error unmarshaling to StdTx SendReq response") | ||
} | ||
|
||
return tx, nil | ||
} | ||
|
||
func generateSendReq(from authtypes.AccountI, amount types.Coins) bankrest.SendReq { | ||
baseReq := rest.NewBaseReq( | ||
from.GetAddress().String(), | ||
"someMemo", | ||
"some-id", | ||
"10000", | ||
fmt.Sprintf("%f", 1.0), | ||
from.GetAccountNumber(), | ||
from.GetSequence(), | ||
types.NewCoins(), | ||
nil, | ||
false, | ||
) | ||
|
||
return bankrest.SendReq{ | ||
BaseReq: baseReq, | ||
Amount: amount, | ||
} | ||
} | ||
|
||
func getAccountInfo(val *testutil.Validator) (authtypes.AccountI, error) { | ||
url := fmt.Sprintf("%s/auth/accounts/%s", val.APIAddress, val.Address) | ||
|
||
resp, err := rest.GetRequest(url) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
bz, err := rest.ParseResponseWithHeight(val.ClientCtx.JSONMarshaler, resp) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
var acc authtypes.AccountI | ||
err = val.ClientCtx.JSONMarshaler.UnmarshalJSON(bz, &acc) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return acc, nil | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
I don't think this bug fix entry is actually needed because it wasn't broken before... just noting.