Skip to content

Commit e82f98e

Browse files
authored
feat: adjust estimated gas for deposit shortcuts (#648)
For ACT-1394. Changes include: - Adjusting postHook default estimatedGas for swap-deposit txs - Using gas from simulation instead of swap provider for swap-deposit txs - Swaps use swap provider gas and ignores simulation results here for gas limit of the tx https://github.com/valora-inc/valora-rest-api/blob/ddf710202b63bfd9f02dc104b6aa812d64809c47/src/swaps/simulateSwap.ts#L156, this can be adjusted in swaps but for now trying this out in swap deposit - Adding 15% buffer to simulation results to be extra cautious - Replaces the static 250k padding for deposits, but that was actually needed because of an issue with negative gas refunds (fixed in valora-inc/valora-rest-api#1201) This can be monitored and adjusted based on these metrics: https://mixpanel.com/project/2786968/view/3321744/app/insights#vAou6TfFHjbW
1 parent 1e77bcb commit e82f98e

File tree

4 files changed

+23
-88
lines changed

4 files changed

+23
-88
lines changed

src/apps/aave/shortcuts.ts

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,8 @@ import { ChainType, SquidCallType } from '@0xsquid/squid-types'
2828
import { prepareSwapTransactions } from '../../utils/prepareSwapTransactions'
2929

3030
// Hardcoded fallback if simulation isn't enabled
31-
const GAS = 1_000_000n
32-
// Padding we add to simulation gas to ensure we specify enough
33-
const SIMULATED_DEPOSIT_GAS_PADDING = 250_000n
31+
const DEFAULT_DEPOSIT_GAS = 1_000_000n
32+
const DEFAULT_APPROVE_GAS = 200_000n
3433

3534
const hook: ShortcutsHook = {
3635
async getShortcutDefinitions(networkId: NetworkId, _address?: string) {
@@ -115,13 +114,12 @@ const hook: ShortcutsHook = {
115114
const supplySimulatedTx =
116115
simulatedTransactions[simulatedTransactions.length - 1]
117116

118-
supplyTx.gas =
119-
BigInt(supplySimulatedTx.gasNeeded) +
120-
SIMULATED_DEPOSIT_GAS_PADDING
117+
// 15% buffer on the estimation from the simulation
118+
supplyTx.gas = (BigInt(supplySimulatedTx.gasNeeded) * 115n) / 100n
121119
supplyTx.estimatedGasUse = BigInt(supplySimulatedTx.gasUsed)
122120
} catch (error) {
123-
supplyTx.gas = GAS
124-
supplyTx.estimatedGasUse = GAS / 3n
121+
supplyTx.gas = DEFAULT_DEPOSIT_GAS
122+
supplyTx.estimatedGasUse = DEFAULT_DEPOSIT_GAS / 3n
125123
}
126124

127125
return { transactions }
@@ -277,7 +275,7 @@ const hook: ShortcutsHook = {
277275
},
278276
// no native token transfer. this is optional per types, but squid request fails without it
279277
value: '0',
280-
estimatedGas: GAS.toString(),
278+
estimatedGas: DEFAULT_APPROVE_GAS.toString(),
281279
},
282280
{
283281
chainType: ChainType.EVM,
@@ -290,7 +288,7 @@ const hook: ShortcutsHook = {
290288
},
291289
// no native token transfer. this is optional per types, but squid request fails without it
292290
value: '0',
293-
estimatedGas: GAS.toString(),
291+
estimatedGas: DEFAULT_DEPOSIT_GAS.toString(),
294292
},
295293
],
296294
description: 'Deposit into aave pool',

src/apps/beefy/shortcuts.ts

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,7 @@ import { prepareSwapTransactions } from '../../utils/prepareSwapTransactions'
2222

2323
// Hardcoded fallback if simulation isn't enabled
2424
const DEFAULT_DEPOSIT_GAS = 750_000n
25-
// Padding we add to simulation gas to ensure we specify enough
26-
const SIMULATED_DEPOSIT_GAS_PADDING = 200_000n
25+
const DEFAULT_APPROVE_GAS = 200_000n
2726

2827
const hook: ShortcutsHook = {
2928
async getShortcutDefinitions(networkId: NetworkId) {
@@ -103,9 +102,8 @@ const hook: ShortcutsHook = {
103102
const supplySimulatedTx =
104103
simulatedTransactions[simulatedTransactions.length - 1]
105104

106-
depositTx.gas =
107-
BigInt(supplySimulatedTx.gasNeeded) +
108-
SIMULATED_DEPOSIT_GAS_PADDING
105+
// 15% buffer on the estimation from the simulation
106+
depositTx.gas = (BigInt(supplySimulatedTx.gasNeeded) * 115n) / 100n
109107
depositTx.estimatedGasUse = BigInt(supplySimulatedTx.gasUsed)
110108
} catch (error) {
111109
if (!(error instanceof UnsupportedSimulateRequest)) {
@@ -215,7 +213,6 @@ const hook: ShortcutsHook = {
215213
swapFromToken,
216214
swapToTokenAddress: tokenAddress,
217215
walletAddress,
218-
simulatedGasPadding: [0n, SIMULATED_DEPOSIT_GAS_PADDING],
219216
enableAppFee,
220217
// based off of https://docs.squidrouter.com/building-with-squid-v2/key-concepts/hooks/build-a-posthook
221218
postHook: {
@@ -232,7 +229,7 @@ const hook: ShortcutsHook = {
232229
},
233230
// no native token transfer. this is optional per types, but squid request fails without it
234231
value: '0',
235-
estimatedGas: DEFAULT_DEPOSIT_GAS.toString(),
232+
estimatedGas: DEFAULT_APPROVE_GAS.toString(),
236233
},
237234
{
238235
chainType: ChainType.EVM,

src/utils/prepareSwapTransactions.test.ts

Lines changed: 5 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,6 @@ describe('prepareSwapTransactions', () => {
8585
swapFromToken: mockNativeSwapFromToken,
8686
swapToTokenAddress: '0x724dc807b04555b71ed48a6896b6f41593b8c637',
8787
postHook: mockPostHook,
88-
simulatedGasPadding: [1n, 100n],
8988
})
9089

9190
expect(transactions).toHaveLength(1)
@@ -95,7 +94,7 @@ describe('prepareSwapTransactions', () => {
9594
to: '0x12345678',
9695
data: '0xswapdata',
9796
value: 111n,
98-
gas: 12345n,
97+
gas: 14042n,
9998
estimatedGasUse: 12211n,
10099
})
101100
expect(dataProps).toEqual({ swapTransaction })
@@ -111,19 +110,7 @@ describe('prepareSwapTransactions', () => {
111110
sellNetworkId: NetworkId['arbitrum-one'],
112111
sellAmount: (1e18).toString(),
113112
slippagePercentage: '1',
114-
postHook: {
115-
...mockPostHook,
116-
calls: [
117-
{
118-
...mockPostHook.calls[0],
119-
estimatedGas: '1000',
120-
},
121-
{
122-
...mockPostHook.calls[1],
123-
estimatedGas: '2000',
124-
},
125-
],
126-
},
113+
postHook: mockPostHook,
127114
userAddress: mockWalletAddress,
128115
},
129116
},
@@ -152,7 +139,7 @@ describe('prepareSwapTransactions', () => {
152139
to: '0x12345678',
153140
data: '0xswapdata',
154141
value: 111n,
155-
gas: 12345n,
142+
gas: 14042n,
156143
estimatedGasUse: 12211n,
157144
})
158145
expect(dataProps).toEqual({ swapTransaction })
@@ -169,19 +156,7 @@ describe('prepareSwapTransactions', () => {
169156
sellNetworkId: NetworkId['arbitrum-one'],
170157
sellAmount: (1e6).toString(),
171158
slippagePercentage: '1',
172-
postHook: {
173-
...mockPostHook,
174-
calls: [
175-
{
176-
...mockPostHook.calls[0],
177-
estimatedGas: '1000',
178-
},
179-
{
180-
...mockPostHook.calls[1],
181-
estimatedGas: '2000',
182-
},
183-
],
184-
},
159+
postHook: mockPostHook,
185160
userAddress: mockWalletAddress,
186161
},
187162
},
@@ -206,48 +181,9 @@ describe('prepareSwapTransactions', () => {
206181
to: '0x12345678',
207182
data: '0xswapdata',
208183
value: 111n,
209-
gas: 12345n,
210-
estimatedGasUse: 12211n,
211-
})
212-
})
213-
214-
it('uses default gas for postHook', async () => {
215-
const { transactions } = await prepareSwapTransactions({
216-
networkId: NetworkId['arbitrum-one'],
217-
walletAddress: '0x2b8441ef13333ffa955c9ea5ab5b3692da95260d',
218-
swapFromToken: mockNativeSwapFromToken,
219-
swapToTokenAddress: '0x724dc807b04555b71ed48a6896b6f41593b8c637',
220-
postHook: mockPostHook,
221-
simulatedGasPadding: [1n, 100n],
222-
})
223-
224-
expect(transactions).toHaveLength(1)
225-
expect(transactions[0]).toEqual({
226-
networkId: NetworkId['arbitrum-one'],
227-
from: mockWalletAddress,
228-
to: '0x12345678',
229-
data: '0xswapdata',
230-
value: 111n,
231-
gas: 12345n,
184+
gas: 14042n,
232185
estimatedGasUse: 12211n,
233186
})
234-
expect(got.post).toHaveBeenCalledTimes(1)
235-
expect(got.post).toHaveBeenCalledWith(
236-
'https://api.mainnet.valora.xyz/getSwapQuote',
237-
{
238-
json: {
239-
buyToken: '0x724dc807b04555b71ed48a6896b6f41593b8c637',
240-
buyIsNative: false,
241-
buyNetworkId: NetworkId['arbitrum-one'],
242-
sellIsNative: true,
243-
sellNetworkId: NetworkId['arbitrum-one'],
244-
sellAmount: (1e18).toString(),
245-
slippagePercentage: '1',
246-
postHook: mockPostHook,
247-
userAddress: mockWalletAddress,
248-
},
249-
},
250-
)
251187
})
252188

253189
it('throws if getting swap quote fails', async () => {

src/utils/prepareSwapTransactions.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ export async function prepareSwapTransactions({
4444
swapToTokenAddress: Address
4545
networkId: NetworkId
4646
walletAddress: Address
47-
simulatedGasPadding?: bigint[]
4847
enableAppFee?: boolean
4948
}): Promise<TriggerOutputShape<'swap-deposit'>> {
5049
const amountToSwap = parseUnits(swapFromToken.amount, swapFromToken.decimals)
@@ -137,7 +136,12 @@ export async function prepareSwapTransactions({
137136
to,
138137
data,
139138
value: BigInt(value),
140-
gas: BigInt(gas),
139+
// estimatedGasUse is from the simulation, gas is from the swap provider
140+
// add 15% padding to the simulation if it's available, otherwise fallback
141+
// to the swap provider's gas
142+
gas: estimatedGasUse
143+
? (BigInt(estimatedGasUse) * 115n) / 100n
144+
: BigInt(gas),
141145
estimatedGasUse: estimatedGasUse ? BigInt(estimatedGasUse) : undefined,
142146
}
143147

0 commit comments

Comments
 (0)