-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2b67a82
commit 7d9751d
Showing
5 changed files
with
115 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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!, | ||
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 }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters