|
| 1 | +import { |
| 2 | + axelarConfig, |
| 3 | + axelarConfigTestnet, |
| 4 | +} from '@aglocal/portfolio-deploy/src/axelar-configs.js'; |
| 5 | +import { buildGMPPayload } from '@agoric/orchestration/src/utils/gmp.js'; |
| 6 | +import type { AxelarChain } from '@agoric/portfolio-api/src/constants'; |
| 7 | +import { ethers } from 'ethers'; |
| 8 | +import { getEvmRpcMap } from './support.ts'; |
| 9 | + |
| 10 | +// arbitrary value. it just mimicks a valid command id for axelar |
| 11 | +const COMMAND_ID = |
| 12 | + '0xddea323337dfb73c82df7393d76b2f38835d5c2bda0123c47d1a2fc06527d19f'; |
| 13 | +const AGORIC_CHAIN = 'agoric'; |
| 14 | +const BLD_TOKEN = 'ubld'; |
| 15 | +const GAS_ESTIMATOR_CONTRACT_ABI = [ |
| 16 | + 'function executeFactory(bytes32 commandId, string sourceChain, string sourceAddress, bytes payload)', |
| 17 | + 'function executeWallet(bytes32 commandId, string sourceChain, string sourceAddress, bytes payload)', |
| 18 | +]; |
| 19 | +const GAS_ESTIMATOR_OWNER = 'agoric1owner'; |
| 20 | + |
| 21 | +const bytesToHex = (bytes: number[]): string => |
| 22 | + '0x' + bytes.map(n => n.toString(16).padStart(2, '0')).join(''); |
| 23 | + |
| 24 | +const gasEstimatorContracts = { |
| 25 | + mainnet: {}, |
| 26 | + testnet: { |
| 27 | + Avalanche: '0x0010F196F5CD0314f68FF665b8c8eD93531112Fe', |
| 28 | + }, |
| 29 | +}; |
| 30 | + |
| 31 | +export const makeGasEstimatorKit = ({ |
| 32 | + alchemyApiKey, |
| 33 | + clusterName, |
| 34 | + chainName, |
| 35 | +}: { |
| 36 | + alchemyApiKey: string; |
| 37 | + clusterName: 'mainnet' | 'testnet'; |
| 38 | + chainName: AxelarChain; |
| 39 | +}) => { |
| 40 | + const axelarConf = |
| 41 | + clusterName === 'mainnet' ? axelarConfig : axelarConfigTestnet; |
| 42 | + const chainConfig = axelarConf[chainName]; |
| 43 | + |
| 44 | + const evmContracts = chainConfig.contracts; |
| 45 | + const gasEstimatorContract = gasEstimatorContracts[clusterName][chainName]; |
| 46 | + |
| 47 | + const axelarApiAddress = |
| 48 | + clusterName === 'mainnet' |
| 49 | + ? 'https://api.axelarscan.io/gmp/estimateGasFee' |
| 50 | + : 'https://testnet.api.axelarscan.io/gmp/estimateGasFee'; |
| 51 | + |
| 52 | + const evmRpcMap = getEvmRpcMap(clusterName, alchemyApiKey); |
| 53 | + const evmRpcAddress = |
| 54 | + evmRpcMap[ |
| 55 | + `${chainConfig.chainInfo.namespace}:${chainConfig.chainInfo.reference}` |
| 56 | + ]; |
| 57 | + |
| 58 | + const generateAaveSupplyPayload = ( |
| 59 | + chainName: AxelarChain, |
| 60 | + contractAddress: string, |
| 61 | + payment: string, |
| 62 | + ) => { |
| 63 | + const contractCalls = [ |
| 64 | + { |
| 65 | + target: evmContracts.usdc, |
| 66 | + functionSignature: 'approve(address,uint256)', |
| 67 | + args: [evmContracts.aavePool, payment], |
| 68 | + }, |
| 69 | + { |
| 70 | + target: evmContracts.aavePool, |
| 71 | + functionSignature: 'supply(address,uint256,address,uint16)', |
| 72 | + args: [evmContracts.usdc, payment, contractAddress, '0'], |
| 73 | + }, |
| 74 | + ]; |
| 75 | + return bytesToHex(buildGMPPayload(contractCalls)); |
| 76 | + }; |
| 77 | + |
| 78 | + const queryAxelarGasAPI = async ( |
| 79 | + sourceChain: AxelarChain | 'agoric', |
| 80 | + destinationChain: AxelarChain | 'agoric', |
| 81 | + gasLimit: bigint, |
| 82 | + gasToken?: string, |
| 83 | + ) => { |
| 84 | + const response = await fetch(axelarApiAddress, { |
| 85 | + method: 'POST', |
| 86 | + headers: { |
| 87 | + 'Content-Type': 'application/json', |
| 88 | + }, |
| 89 | + body: JSON.stringify({ |
| 90 | + sourceChain, |
| 91 | + destinationChain, |
| 92 | + gasLimit: gasLimit.toString(), |
| 93 | + sourceTokenSymbol: gasToken, |
| 94 | + gasMultiplier: '1', |
| 95 | + }), |
| 96 | + }); |
| 97 | + |
| 98 | + if (!response.ok) { |
| 99 | + throw new Error(`HTTP error! status: ${response.status}`); |
| 100 | + } |
| 101 | + |
| 102 | + const data = await response.json(); |
| 103 | + return data; |
| 104 | + }; |
| 105 | + |
| 106 | + const queryEstimateGas = async ( |
| 107 | + provider: ethers.Provider, |
| 108 | + contractAddress: string, |
| 109 | + abi: ethers.InterfaceAbi, |
| 110 | + functionName: string, |
| 111 | + params: any[] = [], |
| 112 | + fromAddress?: string, |
| 113 | + ): Promise<bigint> => { |
| 114 | + const contract = new ethers.Contract(contractAddress, abi, provider); |
| 115 | + |
| 116 | + const gasEstimate = await contract[functionName].estimateGas( |
| 117 | + ...params, |
| 118 | + fromAddress ? { from: fromAddress } : {}, |
| 119 | + ); |
| 120 | + |
| 121 | + return gasEstimate; |
| 122 | + }; |
| 123 | + |
| 124 | + const createGasEstimator = (rpcUrl: string, contractAddress: string) => { |
| 125 | + return async ( |
| 126 | + functionName: string, |
| 127 | + params: any[] = [], |
| 128 | + fromAddress?: string, |
| 129 | + ): Promise<bigint> => { |
| 130 | + const provider = new ethers.JsonRpcProvider(rpcUrl); |
| 131 | + |
| 132 | + return queryEstimateGas( |
| 133 | + provider, |
| 134 | + contractAddress, |
| 135 | + GAS_ESTIMATOR_CONTRACT_ABI, |
| 136 | + functionName, |
| 137 | + params, |
| 138 | + fromAddress, |
| 139 | + ); |
| 140 | + }; |
| 141 | + }; |
| 142 | + |
| 143 | + const getWalletEstimate = async () => { |
| 144 | + const ethGasEstimator = createGasEstimator( |
| 145 | + evmRpcAddress, |
| 146 | + gasEstimatorContract, |
| 147 | + ); |
| 148 | + |
| 149 | + const payload = generateAaveSupplyPayload( |
| 150 | + chainName, |
| 151 | + gasEstimatorContract, |
| 152 | + '1', // Arbitrary minimum value needed to execute successfully |
| 153 | + ); |
| 154 | + |
| 155 | + const gasLimit = await ethGasEstimator('executeWallet', [ |
| 156 | + COMMAND_ID, |
| 157 | + AGORIC_CHAIN, |
| 158 | + GAS_ESTIMATOR_OWNER, |
| 159 | + payload, |
| 160 | + ]); |
| 161 | + |
| 162 | + const estimate = (await queryAxelarGasAPI( |
| 163 | + AGORIC_CHAIN, |
| 164 | + chainName, |
| 165 | + gasLimit, |
| 166 | + BLD_TOKEN, |
| 167 | + )) as string; |
| 168 | + |
| 169 | + return BigInt(estimate); |
| 170 | + }; |
| 171 | + |
| 172 | + const getFactoryContractEstimate = async () => { |
| 173 | + const ethGasEstimator = createGasEstimator( |
| 174 | + evmRpcAddress, |
| 175 | + gasEstimatorContract, |
| 176 | + ); |
| 177 | + |
| 178 | + // Payload should be 0 since no eth will be present on the gas estimator contract |
| 179 | + const payload = ethers.AbiCoder.defaultAbiCoder().encode(['uint256'], [0]); |
| 180 | + |
| 181 | + const gasLimit = await ethGasEstimator('executeFactory', [ |
| 182 | + COMMAND_ID, |
| 183 | + AGORIC_CHAIN, |
| 184 | + GAS_ESTIMATOR_OWNER, |
| 185 | + payload, |
| 186 | + ]); |
| 187 | + |
| 188 | + const estimate = (await queryAxelarGasAPI( |
| 189 | + AGORIC_CHAIN, |
| 190 | + chainName, |
| 191 | + gasLimit, |
| 192 | + BLD_TOKEN, |
| 193 | + )) as string; |
| 194 | + |
| 195 | + return BigInt(estimate); |
| 196 | + }; |
| 197 | + |
| 198 | + const getReturnFeeEstimate = async () => { |
| 199 | + const fee = (await queryAxelarGasAPI(chainName, 'agoric', 1n)) as string; |
| 200 | + |
| 201 | + return BigInt(fee); |
| 202 | + }; |
| 203 | + |
| 204 | + return { |
| 205 | + getWalletEstimate, |
| 206 | + getFactoryContractEstimate, |
| 207 | + getReturnFeeEstimate, |
| 208 | + }; |
| 209 | +}; |
0 commit comments