Skip to content

Commit

Permalink
feat(evm): EVM fungible token protobufs and encoding tests (#1936)
Browse files Browse the repository at this point in the history
* refactor(e2e-evm): unused vars and better type hint

Squashed commit of the following:

commit 51071dad60c12e02093d92d1311d33b70b81304f
Author: Unique-Divine <[email protected]>
Date:   Mon Jun 10 12:32:00 2024 -0500

    refactor(e2e-evm): unused vars and better type hint

commit 6729b97
Author: Unique-Divine <[email protected]>
Date:   Fri Jun 7 22:46:14 2024 -0500

    test(e2e-evm): more type safety

commit 25c1557
Author: Unique-Divine <[email protected]>
Date:   Fri Jun 7 22:01:21 2024 -0500

    Squashed commit of the following:

    commit 021d161d176112cf24e28780ad64f61155f70ea2
    Author: Unique-Divine <[email protected]>
    Date:   Fri Jun 7 22:00:10 2024 -0500

        test(e2e): (1) Generated smart contract types for ethers. (2) TypeScript support. (3) Formatter

    commit af7e7b3
    Author: Unique-Divine <[email protected]>
    Date:   Fri Jun 7 16:11:45 2024 -0500

        chore: another issue ticket

    commit 36745fd
    Author: Unique-Divine <[email protected]>
    Date:   Fri Jun 7 16:07:20 2024 -0500

        chore: add issue number for TODO comment

    commit 8a76c0e
    Author: Unique-Divine <[email protected]>
    Date:   Fri Jun 7 15:54:07 2024 -0500

        refactor(evm): Remove dead code and document non-EVM ante handler

    commit e4e11df
    Author: Unique-Divine <[email protected]>
    Date:   Fri Jun 7 15:52:38 2024 -0500

        refactor: remove dead code

    commit cad00c0
    Merge: dc5f4dd 359e310
    Author: Unique-Divine <[email protected]>
    Date:   Fri Jun 7 15:41:53 2024 -0500

        Merge branch 'main' into ud/ante-test

    commit dc5f4dd
    Author: Unique-Divine <[email protected]>
    Date:   Fri Jun 7 15:28:53 2024 -0500

        refactor: ante handler and evm cleanup

    commit f73cdc3
    Merge: d3a6ea9 290c372
    Author: Unique-Divine <[email protected]>
    Date:   Wed Jun 5 20:59:39 2024 -0500

        Merge branch 'test/evm-grpc-query' of https://github.com/NibiruChain/nibiru into test/evm-grpc-query

    commit d3a6ea9
    Merge: 376596d 70ee1bf
    Author: Unique-Divine <[email protected]>
    Date:   Wed Jun 5 20:59:30 2024 -0500

        Merge branch 'main' into test/evm-grpc-query

    commit 376596d
    Author: Unique-Divine <[email protected]>
    Date:   Wed Jun 5 20:58:40 2024 -0500

        Squashed commit of the following:

        commit b5687130ff5f3d020a3b14d219fec3a816579c30
        Author: Unique-Divine <[email protected]>
        Date:   Wed Jun 5 20:57:44 2024 -0500

            chore: run tidy

        commit 1f1f938
        Merge: 3e3cc83 bbcc6f8
        Author: Unique-Divine <[email protected]>
        Date:   Wed Jun 5 19:16:30 2024 -0500

            Merge branch 'main' into ud/fix-race-condition

        commit 3e3cc83
        Author: Unique-Divine <[email protected]>
        Date:   Wed Jun 5 19:15:40 2024 -0500

            chore: changelog

        commit 3876ccb
        Author: Unique-Divine <[email protected]>
        Date:   Wed Jun 5 19:04:00 2024 -0500

            refactor: more consistent test names

        commit aaa0a19
        Author: Unique-Divine <[email protected]>
        Date:   Wed Jun 5 18:53:09 2024 -0500

            test(oracle): Fix missing tear down step for oracle integration test

        commit 8c3c35e
        Author: Unique-Divine <[email protected]>
        Date:   Wed Jun 5 17:55:56 2024 -0500

            chore: add test comands to justfile

        commit 4916282
        Merge: 64ed0a2 e7e708d
        Author: Unique-Divine <[email protected]>
        Date:   Fri May 31 09:35:33 2024 -0500

            Merge branch 'main' into ud/fix-race-condition

        commit 64ed0a2
        Author: Unique-Divine <[email protected]>
        Date:   Fri May 31 01:44:55 2024 -0500

            fix(gosdk): tests parallel race condition

    commit 290c372
    Merge: 0d1c894 70ee1bf
    Author: Unique Divine <[email protected]>
    Date:   Wed Jun 5 20:05:19 2024 -0500

        Merge branch 'main' into test/evm-grpc-query

    commit 0d1c894
    Merge: 9170835 ad173e9
    Author: Unique Divine <[email protected]>
    Date:   Wed Jun 5 19:34:38 2024 -0500

        Merge branch 'main' into test/evm-grpc-query

    commit 9170835
    Author: Oleg Nikonychev <[email protected]>
    Date:   Wed Jun 5 13:55:14 2024 +0400

        fix: removed hardcoded gas value in grpc_query test

    commit 4337858
    Author: Oleg Nikonychev <[email protected]>
    Date:   Wed Jun 5 13:14:34 2024 +0400

        chore: refactored eth util methods

    commit 7df84e2
    Merge: 8918498 bbcc6f8
    Author: Oleg Nikonychev <[email protected]>
    Date:   Wed Jun 5 12:23:51 2024 +0400

        chore: resolve conflicts

    commit 8918498
    Merge: 3fd45ce e7e708d
    Author: Oleg Nikonychev <[email protected]>
    Date:   Mon Jun 3 21:56:39 2024 +0400

        Merge branch 'main' into test/evm-grpc-query

    commit 3fd45ce
    Author: Oleg Nikonychev <[email protected]>
    Date:   Mon Jun 3 21:56:23 2024 +0400

        chore: changelog update

    commit 3348876
    Author: Oleg Nikonychev <[email protected]>
    Date:   Mon Jun 3 21:53:54 2024 +0400

        test(evm): grpc_query full coverage

* wip!: save progress

* chore: changelog

* chore: linter

* feat(eth): finish todos for hex.go

* feat(eth): finalize protobuf impl for eth.HexAddr

* chore: small typos in comments

---------

Co-authored-by: Kevin Yang <[email protected]>
  • Loading branch information
Unique-Divine and k-yang authored Jun 26, 2024
1 parent 8db3e89 commit 422c676
Show file tree
Hide file tree
Showing 18 changed files with 1,147 additions and 174 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- [#1914](https://github.com/NibiruChain/nibiru/pull/1914) - refactor(evm): Remove dead code and document non-EVM ante handler- [#1917](https://github.com/NibiruChain/nibiru/pull/1917) - test(e2e-evm): TypeScript support. Type generation from compiled contracts. Formatter for TS code.
- [#1917](https://github.com/NibiruChain/nibiru/pull/1917) - test(e2e-evm): TypeScript support. Type generation from compiled contracts. Formatter for TS code.
- [#1922](https://github.com/NibiruChain/nibiru/pull/1922) - feat(evm): tracer option is read from the config.
- [#1936](https://github.com/NibiruChain/nibiru/pull/1936) - feat(evm): EVM fungible token protobufs and encoding tests

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

Expand Down
7 changes: 3 additions & 4 deletions e2e/evm/test/contract_send_nibi.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import { describe, it, expect, beforeAll } from "bun:test" // eslint-disable-line import/no-unresolved
import { AddressLike, ethers } from "ethers"
import { describe, it, expect } from "bun:test" // eslint-disable-line import/no-unresolved
import { ethers } from "ethers"
import { account, provider, deployContract } from "./setup"
import { SendNibiCompiled } from "../types/ethers-contracts"
import { TypedContractMethod } from "../types/ethers-contracts/common"

type SendMethod = TypedContractMethod<[_to: AddressLike], [void], "payable">
type SendMethod = SendNibiCompiled["sendViaCall"]

const doContractSend = async (sendMethod: SendMethod) => {
const recipientAddress = ethers.Wallet.createRandom().address
Expand Down
128 changes: 128 additions & 0 deletions eth/hex.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
package eth

import (
"encoding/json"
"fmt"

sdk "github.com/cosmos/cosmos-sdk/types"
gethcommon "github.com/ethereum/go-ethereum/common"
)

/////////// HexAddr

// HexAddr: An ERC55-compliant hexadecimal-encoded string representing the 20
// byte address of an Ethereum account.
type HexAddr string

var _ sdk.CustomProtobufType = (*HexAddr)(nil)

func NewHexAddr(addr EthAddr) HexAddr {
return HexAddr(addr.Hex())
}

func NewHexAddrFromStr(addr string) (HexAddr, error) {
hexAddr := HexAddr(gethcommon.HexToAddress(addr).Hex())
if !gethcommon.IsHexAddress(addr) {
return hexAddr, fmt.Errorf(
"%s: input \"%s\" is not an ERC55-compliant, 20 byte hex address",
HexAddrError, addr,
)
}
return hexAddr, hexAddr.Valid()
}

// MustNewHexAddrFromStr is the same as [NewHexAddrFromStr], except it panics
// when there's an error.
func MustNewHexAddrFromStr(addr string) HexAddr {
hexAddr, err := NewHexAddrFromStr(addr)
if err != nil {
panic(err)
}
return hexAddr
}

const HexAddrError = "HexAddrError"

func (h HexAddr) Valid() error {
// Check address encoding bijectivity
wantAddr := h.ToAddr().Hex() // gethcommon.Address.Hex()
haveAddr := string(h) // should be equivalent to ↑

if !gethcommon.IsHexAddress(haveAddr) || haveAddr != wantAddr {
return fmt.Errorf(
"%s: Ethereum address is not represented as expected. We have encoding \"%s\" and instead need \"%s\" (gethcommon.Address.Hex)",
HexAddrError, haveAddr, wantAddr,
)
}

return nil
}

func (h HexAddr) ToAddr() EthAddr {
return gethcommon.HexToAddress(string(h))
}

// ToBytes gets the string representation of the underlying address.
func (h HexAddr) ToBytes() []byte {
return h.ToAddr().Bytes()
}

func (h HexAddr) String() string { return h.ToAddr().Hex() }

// Marshal implements the gogo proto custom type interface.
// Ref: https://github.com/cosmos/gogoproto/blob/v1.5.0/custom_types.md
func (h HexAddr) Marshal() ([]byte, error) {
return []byte(h), nil
}

// MarshalJSON returns the [HexAddr] as JSON bytes.
// Implements the gogo proto custom type interface.
// Ref: https://github.com/cosmos/gogoproto/blob/v1.5.0/custom_types.md
func (h HexAddr) MarshalJSON() ([]byte, error) {
return []byte("\"" + h.String() + "\""), nil // a string is already JSON
}

// MarshalTo serializes a pre-allocated byte slice ("data") directly into the
// [HexAddr] value, avoiding unnecessary memory allocations.
// MarshalTo implements the gogo proto custom type interface.
// Implements the gogo proto custom type interface.
// Ref: https://github.com/cosmos/gogoproto/blob/v1.5.0/custom_types.md
func (h *HexAddr) MarshalTo(data []byte) (n int, err error) {
bz := []byte{}
copy(data, bz)
hexAddr, err := NewHexAddrFromStr(string(bz))
*h = hexAddr
return h.Size(), err
}

// Unmarshal implements the gogo proto custom type interface.
// Ref: https://github.com/cosmos/gogoproto/blob/v1.5.0/custom_types.md
func (h *HexAddr) Unmarshal(data []byte) error {
hexAddr, err := NewHexAddrFromStr(string(data))
*h = hexAddr
return err
}

// UnmarshalJSON implements the gogo proto custom type interface.
// Ref: https://github.com/cosmos/gogoproto/blob/v1.5.0/custom_types.md
func (h *HexAddr) UnmarshalJSON(bz []byte) error {
text := new(string)
if err := json.Unmarshal(bz, text); err != nil {
return err
}

hexAddr, err := NewHexAddrFromStr(*text)
if err != nil {
return err
}

*h = hexAddr

return nil
}

// Size implements the gogo proto custom type interface.
// Ref: https://github.com/cosmos/gogoproto/blob/v1.5.0/custom_types.md
func (h HexAddr) Size() int {
return len(h)
}
Loading

0 comments on commit 422c676

Please sign in to comment.