Skip to content

Commit

Permalink
feat: Add support for mainnet chains and relaying without token (#32)
Browse files Browse the repository at this point in the history
* feat: Add support for mainnet chains and relaying without token

* environment config
  • Loading branch information
rbajollari authored Apr 22, 2024
1 parent 32a1f47 commit 2fa91ef
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 26 deletions.
3 changes: 3 additions & 0 deletions frontend/.env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
VITE_OJO_ADDRESS=
VITE_MOCK_OJO_ADDRESS=
VITE_ENVIRONMENT=testnet
51 changes: 33 additions & 18 deletions frontend/src/components/RelayButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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[];
Expand All @@ -24,8 +25,8 @@ const RelayPricesButton: React.FC<RelayPricesParameters> = ({ 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
}

Expand All @@ -41,13 +42,17 @@ const RelayPricesButton: React.FC<RelayPricesParameters> = ({ 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",
Expand All @@ -56,21 +61,31 @@ const RelayPricesButton: React.FC<RelayPricesParameters> = ({ 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 {
Expand Down
12 changes: 10 additions & 2 deletions frontend/src/components/lib/AxelarChains.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand All @@ -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 {
Expand Down
25 changes: 19 additions & 6 deletions frontend/src/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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()]
);

Expand Down

0 comments on commit 2fa91ef

Please sign in to comment.