Skip to content

Commit

Permalink
Update client.go
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaying-peng committed Oct 10, 2024
1 parent 5f86092 commit ca4d373
Showing 1 changed file with 16 additions and 28 deletions.
44 changes: 16 additions & 28 deletions examples/ethereum/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,55 +61,43 @@ func (c *EthereumClient) GetBlockReceipts(
txs []evmClient.RPCTransaction,
baseFee *big.Int,
) ([]*evmClient.RosettaTxReceipt, error) {
receipts := make([]*evmClient.RosettaTxReceipt, len(txs))
if len(txs) == 0 {
return receipts, nil
return []*evmClient.RosettaTxReceipt{}, nil
}

ethReceipts := make([]*EthTypes.Receipt, len(txs))
reqs := make([]rpc.BatchElem, len(txs))
for i := range reqs {
reqs[i] = rpc.BatchElem{
Method: "eth_getTransactionReceipt",
Args: []interface{}{txs[i].TxExtraInfo.TxHash.String()},
Result: &ethReceipts[i],
}
}
if err := c.BatchCallContext(ctx, reqs); err != nil {
var ethReceipts []*EthTypes.Receipt
err := c.CallContext(ctx, &ethReceipts, "eth_getBlockReceipts", blockHash.Hex())
if err != nil {
return nil, err
}
for i := range reqs {
if reqs[i].Error != nil {
return nil, reqs[i].Error
}

if len(ethReceipts) != len(txs) {
return nil, fmt.Errorf("mismatch in number of transactions and receipts")
}

receipts := make([]*evmClient.RosettaTxReceipt, len(txs))
for i, ethReceipt := range ethReceipts {
gasPrice, err := evmClient.EffectiveGasPrice(txs[i].Tx, baseFee)
if err != nil {
return nil, err
}
gasUsed := new(big.Int).SetUint64(ethReceipts[i].GasUsed)
gasUsed := new(big.Int).SetUint64(ethReceipt.GasUsed)
feeAmount := new(big.Int).Mul(gasUsed, gasPrice)

receipt := &evmClient.RosettaTxReceipt{
Type: ethReceipts[i].Type,
receipts[i] = &evmClient.RosettaTxReceipt{
Type: ethReceipt.Type,
GasPrice: gasPrice,
GasUsed: gasUsed,
Logs: ethReceipts[i].Logs,
Logs: ethReceipt.Logs,
RawMessage: nil,
TransactionFee: feeAmount,
}

receipts[i] = receipt

if ethReceipts[i] == nil {
return nil, fmt.Errorf("got empty receipt for %x", txs[i].Tx.Hash().Hex())
}

if ethReceipts[i].BlockHash != blockHash {
if ethReceipt.BlockHash != blockHash {
return nil, fmt.Errorf(
"expected block hash %s for Transaction but got %s: %w",
blockHash.Hex(),
ethReceipts[i].BlockHash.Hex(),
ethReceipt.BlockHash.Hex(),
sdkTypes.ErrClientBlockOrphaned,
)
}
Expand Down

0 comments on commit ca4d373

Please sign in to comment.