mirror of
https://github.com/logos-storage/nim-ethers.git
synced 2026-01-04 06:33:07 +00:00
Raise EthersError when JSON-RPC fails
This commit is contained in:
parent
1a6cff211d
commit
95c5282b8a
@ -2,6 +2,7 @@ import std/json
|
|||||||
import std/tables
|
import std/tables
|
||||||
import std/uri
|
import std/uri
|
||||||
import pkg/json_rpc/rpcclient
|
import pkg/json_rpc/rpcclient
|
||||||
|
import pkg/json_rpc/errors
|
||||||
import ../basics
|
import ../basics
|
||||||
import ../provider
|
import ../provider
|
||||||
import ../signer
|
import ../signer
|
||||||
@ -30,6 +31,12 @@ type
|
|||||||
template raiseProviderError(message: string) =
|
template raiseProviderError(message: string) =
|
||||||
raise newException(JsonRpcProviderError, message)
|
raise newException(JsonRpcProviderError, message)
|
||||||
|
|
||||||
|
template convertError(body) =
|
||||||
|
try:
|
||||||
|
body
|
||||||
|
except JsonRpcError as error:
|
||||||
|
raiseProviderError(error.msg)
|
||||||
|
|
||||||
# Provider
|
# Provider
|
||||||
|
|
||||||
const defaultUrl = "http://localhost:8545"
|
const defaultUrl = "http://localhost:8545"
|
||||||
@ -77,12 +84,14 @@ proc new*(_: type JsonRpcProvider, url=defaultUrl): JsonRpcProvider =
|
|||||||
proc send*(provider: JsonRpcProvider,
|
proc send*(provider: JsonRpcProvider,
|
||||||
call: string,
|
call: string,
|
||||||
arguments: seq[JsonNode] = @[]): Future[JsonNode] {.async.} =
|
arguments: seq[JsonNode] = @[]): Future[JsonNode] {.async.} =
|
||||||
let client = await provider.client
|
convertError:
|
||||||
return await client.call(call, %arguments)
|
let client = await provider.client
|
||||||
|
return await client.call(call, %arguments)
|
||||||
|
|
||||||
proc listAccounts*(provider: JsonRpcProvider): Future[seq[Address]] {.async.} =
|
proc listAccounts*(provider: JsonRpcProvider): Future[seq[Address]] {.async.} =
|
||||||
let client = await provider.client
|
convertError:
|
||||||
return await client.eth_accounts()
|
let client = await provider.client
|
||||||
|
return await client.eth_accounts()
|
||||||
|
|
||||||
proc getSigner*(provider: JsonRpcProvider): JsonRpcSigner =
|
proc getSigner*(provider: JsonRpcProvider): JsonRpcSigner =
|
||||||
JsonRpcSigner(provider: provider)
|
JsonRpcSigner(provider: provider)
|
||||||
@ -91,60 +100,69 @@ proc getSigner*(provider: JsonRpcProvider, address: Address): JsonRpcSigner =
|
|||||||
JsonRpcSigner(provider: provider, address: some address)
|
JsonRpcSigner(provider: provider, address: some address)
|
||||||
|
|
||||||
method getBlockNumber*(provider: JsonRpcProvider): Future[UInt256] {.async.} =
|
method getBlockNumber*(provider: JsonRpcProvider): Future[UInt256] {.async.} =
|
||||||
let client = await provider.client
|
convertError:
|
||||||
return await client.eth_blockNumber()
|
let client = await provider.client
|
||||||
|
return await client.eth_blockNumber()
|
||||||
|
|
||||||
method getBlock*(provider: JsonRpcProvider,
|
method getBlock*(provider: JsonRpcProvider,
|
||||||
tag: BlockTag): Future[?Block] {.async.} =
|
tag: BlockTag): Future[?Block] {.async.} =
|
||||||
let client = await provider.client
|
convertError:
|
||||||
return await client.eth_getBlockByNumber(tag, false)
|
let client = await provider.client
|
||||||
|
return await client.eth_getBlockByNumber(tag, false)
|
||||||
|
|
||||||
method call*(provider: JsonRpcProvider,
|
method call*(provider: JsonRpcProvider,
|
||||||
tx: Transaction,
|
tx: Transaction,
|
||||||
blockTag = BlockTag.latest): Future[seq[byte]] {.async.} =
|
blockTag = BlockTag.latest): Future[seq[byte]] {.async.} =
|
||||||
let client = await provider.client
|
convertError:
|
||||||
return await client.eth_call(tx, blockTag)
|
let client = await provider.client
|
||||||
|
return await client.eth_call(tx, blockTag)
|
||||||
|
|
||||||
method getGasPrice*(provider: JsonRpcProvider): Future[UInt256] {.async.} =
|
method getGasPrice*(provider: JsonRpcProvider): Future[UInt256] {.async.} =
|
||||||
let client = await provider.client
|
convertError:
|
||||||
return await client.eth_gasprice()
|
let client = await provider.client
|
||||||
|
return await client.eth_gasprice()
|
||||||
|
|
||||||
method getTransactionCount*(provider: JsonRpcProvider,
|
method getTransactionCount*(provider: JsonRpcProvider,
|
||||||
address: Address,
|
address: Address,
|
||||||
blockTag = BlockTag.latest):
|
blockTag = BlockTag.latest):
|
||||||
Future[UInt256] {.async.} =
|
Future[UInt256] {.async.} =
|
||||||
let client = await provider.client
|
convertError:
|
||||||
return await client.eth_getTransactionCount(address, blockTag)
|
let client = await provider.client
|
||||||
|
return await client.eth_getTransactionCount(address, blockTag)
|
||||||
|
|
||||||
method getTransactionReceipt*(provider: JsonRpcProvider,
|
method getTransactionReceipt*(provider: JsonRpcProvider,
|
||||||
txHash: TransactionHash):
|
txHash: TransactionHash):
|
||||||
Future[?TransactionReceipt] {.async.} =
|
Future[?TransactionReceipt] {.async.} =
|
||||||
let client = await provider.client
|
convertError:
|
||||||
return await client.eth_getTransactionReceipt(txHash)
|
let client = await provider.client
|
||||||
|
return await client.eth_getTransactionReceipt(txHash)
|
||||||
|
|
||||||
method estimateGas*(provider: JsonRpcProvider,
|
method estimateGas*(provider: JsonRpcProvider,
|
||||||
transaction: Transaction): Future[UInt256] {.async.} =
|
transaction: Transaction): Future[UInt256] {.async.} =
|
||||||
let client = await provider.client
|
convertError:
|
||||||
return await client.eth_estimateGas(transaction)
|
let client = await provider.client
|
||||||
|
return await client.eth_estimateGas(transaction)
|
||||||
|
|
||||||
method getChainId*(provider: JsonRpcProvider): Future[UInt256] {.async.} =
|
method getChainId*(provider: JsonRpcProvider): Future[UInt256] {.async.} =
|
||||||
let client = await provider.client
|
convertError:
|
||||||
try:
|
let client = await provider.client
|
||||||
return await client.eth_chainId()
|
try:
|
||||||
except CatchableError:
|
return await client.eth_chainId()
|
||||||
return parse(await client.net_version(), UInt256)
|
except CatchableError:
|
||||||
|
return parse(await client.net_version(), UInt256)
|
||||||
|
|
||||||
proc subscribe(provider: JsonRpcProvider,
|
proc subscribe(provider: JsonRpcProvider,
|
||||||
name: string,
|
name: string,
|
||||||
filter: ?Filter,
|
filter: ?Filter,
|
||||||
handler: SubscriptionHandler): Future[Subscription] {.async.} =
|
handler: SubscriptionHandler): Future[Subscription] {.async.} =
|
||||||
let client = await provider.client
|
convertError:
|
||||||
doAssert client of RpcWebSocketClient, "subscriptions require websockets"
|
let client = await provider.client
|
||||||
|
doAssert client of RpcWebSocketClient, "subscriptions require websockets"
|
||||||
|
|
||||||
let id = await client.eth_subscribe(name, filter)
|
let id = await client.eth_subscribe(name, filter)
|
||||||
provider.subscriptions[id] = handler
|
provider.subscriptions[id] = handler
|
||||||
|
|
||||||
return JsonRpcSubscription(id: id, provider: provider)
|
return JsonRpcSubscription(id: id, provider: provider)
|
||||||
|
|
||||||
method subscribe*(provider: JsonRpcProvider,
|
method subscribe*(provider: JsonRpcProvider,
|
||||||
filter: Filter,
|
filter: Filter,
|
||||||
@ -164,10 +182,11 @@ method subscribe*(provider: JsonRpcProvider,
|
|||||||
return await provider.subscribe("newHeads", Filter.none, handler)
|
return await provider.subscribe("newHeads", Filter.none, handler)
|
||||||
|
|
||||||
method unsubscribe*(subscription: JsonRpcSubscription) {.async.} =
|
method unsubscribe*(subscription: JsonRpcSubscription) {.async.} =
|
||||||
let provider = subscription.provider
|
convertError:
|
||||||
provider.subscriptions.del(subscription.id)
|
let provider = subscription.provider
|
||||||
let client = await provider.client
|
provider.subscriptions.del(subscription.id)
|
||||||
discard await client.eth_unsubscribe(subscription.id)
|
let client = await provider.client
|
||||||
|
discard await client.eth_unsubscribe(subscription.id)
|
||||||
|
|
||||||
# Signer
|
# Signer
|
||||||
|
|
||||||
@ -186,14 +205,16 @@ method getAddress*(signer: JsonRpcSigner): Future[Address] {.async.} =
|
|||||||
|
|
||||||
method signMessage*(signer: JsonRpcSigner,
|
method signMessage*(signer: JsonRpcSigner,
|
||||||
message: seq[byte]): Future[seq[byte]] {.async.} =
|
message: seq[byte]): Future[seq[byte]] {.async.} =
|
||||||
let client = await signer.provider.client
|
convertError:
|
||||||
let address = await signer.getAddress()
|
let client = await signer.provider.client
|
||||||
return await client.eth_sign(address, message)
|
let address = await signer.getAddress()
|
||||||
|
return await client.eth_sign(address, message)
|
||||||
|
|
||||||
method sendTransaction*(signer: JsonRpcSigner,
|
method sendTransaction*(signer: JsonRpcSigner,
|
||||||
transaction: Transaction): Future[TransactionResponse] {.async.} =
|
transaction: Transaction): Future[TransactionResponse] {.async.} =
|
||||||
let
|
convertError:
|
||||||
client = await signer.provider.client
|
let
|
||||||
hash = await client.eth_sendTransaction(transaction)
|
client = await signer.provider.client
|
||||||
|
hash = await client.eth_sendTransaction(transaction)
|
||||||
|
|
||||||
return TransactionResponse(hash: hash, provider: signer.provider)
|
return TransactionResponse(hash: hash, provider: signer.provider)
|
||||||
|
|||||||
@ -241,3 +241,17 @@ suite "JsonRpcProvider":
|
|||||||
wantedConfirms = int.high
|
wantedConfirms = int.high
|
||||||
check receipt.hasBeenMined(currentBlock, wantedConfirms)
|
check receipt.hasBeenMined(currentBlock, wantedConfirms)
|
||||||
|
|
||||||
|
test "raises JsonRpcProviderError when something goes wrong":
|
||||||
|
let provider = JsonRpcProvider.new("http://invalid.")
|
||||||
|
expect JsonRpcProviderError:
|
||||||
|
discard await provider.listAccounts()
|
||||||
|
expect JsonRpcProviderError:
|
||||||
|
discard await provider.send("evm_mine")
|
||||||
|
expect JsonRpcProviderError:
|
||||||
|
discard await provider.getBlockNumber()
|
||||||
|
expect JsonRpcProviderError:
|
||||||
|
discard await provider.getBlock(BlockTag.latest)
|
||||||
|
expect JsonRpcProviderError:
|
||||||
|
discard await provider.subscribe(proc(_: Block) {.async.} = discard)
|
||||||
|
expect JsonRpcProviderError:
|
||||||
|
discard await provider.getSigner().sendTransaction(Transaction.example)
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user