Skip to content

Commit

Permalink
feat: estimation no signature
Browse files Browse the repository at this point in the history
  • Loading branch information
franciscotobar committed Aug 27, 2024
1 parent 2b67a82 commit 7d9751d
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/gasEstimator/gasEstimator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
} from './utils';
import type { RelayTxOptions } from 'src/common';

//TODO: add validation where tokenAmount is zero
const estimateRelayMaxPossibleGas = async (
envelopingRequest: EnvelopingTxRequest,
relayWorkerAddress: string,
Expand Down
111 changes: 111 additions & 0 deletions src/gasEstimator/gasEstimatorNoSignature.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
import type { Wallet } from 'ethers';
import { BigNumber, constants, utils } from 'ethers';
import { RelayHub__factory } from '@rsksmart/rif-relay-contracts';
import type { EnvelopingRequest } from '../common/relayRequest.types';
import {
/* applyGasCorrectionFactor,
ESTIMATED_GAS_CORRECTION_FACTOR, */
estimateInternalCallGas,
estimatePaymentGas,
getSmartWalletAddress,
} from '../utils';
import { getProvider } from '../common/clientConfigurator';
import AccountManager from '../AccountManager';
import { isDeployRequest, RelayTxOptions } from '../common';

const noSignatureMaxPossibleGasEstimation = async (
envelopingRequest: EnvelopingRequest,
signer: Wallet,
options?: RelayTxOptions
) => {
const {
request,
relayData: { gasPrice },
} = envelopingRequest;

const tokenAmount = await request.tokenAmount;

if (!BigNumber.from(tokenAmount).isZero()) {
throw Error(
'Max Possible Gas Estimation request needs to have token amount equals to 0'
);
}

const isSmartWalletDeploy = isDeployRequest(envelopingRequest);

let relayEstimation = BigNumber.from(70000);
if (isSmartWalletDeploy) {
const updatedRelayRequest = {
request: {
...envelopingRequest.request,
from: signer.address,
to: constants.AddressZero,
},
relayData: {
...envelopingRequest.relayData,
},
};

const provider = getProvider();

const relayHub = RelayHub__factory.connect(
await request.relayHub,
provider
);

const accountManager = AccountManager.getInstance();

const signature = accountManager.sign(updatedRelayRequest, signer);

relayEstimation = await relayHub.estimateGas.deployCall(
updatedRelayRequest,
signature,
{ gasPrice, from: signer.address }
);
}

const { from, index, recoverer, to, data } = envelopingRequest.request;
const smartWalletIndex = await index;

const callForwarder = envelopingRequest.relayData.callForwarder.toString();

const isCustom = options?.isCustom;
const preDeploySWAddress = isSmartWalletDeploy
? await getSmartWalletAddress({
owner: await from,
smartWalletIndex: smartWalletIndex!,

Check warning on line 76 in src/gasEstimator/gasEstimatorNoSignature.ts

View workflow job for this annotation

GitHub Actions / lint_and_test

Forbidden non-null assertion
recoverer: await recoverer,
to: await to,
data: await data,
factoryAddress: callForwarder,
isCustom,
})
: undefined;

const tokenEstimation = await estimatePaymentGas({
relayRequest: {
...envelopingRequest,
request: {
...envelopingRequest.request,
tokenAmount: utils.formatUnits(1, 'wei'),
},
},
preDeploySWAddress,
});

let internalEstimation = BigNumber.from(0);
if (to != constants.AddressZero) {
internalEstimation = await estimateInternalCallGas({
data,
from: preDeploySWAddress ?? callForwarder,
to,
gasPrice,
});
}

//TODO: decide how to handle the transfer back around 35k

return relayEstimation.add(tokenEstimation).add(internalEstimation);
};

export { noSignatureMaxPossibleGasEstimation };
1 change: 1 addition & 0 deletions src/gasEstimator/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ export {
standardMaxPossibleGasEstimation,
linearFitMaxPossibleGasEstimation,
} from './utils';
export { noSignatureMaxPossibleGasEstimation } from './gasEstimatorNoSignature';
1 change: 1 addition & 0 deletions src/gasEstimator/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ const standardMaxPossibleGasEstimation = async (
return correctedEstimation;
};

// TODO: fix the from, the from needs to be the smart wallet address
const linearFitMaxPossibleGasEstimation = async (
envelopingRequest: EnvelopingRequest,
tokenEstimation: BigNumber,
Expand Down
2 changes: 1 addition & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ const estimatePaymentGas = async ({
// This may be wrong in case the feesReceiver performs some operations using
// the msg.sender address in the fallback/receive function.
return await estimateInternalCallGas({
from: feesReceiver,
from: tokenOrigin,
to: feesReceiver,
gasPrice,
value: tokenAmount,
Expand Down

0 comments on commit 7d9751d

Please sign in to comment.