Skip to content

Commit e1a4a98

Browse files
committed
chore: use hardcoded gaslimits
1 parent 6624840 commit e1a4a98

File tree

9 files changed

+21
-199
lines changed

9 files changed

+21
-199
lines changed

services/ymax-planner/src/engine.ts

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,6 @@ import {
4949
vstoragePathIsParentOf,
5050
STALE_RESPONSE,
5151
} from './vstorage-utils.ts';
52-
import type { AxelarChainConfigMap } from '@aglocal/portfolio-deploy/src/axelar-configs.js';
53-
import type { AxelarChain } from '@agoric/portfolio-api/src/constants.js';
5452

5553
const { entries, fromEntries, values } = Object;
5654

@@ -124,8 +122,6 @@ type Powers = {
124122
cosmosRest: CosmosRestClient;
125123
signingSmartWalletKit: SigningSmartWalletKit;
126124
now: typeof Date.now;
127-
axelarConfig: AxelarChainConfigMap;
128-
gasEstimatorContracts: Record<AxelarChain, `0x${string}`>;
129125
axelarApiAddress: string;
130126
};
131127

@@ -335,8 +331,6 @@ export const startEngine = async (
335331
signingSmartWalletKit,
336332
now,
337333
axelarApiAddress,
338-
axelarConfig,
339-
gasEstimatorContracts,
340334
}: Powers,
341335
{
342336
depositBrandName,
@@ -665,10 +659,7 @@ export const startEngine = async (
665659
readPublished: query.readPublished,
666660
spectrum,
667661
cosmosRest,
668-
axelarConfig,
669-
gasEstimatorContracts,
670662
axelarApiAddress,
671-
evmProviders: evmCtx.evmProviders,
672663
},
673664
);
674665

services/ymax-planner/src/gas-estimation.ts

Lines changed: 4 additions & 117 deletions
Original file line numberDiff line numberDiff line change
@@ -1,70 +1,17 @@
1-
import { type AxelarChainConfigMap } from '@aglocal/portfolio-deploy/src/axelar-configs.js';
2-
import type { CaipChainId } from '@agoric/orchestration';
3-
import { buildGMPPayload } from '@agoric/orchestration/src/utils/gmp.js';
41
import type { AxelarChain } from '@agoric/portfolio-api/src/constants';
5-
import { ethers } from 'ethers';
2+
import { gasLimitEstimates } from './support.ts';
63

7-
// arbitrary value. it just mimicks a valid command id for axelar
8-
const COMMAND_ID =
9-
'0xddea323337dfb73c82df7393d76b2f38835d5c2bda0123c47d1a2fc06527d19f';
104
const AGORIC_CHAIN = 'agoric';
115
const BLD_TOKEN = 'ubld';
126

