-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
♻️Refactor useAggregator to toggle aggregator by network
- Loading branch information
1 parent
8b29f09
commit 4d012fa
Showing
2 changed files
with
44 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; | ||
}; |