Skip to content

Commit

Permalink
Merge pull request #43 from testinprod-io/pcw109550/fix-ots-api-fee-g…
Browse files Browse the repository at this point in the history
…asUsed

Add Deposit Tx gasUsed and Fix block Fee Calculation for Otterscan
  • Loading branch information
pcw109550 authored Apr 20, 2023
2 parents e68e3a8 + 5caa80a commit 4a7b3f5
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 5 deletions.
12 changes: 9 additions & 3 deletions cmd/rpcdaemon/commands/otterscan_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -724,12 +724,13 @@ func (api *OtterscanAPIImpl) delegateIssuance(tx kv.Tx, block *types.Block, chai
return ret, nil
}

func (api *OtterscanAPIImpl) delegateBlockFees(ctx context.Context, tx kv.Tx, block *types.Block, senders []common.Address, chainConfig *chain.Config) (uint64, error) {
func (api *OtterscanAPIImpl) delegateBlockFees(ctx context.Context, tx kv.Tx, block *types.Block, senders []common.Address, chainConfig *chain.Config) (uint64, uint64, error) {
receipts, err := api.getReceipts(ctx, tx, chainConfig, block, senders)
if err != nil {
return 0, fmt.Errorf("getReceipts error: %v", err)
return 0, 0, fmt.Errorf("getReceipts error: %v", err)
}

gasUsedDepositTx := uint64(0)
fees := uint64(0)
for _, receipt := range receipts {
txn := block.Transactions()[receipt.TransactionIndex]
Expand All @@ -738,13 +739,18 @@ func (api *OtterscanAPIImpl) delegateBlockFees(ctx context.Context, tx kv.Tx, bl
effectiveGasPrice = txn.GetPrice().Uint64()
} else {
baseFee, _ := uint256.FromBig(block.BaseFee())
if chainConfig.IsOptimism() && receipt.IsDepositTxReceipt() {
// if depositTx, no fee consumption
gasUsedDepositTx += receipt.GasUsed
continue
}
gasPrice := new(big.Int).Add(block.BaseFee(), txn.GetEffectiveGasTip(baseFee).ToBig())
effectiveGasPrice = gasPrice.Uint64()
}
fees += effectiveGasPrice * receipt.GasUsed
}

return fees, nil
return fees, gasUsedDepositTx, nil
}

func (api *OtterscanAPIImpl) getBlockWithSenders(ctx context.Context, number rpc.BlockNumber, tx kv.Tx) (*types.Block, []common.Address, error) {
Expand Down
10 changes: 8 additions & 2 deletions cmd/rpcdaemon/commands/otterscan_block_details.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func (api *OtterscanAPIImpl) GetBlockDetails(ctx context.Context, number rpc.Blo
if err != nil {
return nil, err
}
feesRes, err := api.delegateBlockFees(ctx, tx, b, senders, chainConfig)
feesRes, gasUsedDepositTxRes, err := api.delegateBlockFees(ctx, tx, b, senders, chainConfig)
if err != nil {
return nil, err
}
Expand All @@ -52,6 +52,9 @@ func (api *OtterscanAPIImpl) GetBlockDetails(ctx context.Context, number rpc.Blo
response["block"] = getBlockRes
response["issuance"] = getIssuanceRes
response["totalFees"] = hexutil.Uint64(feesRes)
if chainConfig.IsOptimism() {
response["gasUsedDepositTx"] = hexutil.Uint64(gasUsedDepositTxRes)
}
return response, nil
}

Expand Down Expand Up @@ -89,7 +92,7 @@ func (api *OtterscanAPIImpl) GetBlockDetailsByHash(ctx context.Context, hash com
if err != nil {
return nil, err
}
feesRes, err := api.delegateBlockFees(ctx, tx, b, senders, chainConfig)
feesRes, gasUsedDepositTxRes, err := api.delegateBlockFees(ctx, tx, b, senders, chainConfig)
if err != nil {
return nil, err
}
Expand All @@ -98,5 +101,8 @@ func (api *OtterscanAPIImpl) GetBlockDetailsByHash(ctx context.Context, hash com
response["block"] = getBlockRes
response["issuance"] = getIssuanceRes
response["totalFees"] = hexutil.Uint64(feesRes)
if chainConfig.IsOptimism() {
response["gasUsedDepositTx"] = hexutil.Uint64(gasUsedDepositTxRes)
}
return response, nil
}
4 changes: 4 additions & 0 deletions core/types/receipt.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,10 @@ func NewReceipt(failed bool, cumulativeGasUsed uint64) *Receipt {
return r
}

func (r Receipt) IsDepositTxReceipt() bool {
return r.Type == DepositTxType
}

// EncodeRLP implements rlp.Encoder, and flattens the consensus fields of a receipt
// into an RLP stream. If no post state is present, byzantium fork is assumed.
func (r Receipt) EncodeRLP(w io.Writer) error {
Expand Down

0 comments on commit 4a7b3f5

Please sign in to comment.