Skip to content

Commit

Permalink
♻️Refactor useAggregator to toggle aggregator by network
Browse files Browse the repository at this point in the history
  • Loading branch information
MattPoblete committed Sep 24, 2024
1 parent 8b29f09 commit 4d012fa
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 27 deletions.
27 changes: 20 additions & 7 deletions .env.production.example
Original file line number Diff line number Diff line change
@@ -1,9 +1,22 @@
NEXT_PUBLIC_BACKEND_URL=https://api.soroswap.finance
NEXT_PUBLIC_DEFAULT_NETWORK=testnet
# This is an example environment configuration file for the Soroswap frontend in production.
# Fill in the values for each environment variable as needed.

# NEXT_PUBLIC_AGGREGATOR_ENABLED_MAINNET: Enable or disable the aggregator for the mainnet.
# NEXT_PUBLIC_AGGREGATOR_ENABLED_TESTNET: Enable or disable the aggregator for the testnet.
# NEXT_PUBLIC_BACKEND_URL: The URL of the backend service.
# NEXT_PUBLIC_DEFAULT_NETWORK: The default network to be used.
# NEXT_PUBLIC_SOROSWAP_BACKEND_ENABLED: Enable or disable the Soroswap backend.
# NEXT_PUBLIC_SOROSWAP_BACKEND_API_KEY: The API key for the Soroswap backend.
# NEXT_PUBLIC_SOROSWAP_BACKEND_URL: The URL of the Soroswap backend service.
# NEXT_PUBLIC_TRUSTLINE_WALLET_PUBLIC_KEY: The public key for the trustline wallet.
# NEXT_PUBLIC_TEST_TOKENS_ADMIN_SECRET_KEY: The secret key for the test tokens admin.

NEXT_PUBLIC_AGGREGATOR_ENABLED_MAINNET=
NEXT_PUBLIC_AGGREGATOR_ENABLED_TESTNET=
NEXT_PUBLIC_BACKEND_URL=
NEXT_PUBLIC_DEFAULT_NETWORK=
NEXT_PUBLIC_SOROSWAP_BACKEND_ENABLED=
NEXT_PUBLIC_SOROSWAP_BACKEND_API_KEY=
NEXT_PUBLIC_SOROSWAP_BACKEND_ENABLED=false
NEXT_PUBLIC_SOROSWAP_BACKEND_URL=https://backend.soroswap.finance
NEXT_PUBLIC_TAG_ID=
NEXT_PUBLIC_TEST_TOKENS_ADMIN_SECRET_KEY=
NEXT_PUBLIC_SOROSWAP_BACKEND_URL=
NEXT_PUBLIC_TRUSTLINE_WALLET_PUBLIC_KEY=
NEXT_PUBLIC_AGGREGATOR_ENABLED=
NEXT_PUBLIC_TEST_TOKENS_ADMIN_SECRET_KEY=
44 changes: 24 additions & 20 deletions src/hooks/useAggregator.tsx
Original file line number Diff line number Diff line change
@@ -1,35 +1,39 @@
import { SorobanContextType, useSorobanReact } from '@soroban-react/core';
import { useEffect, useState } from 'react';
import { useEffect, useMemo, useState } from 'react';
import axios from 'axios';

const shouldUseAggregator = process.env.NEXT_PUBLIC_AGGREGATOR_ENABLED === 'true';
const aggregatorMainnet = process.env.NEXT_PUBLIC_AGGREGATOR_ENABLED_MAINNET === 'true';
const aggregatorTestnet = process.env.NEXT_PUBLIC_AGGREGATOR_ENABLED_TESTNET === 'true';

export const useAggregator = () => {
const sorobanContext: SorobanContextType = useSorobanReact();

const { activeChain } = sorobanContext;

const [address, setAddress] = useState<string>();
const [isEnabled, setIsAggregatorEnabled] = useState<boolean>(false);

const shouldUseAggregator = useMemo(() => {
if (activeChain?.id === 'mainnet') {
return !!aggregatorMainnet
} else if (activeChain?.id === 'testnet') {
return !!aggregatorTestnet
}
}, [activeChain?.id])

useEffect(() => {
const setAggregatorData = async () => {
if (!sorobanContext) return;
const { data } = await axios.get(
`https://raw.githubusercontent.com/soroswap/aggregator/refs/heads/main/public/${activeChain?.id}.contracts.json`
).catch((error) => {
console.error('Error fetching aggregator data', error);
console.warn('No address found Aggregator is disabled');
setIsAggregatorEnabled(false);
return { data: { ids: { aggregator: '' } } };
});
const aggregatorAddress = data.ids.aggregator;
setAddress(aggregatorAddress);
setIsAggregatorEnabled(shouldUseAggregator && !!aggregatorAddress);
};
setAggregatorData();
}, [activeChain?.id, sorobanContext]);
const isEnabled = useMemo(async () => {
if (!sorobanContext) return;
const { data } = await axios.get(
`https://raw.githubusercontent.com/soroswap/aggregator/refs/heads/main/public/${activeChain?.id}.contracts.json`
).catch((error) => {
console.error('Error fetching aggregator data', error);
console.warn('No address found Aggregator is disabled');
return { data: { ids: { aggregator: '' } } };
});
const aggregatorAddress = data.ids.aggregator;
setAddress(aggregatorAddress);
const isEnabled = !!shouldUseAggregator && !!aggregatorAddress;
return isEnabled;
}, [activeChain?.id, shouldUseAggregator])

return { address, isEnabled };
};

0 comments on commit 4d012fa

Please sign in to comment.