From 93f62a16725e5330924aeb8e3794bf781a75a57d Mon Sep 17 00:00:00 2001 From: Michal Iskierko Date: Thu, 18 Apr 2024 12:09:23 +0200 Subject: [PATCH] feat(@desktop/communitytokens): Add l1 fees to community token estimations There is only one status-go call for all fees: suggested fees, gas and l1 fee. Issue #14166 --- src/app_service/common/conversion.nim | 4 ++ .../service/community_tokens/async_tasks.nim | 59 +++++++++---------- .../service/community_tokens/service.nim | 11 ++-- src/app_service/service/ens/utils.nim | 10 ++++ 4 files changed, 48 insertions(+), 36 deletions(-) diff --git a/src/app_service/common/conversion.nim b/src/app_service/common/conversion.nim index 0122a3a633..36bb162bf4 100644 --- a/src/app_service/common/conversion.nim +++ b/src/app_service/common/conversion.nim @@ -61,6 +61,10 @@ proc wei2Eth*(input: Stuint[256], decimals: int = 18): string = fmt"{eth}.{leading_zeros}{remainder}" +proc gwei2Eth*(gwei: float): string = + let weis = gwei2Wei(gwei) + return wei2Eth(weis) + proc wei2Eth*(input: string, decimals: int): string = try: var input256: Stuint[256] diff --git a/src/app_service/service/community_tokens/async_tasks.nim b/src/app_service/service/community_tokens/async_tasks.nim index 54e379c332..e40c9c5a86 100644 --- a/src/app_service/service/community_tokens/async_tasks.nim +++ b/src/app_service/service/community_tokens/async_tasks.nim @@ -41,11 +41,11 @@ const asyncGetDeployOwnerContractsFeesTask: Task = proc(argEncoded: string) {.gc try: var gasTable: Table[ContractTuple, int] # gas per contract var feeTable: Table[int, SuggestedFeesDto] # fees for chain - let response = eth.suggestedFees(arg.chainId).result - feeTable[arg.chainId] = response.toSuggestedFeesDto() - let deployGas = community_tokens.deployOwnerTokenEstimate(arg.chainId, arg.addressFrom, arg.ownerParams, arg.masterParams, arg.communityId, arg.signerPubKey).result.getInt - gasTable[(arg.chainId, "")] = deployGas + let estimations = community_tokens.deployOwnerTokenEstimate(arg.chainId, arg.addressFrom, arg.ownerParams, arg.masterParams, arg.communityId, arg.signerPubKey).result + gasTable[(arg.chainId, "")] = estimations{"gasUnits"}.getInt + feeTable[arg.chainId] = estimations{"suggestedFees"}.toSuggestedFeesDto() + arg.finish(%* { "feeTable": tableToJsonArray(feeTable), "gasTable": tableToJsonArray(gasTable), @@ -72,12 +72,12 @@ const asyncGetDeployFeesTask: Task = proc(argEncoded: string) {.gcsafe, nimcall. try: var gasTable: Table[ContractTuple, int] # gas per contract var feeTable: Table[int, SuggestedFeesDto] # fees for chain - let response = eth.suggestedFees(arg.chainId).result - feeTable[arg.chainId] = response.toSuggestedFeesDto() - let deployGas = if arg.tokenType == TokenType.ERC721: community_tokens.deployCollectiblesEstimate(arg.chainId, arg.addressFrom).result.getInt - else: community_tokens.deployAssetsEstimate(arg.chainId, arg.addressFrom).result.getInt - - gasTable[(arg.chainId, "")] = deployGas + + let estimations = if arg.tokenType == TokenType.ERC721: community_tokens.deployCollectiblesEstimate(arg.chainId, arg.addressFrom).result + else: community_tokens.deployAssetsEstimate(arg.chainId, arg.addressFrom).result + gasTable[(arg.chainId, "")] = estimations{"gasUnits"}.getInt + feeTable[arg.chainId] = estimations{"suggestedFees"}.toSuggestedFeesDto() + arg.finish(%* { "feeTable": tableToJsonArray(feeTable), "gasTable": tableToJsonArray(gasTable), @@ -105,10 +105,11 @@ const asyncSetSignerFeesTask: Task = proc(argEncoded: string) {.gcsafe, nimcall. try: var gasTable: Table[ContractTuple, int] # gas per contract var feeTable: Table[int, SuggestedFeesDto] # fees for chain - let response = eth.suggestedFees(arg.chainId).result - feeTable[arg.chainId] = response.toSuggestedFeesDto() - let gasUsed = community_tokens.estimateSetSignerPubKey(arg.chainId, arg.contractAddress, arg.addressFrom, arg.newSignerPubKey).result.getInt - gasTable[(arg.chainId, "")] = gasUsed + + let estimations = community_tokens.estimateSetSignerPubKey(arg.chainId, arg.contractAddress, arg.addressFrom, arg.newSignerPubKey).result + gasTable[(arg.chainId, arg.contractAddress)] = estimations{"gasUnits"}.getInt + feeTable[arg.chainId] = estimations{"suggestedFees"}.toSuggestedFeesDto() + arg.finish(%* { "feeTable": tableToJsonArray(feeTable), "gasTable": tableToJsonArray(gasTable), @@ -136,10 +137,11 @@ const asyncGetRemoteBurnFeesTask: Task = proc(argEncoded: string) {.gcsafe, nimc try: var gasTable: Table[ContractTuple, int] # gas per contract var feeTable: Table[int, SuggestedFeesDto] # fees for chain - let fee = eth.suggestedFees(arg.chainId).result.toSuggestedFeesDto() - let burnGas = community_tokens.estimateRemoteBurn(arg.chainId, arg.contractAddress, arg.addressFrom, arg.tokenIds).result.getInt - feeTable[arg.chainId] = fee - gasTable[(arg.chainId, arg.contractAddress)] = burnGas + + let estimations = community_tokens.estimateRemoteBurn(arg.chainId, arg.contractAddress, arg.addressFrom, arg.tokenIds).result + gasTable[(arg.chainId, arg.contractAddress)] = estimations{"gasUnits"}.getInt + feeTable[arg.chainId] = estimations{"suggestedFees"}.toSuggestedFeesDto() + arg.finish(%* { "feeTable": tableToJsonArray(feeTable), "gasTable": tableToJsonArray(gasTable), @@ -167,10 +169,11 @@ const asyncGetBurnFeesTask: Task = proc(argEncoded: string) {.gcsafe, nimcall.} try: var gasTable: Table[ContractTuple, int] # gas per contract var feeTable: Table[int, SuggestedFeesDto] # fees for chain - let fee = eth.suggestedFees(arg.chainId).result.toSuggestedFeesDto() - let burnGas = community_tokens.estimateBurn(arg.chainId, arg.contractAddress, arg.addressFrom, arg.amount).result.getInt - feeTable[arg.chainId] = fee - gasTable[(arg.chainId, arg.contractAddress)] = burnGas + + let estimations = community_tokens.estimateBurn(arg.chainId, arg.contractAddress, arg.addressFrom, arg.amount).result + gasTable[(arg.chainId, arg.contractAddress)] = estimations{"gasUnits"}.getInt + feeTable[arg.chainId] = estimations{"suggestedFees"}.toSuggestedFeesDto() + arg.finish(%* { "feeTable": tableToJsonArray(feeTable), "gasTable": tableToJsonArray(gasTable), @@ -200,15 +203,12 @@ const asyncGetMintFeesTask: Task = proc(argEncoded: string) {.gcsafe, nimcall.} for collectibleAndAmount in arg.collectiblesAndAmounts: # get fees if we do not have for this chain yet let chainId = collectibleAndAmount.communityToken.chainId - if not feeTable.hasKey(chainId): - let feesResponse = eth.suggestedFees(chainId).result - feeTable[chainId] = feesResponse.toSuggestedFeesDto() - # get gas for smart contract - let gas = community_tokens.estimateMintTokens(chainId, + let estimations = community_tokens.estimateMintTokens(chainId, collectibleAndAmount.communityToken.address, arg.addressFrom, - arg.walletAddresses, collectibleAndAmount.amount).result.getInt - gasTable[(chainId, collectibleAndAmount.communityToken.address)] = gas + arg.walletAddresses, collectibleAndAmount.amount).result + gasTable[(chainId, collectibleAndAmount.communityToken.address)] = estimations{"gasUnits"}.getInt + feeTable[chainId] = estimations{"suggestedFees"}.toSuggestedFeesDto() arg.finish(%* { "feeTable": tableToJsonArray(feeTable), "gasTable": tableToJsonArray(gasTable), @@ -311,7 +311,6 @@ const fetchAssetOwnersTaskArg: Task = proc(argEncoded: string) {.gcsafe, nimcall } arg.finish(output) except Exception as e: - echo "Exception", e.msg let output = %* { "chainId": arg.chainId, "contractAddress": arg.contractAddress, diff --git a/src/app_service/service/community_tokens/service.nim b/src/app_service/service/community_tokens/service.nim index 27a9e82e7a..414d8086ff 100644 --- a/src/app_service/service/community_tokens/service.nim +++ b/src/app_service/service/community_tokens/service.nim @@ -614,10 +614,7 @@ QtObject: if suggestedFees == nil: error "Can't find suggested fees for chainId", chainId=chainId return - return ens_utils.buildTransaction(parseAddress(addressFrom), 0.u256, $gasUnits, - if suggestedFees.eip1559Enabled: "" else: $suggestedFees.gasPrice, suggestedFees.eip1559Enabled, - if suggestedFees.eip1559Enabled: $suggestedFees.maxPriorityFeePerGas else: "", - if suggestedFees.eip1559Enabled: $suggestedFees.maxFeePerGasM else: "") + return ens_utils.buildTransactionDataDto(gasUnits, suggestedFees, addressFrom, chainId, contractAddress) proc temporaryMasterContractAddress*(ownerContractTransactionHash: string): string = return ownerContractTransactionHash & "-master" @@ -1083,13 +1080,15 @@ QtObject: let (ethCurrency, fiatCurrency) = self.create0CurrencyAmounts() return ComputeFeeArgs(ethCurrency: ethCurrency, fiatCurrency: fiatCurrency, errorCode: errorCode) + # Returns eth value with l1 fee included proc computeEthValue(self:Service, gasUnits: int, suggestedFees: SuggestedFeesDto): float = try: let maxFees = suggestedFees.maxFeePerGasM let gasPrice = if suggestedFees.eip1559Enabled: maxFees else: suggestedFees.gasPrice let weiValue = gwei2Wei(gasPrice) * gasUnits.u256 - let ethValueStr = wei2Eth(weiValue) + let l1FeeInWei = gwei2Wei(suggestedFees.l1GasFee) + let ethValueStr = wei2Eth(weiValue + l1FeeInWei) return parseFloat(ethValueStr) except Exception as e: error "Error computing eth value", msg = e.msg @@ -1115,7 +1114,7 @@ QtObject: proc createComputeFeeArgs(self: Service, gasUnits: int, suggestedFees: SuggestedFeesDto, chainId: int, walletAddress: string): ComputeFeeArgs = let ethValue = self.computeEthValue(gasUnits, suggestedFees) let balance = self.getWalletBalanceForChain(walletAddress, chainId) - debug "computing fees", walletBalance=balance, ethValue=ethValue + debug "computing fees", walletBalance=balance, ethValueWithL1Fee=ethValue, l1Fee=gwei2Eth(suggestedFees.l1GasFee) return self.createComputeFeeArgsFromEthAndBalance(ethValue, balance) # convert json returned from async task into gas table diff --git a/src/app_service/service/ens/utils.nim b/src/app_service/service/ens/utils.nim index 5b7043729b..0459fc7a61 100644 --- a/src/app_service/service/ens/utils.nim +++ b/src/app_service/service/ens/utils.nim @@ -7,6 +7,7 @@ import ../eth/dto/transaction as eth_transaction_dto import ../../../backend/ens as status_ens import ../../common/account_constants import ../../common/utils +import ../../service/transaction/dto logScope: topics = "ens-utils" @@ -87,6 +88,15 @@ proc buildTransaction*( else: result.txType = "0x00" +proc buildTransactionDataDto*(gasUnits: int, suggestedFees: SuggestedFeesDto, addressFrom: string, chainId: int, contractAddress: string): TransactionDataDto = + if suggestedFees == nil: + error "Can't find suggested fees for chainId", chainId=chainId + return + return buildTransaction(parseAddress(addressFrom), 0.u256, $gasUnits, + if suggestedFees.eip1559Enabled: "" else: $suggestedFees.gasPrice, suggestedFees.eip1559Enabled, + if suggestedFees.eip1559Enabled: $suggestedFees.maxPriorityFeePerGas else: "", + if suggestedFees.eip1559Enabled: $suggestedFees.maxFeePerGasM else: "") + proc buildTokenTransaction*( source, contractAddress: Address, gas = "", gasPrice = "", isEIP1559Enabled = false, maxPriorityFeePerGas = "", maxFeePerGas = ""