2023-12-06 12:59:24 +00:00
|
|
|
# nim-web3
|
2024-03-16 23:04:38 +00:00
|
|
|
# Copyright (c) 2019-2024 Status Research & Development GmbH
|
2023-12-06 12:59:24 +00:00
|
|
|
# Licensed under either of
|
|
|
|
# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE))
|
|
|
|
# * MIT license ([LICENSE-MIT](LICENSE-MIT))
|
|
|
|
# at your option.
|
|
|
|
# This file may not be copied, modified, or distributed except according to
|
|
|
|
# those terms.
|
|
|
|
|
2018-12-21 17:27:09 +00:00
|
|
|
import
|
2024-10-01 17:41:11 +00:00
|
|
|
std/[tables, uri, macros],
|
|
|
|
httputils, chronos,
|
2024-06-11 23:32:06 +00:00
|
|
|
results,
|
2024-01-03 14:54:30 +00:00
|
|
|
json_rpc/[rpcclient, jsonmarshal],
|
2024-01-08 06:09:23 +00:00
|
|
|
json_rpc/private/jrpc_sys,
|
2024-10-01 17:41:11 +00:00
|
|
|
eth/common/keys,
|
2023-06-04 05:39:28 +00:00
|
|
|
chronos/apps/http/httpclient,
|
2024-01-03 14:54:30 +00:00
|
|
|
web3/[eth_api_types, conversions, transaction_signing, encoding, contract_dsl],
|
|
|
|
web3/eth_api
|
2018-12-21 17:27:09 +00:00
|
|
|
|
2024-01-03 14:54:30 +00:00
|
|
|
from eth/common/eth_types import ChainId
|
2019-06-12 16:46:29 +00:00
|
|
|
|
2024-01-03 14:54:30 +00:00
|
|
|
export
|
2024-10-01 17:41:11 +00:00
|
|
|
ChainId,
|
2024-01-03 14:54:30 +00:00
|
|
|
eth_api_types,
|
|
|
|
conversions,
|
|
|
|
encoding,
|
|
|
|
contract_dsl,
|
|
|
|
HttpClientFlag,
|
|
|
|
HttpClientFlags,
|
|
|
|
eth_api
|
2018-12-21 17:27:09 +00:00
|
|
|
|
|
|
|
type
|
2019-06-12 16:46:29 +00:00
|
|
|
Web3* = ref object
|
|
|
|
provider*: RpcClient
|
|
|
|
subscriptions*: Table[string, Subscription]
|
2019-08-05 20:08:18 +00:00
|
|
|
defaultAccount*: Address
|
2024-06-11 23:32:06 +00:00
|
|
|
privateKey*: Opt[PrivateKey]
|
|
|
|
lastKnownNonce*: Opt[Quantity]
|
2023-10-05 10:37:04 +00:00
|
|
|
onDisconnect*: proc() {.gcsafe, raises: [].}
|
2019-06-12 16:46:29 +00:00
|
|
|
|
2023-12-13 05:26:04 +00:00
|
|
|
Web3SenderImpl = ref object
|
2019-06-12 16:46:29 +00:00
|
|
|
web3*: Web3
|
2019-08-05 20:08:18 +00:00
|
|
|
contractAddress*: Address
|
2019-01-10 11:50:51 +00:00
|
|
|
|
2024-01-09 13:57:29 +00:00
|
|
|
Web3AsyncSenderImpl = ref object
|
|
|
|
web3*: Web3
|
|
|
|
contractAddress*: Address
|
|
|
|
defaultAccount*: Address
|
|
|
|
value*: UInt256
|
|
|
|
gas*: uint64
|
|
|
|
gasPrice*: int
|
2024-06-11 23:32:06 +00:00
|
|
|
chainId*: Opt[ChainId]
|
2024-10-01 17:41:11 +00:00
|
|
|
blockNumber*: Quantity
|
2024-01-09 13:57:29 +00:00
|
|
|
|
2023-12-13 05:26:04 +00:00
|
|
|
Sender*[T] = ContractInstance[T, Web3SenderImpl]
|
2024-01-09 13:57:29 +00:00
|
|
|
AsyncSender*[T] = ContractInstance[T, Web3AsyncSenderImpl]
|
2019-01-22 15:08:24 +00:00
|
|
|
|
2024-01-08 06:09:23 +00:00
|
|
|
SubscriptionEventHandler* = proc (j: JsonString) {.gcsafe, raises: [].}
|
2023-10-05 10:37:04 +00:00
|
|
|
SubscriptionErrorHandler* = proc (err: CatchableError) {.gcsafe, raises: [].}
|
2020-06-26 15:30:55 +00:00
|
|
|
|
2023-10-05 10:37:04 +00:00
|
|
|
BlockHeaderHandler* = proc (b: BlockHeader) {.gcsafe, raises: [].}
|
2020-06-26 15:30:55 +00:00
|
|
|
|
2019-06-12 16:46:29 +00:00
|
|
|
Subscription* = ref object
|
|
|
|
id*: string
|
|
|
|
web3*: Web3
|
2020-06-26 15:30:55 +00:00
|
|
|
eventHandler*: SubscriptionEventHandler
|
|
|
|
errorHandler*: SubscriptionErrorHandler
|
2024-01-08 06:09:23 +00:00
|
|
|
pendingEvents: seq[JsonString]
|
2019-07-02 13:55:39 +00:00
|
|
|
historicalEventsProcessed: bool
|
|
|
|
removed: bool
|
2019-06-12 16:46:29 +00:00
|
|
|
|
2024-01-09 13:57:29 +00:00
|
|
|
ContractInvocation*[TResult, TSender] = object
|
|
|
|
data*: seq[byte]
|
|
|
|
sender*: TSender
|
|
|
|
|
2024-03-27 04:24:31 +00:00
|
|
|
func getValue(params: RequestParamsRx, field: string, FieldType: type):
|
2024-01-08 06:09:23 +00:00
|
|
|
Result[FieldType, string] {.gcsafe, raises: [].} =
|
|
|
|
try:
|
|
|
|
for param in params.named:
|
|
|
|
if param.name == field:
|
|
|
|
when FieldType is JsonString:
|
|
|
|
return ok(param.value)
|
|
|
|
else:
|
|
|
|
let val = JrpcConv.decode(param.value.string, FieldType)
|
|
|
|
return ok(val)
|
|
|
|
except CatchableError as exc:
|
|
|
|
return err(exc.msg)
|
|
|
|
|
2024-03-27 04:24:31 +00:00
|
|
|
func toJsonString(params: RequestParamsRx):
|
2024-01-08 06:09:23 +00:00
|
|
|
Result[JsonString, string] {.gcsafe, raises: [].} =
|
|
|
|
try:
|
|
|
|
let res = JrpcSys.encode(params.toTx)
|
|
|
|
return ok(res.JsonString)
|
|
|
|
except CatchableError as exc:
|
|
|
|
return err(exc.msg)
|
|
|
|
|
|
|
|
proc handleSubscriptionNotification(w: Web3, params: RequestParamsRx):
|
|
|
|
Result[void, string] {.gcsafe, raises: [].} =
|
|
|
|
let subs = params.getValue("subscription", string).valueOr:
|
|
|
|
return err(error)
|
|
|
|
let s = w.subscriptions.getOrDefault(subs)
|
2019-07-02 13:55:39 +00:00
|
|
|
if not s.isNil and not s.removed:
|
|
|
|
if s.historicalEventsProcessed:
|
2024-01-08 06:09:23 +00:00
|
|
|
let res = params.getValue("result", JsonString).valueOr:
|
|
|
|
return err(error)
|
|
|
|
s.eventHandler(res)
|
2019-07-02 13:55:39 +00:00
|
|
|
else:
|
2024-01-08 06:09:23 +00:00
|
|
|
let par = params.toJsonString().valueOr:
|
|
|
|
return err(error)
|
|
|
|
s.pendingEvents.add(par)
|
2019-06-12 16:46:29 +00:00
|
|
|
|
2024-01-08 06:09:23 +00:00
|
|
|
ok()
|
2024-01-24 07:37:32 +00:00
|
|
|
|
2024-03-27 04:24:31 +00:00
|
|
|
func newWeb3*(provider: RpcClient): Web3 =
|
2019-06-12 16:46:29 +00:00
|
|
|
result = Web3(provider: provider)
|
|
|
|
result.subscriptions = initTable[string, Subscription]()
|
2024-01-03 14:54:30 +00:00
|
|
|
let w3 = result
|
|
|
|
|
|
|
|
provider.onProcessMessage = proc(client: RpcClient, line: string):
|
|
|
|
Result[bool, string] {.gcsafe, raises: [].} =
|
|
|
|
try:
|
2024-01-08 06:09:23 +00:00
|
|
|
let req = JrpcSys.decode(line, RequestRx)
|
|
|
|
if req.`method`.isNone:
|
2024-01-03 14:54:30 +00:00
|
|
|
# fallback to regular onProcessMessage
|
|
|
|
return ok(true)
|
|
|
|
|
|
|
|
# This could be subscription notification
|
2024-01-08 06:09:23 +00:00
|
|
|
let name = req.`method`.get
|
2024-01-03 14:54:30 +00:00
|
|
|
if name == "eth_subscription":
|
2024-01-08 06:09:23 +00:00
|
|
|
if req.params.kind != rpNamed:
|
|
|
|
return ok(false)
|
|
|
|
w3.handleSubscriptionNotification(req.params).isOkOr:
|
|
|
|
return err(error)
|
2024-01-03 14:54:30 +00:00
|
|
|
|
|
|
|
# don't fallback, just quit onProcessMessage
|
|
|
|
return ok(false)
|
|
|
|
except CatchableError as exc:
|
|
|
|
return err(exc.msg)
|
2019-06-12 16:46:29 +00:00
|
|
|
|
2022-03-04 19:14:42 +00:00
|
|
|
proc newWeb3*(
|
2023-05-04 23:26:27 +00:00
|
|
|
uri: string,
|
|
|
|
getHeaders: GetJsonRpcRequestHeaders = nil,
|
|
|
|
httpFlags: HttpClientFlags = {}):
|
2022-03-04 19:14:42 +00:00
|
|
|
Future[Web3] {.async.} =
|
2019-07-31 09:16:35 +00:00
|
|
|
let u = parseUri(uri)
|
|
|
|
var provider: RpcClient
|
|
|
|
case u.scheme
|
|
|
|
of "http", "https":
|
2023-05-04 23:26:27 +00:00
|
|
|
let p = newRpcHttpClient(getHeaders = getHeaders,
|
|
|
|
flags = httpFlags)
|
2019-07-31 09:16:35 +00:00
|
|
|
await p.connect(uri)
|
|
|
|
provider = p
|
|
|
|
of "ws", "wss":
|
2022-03-04 19:14:42 +00:00
|
|
|
let p = newRpcWebSocketClient(getHeaders = getHeaders)
|
2019-07-31 09:16:35 +00:00
|
|
|
await p.connect(uri)
|
|
|
|
provider = p
|
|
|
|
else:
|
|
|
|
raise newException(CatchableError, "Unknown web3 url scheme")
|
|
|
|
result = newWeb3(provider)
|
2019-11-22 12:41:12 +00:00
|
|
|
let r = result
|
|
|
|
provider.onDisconnect = proc() =
|
|
|
|
r.subscriptions.clear()
|
|
|
|
if not r.onDisconnect.isNil:
|
|
|
|
r.onDisconnect()
|
2019-07-31 09:16:35 +00:00
|
|
|
|
2019-10-22 15:57:59 +00:00
|
|
|
proc close*(web3: Web3): Future[void] = web3.provider.close()
|
|
|
|
|
2024-01-03 14:54:30 +00:00
|
|
|
proc getHistoricalEvents(s: Subscription, options: FilterOptions) {.async.} =
|
2019-07-02 13:55:39 +00:00
|
|
|
try:
|
2024-01-03 14:54:30 +00:00
|
|
|
let logs = await s.web3.provider.eth_getJsonLogs(options)
|
2019-07-02 13:55:39 +00:00
|
|
|
for l in logs:
|
|
|
|
if s.removed: break
|
2020-06-26 15:30:55 +00:00
|
|
|
s.eventHandler(l)
|
2019-07-02 13:55:39 +00:00
|
|
|
s.historicalEventsProcessed = true
|
|
|
|
var i = 0
|
|
|
|
while i < s.pendingEvents.len: # Mind reentrancy
|
|
|
|
if s.removed: break
|
2020-06-26 15:30:55 +00:00
|
|
|
s.eventHandler(s.pendingEvents[i])
|
2019-07-02 13:55:39 +00:00
|
|
|
inc i
|
|
|
|
s.pendingEvents = @[]
|
2020-03-11 09:03:09 +00:00
|
|
|
except CatchableError as e:
|
2019-07-02 13:55:39 +00:00
|
|
|
echo "Caught exception in getHistoricalEvents: ", e.msg
|
|
|
|
echo e.getStackTrace()
|
|
|
|
|
2024-06-11 23:32:06 +00:00
|
|
|
proc subscribe*(w: Web3, name: string, options: Opt[FilterOptions],
|
2020-06-26 15:30:55 +00:00
|
|
|
eventHandler: SubscriptionEventHandler,
|
|
|
|
errorHandler: SubscriptionErrorHandler): Future[Subscription]
|
|
|
|
{.async.} =
|
|
|
|
## Sets up a new subsciption using the `eth_subscribe` RPC call.
|
|
|
|
##
|
|
|
|
## May raise a `CatchableError` if the subscription is not established.
|
|
|
|
##
|
|
|
|
## Once the subscription is established, the `eventHandler` callback
|
|
|
|
## will be executed for each event of interest.
|
|
|
|
##
|
|
|
|
## In case of any errors or illegal behavior of the remote RPC node,
|
|
|
|
## the `errorHandler` will be executed with relevant information about
|
|
|
|
## the error.
|
2020-09-24 12:56:26 +00:00
|
|
|
|
|
|
|
# Don't send an empty `{}` object as an extra argument if there are no options
|
2024-01-03 14:54:30 +00:00
|
|
|
let id = if options.isNone:
|
2020-09-24 12:56:26 +00:00
|
|
|
await w.provider.eth_subscribe(name)
|
|
|
|
else:
|
2024-01-03 14:54:30 +00:00
|
|
|
await w.provider.eth_subscribe(name, options.get)
|
2020-09-24 12:56:26 +00:00
|
|
|
|
2020-06-26 15:30:55 +00:00
|
|
|
result = Subscription(id: id,
|
|
|
|
web3: w,
|
|
|
|
eventHandler: eventHandler,
|
|
|
|
errorHandler: errorHandler)
|
|
|
|
|
2019-06-12 16:46:29 +00:00
|
|
|
w.subscriptions[id] = result
|
|
|
|
|
2024-01-03 14:54:30 +00:00
|
|
|
proc subscribeForLogs*(w: Web3, options: FilterOptions,
|
2020-06-26 15:30:55 +00:00
|
|
|
logsHandler: SubscriptionEventHandler,
|
2020-06-27 11:50:53 +00:00
|
|
|
errorHandler: SubscriptionErrorHandler,
|
|
|
|
withHistoricEvents = true): Future[Subscription]
|
2020-06-26 15:30:55 +00:00
|
|
|
{.async.} =
|
2024-06-11 23:32:06 +00:00
|
|
|
result = await subscribe(w, "logs", Opt.some(options), logsHandler, errorHandler)
|
2020-06-27 11:50:53 +00:00
|
|
|
if withHistoricEvents:
|
|
|
|
discard getHistoricalEvents(result, options)
|
|
|
|
else:
|
|
|
|
result.historicalEventsProcessed = true
|
2019-07-02 13:55:39 +00:00
|
|
|
|
2024-03-27 04:24:31 +00:00
|
|
|
func addAddressAndSignatureToOptions(options: FilterOptions, address: Address, topic: Topic): FilterOptions =
|
2024-01-03 14:54:30 +00:00
|
|
|
result = options
|
|
|
|
if result.address.kind == slkNull:
|
|
|
|
result.address = AddressOrList(kind: slkSingle, single: address)
|
|
|
|
result.topics.insert(TopicOrList(kind: slkSingle, single: topic), 0)
|
|
|
|
|
|
|
|
proc subscribeForLogs*(s: Web3SenderImpl, options: FilterOptions,
|
|
|
|
topic: Topic,
|
2023-12-13 05:26:04 +00:00
|
|
|
logsHandler: SubscriptionEventHandler,
|
|
|
|
errorHandler: SubscriptionErrorHandler,
|
|
|
|
withHistoricEvents = true): Future[Subscription] =
|
|
|
|
let options = addAddressAndSignatureToOptions(options, s.contractAddress, topic)
|
|
|
|
s.web3.subscribeForLogs(options, logsHandler, errorHandler, withHistoricEvents)
|
|
|
|
|
2020-09-24 12:56:26 +00:00
|
|
|
proc subscribeForBlockHeaders*(w: Web3,
|
2023-10-05 10:37:04 +00:00
|
|
|
blockHeadersCallback: proc(b: BlockHeader) {.gcsafe, raises: [].},
|
2020-06-26 15:30:55 +00:00
|
|
|
errorHandler: SubscriptionErrorHandler): Future[Subscription]
|
|
|
|
{.async.} =
|
2024-01-08 06:09:23 +00:00
|
|
|
proc eventHandler(json: JsonString) {.gcsafe, raises: [].} =
|
2024-01-03 14:54:30 +00:00
|
|
|
|
2021-10-04 15:41:19 +00:00
|
|
|
try:
|
2024-01-08 06:09:23 +00:00
|
|
|
let blk = JrpcConv.decode(json.string, BlockHeader)
|
2021-10-04 15:41:19 +00:00
|
|
|
blockHeadersCallback(blk)
|
|
|
|
except CatchableError as err:
|
|
|
|
errorHandler(err[])
|
2020-06-26 15:30:55 +00:00
|
|
|
|
2020-09-24 12:56:26 +00:00
|
|
|
# `nil` options so that we skip sending an empty `{}` object as an extra argument
|
|
|
|
# to geth for `newHeads`: https://github.com/ethereum/go-ethereum/issues/21588
|
2024-06-11 23:32:06 +00:00
|
|
|
result = await subscribe(w, "newHeads", Opt.none(FilterOptions), eventHandler, errorHandler)
|
2020-06-26 15:30:55 +00:00
|
|
|
result.historicalEventsProcessed = true
|
|
|
|
|
2019-06-12 16:46:29 +00:00
|
|
|
proc unsubscribe*(s: Subscription): Future[void] {.async.} =
|
2019-07-02 13:55:39 +00:00
|
|
|
s.web3.subscriptions.del(s.id)
|
|
|
|
s.removed = true
|
2019-06-12 16:46:29 +00:00
|
|
|
discard await s.web3.provider.eth_unsubscribe(s.id)
|
2018-12-21 17:27:09 +00:00
|
|
|
|
2024-01-03 14:54:30 +00:00
|
|
|
proc getJsonLogs(s: Web3SenderImpl, topic: Topic,
|
2024-06-11 23:32:06 +00:00
|
|
|
fromBlock = Opt.none(RtBlockIdentifier),
|
|
|
|
toBlock = Opt.none(RtBlockIdentifier),
|
2024-10-01 17:41:11 +00:00
|
|
|
blockHash = Opt.none(Hash32)): Future[seq[JsonString]] =
|
2024-01-03 14:54:30 +00:00
|
|
|
|
|
|
|
var options = FilterOptions(
|
|
|
|
address: AddressOrList(kind: slkSingle, single: s.contractAddress),
|
|
|
|
topics: @[TopicOrList(kind: slkSingle, single: topic)],
|
|
|
|
)
|
|
|
|
|
2020-03-07 21:46:10 +00:00
|
|
|
if blockHash.isSome:
|
|
|
|
doAssert fromBlock.isNone and toBlock.isNone
|
2024-01-03 14:54:30 +00:00
|
|
|
options.blockHash = blockHash
|
2020-03-07 21:46:10 +00:00
|
|
|
else:
|
2024-01-03 14:54:30 +00:00
|
|
|
options.fromBlock = fromBlock
|
|
|
|
options.toBlock = toBlock
|
2020-03-07 21:46:10 +00:00
|
|
|
|
2024-01-03 14:54:30 +00:00
|
|
|
# TODO: optimize it instead of double conversion
|
|
|
|
s.web3.provider.eth_getJsonLogs(options)
|
2020-03-07 21:46:10 +00:00
|
|
|
|
2023-12-13 05:26:04 +00:00
|
|
|
proc getJsonLogs*[TContract](s: Sender[TContract],
|
|
|
|
EventName: type,
|
2024-06-11 23:32:06 +00:00
|
|
|
fromBlock = Opt.none(RtBlockIdentifier),
|
|
|
|
toBlock = Opt.none(RtBlockIdentifier),
|
2024-10-01 17:41:11 +00:00
|
|
|
blockHash = Opt.none(Hash32)): Future[seq[JsonString]] {.inline.} =
|
2023-12-13 05:26:04 +00:00
|
|
|
mixin eventTopic
|
2024-02-06 21:26:20 +00:00
|
|
|
getJsonLogs(s.sender, eventTopic(EventName), fromBlock, toBlock, blockHash)
|
2023-12-13 05:26:04 +00:00
|
|
|
|
2023-12-06 12:59:24 +00:00
|
|
|
proc nextNonce*(web3: Web3): Future[Quantity] {.async.} =
|
2020-07-28 19:54:37 +00:00
|
|
|
if web3.lastKnownNonce.isSome:
|
|
|
|
inc web3.lastKnownNonce.get
|
|
|
|
return web3.lastKnownNonce.get
|
|
|
|
else:
|
2024-10-01 17:41:11 +00:00
|
|
|
let fromAddress = web3.privateKey.get().toPublicKey().toCanonicalAddress
|
2023-12-06 12:59:24 +00:00
|
|
|
result = await web3.provider.eth_getTransactionCount(fromAddress, "latest")
|
2024-06-11 23:32:06 +00:00
|
|
|
web3.lastKnownNonce = Opt.some result
|
2020-07-28 19:54:37 +00:00
|
|
|
|
2024-10-01 17:41:11 +00:00
|
|
|
proc send*(web3: Web3, c: TransactionArgs): Future[Hash32] {.async.} =
|
2020-06-21 19:57:43 +00:00
|
|
|
if web3.privateKey.isSome():
|
2019-08-05 20:08:18 +00:00
|
|
|
var cc = c
|
2020-07-28 19:54:37 +00:00
|
|
|
if cc.nonce.isNone:
|
2024-06-11 23:32:06 +00:00
|
|
|
cc.nonce = Opt.some(await web3.nextNonce())
|
2023-12-06 12:59:24 +00:00
|
|
|
let t = encodeTransaction(cc, web3.privateKey.get())
|
2019-08-05 20:08:18 +00:00
|
|
|
return await web3.provider.eth_sendRawTransaction(t)
|
|
|
|
else:
|
|
|
|
return await web3.provider.eth_sendTransaction(c)
|
|
|
|
|
2024-10-01 17:41:11 +00:00
|
|
|
proc send*(web3: Web3, c: TransactionArgs, chainId: ChainId): Future[Hash32] {.deprecated: "Provide chainId in TransactionArgs", async.} =
|
2023-12-13 06:23:31 +00:00
|
|
|
doAssert(web3.privateKey.isSome())
|
|
|
|
var cc = c
|
|
|
|
if cc.nonce.isNone:
|
2024-06-11 23:32:06 +00:00
|
|
|
cc.nonce = Opt.some(await web3.nextNonce())
|
2023-12-13 06:23:31 +00:00
|
|
|
let t = encodeTransaction(cc, web3.privateKey.get(), chainId)
|
|
|
|
return await web3.provider.eth_sendRawTransaction(t)
|
|
|
|
|
2024-01-09 13:57:29 +00:00
|
|
|
proc sendData(web3: Web3,
|
|
|
|
contractAddress: Address,
|
|
|
|
defaultAccount: Address,
|
2023-12-13 05:26:04 +00:00
|
|
|
data: seq[byte],
|
|
|
|
value: UInt256,
|
|
|
|
gas: uint64,
|
2023-12-13 06:23:31 +00:00
|
|
|
gasPrice: int,
|
2024-10-01 17:41:11 +00:00
|
|
|
chainId = Opt.none(ChainId)): Future[Hash32] {.async.} =
|
2020-07-28 19:54:37 +00:00
|
|
|
let
|
2024-06-11 23:32:06 +00:00
|
|
|
gasPrice = if web3.privateKey.isSome() or gasPrice != 0: Opt.some(gasPrice.Quantity)
|
|
|
|
else: Opt.none(Quantity)
|
|
|
|
nonce = if web3.privateKey.isSome(): Opt.some(await web3.nextNonce())
|
|
|
|
else: Opt.none(Quantity)
|
|
|
|
chainId = if chainId.isSome(): Opt.some(Quantity(chainId.get))
|
|
|
|
else: Opt.none(Quantity)
|
2020-07-28 19:54:37 +00:00
|
|
|
|
2024-03-20 04:12:59 +00:00
|
|
|
cc = TransactionArgs(
|
2024-06-11 23:32:06 +00:00
|
|
|
data: Opt.some(data),
|
|
|
|
`from`: Opt.some(defaultAccount),
|
|
|
|
to: Opt.some(contractAddress),
|
|
|
|
gas: Opt.some(Quantity(gas)),
|
|
|
|
value: Opt.some(value),
|
2020-07-28 19:54:37 +00:00
|
|
|
nonce: nonce,
|
2023-12-06 12:59:24 +00:00
|
|
|
gasPrice: gasPrice,
|
2024-06-10 08:14:14 +00:00
|
|
|
chainId: chainId
|
2023-12-06 12:59:24 +00:00
|
|
|
)
|
2020-07-28 19:54:37 +00:00
|
|
|
|
2024-06-10 08:14:14 +00:00
|
|
|
return await web3.send(cc)
|
2019-08-05 20:08:18 +00:00
|
|
|
|
2023-12-13 05:26:04 +00:00
|
|
|
proc send*[T](c: ContractInvocation[T, Web3SenderImpl],
|
|
|
|
value = 0.u256,
|
|
|
|
gas = 3000000'u64,
|
2024-10-01 17:41:11 +00:00
|
|
|
gasPrice = 0): Future[Hash32] =
|
2024-06-11 23:32:06 +00:00
|
|
|
sendData(c.sender.web3, c.sender.contractAddress,
|
|
|
|
c.sender.web3.defaultAccount, c.data, value, gas, gasPrice)
|
2023-12-13 05:26:04 +00:00
|
|
|
|
2023-12-13 06:23:31 +00:00
|
|
|
proc send*[T](c: ContractInvocation[T, Web3SenderImpl],
|
|
|
|
chainId: ChainId,
|
|
|
|
value = 0.u256,
|
|
|
|
gas = 3000000'u64,
|
2024-10-01 17:41:11 +00:00
|
|
|
gasPrice = 0): Future[Hash32] =
|
2024-06-11 23:32:06 +00:00
|
|
|
sendData(c.sender.web3, c.sender.contractAddress,
|
|
|
|
c.sender.web3.defaultAccount, c.data, value, gas, gasPrice, some(chainId))
|
2023-12-13 06:23:31 +00:00
|
|
|
|
2024-03-16 23:04:38 +00:00
|
|
|
proc callAux(
|
|
|
|
web3: Web3,
|
|
|
|
contractAddress: Address,
|
|
|
|
defaultAccount: Address,
|
|
|
|
data: seq[byte],
|
|
|
|
value = 0.u256,
|
|
|
|
gas = 3000000'u64,
|
2024-10-01 17:41:11 +00:00
|
|
|
blockNumber = high(Quantity)): Future[seq[byte]] {.async.} =
|
2024-03-20 04:12:59 +00:00
|
|
|
var cc: TransactionArgs
|
2024-06-11 23:32:06 +00:00
|
|
|
cc.data = Opt.some(data)
|
|
|
|
cc.source = Opt.some(defaultAccount)
|
|
|
|
cc.to = Opt.some(contractAddress)
|
|
|
|
cc.gas = Opt.some(Quantity(gas))
|
|
|
|
cc.value = Opt.some(value)
|
2024-01-09 13:57:29 +00:00
|
|
|
result =
|
2024-10-01 17:41:11 +00:00
|
|
|
if blockNumber != high(Quantity):
|
2024-01-03 14:54:30 +00:00
|
|
|
await web3.provider.eth_call(cc, blockId(blockNumber))
|
2020-03-05 23:56:23 +00:00
|
|
|
else:
|
2023-12-13 05:26:04 +00:00
|
|
|
await web3.provider.eth_call(cc, "latest")
|
2020-03-05 23:56:23 +00:00
|
|
|
|
2024-03-16 23:04:38 +00:00
|
|
|
proc call*[T](
|
|
|
|
c: ContractInvocation[T, Web3SenderImpl],
|
|
|
|
value = 0.u256,
|
|
|
|
gas = 3000000'u64,
|
2024-10-01 17:41:11 +00:00
|
|
|
blockNumber = high(Quantity)): Future[T] {.async.} =
|
2024-06-11 23:32:06 +00:00
|
|
|
let response = await callAux(c.sender.web3, c.sender.contractAddress,
|
|
|
|
c.sender.web3.defaultAccount, c.data, value, gas, blockNumber)
|
2020-03-05 23:56:23 +00:00
|
|
|
if response.len > 0:
|
2024-01-09 13:57:29 +00:00
|
|
|
discard decode(response, 0, 0, result)
|
2020-03-05 23:56:23 +00:00
|
|
|
else:
|
|
|
|
raise newException(CatchableError, "No response from the Web3 provider")
|
2019-08-05 20:08:18 +00:00
|
|
|
|
2024-10-01 17:41:11 +00:00
|
|
|
proc getMinedTransactionReceipt*(web3: Web3, tx: Hash32): Future[ReceiptObject] {.async.} =
|
2019-10-30 13:05:44 +00:00
|
|
|
## Returns the receipt for the transaction. Waits for it to be mined if necessary.
|
|
|
|
# TODO: Potentially more optimal solution is to subscribe and wait for appropriate
|
|
|
|
# notification. Now we're just polling every 500ms which should be ok for most cases.
|
2023-12-06 12:59:24 +00:00
|
|
|
var r: ReceiptObject
|
|
|
|
while r.isNil:
|
2019-10-30 13:05:44 +00:00
|
|
|
r = await web3.provider.eth_getTransactionReceipt(tx)
|
2023-12-06 12:59:24 +00:00
|
|
|
if r.isNil:
|
2019-10-30 13:05:44 +00:00
|
|
|
await sleepAsync(500.milliseconds)
|
2023-12-06 12:59:24 +00:00
|
|
|
result = r
|
2019-10-30 13:05:44 +00:00
|
|
|
|
2023-12-13 05:26:04 +00:00
|
|
|
proc exec*[T](c: ContractInvocation[T, Web3SenderImpl], value = 0.u256, gas = 3000000'u64): Future[T] {.async.} =
|
2019-08-05 20:08:18 +00:00
|
|
|
let h = await c.send(value, gas)
|
2024-01-04 11:36:40 +00:00
|
|
|
let receipt = await c.sender.web3.getMinedTransactionReceipt(h)
|
2019-08-05 20:08:18 +00:00
|
|
|
|
|
|
|
# TODO: decode result from receipt
|
|
|
|
|
|
|
|
|
2018-12-21 17:27:09 +00:00
|
|
|
# This call will generate the `cc.data` part to call that contract method in the code below
|
2019-01-04 17:28:43 +00:00
|
|
|
#sendCoin(fromHex(Stuint[256], "e375b6fb6d0bf0d86707884f3952fee3977251fe"), 600.to(Stuint[256]))
|
2018-12-21 17:27:09 +00:00
|
|
|
|
|
|
|
# Set up a JsonRPC call to send a transaction
|
|
|
|
# The idea here is to let the Web3 object contain the RPC calls, then allow the
|
2024-03-20 04:12:59 +00:00
|
|
|
# above DSL to create helpers to create the TransactionArgs object and perform the
|
2018-12-21 17:27:09 +00:00
|
|
|
# transaction. The current idea is to make all this reduce to something like:
|
|
|
|
# var
|
|
|
|
# w3 = initWeb3("127.0.0.1", 8545)
|
|
|
|
# myContract = contract:
|
|
|
|
# <DSL>
|
|
|
|
# myContract.sender("0x780bc7b4055941c2cb0ee10510e3fc837eb093c1").sendCoin(
|
|
|
|
# fromHex(Stuint[256], "e375b6fb6d0bf0d86707884f3952fee3977251fe"),
|
|
|
|
# 600.to(Stuint[256])
|
|
|
|
# )
|
|
|
|
# If the address of the contract on the chain should be part of the DSL or
|
|
|
|
# dynamically registered is still not decided.
|
2024-03-20 04:12:59 +00:00
|
|
|
#var cc: TransactionArgs
|
2019-01-04 17:28:43 +00:00
|
|
|
#cc.source = [0x78.byte, 0x0b, 0xc7, 0xb4, 0x05, 0x59, 0x41, 0xc2, 0xcb, 0x0e, 0xe1, 0x05, 0x10, 0xe3, 0xfc, 0x83, 0x7e, 0xb0, 0x93, 0xc1]
|
|
|
|
#cc.to = some([0x0a.byte, 0x78, 0xc0, 0x8F, 0x31, 0x4E, 0xB2, 0x5A, 0x35, 0x1B, 0xfB, 0xA9, 0x03,0x21, 0xa6, 0x96, 0x04, 0x74, 0xbD, 0x79])
|
|
|
|
#cc.data = "0x90b98a11000000000000000000000000e375b6fb6d0bf0d86707884f3952fee3977251FE0000000000000000000000000000000000000000000000000000000000000258"
|
|
|
|
|
|
|
|
#var w3 = initWeb3("127.0.0.1", 8545)
|
|
|
|
#let response = waitFor w3.eth.eth_sendTransaction(cc)
|
|
|
|
#echo response
|
|
|
|
|
2024-03-27 04:24:31 +00:00
|
|
|
func contractSender*(web3: Web3, T: typedesc, toAddress: Address): Sender[T] =
|
2023-12-13 05:26:04 +00:00
|
|
|
Sender[T](sender: Web3SenderImpl(web3: web3, contractAddress: toAddress))
|
2019-01-10 11:50:51 +00:00
|
|
|
|
2024-03-27 04:24:31 +00:00
|
|
|
func createMutableContractInvocation*(sender: Web3SenderImpl, ReturnType: typedesc, data: sink seq[byte]): ContractInvocation[ReturnType, Web3SenderImpl] {.inline.} =
|
2024-01-09 13:57:29 +00:00
|
|
|
ContractInvocation[ReturnType, Web3SenderImpl](sender: sender, data: data)
|
|
|
|
|
2024-03-27 04:24:31 +00:00
|
|
|
func createImmutableContractInvocation*(sender: Web3SenderImpl, ReturnType: typedesc, data: sink seq[byte]): ContractInvocation[ReturnType, Web3SenderImpl] {.inline.} =
|
2024-01-09 13:57:29 +00:00
|
|
|
ContractInvocation[ReturnType, Web3SenderImpl](sender: sender, data: data)
|
|
|
|
|
2024-03-27 04:24:31 +00:00
|
|
|
func contractInstance*(
|
2024-03-16 23:04:38 +00:00
|
|
|
web3: Web3, T: typedesc, toAddress: Address): AsyncSender[T] =
|
|
|
|
AsyncSender[T](
|
|
|
|
sender: Web3AsyncSenderImpl(
|
|
|
|
web3: web3,
|
|
|
|
contractAddress: toAddress,
|
|
|
|
defaultAccount: web3.defaultAccount,
|
|
|
|
gas: 3000000,
|
2024-10-01 17:41:11 +00:00
|
|
|
blockNumber: Quantity.high))
|
2024-01-09 13:57:29 +00:00
|
|
|
|
|
|
|
proc createMutableContractInvocation*(sender: Web3AsyncSenderImpl, ReturnType: typedesc, data: sink seq[byte]) {.async.} =
|
|
|
|
assert(sender.gas > 0)
|
|
|
|
let h = await sendData(sender.web3, sender.contractAddress, sender.defaultAccount, data, sender.value, sender.gas, sender.gasPrice, sender.chainId)
|
|
|
|
let receipt = await sender.web3.getMinedTransactionReceipt(h)
|
2024-01-24 07:37:32 +00:00
|
|
|
discard receipt
|
2024-01-09 13:57:29 +00:00
|
|
|
|
2024-03-16 23:04:38 +00:00
|
|
|
proc createImmutableContractInvocation*(
|
|
|
|
sender: Web3AsyncSenderImpl,
|
|
|
|
ReturnType: typedesc,
|
|
|
|
data: sink seq[byte]): Future[ReturnType] {.async.} =
|
|
|
|
let response = await callAux(
|
|
|
|
sender.web3, sender.contractAddress, sender.defaultAccount, data,
|
|
|
|
sender.value, sender.gas, sender.blockNumber)
|
2024-01-09 13:57:29 +00:00
|
|
|
if response.len > 0:
|
|
|
|
discard decode(response, 0, 0, result)
|
|
|
|
else:
|
|
|
|
raise newException(CatchableError, "No response from the Web3 provider")
|
|
|
|
|
|
|
|
proc deployContractAux(web3: Web3, data: seq[byte], gasPrice = 0): Future[Address] {.async.} =
|
2024-03-20 04:12:59 +00:00
|
|
|
var tr: TransactionArgs
|
2024-06-11 23:32:06 +00:00
|
|
|
tr.`from` = Opt.some(web3.defaultAccount)
|
|
|
|
tr.data = Opt.some(data)
|
|
|
|
tr.gas = Opt.some Quantity(30000000)
|
2024-01-09 13:57:29 +00:00
|
|
|
if gasPrice != 0:
|
2024-06-11 23:32:06 +00:00
|
|
|
tr.gasPrice = Opt.some(gasPrice.Quantity)
|
2024-01-09 13:57:29 +00:00
|
|
|
|
|
|
|
let h = await web3.send(tr)
|
|
|
|
let r = await web3.getMinedTransactionReceipt(h)
|
|
|
|
return r.contractAddress.get
|
|
|
|
|
|
|
|
proc createContractDeployment*(web3: Web3, ContractType: typedesc, data: sink seq[byte]): Future[AsyncSender[ContractType]] {.async.} =
|
|
|
|
let a = await deployContractAux(web3, data, gasPrice = 0)
|
|
|
|
return contractInstance(web3, ContractType, a)
|
|
|
|
|
2020-06-19 16:42:55 +00:00
|
|
|
proc isDeployed*(s: Sender, atBlock: RtBlockIdentifier): Future[bool] {.async.} =
|
|
|
|
let
|
|
|
|
codeFut = case atBlock.kind
|
2020-06-22 12:27:47 +00:00
|
|
|
of bidNumber:
|
2023-12-13 05:26:04 +00:00
|
|
|
s.sender.web3.provider.eth_getCode(s.contractAddress, atBlock.number)
|
2020-06-22 12:27:47 +00:00
|
|
|
of bidAlias:
|
2023-12-13 05:26:04 +00:00
|
|
|
s.sender.web3.provider.eth_getCode(s.contractAddress, atBlock.alias)
|
2020-06-19 16:42:55 +00:00
|
|
|
code = await codeFut
|
|
|
|
|
|
|
|
# TODO: Check that all methods of the contract are present by
|
|
|
|
# looking for their ABI signatures within the code:
|
|
|
|
# https://ethereum.stackexchange.com/questions/11856/how-to-detect-from-web3-if-method-exists-on-a-deployed-contract
|
|
|
|
return code.len > 0
|
|
|
|
|
2023-12-13 05:26:04 +00:00
|
|
|
proc subscribe*[TContract](s: Sender[TContract], t: typedesc, cb: proc): Future[Subscription] {.inline.} =
|
2024-01-03 14:54:30 +00:00
|
|
|
subscribe(s, t, FilterOptions(), cb, SubscriptionErrorHandler nil)
|
2024-01-09 13:57:29 +00:00
|
|
|
|
2024-03-27 04:24:31 +00:00
|
|
|
func copy[T](s: AsyncSender[T]): AsyncSender[T] =
|
2024-01-09 13:57:29 +00:00
|
|
|
result = s
|
|
|
|
result.sender.new()
|
|
|
|
result.sender[] = s.sender[]
|
|
|
|
|
|
|
|
macro adjust*(s: AsyncSender, modifications: varargs[untyped]): untyped =
|
|
|
|
## Copies AsyncSender, modifying its properties. E.g.
|
|
|
|
## myContract.adjust(gas = 1000, value = 5.u256).myContractMethod()
|
|
|
|
let cp = genSym(nskLet, "cp")
|
|
|
|
result = quote do:
|
|
|
|
block:
|
|
|
|
let `cp` = copy(`s`)
|
|
|
|
|
|
|
|
for s in modifications:
|
|
|
|
s.expectKind(nnkExprEqExpr)
|
|
|
|
let fieldName = s[0]
|
|
|
|
let fieldVal = s[1]
|
|
|
|
result[1].add quote do:
|
|
|
|
`cp`.sender.`fieldName` = `fieldVal`
|
2024-06-10 08:14:14 +00:00
|
|
|
result[1].add(cp)
|