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
|
|
|
|
Filter* = object
|
|
|
|
address*: Address
|
|
|
|
topics*: seq[Topic]
|
|
|
|
Log* = object
|
|
|
|
data*: seq[byte]
|
|
|
|
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
|
|
|
|
status*: TransactionStatus
|
2022-02-02 15:56:37 +00:00
|
|
|
LogHandler* = proc(log: Log) {.gcsafe, upraises:[].}
|
2022-05-18 13:14:39 +00:00
|
|
|
BlockHandler* = proc(blck: Block): Future[void] {.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-04-24 10:36:08 +00:00
|
|
|
hash*: ?array[32, byte]
|
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
|
|
|
|
2022-01-18 13:26:41 +00:00
|
|
|
method getBlockNumber*(provider: Provider): Future[UInt256] {.base.} =
|
|
|
|
doAssert false, "not implemented"
|
2022-03-16 13:02:44 +00:00
|
|
|
|
2022-03-17 09:16:13 +00:00
|
|
|
method getBlock*(provider: Provider, tag: BlockTag): Future[?Block] {.base.} =
|
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,
|
|
|
|
blockTag = BlockTag.latest): Future[seq[byte]] {.base.} =
|
2022-01-20 11:56:18 +00:00
|
|
|
doAssert false, "not implemented"
|
2022-01-24 11:12:52 +00:00
|
|
|
|
|
|
|
method getGasPrice*(provider: Provider): Future[UInt256] {.base.} =
|
|
|
|
doAssert false, "not implemented"
|
2022-01-24 11:14:31 +00:00
|
|
|
|
|
|
|
method getTransactionCount*(provider: Provider,
|
|
|
|
address: Address,
|
|
|
|
blockTag = BlockTag.latest):
|
|
|
|
Future[UInt256] {.base.} =
|
|
|
|
doAssert false, "not implemented"
|
2022-01-24 13:40:47 +00:00
|
|
|
|
2022-05-18 13:14:39 +00:00
|
|
|
method getTransactionReceipt*(provider: Provider,
|
|
|
|
txHash: TransactionHash):
|
|
|
|
Future[?TransactionReceipt] {.base.} =
|
|
|
|
doAssert false, "not implemented"
|
|
|
|
|
2022-07-14 09:45:37 +00:00
|
|
|
method sendTransaction*(provider: Provider,
|
|
|
|
rawTransaction: seq[byte]):
|
|
|
|
Future[TransactionResponse] {.base.} =
|
|
|
|
doAssert false, "not implemented"
|
|
|
|
|
2022-01-24 13:40:47 +00:00
|
|
|
method estimateGas*(provider: Provider,
|
|
|
|
transaction: Transaction): Future[UInt256] {.base.} =
|
|
|
|
doAssert false, "not implemented"
|
2022-01-24 16:29:25 +00:00
|
|
|
|
|
|
|
method getChainId*(provider: Provider): Future[UInt256] {.base.} =
|
|
|
|
doAssert false, "not implemented"
|
2022-02-02 15:56:37 +00:00
|
|
|
|
|
|
|
method subscribe*(provider: Provider,
|
|
|
|
filter: Filter,
|
|
|
|
callback: LogHandler):
|
|
|
|
Future[Subscription] {.base.} =
|
|
|
|
doAssert false, "not implemented"
|
|
|
|
|
2022-05-16 12:51:39 +00:00
|
|
|
method subscribe*(provider: Provider,
|
|
|
|
callback: BlockHandler):
|
|
|
|
Future[Subscription] {.base.} =
|
|
|
|
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
|
|
|
|
|
|
|
# Removed from `confirm` closure and exported so it can be tested.
|
|
|
|
# Likely there is a better way
|
|
|
|
func confirmations*(receiptBlk, atBlk: UInt256): UInt256 =
|
|
|
|
## Calculates the number of confirmations between two blocks
|
|
|
|
if atBlk < receiptBlk:
|
|
|
|
return 0.u256
|
|
|
|
else:
|
|
|
|
return (atBlk - receiptBlk) + 1 # add 1 for current block
|
|
|
|
|
|
|
|
# Removed from `confirm` closure and exported so it can be tested.
|
|
|
|
# Likely there is a better way
|
2022-05-23 15:39:00 +00:00
|
|
|
func hasBeenMined*(receipt: TransactionReceipt,
|
2022-05-23 15:29:44 +00:00
|
|
|
atBlock: UInt256,
|
|
|
|
wantedConfirms: int): bool =
|
|
|
|
## Returns true if the transaction receipt has been returned from the node
|
|
|
|
## with a valid block number and block hash and the specified number of
|
|
|
|
## blocks have passed since the tx was mined (confirmations)
|
|
|
|
|
2022-05-23 15:39:00 +00:00
|
|
|
if number =? receipt.blockNumber and
|
|
|
|
number > 0 and
|
2022-05-23 15:29:44 +00:00
|
|
|
# from ethers.js: "geth-etc" returns receipts before they are ready
|
2022-05-23 15:39:00 +00:00
|
|
|
receipt.blockHash.isSome:
|
2022-05-23 15:29:44 +00:00
|
|
|
|
2022-05-23 15:39:00 +00:00
|
|
|
return number.confirmations(atBlock) >= wantedConfirms.u256
|
2022-05-23 15:29:44 +00:00
|
|
|
|
|
|
|
return false
|
|
|
|
|
|
|
|
proc confirm*(tx: TransactionResponse,
|
|
|
|
wantedConfirms: Positive = EthersDefaultConfirmations,
|
|
|
|
timeoutInBlocks: Natural = EthersReceiptTimeoutBlks):
|
|
|
|
Future[TransactionReceipt]
|
|
|
|
{.async, upraises: [EthersError].} = # raises for clarity
|
|
|
|
## 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.
|
|
|
|
|
|
|
|
var subscription: Subscription
|
|
|
|
let
|
|
|
|
provider = tx.provider
|
|
|
|
retFut = newFuture[TransactionReceipt]("wait")
|
|
|
|
|
|
|
|
# used to check for block timeouts
|
|
|
|
let startBlock = await provider.getBlockNumber()
|
|
|
|
|
|
|
|
proc newBlock(blk: Block) {.async.} =
|
|
|
|
## subscription callback, called every time a new block event is sent from
|
|
|
|
## the node
|
|
|
|
|
|
|
|
# if ethereum node doesn't include blockNumber in the event
|
|
|
|
without blkNum =? blk.number:
|
|
|
|
return
|
|
|
|
|
2022-05-23 15:39:00 +00:00
|
|
|
if receipt =? (await provider.getTransactionReceipt(tx.hash)) and
|
|
|
|
receipt.hasBeenMined(blkNum, wantedConfirms):
|
2022-05-23 15:29:44 +00:00
|
|
|
# fire and forget
|
|
|
|
discard subscription.unsubscribe()
|
|
|
|
if not retFut.finished:
|
2022-05-23 15:39:00 +00:00
|
|
|
retFut.complete(receipt)
|
2022-05-23 15:29:44 +00:00
|
|
|
|
|
|
|
elif timeoutInBlocks > 0:
|
|
|
|
let blocksPassed = (blkNum - startBlock) + 1
|
|
|
|
if blocksPassed >= timeoutInBlocks.u256:
|
|
|
|
discard subscription.unsubscribe()
|
|
|
|
if not retFut.finished:
|
|
|
|
let message =
|
|
|
|
"Transaction was not mined in " & $timeoutInBlocks & " blocks"
|
|
|
|
retFut.fail(newException(EthersError, message))
|
|
|
|
|
|
|
|
# If our tx is already mined, return the receipt. Otherwise, check each
|
|
|
|
# new block to see if the tx has been mined
|
2022-05-23 15:39:00 +00:00
|
|
|
if receipt =? (await provider.getTransactionReceipt(tx.hash)) and
|
|
|
|
receipt.hasBeenMined(startBlock, wantedConfirms):
|
|
|
|
return receipt
|
2022-05-23 15:29:44 +00:00
|
|
|
else:
|
|
|
|
subscription = await provider.subscribe(newBlock)
|
|
|
|
return (await retFut)
|
|
|
|
|
|
|
|
proc confirm*(tx: Future[TransactionResponse],
|
|
|
|
wantedConfirms: Positive = EthersDefaultConfirmations,
|
|
|
|
timeoutInBlocks: Natural = EthersReceiptTimeoutBlks):
|
|
|
|
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
|
|
|
|
return await txResp.confirm(wantedConfirms, timeoutInBlocks)
|
|
|
|
|
|
|
|
proc confirm*(tx: Future[?TransactionResponse],
|
|
|
|
wantedConfirms: Positive = EthersDefaultConfirmations,
|
|
|
|
timeoutInBlocks: Natural = EthersReceiptTimeoutBlks):
|
|
|
|
Future[TransactionReceipt] {.async.} =
|
|
|
|
## Convenience method that allows wait to be chained to a contract
|
|
|
|
## transaction, eg:
|
|
|
|
## `await token.connect(signer0)
|
|
|
|
## .mint(accounts[1], 100.u256)
|
|
|
|
## .confirm(3)`
|
|
|
|
|
|
|
|
without txResp =? (await tx):
|
|
|
|
raise newException(
|
|
|
|
EthersError,
|
|
|
|
"Transaction hash required. Possibly was a call instead of a send?"
|
|
|
|
)
|
|
|
|
|
|
|
|
return await txResp.confirm(wantedConfirms, timeoutInBlocks)
|
2023-06-27 14:40:29 +00:00
|
|
|
|
|
|
|
method close*(provider: Provider) {.async, base.} =
|
|
|
|
discard
|