Skip to content

Commit

Permalink
♻️ Refactor address fetching logic
Browse files Browse the repository at this point in the history
  • Loading branch information
MattPoblete committed Dec 17, 2024
1 parent 7f17084 commit cae1a51
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 14 deletions.
2 changes: 2 additions & 0 deletions apps/dapp/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ Copy the .env.example file to .env and fill in the values.
cp env.example .env
```

>[!NOTE]
> If you want to use your own deployment of the smart contract, you can set the .env property `NEXT_PUBLIC_IS_LOCAL` to true and uncomment the lines 11-13 of the file `src/helpers/getRemoteConfig.ts` and do not commit this changes.
## Running the app
Run the development server:
Expand Down
3 changes: 2 additions & 1 deletion apps/dapp/env.example
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
NEXT_PUBLIC_TEST_TOKENS_ADMIN= # Test tokens admin address
NEXT_PUBLIC_TEST_TOKENS_ADMIN= # Test tokens admin address
NEXT_PUBLIC_IS_LOCAL= # Defines the environment config
29 changes: 24 additions & 5 deletions apps/dapp/src/constants/constants.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,29 @@
import axios from 'axios'
const baseURL = 'https://raw.githubusercontent.com/paltalabs/defindex/refs/heads/main/public/'
const suffix = '.contracts.json'


const contract_addresses = {
testnet: {
defindex_factory: "CAJJAIJT6E7GMJKFA66RRI7KUNNR22TPNLNUFCNANGCFXJ54RYPVPPJT",
hodl_strategy: "CDTSVTAI4BXYIEZ66F2TLZ337OLW5R5P4ONWMHQY5XTOTBZURZEDZ64N",
fixed_apr_strategy: "CDR6K2L2UN3SZLBOUCJCVNKKKGC4F5DHGQ6QIPVBXU3UTXTTGWWEZ2H3",
blend_strategy: "CCFT4EVTUSYUNO7CYJHC3R2F5ZBTXW7CMJW5Z2PGVYCHWNDG4RS35YZ5"
},
mainnet: {
defindex_factory: undefined,
hodl_strategy: undefined,
fixed_apr_strategy: undefined,
blend_strategy: undefined
}
}
export const configFile = async (network:string)=>{
if(network != 'testnet' && network != 'mainnet' && network!= 'standalone') throw new Error(`Invalid network: ${network}. It should be testnet, mainnet or standalone`)
const url = baseURL + network + suffix
/* if(network != 'testnet' && network != 'mainnet' && network!= 'standalone') throw new Error(`Invalid network: ${network}. It should be testnet, mainnet or standalone`)
const url = `https://raw.githubusercontent.com/paltalabs/defindex/refs/heads/main/public/${network}.contracts.json`
const data = await axios.get(url)
if(data.status === 200) return data.data
if(data.status === 404) throw new Error(`Deployment not found for network: ${network}`)
if(data.status === 404) throw new Error(`Deployment not found for network: ${network}`) */
if(network != 'testnet' && network != 'mainnet' && network != 'standalone') throw new Error(`Invalid network: ${network}.`)
if(network === 'testnet') return contract_addresses.testnet
if(network === 'mainnet') return contract_addresses.mainnet
if(network === 'standalone') return contract_addresses.testnet
return {}
}
11 changes: 7 additions & 4 deletions apps/dapp/src/helpers/getRemoteConfig.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import { configFile } from '@/constants/constants';
import localDeployment from '../../../contracts/.soroban/testnet.contracts.json'

const isLocal = process.env.NEXT_PUBLIC_IS_LOCAL

export const getRemoteConfig = async (network: string) => {
if (isLocal === 'false') {
const deployments = await configFile(network)
return deployments
} else if(isLocal === 'true') {
const localDeployment = require(`../../../contracts/.soroban/${network}.contracts.json`)
return localDeployment
}
}
/* else if(isLocal === 'true') {
return localDeployment.ids
} */
}
6 changes: 3 additions & 3 deletions apps/dapp/src/store/lib/features/vaultStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@ const initialState: NewVaultState = {
//Filtrar Strategies por network y retornar array de Strategies
export const getDefaultStrategies = async (network: string) => {
try {
const remoteStrategies = await getRemoteConfig(network)
const remoteStrategies: any = await getRemoteConfig(network)
const strategies: Strategy[] = []
for (let strategy in remoteStrategies.ids) {
for (let strategy in remoteStrategies) {
if (strategy.includes('strategy')) {
const parsedName = strategy.split('_')[0]
if (!parsedName) continue
const prettierName = parsedName.charAt(0).toUpperCase() + parsedName.slice(1)
strategies.push({
address: remoteStrategies.ids[strategy],
address: remoteStrategies[strategy],
name: parsedName ? prettierName : '',
paused: false,
tempAmount: 0
Expand Down
2 changes: 1 addition & 1 deletion apps/dapp/src/utils/factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export async function fetchFactoryAddress(network: string): Promise<string> {

const remoteConfig: any = await getRemoteConfig(network);
try {
const factoryAddress = remoteConfig.ids.defindex_factory;
const factoryAddress = remoteConfig.defindex_factory;
return factoryAddress;

} catch (error) {
Expand Down

0 comments on commit cae1a51

Please sign in to comment.