Skip to content

Commit

Permalink
config refactor changes
Browse files Browse the repository at this point in the history
  • Loading branch information
kev1n-peters committed Mar 19, 2024
1 parent 2bfeb4e commit 79a314b
Show file tree
Hide file tree
Showing 17 changed files with 150 additions and 134 deletions.
4 changes: 2 additions & 2 deletions wormhole-connect/src/AppRouter.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import { clearWallets } from './store/wallet';
import { clearPorticoBridge } from 'store/porticoBridge';
import { useExternalSearch } from 'hooks/useExternalSearch';
import { clearNtt } from 'store/ntt';
import { wh } from 'utils/sdk';
import internalConfig from 'config';

const useStyles = makeStyles()((theme: any) => ({
appContent: {
Expand Down Expand Up @@ -74,7 +74,7 @@ function AppRouter({ config }: Props) {
dispatch(clearRedeem());
dispatch(clearWallets());
dispatch(clearNtt());
wh.registerProviders(); // reset providers that may have been set during transfer
internalConfig.wh.registerProviders(); // reset providers that may have been set during transfer
}
// reset transfer state on leave
if (prevRoute === bridgeRoute && route !== bridgeRoute) {
Expand Down
11 changes: 5 additions & 6 deletions wormhole-connect/src/hooks/useDeliveryStatus.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { DeliveryStatus } from '@certusone/wormhole-sdk/lib/esm/relayer';
import axios from 'axios';
import { isMainnet } from 'config';
import { Route } from 'config/types';
import { useEffect } from 'react';
import { useDispatch, useSelector } from 'react-redux';
Expand All @@ -9,10 +8,7 @@ import { setRedeemTx, setDeliveryStatus } from 'store/redeem';
import { sleep } from 'utils';
import { isEvmChain } from 'utils/sdk';
import { getEmitterAndSequence } from 'utils/vaa';

const BASE_URL = `https://api.${
isMainnet ? '' : 'testnet.'
}wormholescan.io/api/v1/relays`;
import config from 'config';

const RETRY_DELAY = 15_000;

Expand Down Expand Up @@ -44,12 +40,15 @@ const useDeliveryStatus = (): void => {
}
const { emitterChain, emitterAddress, sequence } =
getEmitterAndSequence(signedMessage);
const baseUrl = `https://api.${
config.isMainnet ? '' : 'testnet.'
}wormholescan.io/api/v1/relays`;
let active = true;
const fetchData = async () => {
while (active) {
try {
const response = await axios.get<RelayResponse>(
`${BASE_URL}/${emitterChain}/${emitterAddress}/${sequence}`,
`${baseUrl}/${emitterChain}/${emitterAddress}/${sequence}`,
);
if (active) {
const { delivery, toTxHash } = response.data?.data || {};
Expand Down
51 changes: 25 additions & 26 deletions wormhole-connect/src/routes/ntt/nttBase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ import {
import { fetchVaa } from 'utils/vaa';
import { hexlify, parseUnits } from 'ethers/lib/utils.js';
import { BaseRoute } from '../bridge';
import { isEvmChain, solanaContext, toChainName, wh } from 'utils/sdk';
import { CHAINS, TOKENS } from 'config';
import { isEvmChain, solanaContext, toChainId, toChainName } from 'utils/sdk';
import {
MAX_DECIMALS,
calculateUSDPrice,
Expand All @@ -41,6 +40,7 @@ import { NttManagerSolana, getMessageSolana } from './platforms/solana';
import { formatGasFee } from 'routes/utils';
import { NO_INPUT } from 'utils/style';
import { estimateAverageGasFee } from 'utils/gas';
import config from 'config';

export abstract class NttBase extends BaseRoute {
isSupportedChain(chain: ChainName): boolean {
Expand Down Expand Up @@ -103,17 +103,17 @@ export abstract class NttBase extends BaseRoute {
destChain: ChainName | ChainId,
): Promise<boolean> {
return await Promise.all([
this.isSupportedChain(wh.toChainName(sourceChain)),
this.isSupportedChain(wh.toChainName(destChain)),
this.isSupportedChain(toChainName(sourceChain)),
this.isSupportedChain(toChainName(destChain)),
this.isSupportedSourceToken(
TOKENS[sourceToken],
TOKENS[destToken],
config.tokens[sourceToken],
config.tokens[destToken],
sourceChain,
destChain,
),
this.isSupportedDestToken(
TOKENS[destToken],
TOKENS[sourceToken],
config.tokens[destToken],
config.tokens[sourceToken],
sourceChain,
destChain,
),
Expand Down Expand Up @@ -164,7 +164,7 @@ export abstract class NttBase extends BaseRoute {
if (isEvmChain(sendingChain)) {
const gasLimit = this.TYPE === Route.NttManual ? 200_000 : 250_000;
return await estimateAverageGasFee(sendingChain, gasLimit);
} else if (wh.toChainName(sendingChain) === 'solana') {
} else if (toChainName(sendingChain) === 'solana') {
return BigNumber.from(10_000);
}
throw new Error('Unsupported chain');
Expand All @@ -181,7 +181,7 @@ export abstract class NttBase extends BaseRoute {
if (isEvmChain(destChain)) {
const gasLimit = 300_000;
return await estimateAverageGasFee(destChain, gasLimit);
} else if (wh.toChainName(destChain) === 'solana') {
} else if (toChainName(destChain) === 'solana') {
return BigNumber.from(65_000); // TODO: check this
}
throw new Error('Unsupported chain');
Expand All @@ -204,7 +204,7 @@ export abstract class NttBase extends BaseRoute {
if (!tokenConfig?.ntt) {
throw new Error('invalid token');
}
const destTokenConfig = TOKENS[destToken];
const destTokenConfig = config.tokens[destToken];
if (!destTokenConfig?.ntt) {
throw new Error('invalid dest token');
}
Expand All @@ -221,7 +221,7 @@ export abstract class NttBase extends BaseRoute {
throw new ContractIsPausedError();
}
const decimals = getTokenDecimals(
wh.toChainId(sendingChain),
toChainId(sendingChain),
tokenConfig.tokenId,
);
const sendAmount = removeDust(parseUnits(amount, decimals), decimals);
Expand Down Expand Up @@ -252,7 +252,7 @@ export abstract class NttBase extends BaseRoute {
if (await nttManager.isPaused()) {
throw new ContractIsPausedError();
}
const nttConfig = TOKENS[receivedTokenKey]?.ntt;
const nttConfig = config.tokens[receivedTokenKey]?.ntt;
if (!nttConfig) {
throw new Error('ntt config not found');
}
Expand All @@ -263,7 +263,7 @@ export abstract class NttBase extends BaseRoute {
);
return await transceiver.receiveMessage(vaa, payer);
}
if (wh.toChainName(chain) === 'solana') {
if (toChainName(chain) === 'solana') {
return await (nttManager as NttManagerSolana).receiveMessage(vaa, payer);
}
throw new Error('Unsupported chain');
Expand Down Expand Up @@ -343,11 +343,8 @@ export abstract class NttBase extends BaseRoute {
if (!tokenConfig) {
throw new Error('invalid token');
}
const key = getNativeVersionOfToken(
tokenConfig.symbol,
wh.toChainName(chain),
);
return TOKENS[key]?.tokenId?.address || null;
const key = getNativeVersionOfToken(tokenConfig.symbol, toChainName(chain));
return config.tokens[key]?.tokenId?.address || null;
}

async getMessage(
Expand All @@ -357,7 +354,7 @@ export abstract class NttBase extends BaseRoute {
if (isEvmChain(chain)) {
return await getMessageEvm(tx, chain);
}
if (wh.toChainName(chain) === 'solana') {
if (toChainName(chain) === 'solana') {
return await getMessageSolana(tx);
}
throw new Error('Unsupported chain');
Expand Down Expand Up @@ -403,7 +400,7 @@ export abstract class NttBase extends BaseRoute {
gasEstimate,
receiveTx,
} = params;
const token = TOKENS[receivedTokenKey];
const token = config.tokens[receivedTokenKey];
const formattedAmt = toNormalizedDecimals(
amount,
tokenDecimals,
Expand All @@ -420,15 +417,15 @@ export abstract class NttBase extends BaseRoute {
],
};
if (this.TYPE === Route.NttManual) {
const { gasToken } = CHAINS[toChain]!;
const { gasToken } = config.chains[toChain]!;
let gas = gasEstimate;
if (receiveTx) {
if (isEvmChain(toChain)) {
const gasFee = await wh.getTxGasFee(toChain, receiveTx);
const gasFee = await config.wh.getTxGasFee(toChain, receiveTx);
if (gasFee) {
gas = formatGasFee(toChain, gasFee);
}
} else if (wh.toChainName(toChain) === 'solana') {
} else if (toChainName(toChain) === 'solana') {
const connection = solanaContext().connection;
if (!connection) throw new Error('Connection not found');
const tx = await connection.getParsedTransaction(receiveTx);
Expand All @@ -439,8 +436,10 @@ export abstract class NttBase extends BaseRoute {
}
result.displayData.push({
title: receiveTx ? 'Gas fee' : 'Gas estimate',
value: gas ? `${gas} ${getDisplayName(TOKENS[gasToken])}` : NO_INPUT,
valueUSD: calculateUSDPrice(gas, tokenPrices, TOKENS[gasToken]),
value: gas
? `${gas} ${getDisplayName(config.tokens[gasToken])}`
: NO_INPUT,
valueUSD: calculateUSDPrice(gas, tokenPrices, config.tokens[gasToken]),
});
}
return result;
Expand Down
35 changes: 20 additions & 15 deletions wormhole-connect/src/routes/ntt/nttRelay.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { BigNumber } from 'ethers';
import { getNttManager } from './platforms';
import { ChainName, ChainId } from '@wormhole-foundation/wormhole-connect-sdk';
import { NttBase } from './nttBase';
import { CHAINS, TOKENS } from 'config';
import {
RelayerFee,
TransferDisplayData,
Expand All @@ -18,12 +17,18 @@ import {
getTokenDecimals,
toNormalizedDecimals,
} from 'utils';
import { ParsedRelayerMessage, isEvmChain, toChainId, wh } from 'utils/sdk';
import {
ParsedRelayerMessage,
isEvmChain,
toChainId,
toChainName,
} from 'utils/sdk';
import { NO_INPUT } from 'utils/style';
import { TokenPrices } from 'store/tokenPrices';
import { toDecimals, toFixedDecimals } from 'utils/balance';
import { NttManagerEvm, WormholeTransceiver } from './platforms/evm';
import { NttQuoter } from './platforms/solana/nttQuoter';
import config from 'config';

export class NttRelay extends NttBase {
readonly NATIVE_GAS_DROPOFF_SUPPORTED: boolean = false;
Expand All @@ -37,7 +42,7 @@ export class NttRelay extends NttBase {
sourceChain: ChainName | ChainId,
destChain: ChainName | ChainId,
): Promise<boolean> {
const nttConfig = TOKENS[sourceToken]?.ntt;
const nttConfig = config.tokens[sourceToken]?.ntt;
if (!nttConfig) {
return false;
}
Expand All @@ -62,7 +67,7 @@ export class NttRelay extends NttBase {
transceiver.isSpecialRelayingEnabled(destChain),
]).then((results) => results.some((r) => r));
}
if (wh.toChainName(sourceChain) === 'solana') {
if (toChainName(sourceChain) === 'solana') {
if (!nttConfig.solanaQuoter) return false;
const quoter = new NttQuoter(nttConfig.solanaQuoter);
return await quoter.isRelayEnabled(destChain);
Expand All @@ -76,7 +81,7 @@ export class NttRelay extends NttBase {
token: string,
destToken: string,
): Promise<RelayerFee | null> {
const nttConfig = TOKENS[token]?.ntt;
const nttConfig = config.tokens[token]?.ntt;
if (!nttConfig) {
throw new Error('invalid token');
}
Expand All @@ -88,7 +93,7 @@ export class NttRelay extends NttBase {
);
return { fee: BigNumber.from(deliveryPrice), feeToken: 'native' };
}
if (wh.toChainName(sourceChain) === 'solana') {
if (toChainName(sourceChain) === 'solana') {
if (!nttConfig.solanaQuoter) throw new Error('no solana quoter');
const quoter = new NttQuoter(nttConfig.solanaQuoter);
const relayCost = await quoter.calcRelayCost(destChain);
Expand All @@ -109,21 +114,21 @@ export class NttRelay extends NttBase {
tokenPrices: TokenPrices,
routeOptions: { relayerFee: number },
): Promise<TransferDisplayData> {
const sendingChainName = wh.toChainName(sendingChain);
const sourceGasToken = CHAINS[sendingChainName]?.gasToken;
const sendingChainName = toChainName(sendingChain);
const sourceGasToken = config.chains[sendingChainName]?.gasToken;
const sourceGasTokenSymbol = sourceGasToken
? getDisplayName(TOKENS[sourceGasToken])
? getDisplayName(config.tokens[sourceGasToken])
: '';
// Calculate the USD value of the gas
const sendingGasEstPrice = calculateUSDPrice(
sendingGasEst,
tokenPrices,
TOKENS[sourceGasToken || ''],
config.tokens[sourceGasToken || ''],
);
const relayerFeePrice = calculateUSDPrice(
routeOptions.relayerFee,
tokenPrices,
TOKENS[sourceGasToken || ''],
config.tokens[sourceGasToken || ''],
);
let totalFeesText = '';
let totalFeesPrice = '';
Expand All @@ -138,7 +143,7 @@ export class NttRelay extends NttBase {
totalFeesPrice = calculateUSDPrice(
feeValue,
tokenPrices,
TOKENS[sourceGasToken],
config.tokens[sourceGasToken],
);
}
return [
Expand Down Expand Up @@ -183,16 +188,16 @@ export class NttRelay extends NttBase {
tokenDecimals,
MAX_DECIMALS,
);
const { gasToken: sourceGasTokenKey } = CHAINS[fromChain]!;
const sourceGasToken = TOKENS[sourceGasTokenKey];
const { gasToken: sourceGasTokenKey } = config.chains[fromChain]!;
const sourceGasToken = config.tokens[sourceGasTokenKey];
const decimals = getTokenDecimals(
toChainId(sourceGasToken.nativeChain),
'native',
);
const formattedGas = gasFee && toDecimals(gasFee, decimals, MAX_DECIMALS);
const formattedFee =
relayerFee && toDecimals(relayerFee, decimals, MAX_DECIMALS);
const token = TOKENS[tokenKey];
const token = config.tokens[tokenKey];

return [
{
Expand Down
Loading

0 comments on commit 79a314b

Please sign in to comment.