Skip to content

Commit 2a8bebb

Browse files
authored
Replace WithdrawalObject with Withdrawal and some cleanup (#180)
1 parent 701eb10 commit 2a8bebb

File tree

6 files changed

+42
-31
lines changed

6 files changed

+42
-31
lines changed

tests/test_json_marshalling.nim

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -33,17 +33,17 @@ proc rand(_: type Address): Address =
3333
proc rand(_: type uint64): uint64 =
3434
var res: array[8, byte]
3535
discard randomBytes(res)
36-
result = uint64.fromBytesBE(res)
36+
uint64.fromBytesBE(res)
3737

3838
proc rand[T: Quantity](_: type T): T =
3939
var res: array[8, byte]
4040
discard randomBytes(res)
41-
result = T(uint64.fromBytesBE(res))
41+
T(uint64.fromBytesBE(res))
4242

4343
proc rand[T: ChainId](_: type T): T =
4444
var res: array[8, byte]
4545
discard randomBytes(res)
46-
result = T(uint64.fromBytesBE(res))
46+
T(uint64.fromBytesBE(res))
4747

4848
proc rand(_: type RlpEncodedBytes): RlpEncodedBytes =
4949
discard randomBytes(distinctBase result)
@@ -52,22 +52,22 @@ proc rand(_: type TypedTransaction): TypedTransaction =
5252
discard randomBytes(distinctBase result)
5353

5454
proc rand(_: type string): string =
55-
result = "random bytes"
55+
"random bytes"
5656

5757
proc rand(_: type bool): bool =
5858
var x: array[1, byte]
5959
discard randomBytes(x)
60-
result = x[0].int mod 2 == 0
60+
x[0].int mod 2 == 0
6161

6262
proc rand(_: type byte): byte =
6363
var x: array[1, byte]
6464
discard randomBytes(x)
65-
result = x[0]
65+
x[0]
6666

6767
proc rand(_: type UInt256): UInt256 =
6868
var x: array[32, byte]
6969
discard randomBytes(x)
70-
result = UInt256.fromBytesBE(x)
70+
UInt256.fromBytesBE(x)
7171

7272
proc rand(_: type RtBlockIdentifier): RtBlockIdentifier =
7373
RtBlockIdentifier(kind: bidNumber, number: rand(Quantity))
@@ -103,9 +103,9 @@ proc rand[X](T: type Opt[X]): T =
103103
var x: array[1, byte]
104104
discard randomBytes(x)
105105
if x[0] > 127:
106-
result = Opt.some(rand(X))
106+
Opt.some(rand(X))
107107
else:
108-
result = Opt.none(X)
108+
Opt.none(X)
109109

110110
proc rand[X: object](T: type X): T =
111111
result = T()
@@ -184,7 +184,7 @@ suite "JSON-RPC Quantity":
184184

185185
test "Random object encoding":
186186
checkRandomObject(SyncObject)
187-
checkRandomObject(WithdrawalObject)
187+
checkRandomObject(Withdrawal)
188188
checkRandomObject(AccessPair)
189189
checkRandomObject(AccessListResult)
190190
checkRandomObject(LogObject)
@@ -259,7 +259,7 @@ suite "JSON-RPC Quantity":
259259
checkType(Quantity)
260260

261261
test "AccessListResult":
262-
var z: AccessListResult
262+
let z = AccessListResult()
263263
let w = JrpcConv.encode(z)
264264
check w == """{"accessList":[],"gasUsed":"0x0"}"""
265265

@@ -271,6 +271,8 @@ suite "JSON-RPC Quantity":
271271
check w == """{"accessList":[],"error":"error","gasUsed":"0x0"}"""
272272

273273
test "Authorization":
274-
var z: Authorization
274+
let z = Authorization()
275275
let w = JrpcConv.encode(z)
276276
check w == """{"chainId":"0x0","address":"0x0000000000000000000000000000000000000000","nonce":"0x0","v":"0x0","r":"0x0","s":"0x0"}"""
277+
let x = JrpcConv.decode(w, Authorization)
278+
check x == z

tests/test_signed_tx.nim

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ suite "Signed transactions":
5252
tx.nonce = Opt.some(Quantity(9))
5353
tx.`from` = Opt.some(Address(address))
5454
tx.value = Opt.some(1000000000000000000.u256)
55-
tx.to = Opt.some(Address(hexToByteArray[20]("0x3535353535353535353535353535353535353535")))
55+
tx.to = Opt.some(address"0x3535353535353535353535353535353535353535")
5656
tx.gas = Opt.some(Quantity(21000'u64))
5757
tx.gasPrice = Opt.some(Quantity(20000000000'i64))
5858

@@ -69,12 +69,12 @@ suite "Signed transactions":
6969
tx.nonce = Opt.some(Quantity(9))
7070
tx.`from` = Opt.some(Address(address))
7171
tx.value = Opt.some(1000000000000000000.u256)
72-
tx.to = Opt.some(Address(hexToByteArray[20]("0x3535353535353535353535353535353535353535")))
72+
tx.to = Opt.some(address"0x3535353535353535353535353535353535353535")
7373
tx.gas = Opt.some(Quantity(21000'u64))
7474
tx.gasPrice = Opt.some(Quantity(20000000000'i64))
7575
tx.chainId = Opt.some(Quantity(1))
7676

77-
let txBytes = encodeTransaction(tx, privateKey, TxLegacy)
77+
let txBytes = encodeTransaction(tx, privateKey)
7878
let txHex = "0x" & txBytes.toHex
7979
check txHex == "0xf86c098504a817c800825208943535353535353535353535353535353535353535880de0b6b3a76400008025a028ef61340bd939bc2195fe537567866003e1a15d3c71ff63e1590620aa636276a067cbe9d8997f761aecb703304b3800ccf555c9f3dc64214b297fb1966a3b6d83"
8080

@@ -88,14 +88,14 @@ suite "Signed transactions":
8888
tx.nonce = Opt.some(Quantity(648))
8989
tx.`from` = Opt.some(Address(address))
9090
tx.value = Opt.some(51284.u256)
91-
tx.to = Opt.some(Address(hexToByteArray[20]("0x6Eb893e3466931517a04a17D153a6330c3f2f1dD")))
91+
tx.to = Opt.some(address"0x6Eb893e3466931517a04a17D153a6330c3f2f1dD")
9292
tx.gas = Opt.some(Quantity(157'u64))
9393
tx.gasPrice = Opt.some(Quantity(9086'u64))
9494
tx.chainId = Opt.some(Quantity(2214903583))
9595
tx.data = Opt.some(hexToSeqByte("0x889e365e59664fb881554ba1175519b5195b1d20390beb806d8f2cda7893e6f79848195dba4c905db6d7257ffb5eefea35f18ae33c"))
9696
tx.accessList = Opt.some(newSeq[AccessPair]())
9797

98-
let txBytes = encodeTransaction(tx, privateKey, TxEip2930)
98+
let txBytes = encodeTransaction(tx, privateKey)
9999
let txHex = "0x" & txBytes.toHex
100100
check txHex == "0x01f89f848404bf1f82028882237e819d946eb893e3466931517a04a17d153a6330c3f2f1dd82c854b5889e365e59664fb881554ba1175519b5195b1d20390beb806d8f2cda7893e6f79848195dba4c905db6d7257ffb5eefea35f18ae33cc080a0775f29642af1045b40e5beae8e6bce2dc9e222023b7a50372be6824dbb7434fba05dacfff85752a0b9fd860bc751c17235a670d318a8b9494d664c1b87e33ac8dd"
101101

@@ -109,15 +109,15 @@ suite "Signed transactions":
109109
tx.nonce = Opt.some(Quantity(648))
110110
tx.`from` = Opt.some(Address(address))
111111
tx.value = Opt.some(51284.u256)
112-
tx.to = Opt.some(Address(hexToByteArray[20]("0x6Eb893e3466931517a04a17D153a6330c3f2f1dD")))
112+
tx.to = Opt.some(address"0x6Eb893e3466931517a04a17D153a6330c3f2f1dD")
113113
tx.gas = Opt.some(Quantity(157'u64))
114114
tx.maxFeePerGas = Opt.some(Quantity(879596102'u64))
115115
tx.maxPriorityFeePerGas = Opt.some(Quantity(2915939'u64))
116116
tx.chainId = Opt.some(Quantity(2214903583))
117117
tx.data = Opt.some(hexToSeqByte("0x889e365e59664fb881554ba1175519b5195b1d20390beb806d8f2cda7893e6f79848195dba4c905db6d7257ffb5eefea35f18ae33c"))
118118
tx.accessList = Opt.some(newSeq[AccessPair]())
119119

120-
let txBytes = encodeTransaction(tx, privateKey, TxEip1559)
120+
let txBytes = encodeTransaction(tx, privateKey)
121121
let txHex = "0x" & txBytes.toHex
122122
check txHex == "0x02f8a5848404bf1f820288832c7e6384346d9246819d946eb893e3466931517a04a17d153a6330c3f2f1dd82c854b5889e365e59664fb881554ba1175519b5195b1d20390beb806d8f2cda7893e6f79848195dba4c905db6d7257ffb5eefea35f18ae33cc080a0f1003f96c6c6620dd46db36d2ae9f12d363947eb0db088c678b6ad1cf494aa6fa06085b5abbf448de5d622dc820da590cfdb6bb77b41c6650962b998a941f8d701"
123123

web3/conversions.nim

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ export eth_types_json_serialization except Topic
3333
#------------------------------------------------------------------------------
3434

3535
SyncObject.useDefaultSerializationIn JrpcConv
36-
WithdrawalObject.useDefaultSerializationIn JrpcConv
36+
Withdrawal.useDefaultSerializationIn JrpcConv
3737
AccessPair.useDefaultSerializationIn JrpcConv
3838
AccessListResult.useDefaultSerializationIn JrpcConv
3939
LogObject.useDefaultSerializationIn JrpcConv
@@ -264,7 +264,7 @@ proc readValue*[F: CommonJsonFlavors](r: var JsonReader[F], val: var uint64)
264264
{.gcsafe, raises: [IOError, JsonReaderError].} =
265265
let hexStr = r.parseString()
266266
if hexStr.invalidQuantityPrefix:
267-
r.raiseUnexpectedValue("Quantity value has invalid leading 0")
267+
r.raiseUnexpectedValue("Uint64 value has invalid leading 0")
268268
wrapValueError:
269269
val = fromHex[uint64](hexStr)
270270

web3/eth_api_types.nim

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,14 @@ import
1313
stint,
1414
./primitives
1515

16+
from eth/common/blocks import Withdrawal
1617
from eth/common/transactions import AccessPair, Authorization
1718

1819
export
1920
primitives,
2021
AccessPair,
21-
Authorization
22+
Authorization,
23+
Withdrawal
2224

2325
type
2426
SyncObject* = object
@@ -94,12 +96,6 @@ type
9496
parentBeaconBlockRoot*: Opt[Hash32] # EIP-4788
9597
requestsHash*: Opt[Hash32] # EIP-7685
9698

97-
WithdrawalObject* = object
98-
index*: Quantity
99-
validatorIndex*: Quantity
100-
address*: Address
101-
amount*: Quantity
102-
10399
## A block object, or null when no block was found
104100
BlockObject* = ref object
105101
number*: Quantity # the block number. null when its pending block.
@@ -123,7 +119,7 @@ type
123119
transactions*: seq[TxOrHash] # list of transaction objects, or 32 Bytes transaction hashes depending on the last given parameter.
124120
uncles*: seq[Hash32] # list of uncle hashes.
125121
baseFeePerGas*: Opt[UInt256] # EIP-1559
126-
withdrawals*: Opt[seq[WithdrawalObject]] # EIP-4895
122+
withdrawals*: Opt[seq[Withdrawal]] # EIP-4895
127123
withdrawalsRoot*: Opt[Hash32] # EIP-4895
128124
blobGasUsed*: Opt[Quantity] # EIP-4844
129125
excessBlobGas*: Opt[Quantity] # EIP-4844

web3/primitives.nim

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@ template ethQuantity(typ: type) {.dirty.} =
5454
func `==`*(a, b: typ): bool {.borrow.}
5555

5656
ethQuantity Quantity
57-
ethQuantity ChainId
5857

5958
template toHex*(x: DynamicBytes): string =
6059
toHex(distinctBase x)

web3/transaction_signing.nim

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ func encodeTransactionEip7702(s: TransactionArgs, pk: PrivateKey): seq[byte] =
9494
tr.authorizationList = s.authorizationList.get
9595
rlp.encode(tr)
9696

97-
func encodeTransaction*(s: TransactionArgs, pk: PrivateKey, txType: TxType = TxLegacy): seq[byte] =
97+
func encodeTransaction*(s: TransactionArgs, pk: PrivateKey, txType: TxType): seq[byte] =
9898
case txType
9999
of TxLegacy:
100100
encodeTransactionLegacy(s, pk)
@@ -107,6 +107,20 @@ func encodeTransaction*(s: TransactionArgs, pk: PrivateKey, txType: TxType = TxL
107107
of TxEip7702:
108108
encodeTransactionEip7702(s, pk)
109109

110+
func txType(s: TransactionArgs): TxType =
111+
if s.authorizationList.isSome:
112+
return TxEip7702
113+
if s.blobVersionedHashes.isSome:
114+
return TxEip4844
115+
if s.gasPrice.isNone:
116+
return TxEip1559
117+
if s.accessList.isSome:
118+
return TxEip2930
119+
TxLegacy
120+
121+
func encodeTransaction*(s: TransactionArgs, pk: PrivateKey): seq[byte] =
122+
encodeTransaction(s, pk, s.txType)
123+
110124
func encodeTransaction*(s: TransactionArgs, pk: PrivateKey, chainId: ChainId): seq[byte] {.deprecated: "Provide chainId in TransactionArgs".} =
111125
var tr = Transaction(txType: TxLegacy, chainId: chainId)
112126
tr.gasLimit = s.gas.get.GasInt

0 commit comments

Comments
 (0)