mirror of
https://github.com/logos-storage/nim-ethers.git
synced 2026-05-05 17:49:29 +00:00
Revert Transaction.data being optional, add better deserialization for Transaction object
This commit is contained in:
parent
45959cf608
commit
f52ce98c6d
@ -69,7 +69,7 @@ proc createTransaction(contract: Contract,
|
|||||||
let data = @selector & AbiEncoder.encode(parameters)
|
let data = @selector & AbiEncoder.encode(parameters)
|
||||||
Transaction(
|
Transaction(
|
||||||
to: contract.address,
|
to: contract.address,
|
||||||
data: some data,
|
data: data,
|
||||||
nonce: overrides.nonce,
|
nonce: overrides.nonce,
|
||||||
chainId: overrides.chainId,
|
chainId: overrides.chainId,
|
||||||
gasPrice: overrides.gasPrice,
|
gasPrice: overrides.gasPrice,
|
||||||
@ -259,6 +259,8 @@ proc confirm*(tx: Future[?TransactionResponse],
|
|||||||
let receipt = await response.confirm(confirmations, timeout)
|
let receipt = await response.confirm(confirmations, timeout)
|
||||||
|
|
||||||
if receipt.status != TransactionStatus.Success:
|
if receipt.status != TransactionStatus.Success:
|
||||||
|
logScope:
|
||||||
|
transactionHash = receipt.transactionHash
|
||||||
echo "[ethers contract] transaction failed, status: ", receipt.status
|
echo "[ethers contract] transaction failed, status: ", receipt.status
|
||||||
trace "transaction failed", status = receipt.status
|
trace "transaction failed", status = receipt.status
|
||||||
without blockNumber =? receipt.blockNumber:
|
without blockNumber =? receipt.blockNumber:
|
||||||
|
|||||||
@ -1,4 +1,5 @@
|
|||||||
import std/json
|
import std/json
|
||||||
|
import std/strformat
|
||||||
import std/strutils
|
import std/strutils
|
||||||
import pkg/json_rpc/jsonmarshal
|
import pkg/json_rpc/jsonmarshal
|
||||||
import pkg/stew/byteutils
|
import pkg/stew/byteutils
|
||||||
@ -87,3 +88,26 @@ func fromJson*(json: JsonNode, name: string, result: var TransactionStatus) =
|
|||||||
|
|
||||||
func `%`*(status: TransactionStatus): JsonNode =
|
func `%`*(status: TransactionStatus): JsonNode =
|
||||||
%(status.int.toHex)
|
%(status.int.toHex)
|
||||||
|
|
||||||
|
# Transaction
|
||||||
|
|
||||||
|
func fromJson*(json: JsonNode, name: string, result: var Transaction) =
|
||||||
|
# Deserializes a transaction response, eg eth_getTransactionByHash.
|
||||||
|
# Spec: https://ethereum.org/en/developers/docs/apis/json-rpc/#eth_gettransactionbyhash
|
||||||
|
let expectedFields =
|
||||||
|
@["input", "from", "to", "value", "nonce", "chainId", "gasPrice"]
|
||||||
|
|
||||||
|
for fieldName in expectedFields:
|
||||||
|
if not json.hasKey(fieldName):
|
||||||
|
raise newException(ValueError,
|
||||||
|
fmt"'{fieldName}' field not found in ${json}")
|
||||||
|
|
||||||
|
result = Transaction(
|
||||||
|
sender: fromJson(?Address, json["from"], "from"),
|
||||||
|
to: Address.fromJson(json["to"], "to"),
|
||||||
|
data: seq[byte].fromJson(json["input"], "input"),
|
||||||
|
value: UInt256.fromJson(json["value"], "value"),
|
||||||
|
nonce: fromJson(?UInt256, json["nonce"], "nonce"),
|
||||||
|
chainId: fromJson(?UInt256, json["chainId"], "chainId"),
|
||||||
|
gasPrice: fromJson(?UInt256, json["gasPrice"], "gasPrice")
|
||||||
|
)
|
||||||
@ -4,7 +4,7 @@ import ./basics
|
|||||||
type Transaction* = object
|
type Transaction* = object
|
||||||
sender*: ?Address
|
sender*: ?Address
|
||||||
to*: Address
|
to*: Address
|
||||||
data*: ?seq[byte]
|
data*: seq[byte]
|
||||||
value*: UInt256
|
value*: UInt256
|
||||||
nonce*: ?UInt256
|
nonce*: ?UInt256
|
||||||
chainId*: ?UInt256
|
chainId*: ?UInt256
|
||||||
@ -19,7 +19,7 @@ func `$`*(transaction: Transaction): string =
|
|||||||
result &= "from: " & $sender & ", "
|
result &= "from: " & $sender & ", "
|
||||||
result &= "to: " & $transaction.to & ", "
|
result &= "to: " & $transaction.to & ", "
|
||||||
result &= "value: " & $transaction.value & ", "
|
result &= "value: " & $transaction.value & ", "
|
||||||
result &= "data: 0x" & $(transaction.data.?toHex)
|
result &= "data: 0x" & $(transaction.data.toHex)
|
||||||
if nonce =? transaction.nonce:
|
if nonce =? transaction.nonce:
|
||||||
result &= ", nonce: " & $nonce
|
result &= ", nonce: " & $nonce
|
||||||
if chainId =? transaction.chainId:
|
if chainId =? transaction.chainId:
|
||||||
|
|||||||
@ -26,7 +26,7 @@ func toSignableTransaction(transaction: Transaction): SignableTransaction =
|
|||||||
signable.gasLimit = GasInt(gasLimit.truncate(uint64))
|
signable.gasLimit = GasInt(gasLimit.truncate(uint64))
|
||||||
signable.to = some EthAddress(transaction.to)
|
signable.to = some EthAddress(transaction.to)
|
||||||
signable.value = transaction.value
|
signable.value = transaction.value
|
||||||
signable.payload = transaction.data |? newSeq[byte]()
|
signable.payload = transaction.data
|
||||||
|
|
||||||
if maxFee =? transaction.maxFee and
|
if maxFee =? transaction.maxFee and
|
||||||
maxPriorityFee =? transaction.maxPriorityFee:
|
maxPriorityFee =? transaction.maxPriorityFee:
|
||||||
|
|||||||
@ -21,4 +21,4 @@ proc example*(_: type UInt256): UInt256 =
|
|||||||
UInt256.fromBytesBE(array[32, byte].example)
|
UInt256.fromBytesBE(array[32, byte].example)
|
||||||
|
|
||||||
proc example*(_: type Transaction): Transaction =
|
proc example*(_: type Transaction): Transaction =
|
||||||
Transaction(to: Address.example, data: some seq[byte].example)
|
Transaction(to: Address.example, data: seq[byte].example)
|
||||||
|
|||||||
@ -119,7 +119,7 @@ suite "JSON Conversions":
|
|||||||
expect ValueError:
|
expect ValueError:
|
||||||
discard Log.fromJson(parseJson(json))
|
discard Log.fromJson(parseJson(json))
|
||||||
|
|
||||||
test "missing data in Transaction isNone":
|
test "getTransactionByHash correctly deserializes 'data' field from 'input' for Transaction":
|
||||||
let json = %*{
|
let json = %*{
|
||||||
"blockHash":"0x595bffbe897e025ea2df3213c4cc52c3f3d69bc04b49011d558f1b0e70038922",
|
"blockHash":"0x595bffbe897e025ea2df3213c4cc52c3f3d69bc04b49011d558f1b0e70038922",
|
||||||
"blockNumber":"0x22e",
|
"blockNumber":"0x22e",
|
||||||
@ -140,4 +140,4 @@ suite "JSON Conversions":
|
|||||||
}
|
}
|
||||||
|
|
||||||
let receipt = Transaction.fromJson(json)
|
let receipt = Transaction.fromJson(json)
|
||||||
check receipt.data.isNone
|
check receipt.data.len > 0
|
||||||
|
|||||||
@ -66,7 +66,7 @@ suite "Wallet":
|
|||||||
gasPrice: some 20 * 10.u256.pow(9),
|
gasPrice: some 20 * 10.u256.pow(9),
|
||||||
gasLimit: some 21000.u256,
|
gasLimit: some 21000.u256,
|
||||||
value: 10.u256.pow(18),
|
value: 10.u256.pow(18),
|
||||||
data: none seq[byte]
|
data: @[]
|
||||||
)
|
)
|
||||||
let signed = await wallet.signTransaction(transaction)
|
let signed = await wallet.signTransaction(transaction)
|
||||||
check signed.toHex == "f86c098504a817c800825208943535353535353535353535353535353535353535880de0b6b3a76400008025a028ef61340bd939bc2195fe537567866003e1a15d3c71ff63e1590620aa636276a067cbe9d8997f761aecb703304b3800ccf555c9f3dc64214b297fb1966a3b6d83"
|
check signed.toHex == "f86c098504a817c800825208943535353535353535353535353535353535353535880de0b6b3a76400008025a028ef61340bd939bc2195fe537567866003e1a15d3c71ff63e1590620aa636276a067cbe9d8997f761aecb703304b3800ccf555c9f3dc64214b297fb1966a3b6d83"
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user