From cd5042e389a40749d055f336386d64d946816531 Mon Sep 17 00:00:00 2001 From: Anthony Laibe Date: Mon, 11 Jul 2022 14:26:21 +0200 Subject: [PATCH] feat(@wallet): multi transaction --- .../service/eth/dto/method_dto.nim | 7 --- src/app_service/service/transaction/dto.nim | 16 ++++- .../service/transaction/service.nim | 58 +++++++++++-------- src/backend/transactions.nim | 8 ++- vendor/status-go | 2 +- 5 files changed, 58 insertions(+), 33 deletions(-) diff --git a/src/app_service/service/eth/dto/method_dto.nim b/src/app_service/service/eth/dto/method_dto.nim index f8b594a85e..5bf99546e1 100644 --- a/src/app_service/service/eth/dto/method_dto.nim +++ b/src/app_service/service/eth/dto/method_dto.nim @@ -89,13 +89,6 @@ proc getEstimateGasData*(self: MethodDto, tx: var TransactionDataDto, procDescri tx.data = self.encodeAbi(procDescriptor) return %*[%tx] -proc send*(self: MethodDto, chainId: int, tx: var TransactionDataDto, procDescriptor: object, password: string, success: var bool): RpcResponse[JsonNode] = - tx.data = self.encodeAbi(procDescriptor) - # this call should not be part of this file, we need to move it to appropriate place, or this should not be a DTO class. - let response = status_eth.sendTransaction(chainId, $(%tx), password) - success = response.error.isNil - return response - proc call[T](self: MethodDto, chainId: int, tx: var TransactionDataDto, procDescriptor: object, success: var bool): T = success = true tx.data = self.encodeAbi(procDescriptor) diff --git a/src/app_service/service/transaction/dto.nim b/src/app_service/service/transaction/dto.nim index abb4ad79ad..528c71e652 100644 --- a/src/app_service/service/transaction/dto.nim +++ b/src/app_service/service/transaction/dto.nim @@ -1,4 +1,4 @@ -import json, strutils, stint +import json, strutils, stint, json_serialization include ../../common/json_utils type @@ -12,6 +12,20 @@ type proc event*(self:PendingTransactionTypeDto):string = result = "transaction:" & $self +type + MultiTransactionType* = enum + MultiTransactionSend = 0, MultiTransactionSwap = 1, MultiTransactionBridge = 2 + +type MultiTransactionDto* = ref object of RootObj + id* {.serializedFieldName("id").}: int + timestamp* {.serializedFieldName("timestamp").}: int + fromAddress* {.serializedFieldName("fromAddress").}: string + toAddress* {.serializedFieldName("toAddress").}: string + fromAsset* {.serializedFieldName("fromAsset").}: string + toAsset* {.serializedFieldName("toAsset").}: string + fromAmount* {.serializedFieldName("fromAmount").}: string + multiTxtype* {.serializedFieldName("type").}: MultiTransactionType + type TransactionDto* = ref object of RootObj id*: string diff --git a/src/app_service/service/transaction/service.nim b/src/app_service/service/transaction/service.nim index c9e22d7bdc..2341a0a554 100644 --- a/src/app_service/service/transaction/service.nim +++ b/src/app_service/service/transaction/service.nim @@ -271,21 +271,24 @@ QtObject: eth_utils.validateTransactionInput(from_addr, to_addr, assetAddress = "", value, gas, gasPrice, data = "", eip1559Enabled, maxPriorityFeePerGas, maxFeePerGas, uuid) - # TODO move this to another thread var tx = ens_utils.buildTransaction(parseAddress(from_addr), eth2Wei(parseFloat(value), 18), gas, gasPrice, eip1559Enabled, maxPriorityFeePerGas, maxFeePerGas) tx.to = parseAddress(to_addr).some - - let json: JsonNode = %tx - let response = eth.sendTransaction(parseInt(chainId), $json, password) - let output = %* { "result": response.result.getStr, "success": %(response.error.isNil), "uuid": %uuid } - self.events.emit(SIGNAL_TRANSACTION_SENT, TransactionSentArgs(result: $output)) - - # TODO ADD READ CHAIN - self.trackPendingTransaction( - response.result.getStr, from_addr, to_addr, - $PendingTransactionTypeDto.WalletTransfer, data = "", 1 + + let response = transactions.createMultiTransaction( + MultiTransactionDto( + fromAddress: from_addr, + toAddress: to_addr, + fromAsset: "ETH", + toAsset: "ETH", + fromAmount: "0x" & tx.value.unsafeGet.toHex, + multiTxtype: MultiTransactionType.MultiTransactionSend, + ), + {chainId: @[tx]}.toTable, + password, ) + let output = %* { "result": response.result{"hashes"}{chainId}[0].getStr, "success": %(response.error.isNil), "uuid": %uuid } + self.events.emit(SIGNAL_TRANSACTION_SENT, TransactionSentArgs(result: $output)) except Exception as e: error "Error sending eth transfer transaction", msg = e.msg return false @@ -316,20 +319,29 @@ QtObject: var tx = ens_utils.buildTokenTransaction(parseAddress(from_addr), token.address, gas, gasPrice, eip1559Enabled, maxPriorityFeePerGas, maxFeePerGas) - var success: bool - let transfer = Transfer(to: parseAddress(to_addr), - value: conversion.eth2Wei(parseFloat(value), token.decimals)) - let transferproc = ERC20_procS.toTable["transfer"] - let response = transferproc.send(parseInt(chainId), tx, transfer, password, success) - let txHash = response.result.getStr - let output = %* { "result": txHash, "success": %success, "uuid": %uuid } - self.events.emit(SIGNAL_TRANSACTION_SENT, TransactionSentArgs(result: $output)) - self.trackPendingTransaction( - txHash, from_addr, to_addr, - $PendingTransactionTypeDto.WalletTransfer, data = "", - network.chainId + let weiValue = conversion.eth2Wei(parseFloat(value), token.decimals) + let transfer = Transfer( + to: parseAddress(to_addr), + value: weiValue, ) + + tx.data = ERC20_procS.toTable["transfer"].encodeAbi(transfer) + let response = transactions.createMultiTransaction( + MultiTransactionDto( + fromAddress: from_addr, + toAddress: to_addr, + fromAsset: tokenSymbol, + toAsset: tokenSymbol, + fromAmount: "0x" & weiValue.toHex, + multiTxtype: MultiTransactionType.MultiTransactionSend, + ), + {chainId: @[tx]}.toTable, + password, + ) + let txHash = response.result.getStr + let output = %* { "result": response.result{"hashes"}{chainId}[0].getStr, "success":true, "uuid": %uuid } + self.events.emit(SIGNAL_TRANSACTION_SENT, TransactionSentArgs(result: $output)) except Exception as e: error "Error sending token transfer transaction", msg = e.msg return false diff --git a/src/backend/transactions.nim b/src/backend/transactions.nim index 2eea7d8c1a..08d2166baf 100644 --- a/src/backend/transactions.nim +++ b/src/backend/transactions.nim @@ -1,5 +1,7 @@ -import json, stint, chronicles +import Tables, json, stint, chronicles, nimcrypto +import ../app_service/service/transaction/dto +import ../app_service/service/eth/dto/transaction import ./core as core proc checkRecentHistory*(chainIds: seq[int], addresses: seq[string]) {.raises: [Exception].} = @@ -43,3 +45,7 @@ proc getPendingOutboundTransactionsByAddress*(chainIds: seq[int], address: strin proc fetchCryptoServices*(): RpcResponse[JsonNode] {.raises: [Exception].} = result = core.callPrivateRPC("wallet_getCryptoOnRamps", %* []) +proc createMultiTransaction*(multiTransaction: MultiTransactionDto, data: Table[string, seq[TransactionDataDto]], password: string): RpcResponse[JsonNode] {.raises: [Exception].} = + var hashed_password = "0x" & $keccak_256.digest(password) + let payload = %* [multiTransaction, data, hashed_password] + result = core.callPrivateRPC("wallet_createMultiTransaction", payload) \ No newline at end of file diff --git a/vendor/status-go b/vendor/status-go index b2ce92fd41..ce0caa0f7f 160000 --- a/vendor/status-go +++ b/vendor/status-go @@ -1 +1 @@ -Subproject commit b2ce92fd41a3b899749e2ed2ebd94ec8d4e1859f +Subproject commit ce0caa0f7f2a351abe3330b57fae903eb624e9ce