-
Notifications
You must be signed in to change notification settings - Fork 457
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: always estimate relay
gasLimit
(#4679)
- Loading branch information
Showing
8 changed files
with
127 additions
and
18 deletions.
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import { faker } from '@faker-js/faker' | ||
import { relayTransaction as _relayTransaction } from '@safe-global/safe-gateway-typescript-sdk' | ||
|
||
import { relayTransaction } from '@/services/tx/relaying' | ||
import { getWeb3ReadOnly } from '@/hooks/wallets/web3' | ||
|
||
jest.mock('@/hooks/wallets/web3') | ||
jest.mock('@safe-global/safe-gateway-typescript-sdk') | ||
|
||
describe('relayTransaction', () => { | ||
let chainId: string | ||
let body: { | ||
version: string | ||
to: string | ||
data: string | ||
gasLimit: string | undefined | ||
} | ||
|
||
beforeEach(() => { | ||
jest.clearAllMocks() | ||
|
||
chainId = faker.string.numeric() | ||
body = { | ||
version: faker.system.semver(), | ||
to: faker.finance.ethereumAddress(), | ||
data: faker.string.hexadecimal(), | ||
gasLimit: undefined, | ||
} | ||
}) | ||
|
||
it('should relay with the correct parameters when gasLimit is provided', async () => { | ||
const bodyWithGasLimit = { | ||
...body, | ||
gasLimit: faker.string.numeric(), | ||
} | ||
|
||
await relayTransaction(chainId, bodyWithGasLimit) | ||
|
||
expect(_relayTransaction).toHaveBeenCalledWith(chainId, bodyWithGasLimit) | ||
}) | ||
|
||
it('should estimate gasLimit if not provided and relay with the estimated gasLimit', async () => { | ||
const gasLimit = faker.number.bigInt() | ||
const mockProvider = { | ||
estimateGas: jest.fn().mockResolvedValue(gasLimit), | ||
} | ||
;(getWeb3ReadOnly as jest.Mock).mockReturnValue(mockProvider) | ||
|
||
await relayTransaction(chainId, body) | ||
|
||
expect(mockProvider.estimateGas).toHaveBeenCalledWith(body) | ||
expect(_relayTransaction).toHaveBeenCalledWith(chainId, { ...body, gasLimit: gasLimit.toString() }) | ||
}) | ||
|
||
it('should relay with undefined gasLimit if estimation fails', async () => { | ||
const mockProvider = { | ||
estimateGas: jest.fn().mockRejectedValue(new Error('Estimation failed')), | ||
} | ||
;(getWeb3ReadOnly as jest.Mock).mockReturnValue(mockProvider) | ||
|
||
await relayTransaction(chainId, body) | ||
|
||
expect(mockProvider.estimateGas).toHaveBeenCalledWith(body) | ||
expect(_relayTransaction).toHaveBeenCalledWith(chainId, { ...body, gasLimit: undefined }) | ||
}) | ||
}) |
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,29 @@ | ||
import { relayTransaction as _relayTransaction } from '@safe-global/safe-gateway-typescript-sdk' | ||
|
||
import { getWeb3ReadOnly } from '@/hooks/wallets/web3' | ||
|
||
export async function relayTransaction( | ||
chainId: string, | ||
body: { version: string; to: string; data: string; gasLimit: string | undefined }, | ||
) { | ||
/** | ||
* Ensures `gasLimit` is estimate so 150k execution overhead buffer can be added by CGW. | ||
* | ||
* @see https://github.com/safe-global/safe-client-gateway/blob/b7640ed4bd7416ecb7696d21ba105fcb5af52a10/src/datasources/relay-api/gelato-api.service.ts#L62-L75 | ||
* | ||
* - If provided, CGW adds a 150k buffer before submission to Gelato. | ||
* - If not provided, no buffer is added, and Gelato estimates the it. | ||
* | ||
* Note: particularly important on networks like Arbitrum, where estimation is unreliable. | ||
*/ | ||
if (!body.gasLimit) { | ||
const provider = getWeb3ReadOnly() | ||
|
||
body.gasLimit = await provider | ||
?.estimateGas(body) | ||
.then(String) | ||
.catch(() => undefined) | ||
} | ||
|
||
return _relayTransaction(chainId, body) | ||
} |
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