2023-10-25 00:36:00 +00:00
|
|
|
import pkg/chronicles
|
|
|
|
import pkg/stew/byteutils
|
2022-01-18 13:26:41 +00:00
|
|
|
import ./basics
|
2022-01-20 11:56:18 +00:00
|
|
|
import ./transaction
|
2022-01-24 11:14:31 +00:00
|
|
|
import ./blocktag
|
2022-01-18 13:26:41 +00:00
|
|
|
|
|
|
|
export basics
|
2022-01-20 11:56:18 +00:00
|
|
|
export transaction
|
2022-01-24 11:14:31 +00:00
|
|
|
export blocktag
|
2022-01-18 13:26:41 +00:00
|
|
|
|
2022-01-18 13:51:53 +00:00
|
|
|
push: {.upraises: [].}
|
|
|
|
|
2022-01-18 11:10:20 +00:00
|
|
|
type
|
|
|
|
Provider* = ref object of RootObj
|
2022-09-20 11:24:47 +00:00
|
|
|
ProviderError* = object of EthersError
|
2022-02-02 15:56:37 +00:00
|
|
|
Subscription* = ref object of RootObj
|
2023-07-20 05:51:28 +00:00
|
|
|
EventFilter* = ref object of RootObj
|
2022-02-02 15:56:37 +00:00
|
|
|
address*: Address
|
|
|
|
topics*: seq[Topic]
|
2023-07-20 05:51:28 +00:00
|
|
|
Filter* = ref object of EventFilter
|
|
|
|
fromBlock*: BlockTag
|
|
|
|
toBlock*: BlockTag
|
|
|
|
FilterByBlockHash* = ref object of EventFilter
|
|
|
|
blockHash*: BlockHash
|
2022-02-02 15:56:37 +00:00
|
|
|
Log* = object
|
2023-07-20 05:51:28 +00:00
|
|
|
blockNumber*: UInt256
|
2022-02-02 15:56:37 +00:00
|
|
|
data*: seq[byte]
|
2023-07-20 05:51:28 +00:00
|
|
|
logIndex*: UInt256
|
|
|
|
removed*: bool
|
2022-02-02 15:56:37 +00:00
|
|
|
topics*: seq[Topic]
|
2022-05-17 02:34:22 +00:00
|
|
|
TransactionHash* = array[32, byte]
|
|
|
|
BlockHash* = array[32, byte]
|
|
|
|
TransactionStatus* = enum
|
|
|
|
Failure = 0,
|
|
|
|
Success = 1,
|
|
|
|
Invalid = 2
|
|
|
|
TransactionResponse* = object
|
|
|
|
provider*: Provider
|
|
|
|
hash*: TransactionHash
|
|
|
|
TransactionReceipt* = object
|
|
|
|
sender*: ?Address
|
|
|
|
to*: ?Address
|
|
|
|
contractAddress*: ?Address
|
|
|
|
transactionIndex*: UInt256
|
|
|
|
gasUsed*: UInt256
|
|
|
|
logsBloom*: seq[byte]
|
2022-05-18 13:14:39 +00:00
|
|
|
blockHash*: ?BlockHash
|
2022-05-17 02:34:22 +00:00
|
|
|
transactionHash*: TransactionHash
|
|
|
|
logs*: seq[Log]
|
|
|
|
blockNumber*: ?UInt256
|
|
|
|
cumulativeGasUsed*: UInt256
|
2023-10-25 00:36:00 +00:00
|
|
|
effectiveGasPrice*: ?UInt256
|
2022-05-17 02:34:22 +00:00
|
|
|
status*: TransactionStatus
|
2023-10-25 00:36:00 +00:00
|
|
|
transactionType*: TransactionType
|
2022-02-02 15:56:37 +00:00
|
|
|
LogHandler* = proc(log: Log) {.gcsafe, upraises:[].}
|
2023-06-29 07:59:48 +00:00
|
|
|
BlockHandler* = proc(blck: Block) {.gcsafe, upraises:[].}
|
2022-02-02 15:56:37 +00:00
|
|
|
Topic* = array[32, byte]
|
2022-03-16 13:02:44 +00:00
|
|
|
Block* = object
|
2022-05-18 13:14:39 +00:00
|
|
|
number*: ?UInt256
|
2022-03-16 13:02:44 +00:00
|
|
|
timestamp*: UInt256
|
2023-06-29 08:00:03 +00:00
|
|
|
hash*: ?BlockHash
|
2023-10-25 00:36:00 +00:00
|
|
|
PastTransaction* = object
|
|
|
|
blockHash*: BlockHash
|
|
|
|
blockNumber*: UInt256
|
|
|
|
sender*: Address
|
|
|
|
gas*: UInt256
|
|
|
|
gasPrice*: UInt256
|
|
|
|
hash*: TransactionHash
|
|
|
|
input*: seq[byte]
|
|
|
|
nonce*: UInt256
|
|
|
|
to*: Address
|
|
|
|
transactionIndex*: UInt256
|
|
|
|
transactionType*: ?TransactionType
|
|
|
|
chainId*: ?UInt256
|
|
|
|
value*: UInt256
|
|
|
|
v*, r*, s*: UInt256
|
2022-01-18 13:26:41 +00:00
|
|
|
|
2022-05-18 13:14:39 +00:00
|
|
|
const EthersDefaultConfirmations* {.intdefine.} = 12
|
|
|
|
const EthersReceiptTimeoutBlks* {.intdefine.} = 50 # in blocks
|
2022-05-17 02:34:22 +00:00
|
|
|
|
2023-10-25 00:36:00 +00:00
|
|
|
logScope:
|
|
|
|
topics = "ethers provider"
|
|
|
|
|
|
|
|
template raiseProviderError(msg: string) =
|
|
|
|
raise newException(ProviderError, msg)
|
|
|
|
|
|
|
|
func toTransaction*(past: PastTransaction): Transaction =
|
|
|
|
Transaction(
|
|
|
|
sender: some past.sender,
|
|
|
|
gasPrice: some past.gasPrice,
|
|
|
|
data: past.input,
|
|
|
|
nonce: some past.nonce,
|
|
|
|
to: past.to,
|
|
|
|
transactionType: past.transactionType,
|
|
|
|
gasLimit: some past.gas,
|
|
|
|
chainId: past.chainId
|
|
|
|
)
|
|
|
|
|
2023-08-29 09:32:44 +00:00
|
|
|
method getBlockNumber*(provider: Provider): Future[UInt256] {.base, gcsafe.} =
|
2022-01-18 13:26:41 +00:00
|
|
|
doAssert false, "not implemented"
|
2022-03-16 13:02:44 +00:00
|
|
|
|
2023-08-29 09:32:44 +00:00
|
|
|
method getBlock*(provider: Provider, tag: BlockTag): Future[?Block] {.base, gcsafe.} =
|
2022-03-16 13:02:44 +00:00
|
|
|
doAssert false, "not implemented"
|
2022-01-20 11:56:18 +00:00
|
|
|
|
2022-04-10 20:21:59 +00:00
|
|
|
method call*(provider: Provider,
|
|
|
|
tx: Transaction,
|
2023-08-29 09:32:44 +00:00
|
|
|
blockTag = BlockTag.latest): Future[seq[byte]] {.base, gcsafe.} =
|
2022-01-20 11:56:18 +00:00
|
|
|
doAssert false, "not implemented"
|
2022-01-24 11:12:52 +00:00
|
|
|
|
2023-08-29 09:32:44 +00:00
|
|
|
method getGasPrice*(provider: Provider): Future[UInt256] {.base, gcsafe.} =
|
2022-01-24 11:12:52 +00:00
|
|
|
doAssert false, "not implemented"
|
2022-01-24 11:14:31 +00:00
|
|
|
|
|
|
|
method getTransactionCount*(provider: Provider,
|
|
|
|
address: Address,
|
|
|
|
blockTag = BlockTag.latest):
|
2023-08-29 09:32:44 +00:00
|
|
|
Future[UInt256] {.base, gcsafe.} =
|
2022-01-24 11:14:31 +00:00
|
|
|
doAssert false, "not implemented"
|
2022-01-24 13:40:47 +00:00
|
|
|
|
2023-10-25 00:36:00 +00:00
|
|
|
method getTransaction*(provider: Provider,
|
|
|
|
txHash: TransactionHash):
|
|
|
|
Future[?PastTransaction] {.base, gcsafe.} =
|
|
|
|
doAssert false, "not implemented"
|
|
|
|
|
2022-05-18 13:14:39 +00:00
|
|
|
method getTransactionReceipt*(provider: Provider,
|
|
|
|
txHash: TransactionHash):
|
2023-08-29 09:32:44 +00:00
|
|
|
Future[?TransactionReceipt] {.base, gcsafe.} =
|
2022-05-18 13:14:39 +00:00
|
|
|
doAssert false, "not implemented"
|
|
|
|
|
2022-07-14 09:45:37 +00:00
|
|
|
method sendTransaction*(provider: Provider,
|
|
|
|
rawTransaction: seq[byte]):
|
2023-08-29 09:32:44 +00:00
|
|
|
Future[TransactionResponse] {.base, gcsafe.} =
|
2022-07-14 09:45:37 +00:00
|
|
|
doAssert false, "not implemented"
|
|
|
|
|
2023-07-20 05:51:28 +00:00
|
|
|
method getLogs*(provider: Provider,
|
2023-08-29 09:32:44 +00:00
|
|
|
filter: EventFilter): Future[seq[Log]] {.base, gcsafe.} =
|
2023-07-20 05:51:28 +00:00
|
|
|
doAssert false, "not implemented"
|
|
|
|
|
2022-01-24 13:40:47 +00:00
|
|
|
method estimateGas*(provider: Provider,
|
2023-10-25 00:36:00 +00:00
|
|
|
transaction: Transaction,
|
|
|
|
blockTag = BlockTag.latest): Future[UInt256] {.base, gcsafe.} =
|
2022-01-24 13:40:47 +00:00
|
|
|
doAssert false, "not implemented"
|
2022-01-24 16:29:25 +00:00
|
|
|
|
2023-08-29 09:32:44 +00:00
|
|
|
method getChainId*(provider: Provider): Future[UInt256] {.base, gcsafe.} =
|
2022-01-24 16:29:25 +00:00
|
|
|
doAssert false, "not implemented"
|
2022-02-02 15:56:37 +00:00
|
|
|
|
|
|
|
method subscribe*(provider: Provider,
|
2023-07-20 05:51:28 +00:00
|
|
|
filter: EventFilter,
|
2022-02-02 15:56:37 +00:00
|
|
|
callback: LogHandler):
|
2023-08-29 09:32:44 +00:00
|
|
|
Future[Subscription] {.base, gcsafe.} =
|
2022-02-02 15:56:37 +00:00
|
|
|
doAssert false, "not implemented"
|
|
|
|
|
2022-05-16 12:51:39 +00:00
|
|
|
method subscribe*(provider: Provider,
|
|
|
|
callback: BlockHandler):
|
2023-08-29 09:32:44 +00:00
|
|
|
Future[Subscription] {.base, gcsafe.} =
|
2022-05-16 12:51:39 +00:00
|
|
|
doAssert false, "not implemented"
|
|
|
|
|
2022-02-02 15:56:37 +00:00
|
|
|
method unsubscribe*(subscription: Subscription) {.base, async.} =
|
|
|
|
doAssert false, "not implemented"
|
2022-05-23 15:29:44 +00:00
|
|
|
|
2023-10-25 00:36:00 +00:00
|
|
|
proc replay*(provider: Provider, tx: Transaction, blockNumber: UInt256) {.async.} =
|
|
|
|
# Replay transaction at block. Useful for fetching revert reasons, which will
|
|
|
|
# be present in the raised error message. The replayed block number should
|
|
|
|
# include the state of the chain in the block previous to the block in which
|
|
|
|
# the transaction was mined. This means that transactions that were mined in
|
|
|
|
# the same block BEFORE this transaction will not have their state transitions
|
|
|
|
# included in the replay.
|
|
|
|
# More information: https://snakecharmers.ethereum.org/web3py-revert-reason-parsing/
|
|
|
|
trace "replaying transaction", gasLimit = tx.gasLimit, tx = $tx
|
|
|
|
discard await provider.call(tx, BlockTag.init(blockNumber))
|
|
|
|
|
|
|
|
method getRevertReason*(
|
|
|
|
provider: Provider,
|
|
|
|
hash: TransactionHash,
|
|
|
|
blockNumber: UInt256
|
|
|
|
): Future[?string] {.base, async.} =
|
|
|
|
|
|
|
|
without pastTx =? await provider.getTransaction(hash):
|
|
|
|
return none string
|
|
|
|
|
|
|
|
try:
|
|
|
|
await provider.replay(pastTx.toTransaction, blockNumber)
|
|
|
|
return none string
|
|
|
|
except ProviderError as e:
|
|
|
|
# should contain the revert reason
|
|
|
|
return some e.msg
|
|
|
|
|
|
|
|
method getRevertReason*(
|
|
|
|
provider: Provider,
|
|
|
|
receipt: TransactionReceipt
|
|
|
|
): Future[?string] {.base, async.} =
|
|
|
|
|
|
|
|
if receipt.status != TransactionStatus.Failure:
|
|
|
|
return none string
|
|
|
|
|
|
|
|
without blockNumber =? receipt.blockNumber:
|
|
|
|
return none string
|
|
|
|
|
|
|
|
return await provider.getRevertReason(receipt.transactionHash, blockNumber - 1)
|
|
|
|
|
|
|
|
proc ensureSuccess(
|
|
|
|
provider: Provider,
|
|
|
|
receipt: TransactionReceipt
|
|
|
|
) {.async, upraises: [ProviderError].} =
|
|
|
|
## If the receipt.status is Failed, the tx is replayed to obtain a revert
|
|
|
|
## reason, after which a ProviderError with the revert reason is raised.
|
|
|
|
## If no revert reason was obtained
|
|
|
|
|
|
|
|
# TODO: handle TransactionStatus.Invalid?
|
|
|
|
if receipt.status == TransactionStatus.Failure:
|
|
|
|
logScope:
|
|
|
|
transactionHash = receipt.transactionHash.to0xHex
|
|
|
|
|
|
|
|
trace "transaction failed, replaying transaction to get revert reason"
|
|
|
|
|
|
|
|
if revertReason =? await provider.getRevertReason(receipt):
|
|
|
|
trace "transaction revert reason obtained", revertReason
|
|
|
|
raiseProviderError(revertReason)
|
|
|
|
else:
|
|
|
|
trace "transaction replay completed, no revert reason obtained"
|
|
|
|
raiseProviderError("Transaction reverted with unknown reason")
|
|
|
|
|
2022-05-23 15:29:44 +00:00
|
|
|
proc confirm*(tx: TransactionResponse,
|
2023-06-29 07:59:48 +00:00
|
|
|
confirmations = EthersDefaultConfirmations,
|
|
|
|
timeout = EthersReceiptTimeoutBlks):
|
|
|
|
Future[TransactionReceipt]
|
2023-10-25 00:36:00 +00:00
|
|
|
{.async, upraises: [ProviderError, EthersError].} =
|
2022-05-23 15:29:44 +00:00
|
|
|
## Waits for a transaction to be mined and for the specified number of blocks
|
|
|
|
## to pass since it was mined (confirmations).
|
|
|
|
## A timeout, in blocks, can be specified that will raise an error if too many
|
|
|
|
## blocks have passed without the tx having been mined.
|
|
|
|
|
2023-06-29 07:59:48 +00:00
|
|
|
var blockNumber: UInt256
|
|
|
|
let blockEvent = newAsyncEvent()
|
|
|
|
|
|
|
|
proc onBlockNumber(number: UInt256) =
|
|
|
|
blockNumber = number
|
|
|
|
blockEvent.fire()
|
|
|
|
|
|
|
|
proc onBlock(blck: Block) =
|
|
|
|
if number =? blck.number:
|
|
|
|
onBlockNumber(number)
|
|
|
|
|
|
|
|
onBlockNumber(await tx.provider.getBlockNumber())
|
|
|
|
let subscription = await tx.provider.subscribe(onBlock)
|
|
|
|
|
|
|
|
let finish = blockNumber + timeout.u256
|
|
|
|
var receipt: ?TransactionReceipt
|
|
|
|
|
|
|
|
while true:
|
|
|
|
await blockEvent.wait()
|
|
|
|
blockEvent.clear()
|
|
|
|
|
|
|
|
if blockNumber >= finish:
|
|
|
|
await subscription.unsubscribe()
|
|
|
|
raise newException(EthersError, "tx not mined before timeout")
|
|
|
|
|
|
|
|
if receipt.?blockNumber.isNone:
|
|
|
|
receipt = await tx.provider.getTransactionReceipt(tx.hash)
|
|
|
|
|
|
|
|
without receipt =? receipt and txBlockNumber =? receipt.blockNumber:
|
|
|
|
continue
|
|
|
|
|
|
|
|
if txBlockNumber + confirmations.u256 <= blockNumber + 1:
|
|
|
|
await subscription.unsubscribe()
|
2023-10-25 00:36:00 +00:00
|
|
|
await tx.provider.ensureSuccess(receipt)
|
2023-06-29 07:59:48 +00:00
|
|
|
return receipt
|
2022-05-23 15:29:44 +00:00
|
|
|
|
|
|
|
proc confirm*(tx: Future[TransactionResponse],
|
2023-06-29 07:59:48 +00:00
|
|
|
confirmations: int = EthersDefaultConfirmations,
|
|
|
|
timeout: int = EthersReceiptTimeoutBlks):
|
2022-05-23 15:29:44 +00:00
|
|
|
Future[TransactionReceipt] {.async.} =
|
|
|
|
## Convenience method that allows wait to be chained to a sendTransaction
|
|
|
|
## call, eg:
|
|
|
|
## `await signer.sendTransaction(populated).confirm(3)`
|
|
|
|
|
|
|
|
let txResp = await tx
|
2023-06-29 07:59:48 +00:00
|
|
|
return await txResp.confirm(confirmations, timeout)
|
2022-05-23 15:29:44 +00:00
|
|
|
|
2023-06-27 14:40:29 +00:00
|
|
|
method close*(provider: Provider) {.async, base.} =
|
|
|
|
discard
|