From 2fa91efa675fb33ed31e50329fbcb7ca04f62e51 Mon Sep 17 00:00:00 2001 From: ryanbajollari <54822716+rbajollari@users.noreply.github.com> Date: Mon, 22 Apr 2024 10:38:04 -0400 Subject: [PATCH] feat: Add support for mainnet chains and relaying without token (#32) * feat: Add support for mainnet chains and relaying without token * environment config --- frontend/.env.example | 3 ++ frontend/src/components/RelayButton.tsx | 51 +++++++++++++-------- frontend/src/components/lib/AxelarChains.ts | 12 ++++- frontend/src/main.tsx | 25 +++++++--- 4 files changed, 65 insertions(+), 26 deletions(-) create mode 100644 frontend/.env.example diff --git a/frontend/.env.example b/frontend/.env.example new file mode 100644 index 0000000..5c382a4 --- /dev/null +++ b/frontend/.env.example @@ -0,0 +1,3 @@ +VITE_OJO_ADDRESS= +VITE_MOCK_OJO_ADDRESS= +VITE_ENVIRONMENT=testnet diff --git a/frontend/src/components/RelayButton.tsx b/frontend/src/components/RelayButton.tsx index 2b79af9..359c431 100644 --- a/frontend/src/components/RelayButton.tsx +++ b/frontend/src/components/RelayButton.tsx @@ -13,6 +13,7 @@ import { ethers } from 'ethers'; import { useNetwork } from 'wagmi'; const ojoAddress = import.meta.env.VITE_OJO_ADDRESS as `0x${string}`; const mockOjoAddress = import.meta.env.VITE_MOCK_OJO_ADDRESS as `0x${string}`; +const environment = import.meta.env.VITE_ENVIRONMENT as Environment; type RelayPricesParameters = { assetNames: string[]; @@ -24,8 +25,8 @@ const RelayPricesButton: React.FC = ({ assetNames, symbol const { chain } = useNetwork(); const relayPrices = async () => { - if (assetNames.length === 0 || !symbol || !amount) { - alert("Must select assets, fee token, and amount to relay price data"); + if (assetNames.length === 0) { + alert("Must select assets to relay price data"); return } @@ -41,13 +42,17 @@ const RelayPricesButton: React.FC = ({ assetNames, symbol return } - // fetch token address of fee token - const axelarGatewayAddress = axelarGatewayAddresses[chain.name]; - const axelarGatewayContract = new ethers.Contract(axelarGatewayAddress, IAxelarGateway.abi, signer); - const tokenAddress = await axelarGatewayContract.tokenAddresses(symbol); + // fetch token address of fee token if selected + let tokenAddress; + if (symbol) { + const axelarGatewayAddress = axelarGatewayAddresses[chain.name]; + const axelarGatewayContract = new ethers.Contract(axelarGatewayAddress, IAxelarGateway.abi, signer); + tokenAddress = await axelarGatewayContract.tokenAddresses(symbol); + } // estimate axelar gmp fee - const api = new AxelarQueryAPI({ environment: Environment.TESTNET }); + + const api = new AxelarQueryAPI({ environment: environment }); const gasFee = await api.estimateGasFee( axelarChains[chain?.name], "ojo", @@ -56,21 +61,31 @@ const RelayPricesButton: React.FC = ({ assetNames, symbol 2, ); - // approve MockOjo Contract to spend fee token - const tokenContract = new ethers.Contract(tokenAddress, IERC20.abi, signer); - const tx1 = await tokenContract.approve(mockOjoAddress, ethers.parseUnits(amount, 6)); - await tx1.wait(); + // approve MockOjo Contract to spend fee token if selected + if (symbol) { + const tokenContract = new ethers.Contract(tokenAddress, IERC20.abi, signer); + const tx1 = await tokenContract.approve(mockOjoAddress, ethers.parseUnits(amount, 6)); + await tx1.wait(); + } // send relay price data tx const assetNamesArray = assetNames.map(name => ethers.encodeBytes32String(name)); const mockOjoContract = new ethers.Contract(mockOjoAddress, MockOjo.abi, signer); - const tx2 = await mockOjoContract.relayOjoPriceDataWithToken( - assetNamesArray, - symbol, - ethers.parseUnits(amount, 6), - tokenAddress, - { value: gasFee } - ); + let tx2; + if (symbol) { + tx2 = await mockOjoContract.relayOjoPriceDataWithToken( + assetNamesArray, + symbol, + ethers.parseUnits(amount, 6), + tokenAddress, + { value: gasFee } + ); + } else { + tx2 = await mockOjoContract.relayOjoPriceData( + assetNamesArray, + { value: gasFee } + ) + } await tx2.wait(); alert("Relay tx sent succesfully, check status on https://testnet.axelarscan.io/gmp/search?chain=ojo") } else { diff --git a/frontend/src/components/lib/AxelarChains.ts b/frontend/src/components/lib/AxelarChains.ts index 8898e58..46c91b3 100644 --- a/frontend/src/components/lib/AxelarChains.ts +++ b/frontend/src/components/lib/AxelarChains.ts @@ -16,7 +16,11 @@ export const axelarChains = { "Alfajores": "celo", "Kava EVM Testnet": "kava", "Filecoin Calibration": "filecoin-2", - "Linea Goerli Testnet": "linea" + "Linea Goerli Testnet": "linea", + "Ethereum": "mainnet", + "Arbitrum One": "arbitrum", + "Base": "base", + "OP Mainnet": "optimism" }; export const axelarGatewayAddresses = { @@ -36,7 +40,11 @@ export const axelarGatewayAddresses = { "Alfajores": "0xe432150cce91c13a887f7D836923d5597adD8E31", "Kava EVM Testnet": "0xC8D18F85cB0Cee5C95eC29c69DeaF6cea972349c", "Filecoin Calibration": "0x999117D44220F33e0441fbAb2A5aDB8FF485c54D", - "Linea Goerli Testnet": "0xe432150cce91c13a887f7D836923d5597adD8E31" + "Linea Goerli Testnet": "0xe432150cce91c13a887f7D836923d5597adD8E31", + "Ethereum": "0xe432150cce91c13a887f7D836923d5597adD8E31", + "Arbitrum One": "0xe432150cce91c13a887f7D836923d5597adD8E31", + "Base": "0xe432150cce91c13a887f7D836923d5597adD8E31", + "OP Mainnet": "0xe432150cce91c13a887f7D836923d5597adD8E31" } export function isAxelarChain(key: any): key is keyof typeof axelarChains { diff --git a/frontend/src/main.tsx b/frontend/src/main.tsx index 8ce2700..d0647e8 100644 --- a/frontend/src/main.tsx +++ b/frontend/src/main.tsx @@ -6,7 +6,7 @@ import App from './App.tsx' import './index.css' import { getDefaultWallets, RainbowKitProvider } from '@rainbow-me/rainbowkit'; -import { configureChains, createConfig, WagmiConfig } from 'wagmi'; +import { configureChains, createConfig, WagmiConfig, Chain } from 'wagmi'; import { publicProvider } from 'wagmi/providers/public'; import { goerli, @@ -25,14 +25,27 @@ import { celoAlfajores, kavaTestnet, filecoinCalibration, - lineaTestnet + lineaTestnet, + mainnet, + arbitrum, + base, + optimism, } from 'wagmi/chains'; +const environment = import.meta.env.VITE_ENVIRONMENT as string; + +function getChains(): Chain[] { + if (environment == "mainnet") { + return [mainnet, arbitrum, base, optimism] + } else { + return [goerli, sepolia, bscTestnet, polygonMumbai, polygonZkEvmTestnet, + avalancheFuji, fantomTestnet, moonbaseAlpha, arbitrumGoerli, + arbitrumSepolia, optimismGoerli, baseGoerli, mantleTestnet, + celoAlfajores, kavaTestnet, filecoinCalibration, lineaTestnet] + } +} const { chains, publicClient } = configureChains( - [goerli, sepolia, bscTestnet, polygonMumbai, polygonZkEvmTestnet, - avalancheFuji, fantomTestnet, moonbaseAlpha, arbitrumGoerli, - arbitrumSepolia, optimismGoerli, baseGoerli, mantleTestnet, - celoAlfajores, kavaTestnet, filecoinCalibration, lineaTestnet], + getChains(), [publicProvider()] );