Skip to content

Commit

Permalink
Add calculateFeeEstimate and TestCalculateFeeEstimate
Browse files Browse the repository at this point in the history
  • Loading branch information
AnkushinDaniil committed Dec 22, 2024
1 parent 49278bf commit 8e53328
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 33 deletions.
65 changes: 32 additions & 33 deletions rpc/simulation.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ func (h *Handler) SimulateTransactions(id BlockID, transactions []BroadcastedTra
return h.simulateTransactions(id, transactions, simulationFlags, false)
}

//nolint:funlen
func (h *Handler) simulateTransactions(id BlockID, transactions []BroadcastedTransaction,
simulationFlags []SimulationFlag, errOnRevert bool,
) ([]SimulatedTransaction, http.Header, *jsonrpc.Error) {
Expand Down Expand Up @@ -125,38 +124,7 @@ func (h *Handler) simulateTransactions(id BlockID, transactions []BroadcastedTra
for i, overallFee := range overallFees {
feeUnit := feeUnit(txns[i])

var (
l1GasPrice *felt.Felt
l2GasPrice *felt.Felt
l1DataGasPrice *felt.Felt
)

switch feeUnit {
case FRI:
l1GasPrice = header.L1GasPriceSTRK
l2GasPrice = header.L2GasPriceSTRK
l1DataGasPrice = header.L1DataGasPrice.PriceInFri
case WEI:
l1GasPrice = header.L1GasPriceETH
l2GasPrice = header.L2GasPriceETH
l1DataGasPrice = header.L1DataGasPrice.PriceInWei
}

var l1GasConsumed *felt.Felt
l1DataGasConsumed := new(felt.Felt).SetUint64(daGas[i].L1DataGas)
dataGasFee := new(felt.Felt).Mul(l1DataGasConsumed, l1DataGasPrice)
l1GasConsumed = new(felt.Felt).Sub(overallFee, dataGasFee)

estimate := FeeEstimate{
L1GasConsumed: l1GasConsumed,
L2GasConsumed: &felt.Zero, // TODO: Fix when we have l2 gas price
L1GasPrice: l1GasPrice,
L2GasPrice: l2GasPrice,
L1DataGasConsumed: l1DataGasConsumed,
L1DataGasPrice: l1DataGasPrice,
OverallFee: overallFee,
Unit: utils.Ptr(feeUnit),
}
estimate := calculateFeeEstimate(overallFee, daGas[i].L1DataGas, feeUnit, header)

trace := traces[i]
executionResources := trace.TotalExecutionResources()
Expand All @@ -175,6 +143,37 @@ func (h *Handler) simulateTransactions(id BlockID, transactions []BroadcastedTra
return result, httpHeader, nil
}

func calculateFeeEstimate(overallFee *felt.Felt, l1DataGas uint64, feeUnit FeeUnit, header *core.Header) FeeEstimate {
var l1GasPrice, l2GasPrice, l1DataGasPrice *felt.Felt

switch feeUnit {
case FRI:
l1GasPrice = header.L1GasPriceSTRK
l2GasPrice = header.L2GasPriceSTRK
l1DataGasPrice = header.L1DataGasPrice.PriceInFri
case WEI:
l1GasPrice = header.L1GasPriceETH
l2GasPrice = header.L2GasPriceETH
l1DataGasPrice = header.L1DataGasPrice.PriceInWei
}

l1DataGasConsumed := new(felt.Felt).SetUint64(l1DataGas)
dataGasFee := new(felt.Felt).Mul(l1DataGasConsumed, l1DataGasPrice)
l1GasConsumed := new(felt.Felt).Sub(overallFee, dataGasFee)
l1GasConsumed = l1GasConsumed.Div(l1GasConsumed, l1GasPrice)

return FeeEstimate{
L1GasConsumed: l1GasConsumed,
L2GasConsumed: &felt.Zero, // TODO: Fix when we have l2 gas price
L1GasPrice: l1GasPrice,
L2GasPrice: l2GasPrice,
L1DataGasConsumed: l1DataGasConsumed,
L1DataGasPrice: l1DataGasPrice,
OverallFee: overallFee,
Unit: utils.Ptr(feeUnit),
}
}

type TransactionExecutionErrorData struct {
TransactionIndex uint64 `json:"transaction_index"`
ExecutionError string `json:"execution_error"`
Expand Down
39 changes: 39 additions & 0 deletions rpc/simulation_pkg_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package rpc

import (
"testing"

"github.com/NethermindEth/juno/core"
"github.com/NethermindEth/juno/core/felt"
"github.com/stretchr/testify/assert"
)

func TestCalculateFeeEstimate(t *testing.T) {
l1GasPriceETH := new(felt.Felt).SetUint64(200)
l2GasPriceETH := new(felt.Felt).SetUint64(70)
l1GasPriceSTRK := new(felt.Felt).SetUint64(100)
l2GasPriceSTRK := new(felt.Felt).SetUint64(50)
l1DataGasPrice := &core.GasPrice{
PriceInWei: new(felt.Felt).SetUint64(10),
PriceInFri: new(felt.Felt).SetUint64(5),
}
header := &core.Header{
L1GasPriceETH: l1GasPriceETH,
L2GasPriceETH: l2GasPriceETH,
L1GasPriceSTRK: l1GasPriceSTRK,
L2GasPriceSTRK: l2GasPriceSTRK,
L1DataGasPrice: l1DataGasPrice,
}
l1DataGas := uint64(500)
overallFee := new(felt.Felt).SetUint64(6000)

feeEstimate := calculateFeeEstimate(overallFee, l1DataGas, FRI, header)

assert.Equal(t, l1GasPriceSTRK, feeEstimate.L1GasPrice)
assert.Equal(t, l2GasPriceSTRK, feeEstimate.L2GasPrice)
assert.Equal(t, l1DataGasPrice.PriceInFri, feeEstimate.L1DataGasPrice)
assert.Equal(t, overallFee, feeEstimate.OverallFee)
assert.Equal(t, FRI, *feeEstimate.Unit)
assert.Equal(t, new(felt.Felt).SetUint64(35), feeEstimate.L1GasConsumed)
assert.Equal(t, &felt.Zero, feeEstimate.L2GasConsumed)
}

0 comments on commit 8e53328

Please sign in to comment.