nim-ethers/ethers/providers/jsonrpc.nim

109 lines
3.3 KiB
Nim
Raw Normal View History

2022-01-18 11:10:20 +00:00
import std/uri
import pkg/json_rpc/rpcclient
import ../basics
import ../provider
2022-01-20 13:39:37 +00:00
import ../signer
2022-01-18 11:10:20 +00:00
import ./rpccalls
export basics
export provider
push: {.upraises: [].}
2022-01-20 13:39:37 +00:00
type
JsonRpcProvider* = ref object of Provider
client: Future[RpcClient]
JsonRpcSigner* = ref object of Signer
provider: JsonRpcProvider
address: ?Address
2022-01-25 09:25:09 +00:00
JsonRpcProviderError* = object of EthersError
2022-01-20 13:39:37 +00:00
template raiseProviderError(message: string) =
raise newException(JsonRpcProviderError, message)
# 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 new*(_: type JsonRpcProvider, url=defaultUrl): JsonRpcProvider =
JsonRpcProvider(client: RpcClient.connect(url))
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-01-18 13:24:46 +00:00
let client = await provider.client
2022-01-25 14:05:54 +00:00
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.} =
let client = await provider.client
2022-01-18 11:42:58 +00:00
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.} =
let client = await provider.client
return await client.eth_blockNumber()
2022-01-20 11:56:18 +00:00
method call*(provider: JsonRpcProvider,
tx: Transaction): Future[seq[byte]] {.async.} =
let client = await provider.client
return await client.eth_call(tx)
2022-01-20 13:39:37 +00:00
2022-01-24 11:12:52 +00:00
method getGasPrice*(provider: JsonRpcProvider): Future[UInt256] {.async.} =
let client = await provider.client
return await client.eth_gasprice()
2022-01-24 11:14:31 +00:00
method getTransactionCount*(provider: JsonRpcProvider,
address: Address,
blockTag = BlockTag.latest):
Future[UInt256] {.async.} =
let client = await provider.client
return await client.eth_getTransactionCount(address, blockTag)
2022-01-24 13:40:47 +00:00
method estimateGas*(provider: JsonRpcProvider,
transaction: Transaction): Future[UInt256] {.async.} =
let client = await provider.client
return await client.eth_estimateGas(transaction)
2022-01-24 16:29:25 +00:00
method getChainId*(provider: JsonRpcProvider): Future[UInt256] {.async.} =
let client = await provider.client
try:
return await client.eth_chainId()
except CatchableError:
return parse(await client.net_version(), UInt256)
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
method sendTransaction*(signer: JsonRpcSigner,
transaction: Transaction) {.async.} =
let client = await signer.provider.client
discard await client.eth_sendTransaction(transaction)