Skip to content

Commit

Permalink
RPC Get Evm Hash (sei-protocol#1713)
Browse files Browse the repository at this point in the history
* RPC Get Evm Hash

* Add mock tm client for Tx()
  • Loading branch information
Kbhat1 authored Jun 5, 2024
1 parent 083588f commit 8884343
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 5 deletions.
5 changes: 4 additions & 1 deletion contracts/test/EVMCompatabilityTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const {isBigNumber} = require("hardhat/common");
const {uniq, shuffle} = require("lodash");
const { ethers, upgrades } = require('hardhat');
const { getImplementationAddress } = require('@openzeppelin/upgrades-core');
const { deployEvmContract, setupSigners, fundAddress, getCosmosTx} = require("./lib")
const { deployEvmContract, setupSigners, fundAddress, getCosmosTx, getEvmTx} = require("./lib")
const axios = require("axios");

function sleep(ms) {
Expand Down Expand Up @@ -320,6 +320,9 @@ describe("EVM Test", function () {
const cosmosTx = await getCosmosTx(ethers.provider, receipt.hash)
expect(cosmosTx.length).to.be.equal(64)

const evmTx = await getEvmTx(ethers.provider, cosmosTx)
expect(evmTx).to.be.equal(receipt.hash)

await expect(txResponse)
.to.emit(evmTester, 'StringSet')
.withArgs(owner.address, "test");
Expand Down
5 changes: 5 additions & 0 deletions contracts/test/lib.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ async function getCosmosTx(provider, evmTxHash) {
return await provider.send("sei_getCosmosTx", [evmTxHash])
}

async function getEvmTx(provider, cosmosTxHash) {
return await provider.send("sei_getEvmTx", [cosmosTxHash])
}

async function fundAddress(addr, amount="10000000000000000000") {
const result = await evmSend(addr, adminKeyName, amount)
await delay()
Expand Down Expand Up @@ -469,6 +473,7 @@ module.exports = {
evmSend,
waitForReceipt,
getCosmosTx,
getEvmTx,
isDocker,
testAPIEnabled,
incrementPointerVersion,
Expand Down
19 changes: 19 additions & 0 deletions evmrpc/association.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,3 +160,22 @@ func (t *AssociationAPI) GetCosmosTx(ctx context.Context, ethHash common.Hash) (
}
return "", fmt.Errorf("transaction not found")
}

func (t *AssociationAPI) GetEvmTx(ctx context.Context, cosmosHash string) (result string, returnErr error) {
startTime := time.Now()
defer recordMetrics("sei_getEvmTx", t.connectionType, startTime, returnErr == nil)
hashBytes, err := hex.DecodeString(cosmosHash)
if err != nil {
return "", fmt.Errorf("failed to decode cosmosHash: %w", err)
}

txResponse, err := t.tmClient.Tx(ctx, hashBytes, false)
if err != nil {
return "", err
}
if txResponse.TxResult.EvmTxInfo == nil {
return "", fmt.Errorf("transaction not found")
}

return txResponse.TxResult.EvmTxInfo.TxHash, nil
}
14 changes: 10 additions & 4 deletions evmrpc/association_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,21 +31,27 @@ func TestAssocation(t *testing.T) {
}

body := sendRequestGoodWithNamespace(t, "sei", "associate", txArgs)
require.Equal(t, body["result"], nil)
require.Equal(t, nil, body["result"])
}

func TestGetSeiAddress(t *testing.T) {
body := sendRequestGoodWithNamespace(t, "sei", "getSeiAddress", "0x1df809C639027b465B931BD63Ce71c8E5834D9d6")
require.Equal(t, body["result"], "sei1mf0llhmqane5w2y8uynmghmk2w4mh0xll9seym")
require.Equal(t, "sei1mf0llhmqane5w2y8uynmghmk2w4mh0xll9seym", body["result"])
}

func TestGetEvmAddress(t *testing.T) {
body := sendRequestGoodWithNamespace(t, "sei", "getEVMAddress", "sei1mf0llhmqane5w2y8uynmghmk2w4mh0xll9seym")
require.Equal(t, body["result"], "0x1df809C639027b465B931BD63Ce71c8E5834D9d6")
require.Equal(t, "0x1df809C639027b465B931BD63Ce71c8E5834D9d6", body["result"])
}

func TestGetCosmosTx(t *testing.T) {
body := sendRequestGoodWithNamespace(t, "sei", "getCosmosTx", "0xf02362077ac075a397344172496b28e913ce5294879d811bb0269b3be20a872e")
fmt.Println(body)
require.Equal(t, body["result"], "690D39ADF56D4C811B766DFCD729A415C36C4BFFE80D63E305373B9518EBFB14")
require.Equal(t, "690D39ADF56D4C811B766DFCD729A415C36C4BFFE80D63E305373B9518EBFB14", body["result"])
}

func TestGetEvmTx(t *testing.T) {
body := sendRequestGoodWithNamespace(t, "sei", "getEvmTx", "690D39ADF56D4C811B766DFCD729A415C36C4BFFE80D63E305373B9518EBFB14")
fmt.Println(body)
require.Equal(t, "0xf02362077ac075a397344172496b28e913ce5294879d811bb0269b3be20a872e", body["result"])
}
7 changes: 7 additions & 0 deletions evmrpc/setup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ const DebugTraceMockHeight = 101
var DebugTraceHashHex = "0x1234567890123456789023456789012345678901234567890123456789000004"
var DebugTraceBlockHash = "BE17E0261E539CB7E9A91E123A6D794E0163D656FCF9B8EAC07823F7ED28512B"

var TestCosmosTxHash = "690D39ADF56D4C811B766DFCD729A415C36C4BFFE80D63E305373B9518EBFB14"
var TestEvmTxHash = "0xf02362077ac075a397344172496b28e913ce5294879d811bb0269b3be20a872e"

var EncodingConfig = app.MakeEncodingConfig()
var TxConfig = EncodingConfig.TxConfig
var Encoder = TxConfig.TxEncoder()
Expand Down Expand Up @@ -356,6 +359,10 @@ func (c *MockClient) BroadcastTx(context.Context, tmtypes.Tx) (*coretypes.Result
return &coretypes.ResultBroadcastTx{Code: 0, Hash: []byte("0x123")}, nil
}

func (c *MockClient) Tx(context.Context, bytes.HexBytes, bool) (*coretypes.ResultTx, error) {
return &coretypes.ResultTx{Hash: bytes.HexBytes(TestCosmosTxHash), Height: MockHeight, TxResult: abci.ExecTxResult{EvmTxInfo: &abci.EvmTxInfo{TxHash: TestEvmTxHash}}}, nil
}

func (c *MockClient) UnconfirmedTxs(ctx context.Context, page, perPage *int) (*coretypes.ResultUnconfirmedTxs, error) {
tx, _ := Encoder(UnconfirmedTx)
return &coretypes.ResultUnconfirmedTxs{
Expand Down

0 comments on commit 8884343

Please sign in to comment.