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

Commit ddf6424

Browse files
author
HuangYi
committed
fix get tx by index
1 parent d85fe32 commit ddf6424

File tree

2 files changed

+15
-4
lines changed

2 files changed

+15
-4
lines changed

rpc/namespaces/ethereum/eth/api.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -752,7 +752,7 @@ func (e *PublicAPI) getTransactionByBlockAndIndex(block *tmrpctypes.ResultBlock,
752752
return nil, fmt.Errorf("failed to parse tx events: %d, %v", idx, err)
753753
}
754754

755-
parsedTx := parsedTxs.GetTxByIndex(int(idx))
755+
parsedTx := parsedTxs.GetTxByTxIndex(int(idx))
756756
if parsedTx == nil {
757757
return nil, fmt.Errorf("ethereum tx not found in msgs: %d", idx)
758758
}

rpc/types/events.go

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -155,14 +155,25 @@ func (p *ParsedTxs) GetTxByHash(hash common.Hash) *ParsedTx {
155155
return nil
156156
}
157157

158-
// GetTxByIndex returns ParsedTx by index
159-
func (p *ParsedTxs) GetTxByIndex(i int) *ParsedTx {
160-
if i >= len(p.Txs) {
158+
// GetTxByMsgIndex returns ParsedTx by msg index
159+
func (p *ParsedTxs) GetTxByMsgIndex(i int) *ParsedTx {
160+
if i < 0 || i >= len(p.Txs) {
161161
return nil
162162
}
163163
return &p.Txs[i]
164164
}
165165

166+
// GetTxByTxIndex returns ParsedTx by tx index
167+
func (p *ParsedTxs) GetTxByTxIndex(i int) *ParsedTx {
168+
// assuming the `EthTxIndex` continuously increment.
169+
if len(p.Txs) == 0 {
170+
return nil
171+
}
172+
msgIndex := i - int(p.Txs[0].EthTxIndex)
173+
// GetTxByMsgIndex will check the bound
174+
return p.GetTxByMsgIndex(msgIndex)
175+
}
176+
166177
// AccumulativeGasUsed calculate the accumulated gas used within the batch
167178
func (p *ParsedTxs) AccumulativeGasUsed(msgIndex int) (result uint64) {
168179
for i := 0; i <= msgIndex; i++ {

0 commit comments

Comments
 (0)