13-
/**
14-
* {@link https://github.com/agoric-labs/agoric-to-axelar-local/blob/afe91028d4ae4b4b7f1bfe88d0326f2032c21ada/packages/axelar-local-dev-cosmos/src/__tests__/contracts/GasEstimator.sol#L99}
15-
*/
16-
const GAS_ESTIMATOR_CONTRACT_ABI = [
17-
'function executeFactory(bytes32 commandId, string sourceChain, string sourceAddress, bytes payload)',
18-
'function executeWallet(bytes32 commandId, string sourceChain, string sourceAddress, bytes payload)',
19-
];
20-
// GAS_ESTIMATOR_OWNER is an arbitrary value that we supply to the GasEstimator contract constructor
21-
const GAS_ESTIMATOR_OWNER = 'agoric1owner';
22-
23-
const bytesToHex = (bytes: number[]): string =>
24-
'0x' + bytes.map(n => n.toString(16).padStart(2, '0')).join('');
25-
267
export const prepareGasEstimator =
278
({
28-
evmProviders,
29-
axelarConfig,
30-
gasEstimatorContracts,
319
axelarApiAddress,
3210
}: {
33-
evmProviders: Record<CaipChainId, ethers.Provider>;
34-
axelarConfig: AxelarChainConfigMap;
35-
gasEstimatorContracts: Record<AxelarChain, `0x${string}`>;
3611
axelarApiAddress: string;
3712
}) =>
3813
(chainName: AxelarChain) => {
39-
const chainConfig = axelarConfig[chainName];
40-
41-
const evmContracts = chainConfig.contracts;
4214
const axelarEstimateGasUrl = `${axelarApiAddress}/gmp/estimateGasFee`;
43-
const gasEstimatorContract = gasEstimatorContracts[chainName];
44-
45-
const provider =
46-
evmProviders[
47-
`${chainConfig.chainInfo.namespace}:${chainConfig.chainInfo.reference}`
48-
];
49-
50-
const generateAaveSupplyPayload = (
51-
contractAddress: string,
52-
payment: number,
53-
) => {
54-
const contractCalls = [
55-
{
56-
target: evmContracts.usdc,
57-
functionSignature: 'approve(address,uint256)',
58-
args: [evmContracts.aavePool, payment],
59-
},
60-
{
61-
target: evmContracts.aavePool,
62-
functionSignature: 'supply(address,uint256,address,uint16)',
63-
args: [evmContracts.usdc, payment, contractAddress, 1],
64-
},
65-
];
66-
return bytesToHex(buildGMPPayload(contractCalls));
67-
};
6815

6916
const queryAxelarGasAPI = async (
7017
sourceChain: AxelarChain | 'agoric',
@@ -94,55 +41,8 @@ export const prepareGasEstimator =
9441
return data;
9542
};
9643

97-
const queryEstimateGas = async (
98-
provider: ethers.Provider,
99-
contractAddress: string,
100-
abi: ethers.InterfaceAbi,
101-
functionName: string,
102-
params: any[] = [],
103-
fromAddress?: string,
104-
): Promise<bigint> => {
105-
const contract = new ethers.Contract(contractAddress, abi, provider);
106-
107-
const gasEstimate = await contract[functionName].estimateGas(
108-
...params,
109-
fromAddress ? { from: fromAddress } : {},
110-
);
111-
112-
return gasEstimate;
113-
};
114-
115-
const createGasEstimator = (contractAddress: string) => {
116-
return async (
117-
functionName: string,
118-
params: any[] = [],
119-
fromAddress?: string,
120-
): Promise<bigint> => {
121-
return queryEstimateGas(
122-
provider,
123-
contractAddress,
124-
GAS_ESTIMATOR_CONTRACT_ABI,
125-
functionName,
126-
params,
127-
fromAddress,
128-
);
129-
};
130-
};
131-
13244
const getWalletEstimate = async () => {
133-
const ethGasEstimator = createGasEstimator(gasEstimatorContract);
134-
135-
const payload = generateAaveSupplyPayload(
136-
gasEstimatorContract,
137-
1, // Arbitrary minimum value needed to execute successfully
138-
);
139-
140-
const gasLimit = await ethGasEstimator('executeWallet', [
141-
COMMAND_ID,
142-
AGORIC_CHAIN,
143-
GAS_ESTIMATOR_OWNER,
144-
payload,
145-
]);
45+
const gasLimit = gasLimitEstimates['WALLET'];
14646

14747
const estimate = await queryAxelarGasAPI(
14848
AGORIC_CHAIN,
@@ -155,20 +55,7 @@ export const prepareGasEstimator =
15555
};
15656

15757
const getFactoryContractEstimate = async () => {
158-
const ethGasEstimator = createGasEstimator(gasEstimatorContract);
159-
160-
// Payload should be 0 since no eth will be present on the gas estimator contract
161-
const payload = ethers.AbiCoder.defaultAbiCoder().encode(
162-
['uint256'],
163-
[0],
164-
);
165-
166-
const gasLimit = await ethGasEstimator('executeFactory', [
167-
COMMAND_ID,
168-
AGORIC_CHAIN,
169-
GAS_ESTIMATOR_OWNER,
170-
payload,
171-
]);
58+
const gasLimit = gasLimitEstimates['AAVE_SUPPLY'];
17259

17360
const estimate = await queryAxelarGasAPI(
17461
AGORIC_CHAIN,
@@ -181,7 +68,7 @@ export const prepareGasEstimator =
18168
};
18269

18370
const getReturnFeeEstimate = async () => {
184-
const fee = await queryAxelarGasAPI(chainName, 'agoric', 1n);
71+
const fee = await queryAxelarGasAPI(chainName, AGORIC_CHAIN, 1n);
18572

18673
return BigInt(fee);
18774
};

services/ymax-planner/src/main.ts

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,8 @@ import { loadConfig } from './config.ts';
1313
import { CosmosRestClient } from './cosmos-rest-client.ts';
1414
import { CosmosRPCClient } from './cosmos-rpc.ts';
1515
import { startEngine } from './engine.ts';
16-
import { createEVMContext, gasEstimatorContracts } from './support.ts';
16+
import { createEVMContext } from './support.ts';
1717
import { SpectrumClient } from './spectrum-client.ts';
18-
import {
19-
axelarConfig,
20-
axelarConfigTestnet,
21-
} from '@aglocal/portfolio-deploy/src/axelar-configs.js';
2218

2319
const assertChainId = async (
2420
rpc: CosmosRPCClient,
@@ -92,9 +88,6 @@ export const main = async (
9288
alchemyApiKey: config.alchemyApiKey,
9389
});
9490

95-
const axelarConf =
96-
clusterName === 'mainnet' ? axelarConfig : axelarConfigTestnet;
97-
9891
const axelarApiAddress =
9992
clusterName === 'mainnet'
10093
? 'https://api.axelarscan.io/'
@@ -107,8 +100,6 @@ export const main = async (
107100
cosmosRest,
108101
signingSmartWalletKit,
109102
now: Date.now,
110-
axelarConfig: axelarConf,
111-
gasEstimatorContracts: gasEstimatorContracts[clusterName],
112103
axelarApiAddress,
113104
};
114105
await startEngine(powers, {

services/ymax-planner/src/plan-deposit.ts

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,6 @@ import {
1616
import type { CosmosRestClient } from './cosmos-rest-client.js';
1717
import type { Chain, Pool, SpectrumClient } from './spectrum-client.js';
1818
import { prepareGasEstimator } from './gas-estimation.js';
19-
import type { EvmContext } from './pending-tx-manager.js';
20-
import type { AxelarChainConfigMap } from '@aglocal/portfolio-deploy/src/axelar-configs.js';
21-
import type { AxelarChain } from '@agoric/portfolio-api/src/constants.js';
2219

2320
const getOwn = <O, K extends PropertyKey>(
2421
obj: O,
@@ -71,9 +68,6 @@ export const handleDeposit = async (
7168
readPublished: VstorageKit['readPublished'];
7269
spectrum: SpectrumClient;
7370
cosmosRest: CosmosRestClient;
74-
evmProviders: EvmContext['evmProviders'];
75-
axelarConfig: AxelarChainConfigMap;
76-
gasEstimatorContracts: Record<AxelarChain, `0x${string}`>;
7771
axelarApiAddress: string;
7872
},
7973
) => {

services/ymax-planner/src/support.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,10 @@ export const usdcAddresses: UsdcAddresses = {
4141
},
4242
};
4343

44-
export const gasEstimatorContracts = {
45-
mainnet: {},
46-
testnet: {
47-
Avalanche: '0x0010F196F5CD0314f68FF665b8c8eD93531112Fe', // https://testnet.snowtrace.io/address/0x0010F196F5CD0314f68FF665b8c8eD93531112Fe
48-
},
44+
type GasLimitEstimates = Record<'WALLET' | 'AAVE_SUPPLY', bigint>;
45+
export const gasLimitEstimates: GasLimitEstimates = {
46+
WALLET: 1_209_435n,
47+
AAVE_SUPPLY: 276_809n,
4948
};
5049

5150
export const getEvmRpcMap = (

services/ymax-planner/test/deposit-tools.test.ts

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ import { planDepositTransfers } from '@aglocal/portfolio-contract/tools/portfoli
1313
import { CosmosRestClient } from '../src/cosmos-rest-client.ts';
1414
import { handleDeposit } from '../src/plan-deposit.ts';
1515
import { SpectrumClient } from '../src/spectrum-client.ts';
16-
import { mockAxelarApiAddress, mockAxelarConfig, mockEvmProviders, mockGasEstimatorContracts } from './mocks.ts';
16+
import { mockAxelarApiAddress } from './mocks.ts';
17+
import { gasLimitEstimates } from '../src/support.ts';
1718

1819
const depositBrand = Far('mock brand') as Brand<'nat'>;
1920
const makeDeposit = value => AmountMath.make(depositBrand, value);
@@ -227,9 +228,6 @@ test('handleDeposit works with mocked dependencies', async t => {
227228
readPublished: mockVstorageKit.readPublished,
228229
spectrum: mockSpectrumClient,
229230
cosmosRest: mockCosmosRestClient,
230-
evmProviders: mockEvmProviders,
231-
axelarConfig: mockAxelarConfig,
232-
gasEstimatorContracts: mockGasEstimatorContracts,
233231
axelarApiAddress: mockAxelarApiAddress,
234232
});
235233
t.snapshot(steps);
@@ -293,9 +291,6 @@ test('handleDeposit handles missing targetAllocation gracefully', async t => {
293291
readPublished: mockVstorageKit.readPublished,
294292
spectrum: mockSpectrumClient,
295293
cosmosRest: mockCosmosRestClient,
296-
evmProviders: mockEvmProviders,
297-
axelarConfig: mockAxelarConfig,
298-
gasEstimatorContracts: mockGasEstimatorContracts,
299294
axelarApiAddress: mockAxelarApiAddress,
300295
});
301296

@@ -385,9 +380,6 @@ test('handleDeposit handles different position types correctly', async t => {
385380
readPublished: mockVstorageKit.readPublished,
386381
spectrum: mockSpectrumClient,
387382
cosmosRest: mockCosmosRestClient,
388-
evmProviders: mockEvmProviders,
389-
axelarConfig: mockAxelarConfig,
390-
gasEstimatorContracts: mockGasEstimatorContracts,
391383
axelarApiAddress: mockAxelarApiAddress,
392384
});
393385
t.snapshot(steps);

services/ymax-planner/test/mocks.ts

Lines changed: 2 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -6,47 +6,26 @@ import {
66
} from '@agoric/client-utils';
77
import type { OfferSpec } from '@agoric/smart-wallet/src/offers';
88

9-
import type {
10-
EvmContext,
11-
HandlePendingTxOpts,
12-
} from '../src/pending-tx-manager';
9+
import type { HandlePendingTxOpts } from '../src/pending-tx-manager';
1310
import type { TxId } from '@aglocal/portfolio-contract/src/resolver/types.ts';
1411

1512
import type { CosmosRestClient } from '../src/cosmos-rest-client.ts';
1613
import { PENDING_TX_PATH_PREFIX } from '../src/engine.ts';
1714
import type { CosmosRPCClient } from '../src/cosmos-rpc.ts';
18-
import { contractsMock } from '@aglocal/portfolio-contract/test/mocks.ts';
19-
import { chainInfoWithCCTP } from '@aglocal/portfolio-contract/test/supports.js';
20-
import type { AxelarChainConfigMap } from '@aglocal/portfolio-deploy/src/axelar-configs.js';
2115

2216
// Mock fetch
2317
globalThis.fetch = async (url: string, options?: any) => {
2418
if (url.includes('estimateGasFee')) {
2519
return {
2620
ok: true,
27-
json: async () => '50000',
21+
json: async () => JSON.parse(options.body).gasLimit,
2822
} as Response;
2923
}
3024
throw new Error(`Unmocked fetch call to: ${url}`);
3125
};
3226

33-
export const mockGasEstimatorContracts = {
34-
Avalanche: '0x89B0CC00b2bDd56234b5C6ccD7b6F48EA68a0150' as `0x${string}`,
35-
Arbitrum: '0x89B0CC00b2bDd56234b5C6ccD7b6F48EA68a0150' as `0x${string}`,
36-
Base: '0x89B0CC00b2bDd56234b5C6ccD7b6F48EA68a0150' as `0x${string}`,
37-
Ethereum: '0x89B0CC00b2bDd56234b5C6ccD7b6F48EA68a0150' as `0x${string}`,
38-
Optimism: '0x89B0CC00b2bDd56234b5C6ccD7b6F48EA68a0150' as `0x${string}`,
39-
};
40-
4127
export const mockAxelarApiAddress = 'https://mock.api/';
4228

43-
export const mockAxelarConfig = Object.fromEntries(
44-
['Optimism', 'Avalanche', 'Arbitrum', 'Ethereum', 'Base'].map(name => [
45-
name,
46-
{ chainInfo: chainInfoWithCCTP[name], contracts: contractsMock[name] },
47-
]),
48-
) as AxelarChainConfigMap;
49-
5029
export const createMockProvider = () => {
5130
const eventListeners = new Map<string, Function[]>();
5231

@@ -75,20 +54,9 @@ export const createMockProvider = () => {
7554
listeners.forEach(listener => listener(log));
7655
}
7756
},
78-
estimateGas: async (transaction: any) => {
79-
return BigInt(100000);
80-
},
8157
} as JsonRpcProvider;
8258
};
8359

84-
export const mockEvmProviders = {
85-
'eip155:1': createMockProvider(),
86-
'eip155:42161': createMockProvider(),
87-
'eip155:10': createMockProvider(),
88-
'eip155:43114': createMockProvider(),
89-
'eip155:8453': createMockProvider(),
90-
};
91-
9260
export const createMockSigningSmartWalletKit = (): SigningSmartWalletKit => {
9361
const executedOffers: OfferSpec[] = [];
9462

0 commit comments

Comments
 (0)