Skip to content

Commit 0ca8e65

Browse files
authored
Add L1Fee to evm service Receipt (#2002)
* Add L1Fee to evm service Receipt * Wire l1 fee through CalculateTransactionFee and getTransactionReceipt * Add tests * Lint
1 parent 5570633 commit 0ca8e65

7 files changed

Lines changed: 154 additions & 84 deletions

File tree

pkg/chains/evm/evm.pb.go

Lines changed: 104 additions & 84 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/chains/evm/evm.proto

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ message Receipt {
8383
values.v1.BigInt effective_gas_price = 8; // actual gas price paid in wei (include after EIP-1559)
8484
values.v1.BigInt block_number = 9; // block number containing the transaction
8585
bytes contract_address = 10; // address of the contract if this transaction created one in evm address [20]byte fix-sized array format
86+
values.v1.BigInt l1_Fee = 11; // layer 1 fee paid by L2 chains
8687
}
8788

8889

@@ -320,6 +321,7 @@ message CalculateTransactionFeeRequest {
320321
message ReceiptGasInfo {
321322
uint64 gas_used = 1; // gas used by this transaction (in gas units)
322323
values.v1.BigInt effective_gas_price = 2; // actual gas price paid in wei (include after EIP-1559)
324+
values.v1.BigInt l1_Fee = 3; // layer 1 fee paid by L2 chains
323325
}
324326

325327

pkg/chains/evm/proto_helpers.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@ func ConvertReceiptToProto(receipt *evmtypes.Receipt) (*Receipt, error) {
147147
BlockNumber: valuespb.NewBigIntFromInt(receipt.BlockNumber),
148148
TxIndex: receipt.TransactionIndex,
149149
EffectiveGasPrice: valuespb.NewBigIntFromInt(receipt.EffectiveGasPrice),
150+
L1_Fee: valuespb.NewBigIntFromInt(receipt.L1Fee),
150151
}, nil
151152
}
152153

@@ -186,6 +187,7 @@ func ConvertReceiptFromProto(protoReceipt *Receipt) (*evmtypes.Receipt, error) {
186187
BlockNumber: valuespb.NewIntFromBigInt(protoReceipt.GetBlockNumber()),
187188
TransactionIndex: protoReceipt.GetTxIndex(),
188189
EffectiveGasPrice: valuespb.NewIntFromBigInt(protoReceipt.GetEffectiveGasPrice()),
190+
L1Fee: valuespb.NewIntFromBigInt(protoReceipt.GetL1_Fee()),
189191
}, nil
190192
}
191193

pkg/chains/evm/proto_helpers_test.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package evm_test
33
import (
44
"bytes"
55
"errors"
6+
"math/big"
67
"strings"
78
"testing"
89

@@ -270,6 +271,32 @@ func TestHashConverters(t *testing.T) {
270271
})
271272
}
272273

