Skip to content

Commit

Permalink
Replace std.Option with results.Opt
Browse files Browse the repository at this point in the history
  • Loading branch information
jangko committed Jun 11, 2024
1 parent 954c23c commit b0a5d92
Show file tree
Hide file tree
Showing 19 changed files with 264 additions and 246 deletions.
2 changes: 1 addition & 1 deletion tests/helpers/handlers.nim
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ proc installHandlers*(server: RpcServer) =
if x != "-1".JsonString:
result = decodeFromString(x, UInt256)

server.rpc("eth_feeHistory") do(x: JsonString, blockCount: Quantity, newestBlock: RtBlockIdentifier, rewardPercentiles: Option[seq[float64]]) -> FeeHistoryResult:
server.rpc("eth_feeHistory") do(x: JsonString, blockCount: Quantity, newestBlock: RtBlockIdentifier, rewardPercentiles: Opt[seq[float64]]) -> FeeHistoryResult:
var fh: FeeHistoryResult
if x != "-1".JsonString:
fh = decodeFromString(x, FeeHistoryResult)
Expand Down
10 changes: 5 additions & 5 deletions tests/helpers/utils.nim
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import
std/options,
results,
chronos,
stew/byteutils,
../../web3,
Expand All @@ -8,11 +8,11 @@ import
proc deployContract*(web3: Web3, code: string, gasPrice = 0): Future[ReceiptObject] {.async.} =
var code = code
var tr: TransactionArgs
tr.`from` = some(web3.defaultAccount)
tr.data = some(hexToSeqByte(code))
tr.gas = Quantity(3000000).some
tr.`from` = Opt.some(web3.defaultAccount)
tr.data = Opt.some(hexToSeqByte(code))
tr.gas = Opt.some Quantity(3000000)
if gasPrice != 0:
tr.gasPrice = some(gasPrice.Quantity)
tr.gasPrice = Opt.some(gasPrice.Quantity)

let r = await web3.send(tr)
return await web3.getMinedTransactionReceipt(r)
7 changes: 4 additions & 3 deletions tests/test_contracts.nim
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@
# those terms.

import
std/[options, json],
std/json,
pkg/unittest2,
chronos, stint,
results,
../web3,
./helpers/utils

Expand Down Expand Up @@ -265,8 +266,8 @@ suite "Contracts":

echo "transfers: ", await ns.getJsonLogs(
Transfer,
fromBlock = some(blockId(deployedAtBlock)),
toBlock = some(blockId(1000.BlockNumber)))
fromBlock = Opt.some(blockId(deployedAtBlock)),
toBlock = Opt.some(blockId(1000.BlockNumber)))

await notifFut
await s.unsubscribe()
Expand Down
5 changes: 3 additions & 2 deletions tests/test_deposit_contract.nim
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@
# those terms.

import
std/[options, json],
std/json,
pkg/unittest2,
chronos, stint,
results,
../web3,
./helpers/utils,
./helpers/primitives_utils,
Expand Down Expand Up @@ -43,7 +44,7 @@ suite "Deposit contract":

var fut = newFuture[void]()

let options = FilterOptions(fromBlock: some(blockId(0.BlockNumber)))
let options = FilterOptions(fromBlock: Opt.some(blockId(0.BlockNumber)))
let s = await ns.subscribe(DepositEvent, options) do (
pubkey: DynamicBytes[0, 48], withdrawalCredentials: DynamicBytes[0, 32], amount: DynamicBytes[0, 8], signature: DynamicBytes[0, 96], merkleTreeIndex: DynamicBytes[0, 8])
{.raises: [], gcsafe.}:
Expand Down
48 changes: 24 additions & 24 deletions tests/test_execution_types.nim
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,17 @@ suite "Execution types tests":
baseFeePerGas: 12.u256,
blockHash: h256(13),
transactions: @[TypedTransaction.conv(14)],
withdrawals: some(@[wd]),
blobGasUsed: some(15.Quantity),
excessBlobGas: some(16.Quantity),
withdrawals: Opt.some(@[wd]),
blobGasUsed: Opt.some(15.Quantity),
excessBlobGas: Opt.some(16.Quantity),
)

