Skip to content

Commit

Permalink
feat(@desktop/communitytokens): Add l1 fees to community token estima…
Browse files Browse the repository at this point in the history
…tions

There is only one status-go call for all fees: suggested fees, gas and l1 fee.

Issue #14166
  • Loading branch information
endulab committed May 7, 2024
1 parent 4313176 commit 1e22b9e
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 36 deletions.
4 changes: 4 additions & 0 deletions src/app_service/common/conversion.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
59 changes: 29 additions & 30 deletions src/app_service/service/community_tokens/async_tasks.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -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),
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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,
Expand Down
11 changes: 5 additions & 6 deletions src/app_service/service/community_tokens/service.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
10 changes: 10 additions & 0 deletions src/app_service/service/ens/utils.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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 = ""
Expand Down

0 comments on commit 1e22b9e

Please sign in to comment.