274+
func TestReceiptRoundtrip(t *testing.T) {
275+
blockHash := evmtypes.Hash(mkBytes(evmtypes.HashLength, 0xAA))
276+
txHash := evmtypes.Hash(mkBytes(evmtypes.HashLength, 0xBB))
277+
contractAddr := evmtypes.Address(mkBytes(evmtypes.AddressLength, 0xCC))
278+
279+
in := &evmtypes.Receipt{
280+
Status: 1,
281+
Logs: make([]*evmtypes.Log, 0),
282+
TxHash: txHash,
283+
ContractAddress: contractAddr,
284+
GasUsed: 21000,
285+
BlockHash: blockHash,
286+
BlockNumber: big.NewInt(100),
287+
TransactionIndex: 3,
288+
EffectiveGasPrice: big.NewInt(1e9),
289+
L1Fee: big.NewInt(5000),
290+
}
291+
292+
proto, err := evm.ConvertReceiptToProto(in)
293+
require.NoError(t, err)
294+
295+
got, err := evm.ConvertReceiptFromProto(proto)
296+
require.NoError(t, err)
297+
require.Equal(t, in, got)
298+
}
299+
273300
func TestConvertExpressionsFromProto(t *testing.T) {
274301
testCases := []struct {
275302
Name string

pkg/loop/internal/relayer/evm.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ func (e *EVMClient) CalculateTransactionFee(ctx context.Context, receiptGasInfo
2727
reply, err := e.grpcClient.CalculateTransactionFee(ctx, &evmpb.CalculateTransactionFeeRequest{GasInfo: &evmpb.ReceiptGasInfo{
2828
GasUsed: receiptGasInfo.GasUsed,
2929
EffectiveGasPrice: valuespb.NewBigIntFromInt(receiptGasInfo.EffectiveGasPrice),
30+
L1_Fee: valuespb.NewBigIntFromInt(receiptGasInfo.L1Fee),
3031
}})
3132
if err != nil {
3233
return nil, net.WrapRPCErr(err)
@@ -574,6 +575,7 @@ func (e *evmServer) CalculateTransactionFee(ctx context.Context, request *evmpb.
574575
txFee, err := e.impl.CalculateTransactionFee(ctx, evmtypes.ReceiptGasInfo{
575576
GasUsed: request.GasInfo.GasUsed,
576577
EffectiveGasPrice: valuespb.NewIntFromBigInt(request.GasInfo.EffectiveGasPrice),
578+
L1Fee: valuespb.NewIntFromBigInt(request.GasInfo.L1_Fee),
577579
})
578580
if err != nil {
579581
return nil, err

pkg/loop/internal/relayer/evm_test.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ var (
4343
topic2 = evm.Hash{33, 1, 33}
4444
topic3 = evm.Hash{20, 19, 17}
4545
gas = uint64(10)
46+
l1Fee = big.NewInt(9876)
4647
txHash = evm.Hash{5, 3, 44}
4748
eventSigHash = evm.Hash{14, 16, 29}
4849
filterName = "f name 1"
@@ -246,6 +247,7 @@ func Test_EVMDomainRoundTripThroughGRPC(t *testing.T) {
246247
GasUsed: gas,
247248
BlockNumber: blockNum,
248249
TransactionIndex: uint64(txIndex),
250+
L1Fee: l1Fee,
249251
}
250252
evmService.EXPECT().GetTransactionReceipt(mock.Anything, evm.GeTransactionReceiptRequest{Hash: txHash}).Return(expReceipt, nil).Once()
251253

@@ -340,6 +342,19 @@ func Test_EVMDomainRoundTripThroughGRPC(t *testing.T) {
340342
require.NoError(t, err)
341343
require.Equal(t, expectedNames, actualNames)
342344
})
345+
346+
t.Run("CalculateTransactionFee", func(t *testing.T) {
347+
gasInfo := evm.ReceiptGasInfo{
348+
GasUsed: gas,
349+
EffectiveGasPrice: gasPrice,
350+
L1Fee: l1Fee,
351+
}
352+
evmService.EXPECT().CalculateTransactionFee(mock.Anything, gasInfo).Return(&evm.TransactionFee{TransactionFee: txFee}, nil).Once()
353+
354+
got, err := client.CalculateTransactionFee(ctx, gasInfo)
355+
require.NoError(t, err)
356+
require.Equal(t, txFee, got.TransactionFee)
357+
})
343358
}
344359

345360
func generateFixtureQuery() []query.Expression {

pkg/types/chains/evm/evm.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ type Transaction struct {
100100
type ReceiptGasInfo struct {
101101
GasUsed uint64 // actual gas used during execution in gas units
102102
EffectiveGasPrice *big.Int // actual price in wei paid per gas unit
103+
L1Fee *big.Int // layer 1 fee paid by L2 chains
103104
}
104105

105106
// matches evm-style receipt
@@ -113,6 +114,7 @@ type Receipt struct {
113114
BlockNumber *big.Int // number of the block containing this receipt
114115
TransactionIndex uint64 // index of the transaction inside of the block
115116
EffectiveGasPrice *big.Int // actual price in wei paid per gas unit
117+
L1Fee *big.Int // For L2 chains only
116118
}
117119

118120
// matches simplified evm-style head

0 commit comments

Comments
 (0)