Skip to content

Commit

Permalink
feat(evm): Combine both account queries into "/eth.evm.v1.Query/EthAc…
Browse files Browse the repository at this point in the history
…count", accepting both nibi-prefixed Bech32 addresses and Ethereum-type hexadecimal addresses as input.
  • Loading branch information
Unique-Divine committed Aug 5, 2024
1 parent 7b8addd commit 82a1d33
Show file tree
Hide file tree
Showing 13 changed files with 326 additions and 819 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- [#1979](https://github.com/NibiruChain/nibiru/pull/1979) -refactor(db): use pebbledb as the default db in integration tests
- [#1981](https://github.com/NibiruChain/nibiru/pull/1981) - fix(evm): remove isCheckTx() short circuit on `AnteDecVerifyEthAcc`
- [#1985](https://github.com/NibiruChain/nibiru/pull/1985) - feat(evm)!: Use atto denomination for the wei units in the EVM so that NIBI is "ether" to clients. Only micronibi (unibi) amounts can be transferred. All clients follow the constraint equation, 1 ether == 1 NIBI == 10^6 unibi == 10^18 wei.
- [#1986](https://github.com/NibiruChain/nibiru/pull/1986) - feat(evm): Combine both account queries into "/eth.evm.v1.Query/EthAccount", accepting both nibi-prefixed Bech32 addresses and Ethereum-type hexadecimal addresses as input.

#### Dapp modules: perp, spot, oracle, etc

Expand Down
2 changes: 0 additions & 2 deletions eth/eth_account_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
)

func (s *Suite) TestEthAddrToNibiruAddr() {

accInfo := evmtest.NewEthAccInfo()
s.Equal(
accInfo.EthAddr,
Expand All @@ -19,7 +18,6 @@ func (s *Suite) TestEthAddrToNibiruAddr() {

s.T().Log("unit operation - hex -> nibi -> hex")
{

addr := evmtest.NewEthAccInfo().NibiruAddr
s.Equal(
addr,
Expand Down
29 changes: 0 additions & 29 deletions eth/rpc/backend/mocks/evm_query_client.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion eth/state_encoder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ import (
"testing"

"github.com/NibiruChain/collections"
"github.com/NibiruChain/nibiru/eth"
gethcommon "github.com/ethereum/go-ethereum/common"
"github.com/stretchr/testify/require"

"github.com/NibiruChain/nibiru/eth"
)

func assertBijectiveKey[T any](t *testing.T, encoder collections.KeyEncoder[T], key T) {
Expand Down
45 changes: 15 additions & 30 deletions proto/eth/evm/v1/query.proto
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,12 @@ option go_package = "github.com/NibiruChain/nibiru/x/evm";

// Query defines the gRPC querier service.
service Query {

Check failure on line 15 in proto/eth/evm/v1/query.proto

View workflow job for this annotation

GitHub Actions / break-check

Previously present RPC "NibiruAccount" on service "Query" was deleted.
// EthAccount queries an Ethereum account.
// EthAccount queries a Nibiru account using its EVM address or Bech32 Nibiru
// address.
rpc EthAccount(QueryEthAccountRequest) returns (QueryEthAccountResponse) {
option (google.api.http).get = "/nibiru/evm/v1/eth_account/{address}";
}

// NibiruAccount queries the Bech32 Nibiru address corresponding to Nibiru EVM account.
rpc NibiruAccount(QueryNibiruAccountRequest) returns (QueryNibiruAccountResponse) {
option (google.api.http).get = "/nibiru/evm/v1/nibiru_account/{address}";
}

// ValidatorAccount queries an Ethereum account's from a validator consensus
// Address.
rpc ValidatorAccount(QueryValidatorAccountRequest) returns (QueryValidatorAccountResponse) {
Expand Down Expand Up @@ -86,39 +82,28 @@ message QueryEthAccountRequest {
option (gogoproto.equal) = false;
option (gogoproto.goproto_getters) = false;

// address is the ethereum hex address to query the account for.
// address is the Ethereum hex address or nibi Bech32 address to query the account for.
string address = 1;
}

// QueryEthAccountResponse is the response type for the Query/Account RPC method.
// QueryEthAccountResponse is the response type for the Query/EthAccount RPC method.
message QueryEthAccountResponse {
// balance is the balance of unibi (micronibi).
string balance = 1;
// balance_wei is the balance of wei (attoether, where NIBI is ether).
string balance_wei = 1;
string balance_wei = 2;

Check failure on line 94 in proto/eth/evm/v1/query.proto

View workflow job for this annotation

GitHub Actions / break-check

Field "2" with name "balance_wei" on message "QueryEthAccountResponse" changed option "json_name" from "codeHash" to "balanceWei".

Check failure on line 94 in proto/eth/evm/v1/query.proto

View workflow job for this annotation

GitHub Actions / break-check

Field "2" on message "QueryEthAccountResponse" changed name from "code_hash" to "balance_wei".
// code_hash is the hex-formatted code bytes from the EOA.
string code_hash = 2;
string code_hash = 3;

Check failure on line 96 in proto/eth/evm/v1/query.proto

View workflow job for this annotation

GitHub Actions / break-check

Field "3" with name "code_hash" on message "QueryEthAccountResponse" changed option "json_name" from "nonce" to "codeHash".

Check failure on line 96 in proto/eth/evm/v1/query.proto

View workflow job for this annotation

GitHub Actions / break-check

Field "3" with name "code_hash" on message "QueryEthAccountResponse" changed type from "uint64" to "string".

Check failure on line 96 in proto/eth/evm/v1/query.proto

View workflow job for this annotation

GitHub Actions / break-check

Field "3" on message "QueryEthAccountResponse" changed name from "nonce" to "code_hash".
// nonce is the account's sequence number.
uint64 nonce = 3;
}

// QueryNibiruAccountRequest is the request type for the Query/NibiruAccount RPC
// method.
message QueryNibiruAccountRequest {
option (gogoproto.equal) = false;
option (gogoproto.goproto_getters) = false;
uint64 nonce = 4;

// address is the ethereum hex address to query the account for.
string address = 1;
}
// eth_address: The hexadecimal-encoded string representing the 20 byte address
// of a Nibiru EVM account.
string eth_address = 5;

// QueryNibiruAccountResponse is the response type for the Query/NibiruAccount
// RPC method.
message QueryNibiruAccountResponse {
// Nibiru bech32 account "address"
string address = 1;
// sequence is the account's sequence number.
uint64 sequence = 2;
// account_number is the account number
uint64 account_number = 3;
// bech32_address is the nibi-prefixed address of the account that can receive
// bank transfers ("cosmos.bank.v1beta1.MsgSend").
string bech32_address = 6;
}

// QueryValidatorAccountRequest is the request type for the
Expand Down
11 changes: 3 additions & 8 deletions x/evm/evm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,17 +129,12 @@ func (s *TestSuite) TestModuleAddressEVM() {
// EVM module should have mint perms
deps := evmtest.NewTestDeps()
{
_, err := deps.K.EthAccount(deps.GoCtx(), &evm.QueryEthAccountRequest{
resp, err := deps.K.EthAccount(deps.GoCtx(), &evm.QueryEthAccountRequest{
Address: evmModuleAddr.Hex(),
})
s.NoError(err)
}
{
resp, err := deps.K.NibiruAccount(deps.GoCtx(), &evm.QueryNibiruAccountRequest{
Address: evmModuleAddr.Hex(),
})
s.NoError(err)
s.Equal(nibiAddr.String(), resp.Address)
s.Equal(nibiAddr.String(), resp.Bech32Address)
s.Equal(evmModuleAddr.String(), resp.EthAddress)
}
}

Expand Down
6 changes: 1 addition & 5 deletions x/evm/evmtest/eth.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,13 @@ func NewEthAccInfo() EthPrivKeyAcc {
ethAddr := crypto.PubkeyToAddress(privKeyE.PublicKey)
return EthPrivKeyAcc{
EthAddr: ethAddr,
NibiruAddr: EthAddrToNibiruAddr(ethAddr),
NibiruAddr: eth.EthAddrToNibiruAddr(ethAddr),
PrivKey: privkey,
PrivKeyE: privKeyE,
KeyringSigner: NewSigner(privkey),
}
}

func EthAddrToNibiruAddr(ethAddr gethcommon.Address) sdk.AccAddress {
return ethAddr.Bytes()
}

type EthPrivKeyAcc struct {
EthAddr gethcommon.Address
NibiruAddr sdk.AccAddress
Expand Down
68 changes: 23 additions & 45 deletions x/evm/keeper/grpc_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,65 +37,43 @@ import (
var _ evm.QueryServer = &Keeper{}

// EthAccount: Implements the gRPC query for "/eth.evm.v1.Query/EthAccount".
// EthAccount retrieves the account details for a given Ethereum hex address.
// EthAccount retrieves the account and balance details for an account with the
// given address.
//
// Parameters:
// - goCtx: The context.Context object representing the request context.
// - req: Request containing the Ethereum hexadecimal address.
//
// Returns:
// - A pointer to the QueryEthAccountResponse object containing the account details.
// - An error if the account retrieval process encounters any issues.
// - req: Request containing the address in either Ethereum hexadecimal or
// Bech32 format.
func (k Keeper) EthAccount(
goCtx context.Context, req *evm.QueryEthAccountRequest,
) (*evm.QueryEthAccountResponse, error) {
if err := req.Validate(); err != nil {
isBech32, err := req.Validate()
if err != nil {
return nil, err
}

addr := gethcommon.HexToAddress(req.Address)
ctx := sdk.UnwrapSDKContext(goCtx)
acct := k.GetAccountOrEmpty(ctx, addr)

return &evm.QueryEthAccountResponse{
BalanceWei: evm.NativeToWei(acct.BalanceEvmDenom).String(),
CodeHash: gethcommon.BytesToHash(acct.CodeHash).Hex(),
Nonce: acct.Nonce,
}, nil
}
var addrEth gethcommon.Address
var addrBech32 sdk.AccAddress

// NibiruAccount: Implements the gRPC query for "/eth.evm.v1.Query/NibiruAccount".
// NibiruAccount retrieves the Cosmos account details for a given Ethereum address.
//
// Parameters:
// - goCtx: The context.Context object representing the request context.
// - req: The QueryNibiruAccountRequest object containing the Ethereum address.
//
// Returns:
// - A pointer to the QueryNibiruAccountResponse object containing the Cosmos account details.
// - An error if the account retrieval process encounters any issues.
func (k Keeper) NibiruAccount(
goCtx context.Context, req *evm.QueryNibiruAccountRequest,
) (resp *evm.QueryNibiruAccountResponse, err error) {
if err := req.Validate(); err != nil {
return resp, err
if isBech32 {
addrBech32 = sdk.MustAccAddressFromBech32(req.Address)
addrEth = eth.NibiruAddrToEthAddr(addrBech32)
} else {
addrEth = gethcommon.HexToAddress(req.Address)
addrBech32 = eth.EthAddrToNibiruAddr(addrEth)
}

ctx := sdk.UnwrapSDKContext(goCtx)
ethAddr := gethcommon.HexToAddress(req.Address)
nibiruAddr := sdk.AccAddress(ethAddr.Bytes())
acct := k.GetAccountOrEmpty(ctx, addrEth)

accountOrNil := k.accountKeeper.GetAccount(ctx, nibiruAddr)
resp = &evm.QueryNibiruAccountResponse{
Address: nibiruAddr.String(),
}

if accountOrNil != nil {
resp.Sequence = accountOrNil.GetSequence()
resp.AccountNumber = accountOrNil.GetAccountNumber()
}

return resp, nil
return &evm.QueryEthAccountResponse{
Balance: acct.BalanceEvmDenom.String(),
BalanceWei: evm.NativeToWei(acct.BalanceEvmDenom).String(),
CodeHash: gethcommon.BytesToHash(acct.CodeHash).Hex(),
Nonce: acct.Nonce,
EthAddress: addrEth.Hex(),
Bech32Address: addrBech32.String(),
}, nil
}

// ValidatorAccount: Implements the gRPC query for
Expand Down
Loading

0 comments on commit 82a1d33

Please sign in to comment.