mirror of
https://github.com/codex-storage/nim-ethers.git
synced 2025-01-10 11:26:05 +00:00
43500c63d7
* Add json de/serialization lib from codex to handle conversions json-rpc now requires nim-json-serialization to convert types to/from json. Use the nim-json-serialization signatures to call the json serialization lib from nim-codex (should be moved to its own lib) * Add ethers implementation for setMethodHandler Was removed in json-rpc * More json conversion updates * Fix json_rpc.call returning JsonString instead of JsonNode * Update exceptions Use {.async: (raises: [...].} where needed Annotate provider with {.push raises:[].} Format signatures * Start fixing tests (mainly conversion fixes) * rename sender to `from`, update json error logging, add more conversions * Refactor exceptions for providers and signers, fix more tests - signer procs raise SignerError, provider procs raise ProviderError - WalletError now inherits from SignerError - move wallet module under signers - create jsonrpo moudle under signers - bump nim-json-rpc for null-handling fixes - All jsonrpc provider tests passing, still need to fix others * remove raises from async annotation for dynamic dispatch - removes async: raises from getAddress and signTransaction because derived JsonRpcSigner methods were not being used when dynamically dispatched. Once `raises` was removed from the async annotation, the dynamic dispatch worked again. This is only the case for getAddress and signTransaction. - add gcsafe annotation to wallet.provider so that it matches the base method * Catch EstimateGasError before ProviderError EstimateGasError is now a ProviderError (it is a SignerError, and SignerError is a ProviderError), so EstimateGasErrors were not being caught * clean up - all tests passing * support nim 2.0 * lock in chronos version * Add serde options to the json util, along with tests next step is to: 1. change back any ethers var names that were changed for serialization purposes, eg `from` and `type` 2. move the json util to its own lib * bump json-rpc to 0.4.0 and fix test * fix: specify raises for getAddress and sendTransaction Fixes issue where getAddress and sendTransaction could not be found for MockSigner in tests. The problem was that the async: raises update had not been applied to the MockSigner. * handle exceptions during jsonrpc init There are too many exceptions to catch individually, including chronos raising CatchableError exceptions in await expansion. There are also many other errors captured inside of the new proc with CatchableError. Instead of making it more complicated and harder to read, I think sticking with excepting CatchableError inside of convertError is a sensible solution * cleanup * deserialize key defaults to serialize key * Add more tests for OptIn/OptOut/Strict modes, fix logic * use nim-serde instead of json util Allows aliasing of de/serialized fields, so revert changes of sender to `from` and transactionType to `type` * Move hash* shim to its own module * address PR feedback - add comments to hashes shim - remove .catch from callback condition - derive SignerError from EthersError instead of ProviderError. This allows Providers and Signers to be separate, as Ledger does it, to isolate functionality. Some signer functions now raise both ProviderError and SignerError - Update reverts to check for SignerError - Update ERC-20 method comment * rename subscriptions.init > subscriptions.start
199 lines
5.6 KiB
Nim
199 lines
5.6 KiB
Nim
import pkg/questionable
|
|
import ./basics
|
|
import ./provider
|
|
|
|
export basics
|
|
|
|
{.push raises: [].}
|
|
|
|
type
|
|
Signer* = ref object of RootObj
|
|
lastSeenNonce: ?UInt256
|
|
populateLock: AsyncLock
|
|
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
|
|
) {.raises: [EstimateGasError] .} =
|
|
let e = (ref EstimateGasError)(
|
|
msg: "Estimate gas failed",
|
|
transaction: transaction,
|
|
parent: parent)
|
|
raise e
|
|
|
|
template convertError(body) =
|
|
try:
|
|
body
|
|
except EthersError as error:
|
|
raiseSignerError(error.msg)
|
|
except CatchableError as error:
|
|
raiseSignerError(error.msg)
|
|
|
|
method provider*(
|
|
signer: Signer): Provider {.base, gcsafe, raises: [SignerError].} =
|
|
doAssert false, "not implemented"
|
|
|
|
method getAddress*(
|
|
signer: Signer): Future[Address]
|
|
{.base, async: (raises:[ProviderError, SignerError]).} =
|
|
|
|
doAssert false, "not implemented"
|
|
|
|
method signMessage*(
|
|
signer: Signer,
|
|
message: seq[byte]): Future[seq[byte]]
|
|
{.base, async: (raises: [SignerError]).} =
|
|
|
|
doAssert false, "not implemented"
|
|
|
|
method sendTransaction*(
|
|
signer: Signer,
|
|
transaction: Transaction): Future[TransactionResponse]
|
|
{.base, async: (raises:[SignerError]).} =
|
|
|
|
doAssert false, "not implemented"
|
|
|
|
method getGasPrice*(
|
|
signer: Signer): Future[UInt256]
|
|
{.base, async: (raises: [ProviderError, SignerError]).} =
|
|
|
|
return await signer.provider.getGasPrice()
|
|
|
|
method getTransactionCount*(
|
|
signer: Signer,
|
|
blockTag = BlockTag.latest): Future[UInt256]
|
|
{.base, async: (raises:[SignerError]).} =
|
|
|
|
convertError:
|
|
let address = await signer.getAddress()
|
|
return await signer.provider.getTransactionCount(address, blockTag)
|
|
|
|
method estimateGas*(
|
|
signer: Signer,
|
|
transaction: Transaction,
|
|
blockTag = BlockTag.latest): Future[UInt256]
|
|
{.base, async: (raises:[SignerError]).} =
|
|
|
|
var transaction = transaction
|
|
var address: Address
|
|
|
|
convertError:
|
|
address = await signer.getAddress
|
|
|
|
transaction.sender = some(address)
|
|
try:
|
|
return await signer.provider.estimateGas(transaction)
|
|
except ProviderError as e:
|
|
raiseEstimateGasError transaction, e
|
|
|
|
method getChainId*(
|
|
signer: Signer): Future[UInt256]
|
|
{.base, async: (raises: [ProviderError, SignerError]).} =
|
|
|
|
return await signer.provider.getChainId()
|
|
|
|
method getNonce(
|
|
signer: Signer): Future[UInt256] {.base, async: (raises: [SignerError]).} =
|
|
|
|
var nonce = await signer.getTransactionCount(BlockTag.pending)
|
|
|
|
if lastSeen =? signer.lastSeenNonce and lastSeen >= nonce:
|
|
nonce = (lastSeen + 1.u256)
|
|
signer.lastSeenNonce = some nonce
|
|
|
|
return nonce
|
|
|
|
method updateNonce*(
|
|
signer: Signer,
|
|
nonce: UInt256
|
|
) {.base, gcsafe.} =
|
|
|
|
without lastSeen =? signer.lastSeenNonce:
|
|
signer.lastSeenNonce = some nonce
|
|
return
|
|
|
|
if nonce > lastSeen:
|
|
signer.lastSeenNonce = some nonce
|
|
|
|
method decreaseNonce*(signer: Signer) {.base, gcsafe.} =
|
|
if lastSeen =? signer.lastSeenNonce and lastSeen > 0:
|
|
signer.lastSeenNonce = some lastSeen - 1
|
|
|
|
method populateTransaction*(
|
|
signer: Signer,
|
|
transaction: Transaction): Future[Transaction]
|
|
{.base, async: (raises: [CancelledError, AsyncLockError, ProviderError, SignerError]).} =
|
|
|
|
var address: Address
|
|
convertError:
|
|
address = await signer.getAddress()
|
|
|
|
if sender =? transaction.sender and sender != address:
|
|
raiseSignerError("from address mismatch")
|
|
if chainId =? transaction.chainId and chainId != await signer.getChainId():
|
|
raiseSignerError("chain id mismatch")
|
|
|
|
if signer.populateLock.isNil:
|
|
signer.populateLock = newAsyncLock()
|
|
|
|
await signer.populateLock.acquire()
|
|
|
|
var populated = transaction
|
|
|
|
try:
|
|
if transaction.sender.isNone:
|
|
populated.sender = some(address)
|
|
if transaction.chainId.isNone:
|
|
populated.chainId = some(await signer.getChainId())
|
|
if transaction.gasPrice.isNone and (transaction.maxFee.isNone or transaction.maxPriorityFee.isNone):
|
|
populated.gasPrice = some(await signer.getGasPrice())
|
|
|
|
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 decreased to prevent nonce gaps and
|
|
# stuck transactions
|
|
populated.nonce = some(await signer.getNonce())
|
|
try:
|
|
populated.gasLimit = some(await signer.estimateGas(populated))
|
|
except EstimateGasError as e:
|
|
signer.decreaseNonce()
|
|
raise e
|
|
except ProviderError as e:
|
|
signer.decreaseNonce()
|
|
raiseSignerError(e.msg)
|
|
|
|
else:
|
|
if transaction.nonce.isNone:
|
|
populated.nonce = some(await signer.getNonce())
|
|
if transaction.gasLimit.isNone:
|
|
populated.gasLimit = some(await signer.estimateGas(populated))
|
|
|
|
finally:
|
|
signer.populateLock.release()
|
|
|
|
return populated
|
|
|
|
method cancelTransaction*(
|
|
signer: Signer,
|
|
tx: Transaction
|
|
): Future[TransactionResponse] {.base, async: (raises: [SignerError]).} =
|
|
# 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)
|
|
convertError:
|
|
cancelTx = await signer.populateTransaction(cancelTx)
|
|
return await signer.sendTransaction(cancelTx)
|