mirror of
https://github.com/logos-storage/nim-ethers.git
synced 2026-01-10 01:23:05 +00:00
add serialization for PastTransaction
This commit is contained in:
parent
35f80e78fe
commit
a6aeb7ec49
@ -1,6 +1,7 @@
|
||||
import std/json
|
||||
import std/strformat
|
||||
import std/strutils
|
||||
import std/typetraits
|
||||
import pkg/json_rpc/jsonmarshal
|
||||
import pkg/stew/byteutils
|
||||
import ../../basics
|
||||
@ -94,7 +95,7 @@ func fromJson*(json: JsonNode, name: string, result: var TransactionStatus) =
|
||||
func `%`*(status: TransactionStatus): JsonNode =
|
||||
%(status.int.toHex)
|
||||
|
||||
# Transaction
|
||||
# PastTransaction
|
||||
|
||||
proc expectFields(json: JsonNode, expectedFields: varargs[string]) =
|
||||
for fieldName in expectedFields:
|
||||
@ -123,4 +124,22 @@ func fromJson*(json: JsonNode, name: string, result: var PastTransaction) =
|
||||
v: UInt256.fromJson(json["v"], "v"),
|
||||
r: UInt256.fromJson(json["r"], "r"),
|
||||
s: UInt256.fromJson(json["s"], "s"),
|
||||
)
|
||||
)
|
||||
|
||||
func `%`*(tx: PastTransaction): JsonNode =
|
||||
%*{
|
||||
"blockHash": tx.blockHash,
|
||||
"blockNumber": tx.blockNumber,
|
||||
"from": tx.sender,
|
||||
"gas": tx.gas,
|
||||
"gasPrice": tx.gasPrice,
|
||||
"hash": tx.hash,
|
||||
"input": tx.input,
|
||||
"nonce": tx.nonce,
|
||||
"to": tx.to,
|
||||
"transactionIndex": tx.transactionIndex,
|
||||
"value": tx.value,
|
||||
"v": tx.v,
|
||||
"r": tx.r,
|
||||
"s": tx.s
|
||||
}
|
||||
|
||||
@ -1,8 +1,13 @@
|
||||
import std/strutils
|
||||
import std/unittest
|
||||
import pkg/ethers/provider
|
||||
import pkg/ethers/providers/jsonrpc/conversions
|
||||
import pkg/stew/byteutils
|
||||
|
||||
func flatten(s: string): string =
|
||||
s.replace(" ")
|
||||
.replace("\n")
|
||||
|
||||
suite "JSON Conversions":
|
||||
|
||||
test "missing block number in Block isNone":
|
||||
@ -155,3 +160,41 @@ suite "JSON Conversions":
|
||||
check tx.v == 0x181bec.u256
|
||||
check tx.r == UInt256.fromBytesBE(hexToSeqByte("0x57ba18460934526333b80b0fea08737c363f3cd5fbec4a25a8a25e3e8acb362a"))
|
||||
check tx.s == UInt256.fromBytesBE(hexToSeqByte("0x33aa50bc8bd719b6b17ad0bf52006bf8943999198f2bf731eb33c118091000f2"))
|
||||
|
||||
test "PastTransaction serializes correctly":
|
||||
let tx = PastTransaction(
|
||||
blockHash: BlockHash(array[32, byte].fromHex("0x595bffbe897e025ea2df3213c4cc52c3f3d69bc04b49011d558f1b0e70038922")),
|
||||
blockNumber: 0x22e.u256,
|
||||
sender: Address.init("0xe00b677c29ff8d8fe6068530e2bc36158c54dd34").get,
|
||||
gas: 0x4d4bb.u256,
|
||||
gasPrice: 0x3b9aca07.u256,
|
||||
hash: TransactionHash(array[32, byte].fromHex("0xa31608907c338d6497b0c6ec81049d845c7d409490ebf78171f35143897ca790")),
|
||||
input: hexToSeqByte("0x6368a471d26ff5c7f835c1a8203235e88846ce1a196d6e79df0eaedd1b8ed3deec2ae5c00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000000012a00000000000000000000000000000000000000000000000000000000000000"),
|
||||
nonce: 0x3.u256,
|
||||
to: Address.init("0x92f09aa59dccb892a9f5406ddd9c0b98f02ea57e").get,
|
||||
transactionIndex: 0x3.u256,
|
||||
value: 0.u256,
|
||||
v: 0x181bec.u256,
|
||||
r: UInt256.fromBytesBE(hexToSeqByte("0x57ba18460934526333b80b0fea08737c363f3cd5fbec4a25a8a25e3e8acb362a")),
|
||||
s: UInt256.fromBytesBE(hexToSeqByte("0x33aa50bc8bd719b6b17ad0bf52006bf8943999198f2bf731eb33c118091000f2"))
|
||||
)
|
||||
let expected = """
|
||||
{
|
||||
"blockHash":"0x595bffbe897e025ea2df3213c4cc52c3f3d69bc04b49011d558f1b0e70038922",
|
||||
"blockNumber":"0x22e",
|
||||
"from":"0xe00b677c29ff8d8fe6068530e2bc36158c54dd34",
|
||||
"gas":"0x4d4bb",
|
||||
"gasPrice":"0x3b9aca07",
|
||||
"hash":"0xa31608907c338d6497b0c6ec81049d845c7d409490ebf78171f35143897ca790",
|
||||
"input":"0x6368a471d26ff5c7f835c1a8203235e88846ce1a196d6e79df0eaedd1b8ed3deec2ae5c00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000000012a00000000000000000000000000000000000000000000000000000000000000",
|
||||
"nonce":"0x3",
|
||||
"to":"0x92f09aa59dccb892a9f5406ddd9c0b98f02ea57e",
|
||||
"transactionIndex":"0x3",
|
||||
"value":"0x0",
|
||||
"v":"0x181bec",
|
||||
"r":"0x57ba18460934526333b80b0fea08737c363f3cd5fbec4a25a8a25e3e8acb362a",
|
||||
"s":"0x33aa50bc8bd719b6b17ad0bf52006bf8943999198f2bf731eb33c118091000f2"
|
||||
}""".flatten
|
||||
# "type":"0x0",
|
||||
# "chainId":"0xc0de4",
|
||||
check $(%tx) == expected
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user