2018-04-06 16:52:10 +02:00
|
|
|
# Nimbus
|
2024-02-15 09:57:05 +07:00
|
|
|
# Copyright (c) 2018-2024 Status Research & Development GmbH
|
2018-04-06 16:52:10 +02:00
|
|
|
# Licensed under either of
|
|
|
|
# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or http://www.apache.org/licenses/LICENSE-2.0)
|
|
|
|
# * MIT license ([LICENSE-MIT](LICENSE-MIT) or http://opensource.org/licenses/MIT)
|
|
|
|
# at your option. This file may not be copied, modified, or distributed except according to those terms.
|
|
|
|
|
2018-01-17 14:57:50 +02:00
|
|
|
import
|
2024-10-26 14:19:48 +07:00
|
|
|
./[constants],
|
2024-10-04 16:34:31 +02:00
|
|
|
eth/common/[addresses, keys, transactions, transactions_rlp, transaction_utils]
|
2018-01-17 14:57:50 +02:00
|
|
|
|
2024-10-04 16:34:31 +02:00
|
|
|
export addresses, keys, transactions
|
2018-11-20 17:35:11 +00:00
|
|
|
|
2024-10-04 16:34:31 +02:00
|
|
|
proc signTransaction*(tx: Transaction, privateKey: PrivateKey, eip155 = true): Transaction =
|
2021-06-27 11:19:43 +07:00
|
|
|
result = tx
|
2024-10-04 16:34:31 +02:00
|
|
|
result.signature = result.sign(privateKey, eip155)
|
2022-04-05 17:22:46 +07:00
|
|
|
|
2023-10-31 15:12:41 +07:00
|
|
|
# deriveChainId derives the chain id from the given v parameter
|
2024-06-14 14:31:08 +07:00
|
|
|
func deriveChainId*(v: uint64, chainId: ChainId): ChainId =
|
2023-10-31 15:12:41 +07:00
|
|
|
if v == 27 or v == 28:
|
|
|
|
chainId
|
|
|
|
else:
|
|
|
|
((v - 35) div 2).ChainId
|
|
|
|
|
|
|
|
func validateChainId*(tx: Transaction, chainId: ChainId): bool =
|
|
|
|
if tx.txType == TxLegacy:
|
|
|
|
chainId.uint64 == deriveChainId(tx.V, chainId).uint64
|
|
|
|
else:
|
|
|
|
chainId.uint64 == tx.chainId.uint64
|
|
|
|
|
2022-04-05 17:22:46 +07:00
|
|
|
func eip1559TxNormalization*(tx: Transaction;
|
2024-06-14 14:31:08 +07:00
|
|
|
baseFeePerGas: GasInt): Transaction =
|
2022-04-05 17:22:46 +07:00
|
|
|
## This function adjusts a legacy transaction to EIP-1559 standard. This
|
|
|
|
## is needed particularly when using the `validateTransaction()` utility
|
|
|
|
## with legacy transactions.
|
|
|
|
result = tx
|
|
|
|
if tx.txType < TxEip1559:
|
2024-06-14 14:31:08 +07:00
|
|
|
result.maxPriorityFeePerGas = tx.gasPrice
|
|
|
|
result.maxFeePerGas = tx.gasPrice
|
2023-08-22 13:05:10 +07:00
|
|
|
else:
|
2024-06-14 14:31:08 +07:00
|
|
|
result.gasPrice = baseFeePerGas +
|
|
|
|
min(result.maxPriorityFeePerGas, result.maxFeePerGas - baseFeePerGas)
|
2022-04-11 17:00:39 +07:00
|
|
|
|
2024-08-19 09:42:07 +02:00
|
|
|
func maxPriorityFeePerGasNorm*(tx: Transaction): GasInt =
|
|
|
|
if tx.txType < TxEip1559:
|
|
|
|
tx.gasPrice
|
|
|
|
else:
|
|
|
|
tx.maxPriorityFeePerGas
|
2022-04-11 17:00:39 +07:00
|
|
|
|
2024-08-19 09:42:07 +02:00
|
|
|
func maxFeePerGasNorm*(tx: Transaction): GasInt =
|
2022-04-11 17:00:39 +07:00
|
|
|
if tx.txType < TxEip1559:
|
2024-08-19 09:42:07 +02:00
|
|
|
tx.gasPrice
|
|
|
|
else:
|
|
|
|
tx.maxFeePerGas
|
|
|
|
|
|
|
|
func effectiveGasPrice*(tx: Transaction, baseFeePerGas: GasInt): GasInt =
|
|
|
|
if tx.txType < TxEip1559:
|
|
|
|
tx.gasPrice
|
|
|
|
else:
|
|
|
|
baseFeePerGas +
|
|
|
|
min(tx.maxPriorityFeePerGas, tx.maxFeePerGas - baseFeePerGas)
|
|
|
|
|
|
|
|
func effectiveGasTip*(tx: Transaction; baseFeePerGas: Opt[UInt256]): GasInt =
|
|
|
|
let
|
|
|
|
baseFeePerGas = baseFeePerGas.get(0.u256).truncate(GasInt)
|
2022-04-11 17:00:39 +07:00
|
|
|
|
2024-08-19 09:42:07 +02:00
|
|
|
min(tx.maxPriorityFeePerGasNorm(), tx.maxFeePerGasNorm() - baseFeePerGas)
|
2022-12-15 13:30:18 +07:00
|
|
|
|
|
|
|
proc decodeTx*(bytes: openArray[byte]): Transaction =
|
|
|
|
var rlp = rlpFromBytes(bytes)
|
|
|
|
result = rlp.read(Transaction)
|
|
|
|
if rlp.hasData:
|
|
|
|
raise newException(RlpError, "rlp: input contains more than one value")
|
2024-05-15 06:07:59 +03:00
|
|
|
|
|
|
|
proc decodePooledTx*(bytes: openArray[byte]): PooledTransaction =
|
|
|
|
var rlp = rlpFromBytes(bytes)
|
|
|
|
result = rlp.read(PooledTransaction)
|
|
|
|
if rlp.hasData:
|
|
|
|
raise newException(RlpError, "rlp: input contains more than one value")
|