Skip to content
This repository has been archived by the owner on Apr 4, 2024. It is now read-only.

Commit

Permalink
fix get tx by index
Browse files Browse the repository at this point in the history
  • Loading branch information
yihuang committed May 24, 2022
1 parent d85fe32 commit ddf6424
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
2 changes: 1 addition & 1 deletion rpc/namespaces/ethereum/eth/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -752,7 +752,7 @@ func (e *PublicAPI) getTransactionByBlockAndIndex(block *tmrpctypes.ResultBlock,
return nil, fmt.Errorf("failed to parse tx events: %d, %v", idx, err)
}

parsedTx := parsedTxs.GetTxByIndex(int(idx))
parsedTx := parsedTxs.GetTxByTxIndex(int(idx))
if parsedTx == nil {
return nil, fmt.Errorf("ethereum tx not found in msgs: %d", idx)
}
Expand Down
17 changes: 14 additions & 3 deletions rpc/types/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,14 +155,25 @@ func (p *ParsedTxs) GetTxByHash(hash common.Hash) *ParsedTx {
return nil
}

// GetTxByIndex returns ParsedTx by index
func (p *ParsedTxs) GetTxByIndex(i int) *ParsedTx {
if i >= len(p.Txs) {
// GetTxByMsgIndex returns ParsedTx by msg index
func (p *ParsedTxs) GetTxByMsgIndex(i int) *ParsedTx {
if i < 0 || i >= len(p.Txs) {
return nil
}
return &p.Txs[i]
}

// GetTxByTxIndex returns ParsedTx by tx index
func (p *ParsedTxs) GetTxByTxIndex(i int) *ParsedTx {
// assuming the `EthTxIndex` continuously increment.
if len(p.Txs) == 0 {
return nil
}
msgIndex := i - int(p.Txs[0].EthTxIndex)
// GetTxByMsgIndex will check the bound
return p.GetTxByMsgIndex(msgIndex)
}

// AccumulativeGasUsed calculate the accumulated gas used within the batch
func (p *ParsedTxs) AccumulativeGasUsed(msgIndex int) (result uint64) {
for i := 0; i <= msgIndex; i++ {
Expand Down

0 comments on commit ddf6424

Please sign in to comment.