nim-ethers/ethers/providers/jsonrpc.nim

234 lines
7.4 KiB
Nim
Raw Normal View History

import std/json
import std/tables
2022-01-18 11:10:20 +00:00
import std/uri
import pkg/json_rpc/rpcclient
2022-06-29 13:13:25 +00:00
import pkg/json_rpc/errors
2022-01-18 11:10:20 +00:00
import ../basics
import ../provider
2022-01-20 13:39:37 +00:00
import ../signer
import ./jsonrpc/rpccalls
import ./jsonrpc/conversions
2022-01-18 11:10:20 +00:00
export basics
export provider
export conversions
2022-01-18 11:10:20 +00:00
push: {.upraises: [].}
2022-01-20 13:39:37 +00:00
type
JsonRpcProvider* = ref object of Provider
client: Future[RpcClient]
subscriptions: Table[JsonNode, SubscriptionHandler]
JsonRpcSubscription = ref object of Subscription
provider: JsonRpcProvider
id: JsonNode
2022-01-20 13:39:37 +00:00
JsonRpcSigner* = ref object of Signer
provider: JsonRpcProvider
address: ?Address
2022-01-25 09:25:09 +00:00
JsonRpcProviderError* = object of EthersError
SubscriptionHandler = proc(id, arguments: JsonNode): Future[void] {.gcsafe, upraises:[].}
2022-01-20 13:39:37 +00:00
template raiseProviderError(message: string) =
raise newException(JsonRpcProviderError, message)
2022-06-29 13:13:25 +00:00
template convertError(body) =
try:
body
except JsonRpcError as error:
raiseProviderError(error.msg)
2022-01-20 13:39:37 +00:00
# Provider
2022-01-18 11:10:20 +00:00
const defaultUrl = "http://localhost:8545"
proc connect(_: type RpcClient, url: string): Future[RpcClient] {.async.} =
case parseUri(url).scheme
of "ws", "wss":
let client = newRpcWebSocketClient()
await client.connect(url)
return client
else:
let client = newRpcHttpClient()
await client.connect(url)
return client
proc connect(provider: JsonRpcProvider, url: string) =
proc getSubscriptionHandler(id: JsonNode): ?SubscriptionHandler =
try:
if provider.subscriptions.hasKey(id):
provider.subscriptions[id].some
else:
SubscriptionHandler.none
except Exception:
SubscriptionHandler.none
proc handleSubscription(arguments: JsonNode) {.upraises: [].} =
if id =? arguments["subscription"].catch and
handler =? getSubscriptionHandler(id):
# fire and forget
discard handler(id, arguments)
proc subscribe: Future[RpcClient] {.async.} =
let client = await RpcClient.connect(url)
client.setMethodHandler("eth_subscription", handleSubscription)
return client
provider.client = subscribe()
2022-01-18 11:10:20 +00:00
proc new*(_: type JsonRpcProvider, url=defaultUrl): JsonRpcProvider =
let provider = JsonRpcProvider()
provider.connect(url)
provider
2022-01-18 11:10:20 +00:00
2022-01-18 13:24:46 +00:00
proc send*(provider: JsonRpcProvider,
call: string,
2022-01-25 14:05:54 +00:00
arguments: seq[JsonNode] = @[]): Future[JsonNode] {.async.} =
2022-06-29 13:13:25 +00:00
convertError:
let client = await provider.client
return await client.call(call, %arguments)
2022-01-18 13:24:46 +00:00
2022-01-18 11:10:20 +00:00
proc listAccounts*(provider: JsonRpcProvider): Future[seq[Address]] {.async.} =
2022-06-29 13:13:25 +00:00
convertError:
let client = await provider.client
return await client.eth_accounts()
2022-01-18 13:26:41 +00:00
2022-01-20 13:39:37 +00:00
proc getSigner*(provider: JsonRpcProvider): JsonRpcSigner =
JsonRpcSigner(provider: provider)
proc getSigner*(provider: JsonRpcProvider, address: Address): JsonRpcSigner =
JsonRpcSigner(provider: provider, address: some address)
2022-01-18 13:26:41 +00:00
method getBlockNumber*(provider: JsonRpcProvider): Future[UInt256] {.async.} =
2022-06-29 13:13:25 +00:00
convertError:
let client = await provider.client
return await client.eth_blockNumber()
2022-01-20 11:56:18 +00:00
method getBlock*(provider: JsonRpcProvider,
tag: BlockTag): Future[?Block] {.async.} =
2022-06-29 13:13:25 +00:00
convertError:
let client = await provider.client
return await client.eth_getBlockByNumber(tag, false)
2022-01-20 11:56:18 +00:00
method call*(provider: JsonRpcProvider,
tx: Transaction,
blockTag = BlockTag.latest): Future[seq[byte]] {.async.} =
2022-06-29 13:13:25 +00:00
convertError:
let client = await provider.client
return await client.eth_call(tx, blockTag)
2022-01-20 13:39:37 +00:00
2022-01-24 11:12:52 +00:00
method getGasPrice*(provider: JsonRpcProvider): Future[UInt256] {.async.} =
2022-06-29 13:13:25 +00:00
convertError:
let client = await provider.client
return await client.eth_gasprice()
2022-01-24 11:12:52 +00:00
2022-01-24 11:14:31 +00:00
method getTransactionCount*(provider: JsonRpcProvider,
address: Address,
blockTag = BlockTag.latest):
Future[UInt256] {.async.} =
2022-06-29 13:13:25 +00:00
convertError:
let client = await provider.client
return await client.eth_getTransactionCount(address, blockTag)
method getTransactionReceipt*(provider: JsonRpcProvider,
txHash: TransactionHash):
Future[?TransactionReceipt] {.async.} =
2022-06-29 13:13:25 +00:00
convertError:
let client = await provider.client
return await client.eth_getTransactionReceipt(txHash)
2022-01-24 11:14:31 +00:00
2022-01-24 13:40:47 +00:00
method estimateGas*(provider: JsonRpcProvider,
transaction: Transaction): Future[UInt256] {.async.} =
2022-06-29 13:13:25 +00:00
convertError:
let client = await provider.client
return await client.eth_estimateGas(transaction)
2022-01-24 13:40:47 +00:00
2022-01-24 16:29:25 +00:00
method getChainId*(provider: JsonRpcProvider): Future[UInt256] {.async.} =
2022-06-29 13:13:25 +00:00
convertError:
let client = await provider.client
try:
return await client.eth_chainId()
except CatchableError:
return parse(await client.net_version(), UInt256)
2022-01-24 16:29:25 +00:00
method sendTransaction*(provider: JsonRpcProvider, rawTransaction: seq[byte]): Future[TransactionResponse] {.async.} =
2022-07-12 18:24:52 +00:00
convertError:
let
client = await provider.client
hash = await client.eth_sendRawTransaction(rawTransaction)
2022-07-14 08:22:54 +00:00
2022-07-12 18:24:52 +00:00
return TransactionResponse(hash: hash, provider: provider)
2022-05-16 12:51:39 +00:00
proc subscribe(provider: JsonRpcProvider,
name: string,
filter: ?Filter,
handler: SubscriptionHandler): Future[Subscription] {.async.} =
2022-06-29 13:13:25 +00:00
convertError:
let client = await provider.client
doAssert client of RpcWebSocketClient, "subscriptions require websockets"
2022-05-16 12:51:39 +00:00
2022-07-21 09:36:54 +00:00
var id: JsonNode
if filter =? filter:
id = await client.eth_subscribe(name, filter)
else:
id = await client.eth_subscribe(name)
2022-07-20 23:19:33 +00:00
provider.subscriptions[id] = handler
return JsonRpcSubscription(id: id, provider: provider)
method subscribe*(provider: JsonRpcProvider,
filter: Filter,
callback: LogHandler):
Future[Subscription] {.async.} =
proc handler(id, arguments: JsonNode) {.async.} =
if log =? Log.fromJson(arguments["result"]).catch:
callback(log)
2022-05-16 12:51:39 +00:00
return await provider.subscribe("logs", filter.some, handler)
2022-05-16 12:51:39 +00:00
method subscribe*(provider: JsonRpcProvider,
callback: BlockHandler):
Future[Subscription] {.async.} =
proc handler(id, arguments: JsonNode) {.async.} =
2022-05-16 12:51:39 +00:00
if blck =? Block.fromJson(arguments["result"]).catch:
await callback(blck)
2022-07-21 09:36:54 +00:00
return await provider.subscribe("newHeads", Filter.none, handler)
method unsubscribe*(subscription: JsonRpcSubscription) {.async.} =
2022-06-29 13:13:25 +00:00
convertError:
let provider = subscription.provider
provider.subscriptions.del(subscription.id)
let client = await provider.client
discard await client.eth_unsubscribe(subscription.id)
2022-01-20 13:39:37 +00:00
# Signer
2022-01-24 11:12:52 +00:00
method provider*(signer: JsonRpcSigner): Provider =
signer.provider
2022-01-20 13:39:37 +00:00
method getAddress*(signer: JsonRpcSigner): Future[Address] {.async.} =
if address =? signer.address:
return address
let accounts = await signer.provider.listAccounts()
if accounts.len > 0:
return accounts[0]
raiseProviderError "no address found"
2022-01-25 16:17:43 +00:00
2022-01-26 10:21:28 +00:00
method signMessage*(signer: JsonRpcSigner,
message: seq[byte]): Future[seq[byte]] {.async.} =
2022-06-29 13:13:25 +00:00
convertError:
let client = await signer.provider.client
let address = await signer.getAddress()
return await client.eth_sign(address, message)
2022-01-26 10:21:28 +00:00
2022-01-25 16:17:43 +00:00
method sendTransaction*(signer: JsonRpcSigner,
transaction: Transaction): Future[TransactionResponse] {.async.} =
2022-06-29 13:13:25 +00:00
convertError:
let
client = await signer.provider.client
hash = await client.eth_sendTransaction(transaction)
2022-06-29 13:13:25 +00:00
return TransactionResponse(hash: hash, provider: signer.provider)