-
Notifications
You must be signed in to change notification settings - Fork 202
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(evm-cli): Add the account query to the EVM command. Cover the CL…
…I with tests. (#2002) * feat(evm-cli): expose account query * undo commit of events code * test(evm-cli): more tests * changelog * refactor: small edits after self-review * chore: typo
- Loading branch information
1 parent
924f9c4
commit 95ba4af
Showing
9 changed files
with
438 additions
and
73 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
package cli_test | ||
|
||
import ( | ||
"context" | ||
"io" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/suite" | ||
|
||
rpcclientmock "github.com/cometbft/cometbft/rpc/client/mock" | ||
sdkclient "github.com/cosmos/cosmos-sdk/client" | ||
"github.com/cosmos/cosmos-sdk/crypto/keyring" | ||
sdktestutil "github.com/cosmos/cosmos-sdk/testutil" | ||
sdktestutilcli "github.com/cosmos/cosmos-sdk/testutil/cli" | ||
testutilmod "github.com/cosmos/cosmos-sdk/types/module/testutil" | ||
|
||
svrcmd "github.com/cosmos/cosmos-sdk/server/cmd" | ||
|
||
"github.com/NibiruChain/nibiru/v2/x/evm/cli" | ||
"github.com/NibiruChain/nibiru/v2/x/evm/evmmodule" | ||
) | ||
|
||
type Suite struct { | ||
suite.Suite | ||
|
||
keyring keyring.Keyring | ||
encCfg testutilmod.TestEncodingConfig | ||
baseCtx sdkclient.Context | ||
clientCtx sdkclient.Context | ||
|
||
testAcc sdktestutil.TestAccount | ||
} | ||
|
||
func (s *Suite) SetupSuite() { | ||
s.encCfg = testutilmod.MakeTestEncodingConfig(evmmodule.AppModuleBasic{}) | ||
s.keyring = keyring.NewInMemory(s.encCfg.Codec) | ||
s.baseCtx = sdkclient.Context{}. | ||
WithKeyring(s.keyring). | ||
WithTxConfig(s.encCfg.TxConfig). | ||
WithCodec(s.encCfg.Codec). | ||
WithClient(sdktestutilcli.MockTendermintRPC{Client: rpcclientmock.Client{}}). | ||
WithAccountRetriever(sdkclient.MockAccountRetriever{}). | ||
WithOutput(io.Discard). | ||
WithChainID("test-chain") | ||
|
||
s.clientCtx = s.baseCtx | ||
|
||
testAccs := sdktestutil.CreateKeyringAccounts(s.T(), s.keyring, 1) | ||
s.testAcc = testAccs[0] | ||
} | ||
|
||
func TestSuite(t *testing.T) { | ||
suite.Run(t, new(Suite)) | ||
} | ||
|
||
// Flags for broadcasting transactions | ||
func commonTxArgs() []string { | ||
return []string{ | ||
"--yes=true", // skip confirmation | ||
"--broadcast-mode=sync", | ||
"--fees=1unibi", | ||
"--chain-id=test-chain", | ||
} | ||
} | ||
|
||
type TestCase struct { | ||
name string | ||
args []string | ||
extraArgs []string | ||
wantErr string | ||
} | ||
|
||
func (tc TestCase) NewCtx(s *Suite) sdkclient.Context { | ||
return s.baseCtx | ||
} | ||
|
||
func (tc TestCase) RunTxCmd(s *Suite) { | ||
s.Run(tc.name, func() { | ||
ctx := svrcmd.CreateExecuteContext(context.Background()) | ||
|
||
cmd := cli.GetTxCmd() | ||
cmd.SetContext(ctx) | ||
args := append(tc.args, commonTxArgs()...) | ||
cmd.SetArgs(append(args, tc.extraArgs...)) | ||
|
||
s.Require().NoError(sdkclient.SetCmdClientContextHandler(tc.NewCtx(s), cmd)) | ||
|
||
err := cmd.Execute() | ||
if tc.wantErr != "" { | ||
s.Require().ErrorContains(err, tc.wantErr) | ||
return | ||
} | ||
s.Require().NoError(err) | ||
}) | ||
} | ||
|
||
func (tc TestCase) RunQueryCmd(s *Suite) { | ||
s.Run(tc.name, func() { | ||
ctx := svrcmd.CreateExecuteContext(context.Background()) | ||
|
||
cmd := cli.GetQueryCmd() | ||
cmd.SetContext(ctx) | ||
args := tc.args // don't append common tx args | ||
cmd.SetArgs(append(args, tc.extraArgs...)) | ||
|
||
s.Require().NoError(sdkclient.SetCmdClientContextHandler(tc.NewCtx(s), cmd)) | ||
|
||
err := cmd.Execute() | ||
if tc.wantErr != "" { | ||
s.Require().ErrorContains(err, tc.wantErr) | ||
return | ||
} | ||
s.Require().NoError(err) | ||
}) | ||
} |
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,173 @@ | ||
package cli_test | ||
|
||
import ( | ||
"fmt" | ||
"math/big" | ||
|
||
gethcommon "github.com/ethereum/go-ethereum/common" | ||
|
||
"github.com/NibiruChain/nibiru/v2/x/evm" | ||
"github.com/NibiruChain/nibiru/v2/x/evm/evmtest" | ||
) | ||
|
||
var ( | ||
dummyAccs = evmtest.NewEthPrivAccs(3) | ||
dummyEthAddr = dummyAccs[1].EthAddr.Hex() | ||
dummyFuntoken = evm.NewFunToken( | ||
gethcommon.BigToAddress(big.NewInt(123)), | ||
"ibc/testtoken", | ||
false, | ||
) | ||
) | ||
|
||
func (s *Suite) TestCmdConvertCoinToEvm() { | ||
testCases := []TestCase{ | ||
{ | ||
name: "happy: convert-coin-to-evm", | ||
args: []string{ | ||
"convert-coin-to-evm", | ||
dummyEthAddr, | ||
fmt.Sprintf("%d%s", 123, dummyFuntoken.BankDenom), | ||
}, | ||
extraArgs: []string{fmt.Sprintf("--from=%s", s.testAcc.Address)}, | ||
wantErr: "", | ||
}, | ||
{ | ||
name: "sad: coin format", | ||
args: []string{ | ||
"convert-coin-to-evm", | ||
dummyAccs[1].EthAddr.Hex(), | ||
fmt.Sprintf("%s %d", dummyFuntoken.BankDenom, 123), | ||
}, | ||
extraArgs: []string{fmt.Sprintf("--from=%s", s.testAcc.Address)}, | ||
wantErr: "invalid decimal coin expression", | ||
}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
tc.RunTxCmd(s) | ||
} | ||
} | ||
|
||
func (s *Suite) TestCmdCreateFunToken() { | ||
testCases := []TestCase{ | ||
{ | ||
name: "happy: create-funtoken (erc20)", | ||
args: []string{ | ||
"create-funtoken", | ||
fmt.Sprintf("--erc20=%s", dummyEthAddr), | ||
}, | ||
extraArgs: []string{fmt.Sprintf("--from=%s", s.testAcc.Address)}, | ||
wantErr: "", | ||
}, | ||
{ | ||
name: "happy: create-funtoken (bank coin)", | ||
args: []string{ | ||
"create-funtoken", | ||
fmt.Sprintf("--bank-denom=%s", dummyFuntoken.BankDenom), | ||
}, | ||
extraArgs: []string{fmt.Sprintf("--from=%s", s.testAcc.Address)}, | ||
wantErr: "", | ||
}, | ||
{ | ||
name: "sad: too many args", | ||
args: []string{ | ||
"create-funtoken", | ||
fmt.Sprintf("--erc20=%s", dummyEthAddr), | ||
fmt.Sprintf("--bank-denom=%s", dummyFuntoken.BankDenom), | ||
}, | ||
extraArgs: []string{fmt.Sprintf("--from=%s", s.testAcc.Address)}, | ||
wantErr: "exactly one of the flags --bank-denom or --erc20 must be specified", | ||
}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
tc.RunTxCmd(s) | ||
} | ||
} | ||
|
||
func (s *Suite) TestCmdQueryAccount() { | ||
testCases := []TestCase{ | ||
{ | ||
name: "happy: query account (bech32)", | ||
args: []string{ | ||
"account", | ||
dummyAccs[0].NibiruAddr.String(), | ||
}, | ||
wantErr: "", | ||
}, | ||
{ | ||
name: "happy: query account (eth hex)", | ||
args: []string{ | ||
"account", | ||
dummyAccs[0].EthAddr.Hex(), | ||
}, | ||
wantErr: "", | ||
}, | ||
{ | ||
name: "happy: query account (eth hex) --offline", | ||
args: []string{ | ||
"account", | ||
dummyAccs[0].EthAddr.Hex(), | ||
"--offline", | ||
}, | ||
wantErr: "", | ||
}, | ||
{ | ||
name: "happy: query account (bech32) --offline", | ||
args: []string{ | ||
"account", | ||
dummyAccs[0].NibiruAddr.String(), | ||
"--offline", | ||
}, | ||
wantErr: "", | ||
}, | ||
{ | ||
name: "sad: too many args", | ||
args: []string{ | ||
"funtoken", | ||
"arg1", | ||
"arg2", | ||
}, | ||
wantErr: "accepts 1 arg", | ||
}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
tc.RunQueryCmd(s) | ||
} | ||
} | ||
|
||
func (s *Suite) TestCmdQueryFunToken() { | ||
testCases := []TestCase{ | ||
{ | ||
name: "happy: query funtoken (bank coin denom)", | ||
args: []string{ | ||
"funtoken", | ||
dummyFuntoken.BankDenom, | ||
}, | ||
wantErr: "", | ||
}, | ||
{ | ||
name: "happy: query funtoken (erc20 addr)", | ||
args: []string{ | ||
"funtoken", | ||
dummyFuntoken.Erc20Addr.String(), | ||
}, | ||
wantErr: "", | ||
}, | ||
{ | ||
name: "sad: too many args", | ||
args: []string{ | ||
"funtoken", | ||
"arg1", | ||
"arg2", | ||
}, | ||
wantErr: "accepts 1 arg", | ||
}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
tc.RunQueryCmd(s) | ||
} | ||
} |
Oops, something went wrong.