attr = PayloadAttributes(
timestamp: 1.Quantity,
prevRandao: h256(2),
suggestedFeeRecipient: address(3),
withdrawals: some(@[wd]),
parentBeaconBlockRoot: some(h256(4)),
withdrawals: Opt.some(@[wd]),
parentBeaconBlockRoot: Opt.some(h256(4)),
)

blobs = BlobsBundleV1(
Expand All @@ -59,9 +59,9 @@ suite "Execution types tests":

response = GetPayloadResponse(
executionPayload: payload,
blockValue: some(1.u256),
blobsBundle: some(blobs),
shouldOverrideBuilder: some(false),
blockValue: Opt.some(1.u256),
blobsBundle: Opt.some(blobs),
shouldOverrideBuilder: Opt.some(false),
)

deposit = DepositReceiptV1(
Expand All @@ -79,14 +79,14 @@ suite "Execution types tests":

test "payload version":
var badv31 = payload
badv31.blobGasUsed = none(Quantity)
badv31.blobGasUsed = Opt.none(Quantity)
var badv32 = payload
badv32.excessBlobGas = none(Quantity)
badv32.excessBlobGas = Opt.none(Quantity)
var v2 = payload
v2.blobGasUsed = none(Quantity)
v2.excessBlobGas = none(Quantity)
v2.blobGasUsed = Opt.none(Quantity)
v2.excessBlobGas = Opt.none(Quantity)
var v1 = v2
v1.withdrawals = none(seq[WithdrawalV1])
v1.withdrawals = Opt.none(seq[WithdrawalV1])
check badv31.version == Version.V3
check badv32.version == Version.V3
check v2.version == Version.V2
Expand All @@ -104,23 +104,23 @@ suite "Execution types tests":

test "attr version":
var v2 = attr
v2.parentBeaconBlockRoot = none(Hash256)
v2.parentBeaconBlockRoot = Opt.none(Hash256)
var v1 = v2
v1.withdrawals = none(seq[WithdrawalV1])
v1.withdrawals = Opt.none(seq[WithdrawalV1])
check attr.version == Version.V3
check v2.version == Version.V2
check v1.version == Version.V1

test "response version":
var badv31 = response
badv31.blobsBundle = none(BlobsBundleV1)
badv31.blobsBundle = Opt.none(BlobsBundleV1)
var badv32 = response
badv32.shouldOverrideBuilder = none(bool)
badv32.shouldOverrideBuilder = Opt.none(bool)
var v2 = response
v2.blobsBundle = none(BlobsBundleV1)
v2.shouldOverrideBuilder = none(bool)
v2.blobsBundle = Opt.none(BlobsBundleV1)
v2.shouldOverrideBuilder = Opt.none(bool)
var v1 = v2
v1.blockValue = none(UInt256)
v1.blockValue = Opt.none(UInt256)
check badv31.version == Version.V3
check badv32.version == Version.V3
check v2.version == Version.V2
Expand Down Expand Up @@ -167,16 +167,16 @@ suite "Execution types tests":

test "payload version 4":
var v4 = payload
v4.depositReceipts = some(@[deposit])
v4.exits = some(@[exit])
v4.depositReceipts = Opt.some(@[deposit])
v4.exits = Opt.some(@[exit])
check v4.version == Version.V4

var bad41 = v4
bad41.depositReceipts = none(seq[DepositReceiptV1])
bad41.depositReceipts = Opt.none(seq[DepositReceiptV1])
check bad41.version == Version.V4

var bad42 = v4
bad42.exits = none(seq[WithdrawalRequestV1])
bad42.exits = Opt.none(seq[WithdrawalRequestV1])
check bad42.version == Version.V4

let v41 = bad41.V4
Expand Down
10 changes: 9 additions & 1 deletion tests/test_json_marshalling.nim
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,14 @@ proc rand[T](_: type seq[T]): seq[T] =
proc rand[T](_: type SingleOrList[T]): SingleOrList[T] =
SingleOrList[T](kind: slkSingle, single: rand(T))

proc rand[X](T: type Opt[X]): T =
var x: array[1, byte]
discard randomBytes(x)
if x[0] > 127:
result = Opt.some(rand(X))
else:
result = Opt.none(X)

proc rand[X: object](T: type X): T =
result = T()
for field in fields(result):
Expand Down Expand Up @@ -237,7 +245,7 @@ suite "JSON-RPC Quantity":

test "AccessListResult with error":
let z = AccessListResult(
error: some("error")
error: Opt.some("error")
)
let w = JrpcConv.encode(z)
check w == """{"accessList":[],"error":"error","gasUsed":"0x0"}"""
5 changes: 3 additions & 2 deletions tests/test_logs.nim
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@
# those terms.

import
std/[options, json, random],
std/[json, random],
pkg/unittest2,
../web3,
chronos, stint,
results,
./helpers/utils

#[ Contract LoggerContract
Expand Down Expand Up @@ -74,7 +75,7 @@ suite "Logs":
let notifFut = newFuture[void]()
var notificationsReceived = 0

let options = FilterOptions(fromBlock: some(blockId(0.BlockNumber)))
let options = FilterOptions(fromBlock: Opt.some(blockId(0.BlockNumber)))
let s = await ns.subscribe(MyEvent, options) do (
sender: Address, value: UInt256)
{.raises: [], gcsafe.}:
Expand Down
30 changes: 15 additions & 15 deletions tests/test_signed_tx.nim
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
# those terms.

import
std/[options],
pkg/unittest2,
chronos, stint,
results,
eth/keys,
eth/common/eth_types,
stew/byteutils,
Expand Down Expand Up @@ -47,12 +47,12 @@ suite "Signed transactions":
publicKey = privateKey.toPublicKey()
address = publicKey.toCanonicalAddress()
var tx: TransactionArgs
tx.nonce = some(Quantity(9))
tx.`from` = some(Address(address))
tx.value = some(1000000000000000000.u256)
tx.to = some(Address(hexToByteArray[20]("0x3535353535353535353535353535353535353535")))
tx.gas = some(Quantity(21000'u64))
tx.gasPrice = some(Quantity(20000000000'i64))
tx.nonce = Opt.some(Quantity(9))
tx.`from` = Opt.some(Address(address))
tx.value = Opt.some(1000000000000000000.u256)
tx.to = Opt.some(Address(hexToByteArray[20]("0x3535353535353535353535353535353535353535")))
tx.gas = Opt.some(Quantity(21000'u64))
tx.gasPrice = Opt.some(Quantity(20000000000'i64))

let txBytes = encodeTransaction(tx, privateKey, ChainId(1))
let txHex = "0x" & txBytes.toHex
Expand All @@ -71,21 +71,21 @@ suite "Signed transactions":
let acc = Address(toCanonicalAddress(pk.toPublicKey()))

var tx: TransactionArgs
tx.`from` = some(accounts[0])
tx.value = some(ethToWei(10.u256))
tx.to = some(acc)
tx.gasPrice = some(gasPrice)
tx.`from` = Opt.some(accounts[0])
tx.value = Opt.some(ethToWei(10.u256))
tx.to = Opt.some(acc)
tx.gasPrice = Opt.some(gasPrice)

# Send 10 eth to acc
discard await web3.send(tx)
var balance = await web3.provider.eth_getBalance(acc, "latest")
assert(balance == ethToWei(10.u256))

# Send 5 eth back
web3.privateKey = some(pk)
tx.value = some(ethToWei(5.u256))
tx.to = some(accounts[0])
tx.gas = some(Quantity(3000000))
web3.privateKey = Opt.some(pk)
tx.value = Opt.some(ethToWei(5.u256))
tx.to = Opt.some(accounts[0])
tx.gas = Opt.some(Quantity(3000000))

discard await web3.send(tx)
balance = await web3.provider.eth_getBalance(acc, "latest")
Expand Down
Loading

0 comments on commit b0a5d92

Please sign in to comment.