Skip to content

Commit

Permalink
docs: explain what the method does and simplify the post tx gas estim…
Browse files Browse the repository at this point in the history
…ation
  • Loading branch information
antomor committed Aug 29, 2024
1 parent cdb8ea6 commit cea75ec
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 12 deletions.
3 changes: 1 addition & 2 deletions src/RelayClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
34 changes: 24 additions & 10 deletions src/gasEstimator/gasEstimator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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 = {
Expand All @@ -93,21 +108,22 @@ 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(
relayRequest,
options
);

const tokenEstimation = await estimatePaymentGas({
const paymentEstimation = await estimatePaymentGas({
relayRequest: {
...relayRequest,
request: {
Expand All @@ -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 };

0 comments on commit cea75ec

Please sign in to comment.