Introduces JsonRpcSigner

This commit is contained in:
Mark Spanbroek 2022-01-20 14:39:37 +01:00
parent 2140084d97
commit 94dcc171af
4 changed files with 58 additions and 2 deletions

View File

@ -2,6 +2,7 @@ import std/uri
import pkg/json_rpc/rpcclient import pkg/json_rpc/rpcclient
import ../basics import ../basics
import ../provider import ../provider
import ../signer
import ./rpccalls import ./rpccalls
export basics export basics
@ -9,8 +10,18 @@ export provider
push: {.upraises: [].} push: {.upraises: [].}
type JsonRpcProvider* = ref object of Provider type
client: Future[RpcClient] JsonRpcProvider* = ref object of Provider
client: Future[RpcClient]
JsonRpcSigner* = ref object of Signer
provider: JsonRpcProvider
address: ?Address
JsonRpcProviderError* = object of IOError
template raiseProviderError(message: string) =
raise newException(JsonRpcProviderError, message)
# Provider
const defaultUrl = "http://localhost:8545" const defaultUrl = "http://localhost:8545"
@ -38,6 +49,12 @@ proc listAccounts*(provider: JsonRpcProvider): Future[seq[Address]] {.async.} =
let client = await provider.client let client = await provider.client
return await client.eth_accounts() return await client.eth_accounts()
proc getSigner*(provider: JsonRpcProvider): JsonRpcSigner =
JsonRpcSigner(provider: provider)
proc getSigner*(provider: JsonRpcProvider, address: Address): JsonRpcSigner =
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 let client = await provider.client
return await client.eth_blockNumber() return await client.eth_blockNumber()
@ -46,3 +63,15 @@ method call*(provider: JsonRpcProvider,
tx: Transaction): Future[seq[byte]] {.async.} = tx: Transaction): Future[seq[byte]] {.async.} =
let client = await provider.client let client = await provider.client
return await client.eth_call(tx) return await client.eth_call(tx)
# Signer
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"

8
ethers/signer.nim Normal file
View File

@ -0,0 +1,8 @@
import ./basics
export basics
type Signer* = ref object of RootObj
method getAddress*(signer: Signer): Future[Address] {.base, async.} =
doAssert false, "not implemented"

View File

@ -1,4 +1,5 @@
import ./testJsonRpcProvider import ./testJsonRpcProvider
import ./testJsonRpcSigner
import ./testContracts import ./testContracts
{.warning[UnusedImport]:off.} {.warning[UnusedImport]:off.}

View File

@ -0,0 +1,18 @@
import pkg/asynctest
import pkg/ethers
suite "JsonRpcSigner":
var provider: JsonRpcProvider
setup:
provider = JsonRpcProvider.new()
test "is connected to the first account of the provider by default":
let signer = provider.getSigner()
check (await signer.getAddress()) == (await provider.listAccounts())[0]
test "can connect to a different account":
let account = (await provider.listAccounts())[1]
let signer = provider.getSigner(account)
check (await signer.getAddress()) == account