2022-01-20 13:39:37 +00:00
|
|
|
import ./basics
|
2022-01-24 11:12:52 +00:00
|
|
|
import ./provider
|
2022-01-20 13:39:37 +00:00
|
|
|
|
|
|
|
export basics
|
|
|
|
|
2023-09-14 23:54:08 +00:00
|
|
|
type
|
|
|
|
Signer* = ref object of RootObj
|
|
|
|
lastSeenNonce: ?UInt256
|
2023-10-24 23:42:25 +00:00
|
|
|
populateLock: AsyncLock
|
2023-09-14 23:54:08 +00:00
|
|
|
|
2023-10-24 23:42:25 +00:00
|
|
|
type
|
|
|
|
SignerError* = object of EthersError
|
|
|
|
EstimateGasError* = object of SignerError
|
|
|
|
transaction*: Transaction
|
|
|
|
|
|
|
|
template raiseSignerError(message: string, parent: ref ProviderError = nil) =
|
|
|
|
raise newException(SignerError, message, parent)
|
|
|
|
|
|
|
|
proc raiseEstimateGasError(
|
|
|
|
transaction: Transaction,
|
|
|
|
parent: ref ProviderError = nil
|
|
|
|
) =
|
|
|
|
let e = (ref EstimateGasError)(
|
|
|
|
msg: "Estimate gas failed",
|
|
|
|
transaction: transaction,
|
|
|
|
parent: parent)
|
|
|
|
raise e
|
2022-01-20 13:39:37 +00:00
|
|
|
|
2023-08-29 09:32:44 +00:00
|
|
|
method provider*(signer: Signer): Provider {.base, gcsafe.} =
|
2022-01-20 13:39:37 +00:00
|
|
|
doAssert false, "not implemented"
|
2022-01-24 11:12:52 +00:00
|
|
|
|
2023-08-29 09:32:44 +00:00
|
|
|
method getAddress*(signer: Signer): Future[Address] {.base, gcsafe.} =
|
2022-01-24 11:12:52 +00:00
|
|
|
doAssert false, "not implemented"
|
|
|
|
|
2022-01-26 10:21:28 +00:00
|
|
|
method signMessage*(signer: Signer,
|
|
|
|
message: seq[byte]): Future[seq[byte]] {.base, async.} =
|
|
|
|
doAssert false, "not implemented"
|
|
|
|
|
2022-01-25 16:17:43 +00:00
|
|
|
method sendTransaction*(signer: Signer,
|
2022-05-17 02:34:22 +00:00
|
|
|
transaction: Transaction): Future[TransactionResponse] {.base, async.} =
|
2022-01-25 16:17:43 +00:00
|
|
|
doAssert false, "not implemented"
|
|
|
|
|
2023-08-29 09:32:44 +00:00
|
|
|
method getGasPrice*(signer: Signer): Future[UInt256] {.base, gcsafe.} =
|
2022-01-24 11:12:52 +00:00
|
|
|
signer.provider.getGasPrice()
|
2022-01-24 11:14:31 +00:00
|
|
|
|
|
|
|
method getTransactionCount*(signer: Signer,
|
|
|
|
blockTag = BlockTag.latest):
|
|
|
|
Future[UInt256] {.base, async.} =
|
|
|
|
let address = await signer.getAddress()
|
|
|
|
return await signer.provider.getTransactionCount(address, blockTag)
|
2022-01-24 13:40:47 +00:00
|
|
|
|
|
|
|
method estimateGas*(signer: Signer,
|
|
|
|
transaction: Transaction): Future[UInt256] {.base, async.} =
|
|
|
|
var transaction = transaction
|
|
|
|
transaction.sender = some(await signer.getAddress)
|
2023-10-24 23:42:25 +00:00
|
|
|
try:
|
|
|
|
return await signer.provider.estimateGas(transaction)
|
|
|
|
except ProviderError as e:
|
|
|
|
raiseEstimateGasError transaction, e
|
2022-01-24 16:29:25 +00:00
|
|
|
|
2023-08-29 09:32:44 +00:00
|
|
|
method getChainId*(signer: Signer): Future[UInt256] {.base, gcsafe.} =
|
2022-01-24 16:29:25 +00:00
|
|
|
signer.provider.getChainId()
|
2022-01-25 09:25:09 +00:00
|
|
|
|
2023-09-14 23:54:08 +00:00
|
|
|
method getNonce(signer: Signer): Future[UInt256] {.base, gcsafe, async.} =
|
|
|
|
var nonce = await signer.getTransactionCount(BlockTag.pending)
|
2023-10-24 23:42:25 +00:00
|
|
|
|
2023-09-14 23:54:08 +00:00
|
|
|
if lastSeen =? signer.lastSeenNonce and lastSeen >= nonce:
|
|
|
|
nonce = (lastSeen + 1.u256)
|
|
|
|
signer.lastSeenNonce = some nonce
|
2023-10-24 23:42:25 +00:00
|
|
|
|
2023-09-14 23:54:08 +00:00
|
|
|
return nonce
|
|
|
|
|
2023-10-24 23:42:25 +00:00
|
|
|
method updateNonce*(
|
|
|
|
signer: Signer,
|
|
|
|
nonce: UInt256
|
|
|
|
) {.base, gcsafe.} =
|
2023-09-14 23:54:08 +00:00
|
|
|
|
|
|
|
without lastSeen =? signer.lastSeenNonce:
|
|
|
|
signer.lastSeenNonce = some nonce
|
|
|
|
return
|
|
|
|
|
|
|
|
if nonce > lastSeen:
|
|
|
|
signer.lastSeenNonce = some nonce
|
|
|
|
|
2023-10-24 23:42:25 +00:00
|
|
|
method decreaseNonce*(signer: Signer) {.base, gcsafe.} =
|
|
|
|
if lastSeen =? signer.lastSeenNonce and lastSeen > 0:
|
|
|
|
signer.lastSeenNonce = some lastSeen - 1
|
|
|
|
|
2022-01-25 09:25:09 +00:00
|
|
|
method populateTransaction*(signer: Signer,
|
|
|
|
transaction: Transaction):
|
|
|
|
Future[Transaction] {.base, async.} =
|
|
|
|
|
|
|
|
if sender =? transaction.sender and sender != await signer.getAddress():
|
|
|
|
raiseSignerError("from address mismatch")
|
|
|
|
if chainId =? transaction.chainId and chainId != await signer.getChainId():
|
|
|
|
raiseSignerError("chain id mismatch")
|
|
|
|
|
2023-10-24 23:42:25 +00:00
|
|
|
if signer.populateLock.isNil:
|
|
|
|
signer.populateLock = newAsyncLock()
|
|
|
|
|
|
|
|
await signer.populateLock.acquire()
|
|
|
|
|
2022-01-25 09:25:09 +00:00
|
|
|
var populated = transaction
|
|
|
|
|
|
|
|
if transaction.sender.isNone:
|
|
|
|
populated.sender = some(await signer.getAddress())
|
|
|
|
if transaction.chainId.isNone:
|
|
|
|
populated.chainId = some(await signer.getChainId())
|
2023-10-24 23:42:25 +00:00
|
|
|
if transaction.gasPrice.isNone and (populated.maxFee.isNone or populated.maxPriorityFee.isNone):
|
2022-01-25 09:25:09 +00:00
|
|
|
populated.gasPrice = some(await signer.getGasPrice())
|
2023-10-24 23:42:25 +00:00
|
|
|
|
|
|
|
if transaction.nonce.isNone and transaction.gasLimit.isNone:
|
|
|
|
# when both nonce and gasLimit are not populated, we must ensure getNonce is
|
|
|
|
# followed by an estimateGas so we can determine if there was an error. If
|
|
|
|
# there is an error, the nonce must be deprecated to prevent nonce gaps and
|
|
|
|
# stuck transactions
|
|
|
|
try:
|
|
|
|
populated.nonce = some(await signer.getNonce())
|
|
|
|
populated.gasLimit = some(await signer.estimateGas(populated))
|
|
|
|
except ProviderError, EstimateGasError:
|
|
|
|
let e = getCurrentException()
|
|
|
|
signer.decreaseNonce()
|
|
|
|
raise e
|
|
|
|
finally:
|
|
|
|
signer.populateLock.release()
|
|
|
|
|
|
|
|
else:
|
|
|
|
if transaction.nonce.isNone:
|
|
|
|
populated.nonce = some(await signer.getNonce())
|
|
|
|
if transaction.gasLimit.isNone:
|
|
|
|
populated.gasLimit = some(await signer.estimateGas(populated))
|
2022-01-25 09:25:09 +00:00
|
|
|
|
|
|
|
return populated
|
2023-10-24 23:42:25 +00:00
|
|
|
|
|
|
|
method cancelTransaction*(
|
|
|
|
signer: Signer,
|
|
|
|
tx: Transaction
|
|
|
|
): Future[TransactionResponse] {.async, base.} =
|
|
|
|
# cancels a transaction by sending with a 0-valued transaction to ourselves
|
|
|
|
# with the failed tx's nonce
|
|
|
|
|
|
|
|
without sender =? tx.sender:
|
|
|
|
raiseSignerError "transaction must have sender"
|
|
|
|
without nonce =? tx.nonce:
|
|
|
|
raiseSignerError "transaction must have nonce"
|
|
|
|
|
|
|
|
var cancelTx = Transaction(to: sender, value: 0.u256, nonce: some nonce)
|
|
|
|
cancelTx = await signer.populateTransaction(cancelTx)
|
|
|
|
return await signer.sendTransaction(cancelTx)
|