Skip to content

Commit

Permalink
♻️ Refactor useAggregator
Browse files Browse the repository at this point in the history
  • Loading branch information
MattPoblete committed Sep 24, 2024
1 parent 4d012fa commit 9df3eec
Showing 1 changed file with 19 additions and 14 deletions.
33 changes: 19 additions & 14 deletions src/hooks/useAggregator.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export const useAggregator = () => {
const { activeChain } = sorobanContext;

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

const shouldUseAggregator = useMemo(() => {
if (activeChain?.id === 'mainnet') {
Expand All @@ -20,20 +21,24 @@ export const useAggregator = () => {
}
}, [activeChain?.id])

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])
useEffect(() => {
console.log('useAggregator', activeChain?.id, shouldUseAggregator);
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, shouldUseAggregator]);

return { address, isEnabled };
};

0 comments on commit 9df3eec

Please sign in to comment.