Skip to content

Commit

Permalink
add sei2_getBlock endpoints to include bank transfers
Browse files Browse the repository at this point in the history
  • Loading branch information
codchen committed Jan 31, 2025
1 parent 6788468 commit b082a18
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 5 deletions.
69 changes: 67 additions & 2 deletions evmrpc/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,14 @@ import (
wasmtypes "github.com/CosmWasm/wasmd/x/wasm/types"
"github.com/cosmos/cosmos-sdk/client"
sdk "github.com/cosmos/cosmos-sdk/types"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
ethtypes "github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/lib/ethapi"
"github.com/ethereum/go-ethereum/rpc"
"github.com/sei-protocol/sei-chain/x/evm/keeper"
"github.com/sei-protocol/sei-chain/x/evm/state"
"github.com/sei-protocol/sei-chain/x/evm/types"
rpcclient "github.com/tendermint/tendermint/rpc/client"
"github.com/tendermint/tendermint/rpc/coretypes"
Expand All @@ -37,6 +39,7 @@ type BlockAPI struct {
connectionType ConnectionType
namespace string
includeShellReceipts bool
includeBankTransfers bool
}

type SeiBlockAPI struct {
Expand All @@ -52,6 +55,7 @@ func NewBlockAPI(tmClient rpcclient.Client, k *keeper.Keeper, ctxProvider func(i
txConfig: txConfig,
connectionType: connectionType,
includeShellReceipts: false,
includeBankTransfers: false,
namespace: "eth",
}
}
Expand All @@ -71,6 +75,7 @@ func NewSeiBlockAPI(
txConfig: txConfig,
connectionType: connectionType,
includeShellReceipts: true,
includeBankTransfers: false,
namespace: "sei",
}
return &SeiBlockAPI{
Expand All @@ -79,6 +84,20 @@ func NewSeiBlockAPI(
}
}

func NewSei2BlockAPI(
tmClient rpcclient.Client,
k *keeper.Keeper,
ctxProvider func(int64) sdk.Context,
txConfig client.TxConfig,
connectionType ConnectionType,
isPanicTx func(ctx context.Context, hash common.Hash) (bool, error),
) *SeiBlockAPI {
blockAPI := NewSeiBlockAPI(tmClient, k, ctxProvider, txConfig, connectionType, isPanicTx)
blockAPI.namespace = "sei2"
blockAPI.includeBankTransfers = true
return blockAPI
}

func (a *SeiBlockAPI) GetBlockByNumberExcludeTraceFail(ctx context.Context, number rpc.BlockNumber, fullTx bool) (result map[string]interface{}, returnErr error) {
// exclude synthetic txs
return a.getBlockByNumber(ctx, number, fullTx, false, a.isPanicTx)
Expand Down Expand Up @@ -130,7 +149,7 @@ func (a *BlockAPI) getBlockByHash(ctx context.Context, blockHash common.Hash, fu
return nil, err
}
blockBloom := a.keeper.GetBlockBloom(a.ctxProvider(block.Block.Height))
return EncodeTmBlock(a.ctxProvider(block.Block.Height), block, blockRes, blockBloom, a.keeper, a.txConfig.TxDecoder(), fullTx, a.includeShellReceipts, isPanicTx)
return EncodeTmBlock(a.ctxProvider(block.Block.Height), block, blockRes, blockBloom, a.keeper, a.txConfig.TxDecoder(), fullTx, a.includeBankTransfers, a.includeShellReceipts, isPanicTx)
}

func (a *BlockAPI) GetBlockByNumber(ctx context.Context, number rpc.BlockNumber, fullTx bool) (result map[string]interface{}, returnErr error) {
Expand Down Expand Up @@ -186,7 +205,7 @@ func (a *BlockAPI) getBlockByNumber(
return nil, err
}
blockBloom := a.keeper.GetBlockBloom(a.ctxProvider(block.Block.Height))
return EncodeTmBlock(a.ctxProvider(block.Block.Height), block, blockRes, blockBloom, a.keeper, a.txConfig.TxDecoder(), fullTx, includeSyntheticTxs, isPanicTx)
return EncodeTmBlock(a.ctxProvider(block.Block.Height), block, blockRes, blockBloom, a.keeper, a.txConfig.TxDecoder(), fullTx, a.includeBankTransfers, includeSyntheticTxs, isPanicTx)
}

func (a *BlockAPI) GetBlockReceipts(ctx context.Context, blockNrOrHash rpc.BlockNumberOrHash) (result []map[string]interface{}, returnErr error) {
Expand Down Expand Up @@ -266,6 +285,7 @@ func EncodeTmBlock(
k *keeper.Keeper,
txDecoder sdk.TxDecoder,
fullTx bool,
includeBankTransfers bool,
includeSyntheticTxs bool,
isPanicOrSynthetic func(ctx context.Context, hash common.Hash) (bool, error),
) (map[string]interface{}, error) {
Expand Down Expand Up @@ -342,6 +362,51 @@ func EncodeTmBlock(
TransactionIndex: (*hexutil.Uint64)(&ti),
})
}
case *banktypes.MsgSend:
if !includeBankTransfers {
continue
}
th := sha256.Sum256(block.Block.Txs[i])
if !fullTx {
transactions = append(transactions, "0x"+hex.EncodeToString(th[:]))
} else {
OUTER:
for _, event := range txRes.Events {
if event.Type == "transfer" {
rpcTx := &ethapi.RPCTransaction{
BlockHash: &blockhash,
BlockNumber: (*hexutil.Big)(number),
Hash: th,
}
for _, attribute := range event.Attributes {
switch string(attribute.Key) {
case "sender":
senderSeiAddr, err := sdk.AccAddressFromBech32(string(attribute.Value))
if err != nil {
continue OUTER
}
rpcTx.From = k.GetEVMAddressOrDefault(ctx, senderSeiAddr)
case "recipient":
recipientSeiAddr, err := sdk.AccAddressFromBech32(string(attribute.Value))
if err != nil {
continue OUTER
}
recipientEvmAddr := k.GetEVMAddressOrDefault(ctx, recipientSeiAddr)
rpcTx.To = &recipientEvmAddr
case "amount":
amt, err := sdk.ParseCoinNormalized(string(attribute.Value))
if err != nil || amt.Denom != "usei" {
continue OUTER
}
amtInWei := amt.Amount.Mul(state.SdkUseiToSweiMultiplier)
rpcTx.Input = amtInWei.BigInt().Bytes()
}
}
transactions = append(transactions, rpcTx)
break
}
}
}
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions evmrpc/block_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ func TestEncodeTmBlock_EmptyTransactions(t *testing.T) {
}

// Call EncodeTmBlock with empty transactions
result, err := evmrpc.EncodeTmBlock(ctx, block, blockRes, ethtypes.Bloom{}, k, Decoder, true, false, nil)
result, err := evmrpc.EncodeTmBlock(ctx, block, blockRes, ethtypes.Bloom{}, k, Decoder, true, false, false, nil)
require.Nil(t, err)

// Assert txHash is equal to ethtypes.EmptyTxsHash
Expand Down Expand Up @@ -251,7 +251,7 @@ func TestEncodeBankMsg(t *testing.T) {
},
},
}
res, err := evmrpc.EncodeTmBlock(ctx, &resBlock, &resBlockRes, ethtypes.Bloom{}, k, Decoder, true, false, nil)
res, err := evmrpc.EncodeTmBlock(ctx, &resBlock, &resBlockRes, ethtypes.Bloom{}, k, Decoder, true, false, false, nil)
require.Nil(t, err)
txs := res["transactions"].([]interface{})
require.Equal(t, 0, len(txs))
Expand Down Expand Up @@ -299,7 +299,7 @@ func TestEncodeWasmExecuteMsg(t *testing.T) {
},
},
}
res, err := evmrpc.EncodeTmBlock(ctx, &resBlock, &resBlockRes, ethtypes.Bloom{}, k, Decoder, true, true, nil)
res, err := evmrpc.EncodeTmBlock(ctx, &resBlock, &resBlockRes, ethtypes.Bloom{}, k, Decoder, true, false, true, nil)
require.Nil(t, err)
txs := res["transactions"].([]interface{})
require.Equal(t, 1, len(txs))
Expand Down
4 changes: 4 additions & 0 deletions evmrpc/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ func NewEVMHTTPServer(
Namespace: "sei",
Service: NewSeiBlockAPI(tmClient, k, ctxProvider, txConfig, ConnectionTypeHTTP, isPanicOrSyntheticTxFunc),
},
{
Namespace: "sei2",
Service: NewSei2BlockAPI(tmClient, k, ctxProvider, txConfig, ConnectionTypeHTTP, isPanicOrSyntheticTxFunc),
},
{
Namespace: "eth",
Service: txAPI,
Expand Down

0 comments on commit b082a18

Please sign in to comment.