diff --git a/src/RelayClient.ts b/src/RelayClient.ts index 521bca2..79fd654 100644 --- a/src/RelayClient.ts +++ b/src/RelayClient.ts @@ -482,8 +482,7 @@ class RelayClient extends EnvelopingEventEmitter { log.debug( `Relay Client - Relay Hub:${envelopingTx.metadata.relayHubAddress.toString()}` ); - - console.log(envelopingTx.metadata); + log.debug('Relay Client - Request metadata:', envelopingTx.metadata); return await this._httpClient.estimateMaxPossibleGas( url.toString(), diff --git a/src/gasEstimator/gasEstimator.ts b/src/gasEstimator/gasEstimator.ts index 07ce081..736e6da 100644 --- a/src/gasEstimator/gasEstimator.ts +++ b/src/gasEstimator/gasEstimator.ts @@ -55,6 +55,21 @@ const estimateRelayMaxPossibleGas = async ( return maxPossibleGas.add(tokenEstimation); }; +/** + * We try to estimation the maximum possible gas that the transaction will consume + * without requiring the signature of the user. The idea is to apply 2 different strategies + * according to the type of request: + * - deploy: the server signs the request and estimates the gas of the deployment with a static call + * - relay: we split the requests into different stages + * - preEnvelopedTxEstimation: the gas required without the payment and the internal call + * - paymentEstimation: the gas required for the payment + * - internalEstimation: the gas required for the internal call + * - postEnvelopedTxEstimation: the gas required after the internal call. + * @param relayRequest + * @param signer + * @param options + * @returns + */ const estimateRelayMaxPossibleGasNoSignature = async ( relayRequest: EnvelopingRequest, signer: Wallet, @@ -72,8 +87,8 @@ const estimateRelayMaxPossibleGasNoSignature = async ( } = relayRequest; const isDeploy = isDeployRequest(relayRequest); - let relayEstimation = BigNumber.from(PRE_RELAY_GAS_COST); - let reduction = POST_DEPLOY_GAS_COST; + let preEnvelopedTxEstimation = BigNumber.from(PRE_RELAY_GAS_COST); + let postEnvelopedTxEstimation = POST_RELAY_GAS_COST; if (isDeploy) { const from = signer.address; const updatedRelayRequest = { @@ -93,13 +108,14 @@ const estimateRelayMaxPossibleGasNoSignature = async ( provider ); - relayEstimation = await relayHub.estimateGas.deployCall( + preEnvelopedTxEstimation = await relayHub.estimateGas.deployCall( updatedRelayRequest, signature, { gasPrice, from } ); - reduction = POST_RELAY_GAS_COST; + // TODO: Are we sure we need this? + postEnvelopedTxEstimation = POST_DEPLOY_GAS_COST; } const smartWalletAddress = await resolveSmartWalletAddress( @@ -107,7 +123,7 @@ const estimateRelayMaxPossibleGasNoSignature = async ( options ); - const tokenEstimation = await estimatePaymentGas({ + const paymentEstimation = await estimatePaymentGas({ relayRequest: { ...relayRequest, request: { @@ -131,12 +147,10 @@ const estimateRelayMaxPossibleGasNoSignature = async ( internalEstimation = BigNumber.from(POST_DEPLOY_GAS_COST_NO_EXECUTION); } - return relayEstimation - .add(tokenEstimation) + return preEnvelopedTxEstimation + .add(paymentEstimation) .add(internalEstimation) - .add(POST_DEPLOY_GAS_COST) - .add(POST_RELAY_GAS_COST) - .sub(reduction); + .add(postEnvelopedTxEstimation); }; export { estimateRelayMaxPossibleGas, estimateRelayMaxPossibleGasNoSignature };