Transform some macro generated code into a generic function

This commit is contained in:
Zahary Karadjov 2020-03-07 23:46:10 +02:00
parent 270fd64620
commit d172765114
No known key found for this signature in database
GPG Key ID: C8936F8A3073D609
1 changed files with 23 additions and 21 deletions

View File

@ -656,7 +656,7 @@ macro contract*(cname: untyped, body: untyped): untyped =
result.add quote do:
type `cbident` = object
template eventTopic(T: type `cbident`): string =
template eventTopic*(T: type `cbident`): string =
"0x" & toLowerAscii($keccak256.digest(`signature`))
proc subscribe(s: Sender[`cname`],
@ -678,29 +678,31 @@ macro contract*(cname: untyped, body: untyped): untyped =
s.web3.subscribeToLogs(options) do(`jsonIdent`: JsonNode):
`argParseBody`
`callWithRawData`
proc getJsonLogs(s: Sender[`cname`],
t: type `cbident`,
fromBlock, toBlock = none(RtBlockIdentifier),
blockHash = none(BlockHash)): Future[JsonNode] =
var options = newJObject()
options["address"] = %s.contractAddress
var topics = newJArray()
topics.elems.insert(%eventTopic(`cbident`), 0)
options["topics"] = topics
if blockHash.isSome:
doAssert fromBlock.isNone and toBlock.isNone
options["blockhash"] = %blockHash.unsafeGet
else:
if fromBlock.isSome:
options["fromBlock"] = %fromBlock.unsafeGet
if toBlock.isSome:
options["toBlock"] = %toBlock.unsafeGet
s.web3.provider.eth_getLogs(options)
else:
discard
proc getJsonLogs*(s: Sender,
EventName: type,
fromBlock, toBlock = none(RtBlockIdentifier),
blockHash = none(BlockHash)): Future[JsonNode] =
mixin eventTopic
var options = newJObject()
options["address"] = %s.contractAddress
var topics = newJArray()
topics.elems.insert(%eventTopic(EventName), 0)
options["topics"] = topics
if blockHash.isSome:
doAssert fromBlock.isNone and toBlock.isNone
options["blockhash"] = %blockHash.unsafeGet
else:
if fromBlock.isSome:
options["fromBlock"] = %fromBlock.unsafeGet
if toBlock.isSome:
options["toBlock"] = %toBlock.unsafeGet
s.web3.provider.eth_getLogs(options)
proc signatureEnabled(w: Web3): bool {.inline.} =
w.privateKey.verify()