nim-ethers/ethers/signer.nim

67 lines
2.4 KiB
Nim
Raw Normal View History

2022-01-20 14:39:37 +01:00
import ./basics
2022-01-24 12:12:52 +01:00
import ./provider
2022-01-20 14:39:37 +01:00
export basics
type Signer* = ref object of RootObj
2022-01-25 10:25:09 +01:00
type SignerError* = object of EthersError
template raiseSignerError(message: string) =
raise newException(SignerError, message)
2022-01-20 14:39:37 +01:00
2023-08-29 11:32:44 +02:00
method provider*(signer: Signer): Provider {.base, gcsafe.} =
2022-01-20 14:39:37 +01:00
doAssert false, "not implemented"
2022-01-24 12:12:52 +01:00
2023-08-29 11:32:44 +02:00
method getAddress*(signer: Signer): Future[Address] {.base, gcsafe.} =
2022-01-24 12:12:52 +01:00
doAssert false, "not implemented"
2022-01-26 11:21:28 +01:00
method signMessage*(signer: Signer,
message: seq[byte]): Future[seq[byte]] {.base, async.} =
doAssert false, "not implemented"
2022-01-25 17:17:43 +01:00
method sendTransaction*(signer: Signer,
transaction: Transaction): Future[TransactionResponse] {.base, async.} =
2022-01-25 17:17:43 +01:00
doAssert false, "not implemented"
2023-08-29 11:32:44 +02:00
method getGasPrice*(signer: Signer): Future[UInt256] {.base, gcsafe.} =
2022-01-24 12:12:52 +01:00
signer.provider.getGasPrice()
2022-01-24 12:14:31 +01: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 14:40:47 +01:00
method estimateGas*(signer: Signer,
transaction: Transaction): Future[UInt256] {.base, async.} =
var transaction = transaction
transaction.sender = some(await signer.getAddress)
return await signer.provider.estimateGas(transaction)
2022-01-24 17:29:25 +01:00
2023-08-29 11:32:44 +02:00
method getChainId*(signer: Signer): Future[UInt256] {.base, gcsafe.} =
2022-01-24 17:29:25 +01:00
signer.provider.getChainId()
2022-01-25 10:25:09 +01: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")
var populated = transaction
if transaction.sender.isNone:
populated.sender = some(await signer.getAddress())
if transaction.nonce.isNone:
populated.nonce = some(await signer.getTransactionCount(BlockTag.pending))
if transaction.chainId.isNone:
populated.chainId = some(await signer.getChainId())
if transaction.gasPrice.isNone and (transaction.maxFee.isNone or transaction.maxPriorityFee.isNone):
2022-01-25 10:25:09 +01:00
populated.gasPrice = some(await signer.getGasPrice())
if transaction.gasLimit.isNone:
populated.gasLimit = some(await signer.estimateGas(populated))
return populated