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' ;
4
1
import type { AxelarChain } from '@agoric/portfolio-api/src/constants' ;
5
- import { ethers } from 'ethers ' ;
2
+ import { gasLimitEstimates } from './support.ts ' ;
6
3
7
- // arbitrary value. it just mimicks a valid command id for axelar
8
- const COMMAND_ID =
9
- '0xddea323337dfb73c82df7393d76b2f38835d5c2bda0123c47d1a2fc06527d19f' ;
10
4
const AGORIC_CHAIN = 'agoric' ;
11
5
const BLD_TOKEN = 'ubld' ;
12
6
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
-
26
7
export const prepareGasEstimator =
27
8
( {
28
- evmProviders,
29
- axelarConfig,
30
- gasEstimatorContracts,
31
9
axelarApiAddress,
32
10
} : {
33
- evmProviders : Record < CaipChainId , ethers . Provider > ;
34
- axelarConfig : AxelarChainConfigMap ;
35
- gasEstimatorContracts : Record < AxelarChain , `0x${string } `> ;
36
11
axelarApiAddress : string ;
37
12
} ) =>
38
13
( chainName : AxelarChain ) => {
39
- const chainConfig = axelarConfig [ chainName ] ;
40
-
41
- const evmContracts = chainConfig . contracts ;
42
14
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
- } ;
68
15
69
16
const queryAxelarGasAPI = async (
70
17
sourceChain : AxelarChain | 'agoric' ,
@@ -94,55 +41,8 @@ export const prepareGasEstimator =
94
41
return data ;
95
42
} ;
96
43
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
-
132
44
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' ] ;
146
46
147
47
const estimate = await queryAxelarGasAPI (
148
48
AGORIC_CHAIN ,
@@ -155,20 +55,7 @@ export const prepareGasEstimator =
155
55
} ;
156
56
157
57
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' ] ;
172
59
173
60
const estimate = await queryAxelarGasAPI (
174
61
AGORIC_CHAIN ,
@@ -181,7 +68,7 @@ export const prepareGasEstimator =
181
68
} ;
182
69
183
70
const getReturnFeeEstimate = async ( ) => {
184
- const fee = await queryAxelarGasAPI ( chainName , 'agoric' , 1n ) ;
71
+ const fee = await queryAxelarGasAPI ( chainName , AGORIC_CHAIN , 1n ) ;
185
72
186
73
return BigInt ( fee ) ;
187
74
} ;
0 commit comments