chore: move gasLimit buffer calculation to utils (#4482)

drive-by: move gasLimit buffer calculation to utils
dan/token-config-schema
Paul Balaji 2 months ago committed by GitHub
parent 741477a0b1
commit 291c5fe36a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 6
      .changeset/gorgeous-panthers-switch.md
  2. 5
      .changeset/quick-students-cover.md
  3. 5
      typescript/helloworld/src/app/app.ts
  4. 5
      typescript/helloworld/src/multiProtocolApp/evmAdapter.ts
  5. 9
      typescript/infra/src/govern/multisend.ts
  6. 10
      typescript/sdk/src/deploy/EvmModuleDeployer.ts
  7. 5
      typescript/sdk/src/deploy/HyperlaneDeployer.ts
  8. 5
      typescript/sdk/src/ism/EvmIsmModule.ts
  9. 13
      typescript/sdk/src/ism/HyperlaneIsmFactory.ts
  10. 11
      typescript/sdk/src/providers/MultiProvider.ts
  11. 3
      typescript/sdk/src/router/HyperlaneRouterDeployer.ts
  12. 18
      typescript/utils/src/amount.ts
  13. 1
      typescript/utils/src/index.ts

@ -0,0 +1,6 @@
---
'@hyperlane-xyz/helloworld': minor
'@hyperlane-xyz/sdk': minor
---
Use addBufferToGasLimit from @hyperlane-xyz/utils

@ -0,0 +1,5 @@
---
'@hyperlane-xyz/utils': minor
---
Add addBufferToGasLimit for gas limit buffer calculations

@ -9,7 +9,7 @@ import {
MultiProvider,
RouterApp,
} from '@hyperlane-xyz/sdk';
import { Address, rootLogger } from '@hyperlane-xyz/utils';
import { Address, addBufferToGasLimit, rootLogger } from '@hyperlane-xyz/utils';
import { HelloWorld } from '../types/index.js';
@ -52,12 +52,11 @@ export class HelloWorldApp extends RouterApp<HelloWorldFactories> {
message,
{ ...transactionOverrides, value },
);
const gasLimit = estimated.mul(12).div(10);
const quote = await sender.quoteDispatch(toDomain, message);
const tx = await sender.sendHelloWorld(toDomain, message, {
gasLimit: addBufferToGasLimit(estimated),
...transactionOverrides,
gasLimit,
value: value.add(quote),
});
this.logger.info('Sending hello message', {

@ -7,7 +7,7 @@ import {
MultiProtocolProvider,
ProviderType,
} from '@hyperlane-xyz/sdk';
import { Address } from '@hyperlane-xyz/utils';
import { Address, addBufferToGasLimit } from '@hyperlane-xyz/utils';
import { HelloWorld, HelloWorld__factory } from '../types/index.js';
@ -54,14 +54,13 @@ export class EvmHelloWorldAdapter
value: BigNumber.from(value).add(quote),
},
);
const gasLimit = estimated.mul(12).div(10);
const tx = await contract.populateTransaction.sendHelloWorld(
toDomain,
message,
{
gasLimit: addBufferToGasLimit(estimated),
...transactionOverrides,
gasLimit,
value: BigNumber.from(value).add(quote),
},
);

@ -3,7 +3,12 @@ import Safe from '@safe-global/protocol-kit';
import { SafeTransaction } from '@safe-global/safe-core-sdk-types';
import { ChainName, MultiProvider } from '@hyperlane-xyz/sdk';
import { Address, CallData, eqAddress } from '@hyperlane-xyz/utils';
import {
Address,
CallData,
addBufferToGasLimit,
eqAddress,
} from '@hyperlane-xyz/utils';
import {
createSafeTransaction,
@ -28,7 +33,7 @@ export class SignerMultiSend extends MultiSend {
for (const call of calls) {
const estimate = await this.multiProvider.estimateGas(this.chain, call);
const receipt = await this.multiProvider.sendTransaction(this.chain, {
gasLimit: estimate.mul(11).div(10), // 10% buffer
gasLimit: addBufferToGasLimit(estimate),
...call,
});
console.log(`confirmed tx ${receipt.transactionHash}`);

@ -7,7 +7,7 @@ import {
TransparentUpgradeableProxy__factory,
} from '@hyperlane-xyz/core';
import { buildArtifact as coreBuildArtifact } from '@hyperlane-xyz/core/buildArtifact.js';
import { Address, rootLogger } from '@hyperlane-xyz/utils';
import { Address, addBufferToGasLimit, rootLogger } from '@hyperlane-xyz/utils';
import { HyperlaneContracts, HyperlaneFactories } from '../contracts/types.js';
import { MultiProvider } from '../providers/MultiProvider.js';
@ -74,10 +74,10 @@ export class EvmModuleDeployer<Factories extends HyperlaneFactories> {
...initializeArgs,
);
// deploy with 10% buffer on gas limit
// deploy with buffer on gas limit
const overrides = this.multiProvider.getTransactionOverrides(chain);
const initTx = await contract.initialize(...initializeArgs, {
gasLimit: estimatedGas.add(estimatedGas.div(10)),
gasLimit: addBufferToGasLimit(estimatedGas),
...overrides,
});
@ -284,13 +284,13 @@ export class EvmModuleDeployer<Factories extends HyperlaneFactories> {
overrides,
);
// add 10% buffer
// add gas buffer
const hash = await factory['deploy(address[],uint8)'](
sortedValues,
threshold,
{
gasLimit: addBufferToGasLimit(estimatedGas),
...overrides,
gasLimit: estimatedGas.add(estimatedGas.div(10)), // 10% buffer
},
);

@ -15,6 +15,7 @@ import { buildArtifact as coreBuildArtifact } from '@hyperlane-xyz/core/buildArt
import {
Address,
ProtocolType,
addBufferToGasLimit,
eqAddress,
isZeroishAddress,
rootLogger,
@ -421,10 +422,10 @@ export abstract class HyperlaneDeployer<
...initializeArgs,
);
// deploy with 10% buffer on gas limit
// deploy with buffer on gas limit
const overrides = this.multiProvider.getTransactionOverrides(chain);
const initTx = await contract.initialize(...initializeArgs, {
gasLimit: estimatedGas.add(estimatedGas.div(10)),
gasLimit: addBufferToGasLimit(estimatedGas),
...overrides,
});
const receipt = await this.multiProvider.handleTx(chain, initTx);

@ -23,6 +23,7 @@ import {
Address,
Domain,
ProtocolType,
addBufferToGasLimit,
assert,
deepEquals,
eqAddress,
@ -538,14 +539,14 @@ export class EvmIsmModule extends HyperlaneModule<
overrides,
);
// deploying new domain routing ISM, add 10% buffer
// deploying new domain routing ISM, add gas buffer
const tx = await domainRoutingIsmFactory.deploy(
owner,
domainIds,
submoduleAddresses,
{
gasLimit: addBufferToGasLimit(estimatedGas),
...overrides,
gasLimit: estimatedGas.add(estimatedGas.div(10)), // 10% buffer
},
);

@ -24,6 +24,7 @@ import {
import {
Address,
Domain,
addBufferToGasLimit,
assert,
eqAddress,
objFilter,
@ -400,14 +401,14 @@ export class HyperlaneIsmFactory extends HyperlaneApp<ProxyFactoryFactories> {
submoduleAddresses,
overrides,
);
// add 10% buffer
// add gas buffer
const tx = await domainRoutingIsmFactory.deploy(
owner,
safeConfigDomains,
submoduleAddresses,
{
gasLimit: addBufferToGasLimit(estimatedGas),
...overrides,
gasLimit: estimatedGas.add(estimatedGas.div(10)), // 10% buffer
},
);
// TODO: Should verify contract here
@ -496,10 +497,10 @@ export class HyperlaneIsmFactory extends HyperlaneApp<ProxyFactoryFactories> {
threshold,
overrides,
);
// add 10% buffer
// add gas buffer
const hash = await factory['deploy(address[],uint8)'](sorted, threshold, {
gasLimit: addBufferToGasLimit(estimatedGas),
...overrides,
gasLimit: estimatedGas.add(estimatedGas.div(10)), // 10% buffer
});
await this.multiProvider.handleTx(chain, hash);
@ -536,13 +537,13 @@ export class HyperlaneIsmFactory extends HyperlaneApp<ProxyFactoryFactories> {
const estimatedGas = await factory.estimateGas[
'deploy((address,uint96)[],uint96)'
](sorted, thresholdWeight, overrides);
// add 10% buffer
// add gas buffer
const hash = await factory['deploy((address,uint96)[],uint96)'](
sorted,
thresholdWeight,
{
gasLimit: addBufferToGasLimit(estimatedGas),
...overrides,
gasLimit: estimatedGas.add(estimatedGas.div(10)), // 10% buffer
},
);

@ -9,7 +9,12 @@ import {
} from 'ethers';
import { Logger } from 'pino';
import { Address, pick, rootLogger } from '@hyperlane-xyz/utils';
import {
Address,
addBufferToGasLimit,
pick,
rootLogger,
} from '@hyperlane-xyz/utils';
import { testChainMetadata, testChains } from '../consts/testChains.js';
import { ChainMetadataManager } from '../metadata/ChainMetadataManager.js';
@ -317,9 +322,9 @@ export class MultiProvider<MetaExt = {}> extends ChainMetadataManager<MetaExt> {
const deployTx = contractFactory.getDeployTransaction(...params);
const gasEstimated = await signer.estimateGas(deployTx);
// deploy with 10% buffer on gas limit
// deploy with buffer on gas limit
const contract = await contractFactory.deploy(...params, {
gasLimit: gasEstimated.add(gasEstimated.div(10)), // 10% buffer
gasLimit: addBufferToGasLimit(gasEstimated),
...overrides,
});

@ -1,6 +1,7 @@
import { Ownable, Router } from '@hyperlane-xyz/core';
import {
Address,
addBufferToGasLimit,
addressToBytes32,
objFilter,
objMap,
@ -90,7 +91,7 @@ export abstract class HyperlaneRouterDeployer<
);
// deploy with 10% buffer on gas limit
const enrollTx = await router.enrollRemoteRouters(domains, addresses, {
gasLimit: estimatedGas.add(estimatedGas.div(10)),
gasLimit: addBufferToGasLimit(estimatedGas),
...this.multiProvider.getTransactionOverrides(chain),
});
await this.multiProvider.handleTx(chain, enrollTx);

@ -1,5 +1,6 @@
import { formatUnits, parseUnits } from '@ethersproject/units';
import { BigNumber } from 'bignumber.js';
import { ethers } from 'ethers';
const DEFAULT_DISPLAY_DECIMALS = 4;
const DEFAULT_TOKEN_DECIMALS = 18;
@ -131,3 +132,20 @@ export function convertDecimals(
return amount.times(BigNumber(10).pow(difference)).toString(10);
}
}
// Default gas limit buffer percentage
const DEFAULT_GAS_LIMIT_BUFFER_PERCENT = 10;
/**
* Calculates the gas limit with a buffer added to the estimated gas.
* @param estimatedGas The estimated gas for the transaction.
* @param bufferPercent The percentage to add as a buffer (default: 10%).
* @returns The calculated gas limit with the buffer added.
*/
export function addBufferToGasLimit(
estimatedGas: ethers.BigNumber,
bufferPercent: number = DEFAULT_GAS_LIMIT_BUFFER_PERCENT,
): ethers.BigNumber {
const bufferMultiplier = 100 + bufferPercent;
return estimatedGas.mul(bufferMultiplier).div(100);
}

@ -39,6 +39,7 @@ export {
strip0x,
} from './addresses.js';
export {
addBufferToGasLimit,
convertDecimals,
eqAmountApproximate,
fromWei,

Loading…
Cancel
Save