chores(@backend): Add macros for api

This commit is contained in:
Anthony Laibe 2022-02-14 16:34:29 +01:00 committed by Iuri Matias
parent bc23762a05
commit 6ebd8cb1cc
4 changed files with 92 additions and 21 deletions

View File

@ -3,7 +3,7 @@ import NimQml, Tables, json, sequtils, chronicles, strformat, strutils
from sugar import `=>`
import web3/ethtypes
from web3/conversions import `$`
import ../../../backend/custom_tokens as custom_tokens
import ../../../backend/backend as backend
import ../../../backend/tokens as token_backend
import ../settings/service as settings_service
@ -71,7 +71,7 @@ QtObject:
self.tokens = initTable[NetworkDto, seq[TokenDto]]()
var activeTokenSymbols = self.settingsService.getWalletVisibleTokens()
let networks = self.networkService.getEnabledNetworks()
let responseCustomTokens = custom_tokens.getCustomTokens()
let responseCustomTokens = backend.getCustomTokens()
for network in networks:
if not activeTokenSymbols.hasKey(network.chainId) and DEFAULT_VISIBLE_TOKENS.hasKey(network.chainId):
@ -109,7 +109,10 @@ QtObject:
proc addCustomToken*(self: Service, chainId: int, address: string, name: string, symbol: string, decimals: int) =
# TODO(alaile): use chainId rather than first enabled network
let networkWIP = self.networkService.getEnabledNetworks()[0]
custom_tokens.addCustomToken(networkWIP.chainId, address, name, symbol, decimals, "")
let backendToken = backend.Token(
name: name, chainId: networkWIP.chainId, address: address, symbol: symbol, decimals: decimals, color: ""
)
discard backend.addCustomToken(backendToken)
let token = newDto(
name,
networkWIP.chainId,
@ -142,7 +145,7 @@ QtObject:
proc removeCustomToken*(self: Service, chainId: int, address: string) =
let network = self.networkService.getNetwork(chainId)
custom_tokens.removeCustomToken(chainId, address)
discard backend.deleteCustomTokenByChainID(chainId, address)
var index = -1
for idx, token in self.tokens[network].pairs():

24
src/backend/backend.nim Normal file
View File

@ -0,0 +1,24 @@
import json, json_serialization
import ./core, ./response_type
from ./gen import rpc
export response_type
type
Token* = ref object of RootObj
name* {.serializedFieldName("name").}: string
chainId* {.serializedFieldName("chainId").}: int
address* {.serializedFieldName("address").}: string
symbol* {.serializedFieldName("symbol").}: string
decimals* {.serializedFieldName("decimals").}: int
color* {.serializedFieldName("color").}: string
rpc(getCustomTokens, "wallet"):
discard
rpc(deleteCustomTokenByChainID, "wallet"):
chainId: int
address: string
rpc(addCustomToken, "wallet"):
token: Token

View File

@ -1,17 +0,0 @@
import json
import ./core, ./response_type
export response_type
proc getCustomTokens*(): RpcResponse[JsonNode] {.raises: [Exception].} =
return callPrivateRPC("wallet_getCustomTokens", %* [])
proc addCustomToken*(
chainId: int, address: string, name: string, symbol: string, decimals: int, color: string
) {.raises: [Exception].} =
discard callPrivateRPC("wallet_addCustomToken", %* [
{"chainId": chainId, "address": address, "name": name, "symbol": symbol, "decimals": decimals, "color": color}
])
proc removeCustomToken*(chainId: int, address: string) {.raises: [Exception].} =
discard callPrivateRPC("wallet_deleteCustomTokenByChainID", %* [chainId, address])

61
src/backend/gen.nim Normal file
View File

@ -0,0 +1,61 @@
import macros
macro rpc*(rpcMethod: untyped, prefix: string, identDefs: untyped): untyped =
let rpcMethodName = rpcMethod.strval
let rpcCall = $prefix & "_" & rpcMethodName
var params = @[
nnkBracketExpr.newTree(
ident("RpcResponse"),
ident("JsonNode")
),
]
var payload: seq[NimNode] = @[]
for identDef in identDefs:
if identDef.kind == NimNodeKind.nnkDiscardStmt:
continue
let fieldNameIdent = identDef[0]
let fieldName = fieldNameIdent.strval
params.add(nnkIdentDefs.newTree(
ident(fieldName),
ident(identDef[1][0].strVal),
newNimNode(nnkEmpty)
))
payload.add(newIdentNode(fieldName))
return nnkProcDef.newTree(
nnkPostfix.newTree(
ident("*"),
ident(rpcMethodName)
),
newEmptyNode(),
newEmptyNode(),
nnkFormalParams.newTree(
params
),
nnkPragma.newTree(
nnkExprColonExpr.newTree(
newIdentNode("raises"),
nnkBracket.newTree(
newIdentNode("Exception")
)
)
),
newEmptyNode(),
nnkStmtList.newTree(
nnkCall.newTree(
newIdentNode("callPrivateRPC"),
newLit(rpcCall),
nnkPrefix.newTree(
newIdentNode("%*"),
nnkBracket.newTree(
payload
)
)
)
)
)