Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions src/config/celo/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ export const commonConfig: AppConfig = {
},
// walkthroughId: 'uouygtywoj3c',
useOpenocean: false,
useSeedData: true,
useEIP7702: true,
},
};
1 change: 1 addition & 0 deletions src/config/configSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,5 +142,6 @@ export const AppConfigSchema = v.object({
walkthroughId: v.optional(v.string()),
useOpenocean: v.optional(v.boolean()),
useEIP7702: v.optional(v.boolean()),
useSeedData: v.optional(v.boolean()),
}),
});
1 change: 1 addition & 0 deletions src/config/coti/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ export const commonConfig: AppConfig = {
'0xfA6f73446b17A97a56e464256DA54AD43c2Cbc3E',
],
},
useSeedData: true,
// walkthroughId: 'd2a5isof88vg',
},
};
1 change: 1 addition & 0 deletions src/config/ethereum/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -212,5 +212,6 @@ export const commonConfig: AppConfig = {
// walkthroughId: 'i2ok96zcpzqw',
useOpenocean: false, // !navigator.webdriver, // use sdk in E2E
useEIP7702: true,
useSeedData: true,
},
};
1 change: 1 addition & 0 deletions src/config/sei/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ export const commonConfig: AppConfig = {
// rewardUrl: 'https://app.merkl.xyz/?chain=1329&protocol=carbon',
// walkthroughId: '51xep69sd3io',
useOpenocean: false, // true,
useSeedData: true,
useEIP7702: true,
},
};
1 change: 1 addition & 0 deletions src/config/tac/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,5 +97,6 @@ export const commonConfig: AppConfig = {
//},
// walkthroughId: 'vjbcftqceykr',
useOpenocean: false,
useSeedData: true,
},
};
1 change: 1 addition & 0 deletions src/config/ton/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,5 +87,6 @@ export const commonConfig: AppConfig = {
showTerms: true,
showPrivacy: true,
showCart: true,
useSeedData: true,
},
};
21 changes: 17 additions & 4 deletions src/libs/sdk/provider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { QueryKey } from 'libs/queries';
import { CHAIN_ID, RPC_URLS, RPC_HEADERS } from 'libs/wagmi';
import { buildTokenPairKey, setIntervalUsingTimeout } from 'utils/helpers';
import { ONE_HOUR_IN_MS } from 'utils/time';
import { carbonApi } from 'services/carbonApi';
import config from 'config';

const contractsConfig: ContractsConfig = {
Expand Down Expand Up @@ -49,12 +50,23 @@ export const SDKProvider: FC<Props> = ({ children }) => {

useEffect(() => {
const initSDK = async () => {
const shouldUseSeedData = () => {
if (!import.meta.env.PROD) return false;
if (!config.ui.useSeedData) return false;
if (lsService.getItem('tenderlyRpc')) return false;
return true;
};

try {
setIsLoading(true);
const { timestamp, ttl } = lsService.getItem('lastSdkCache') ?? {};
let cacheData: string | undefined;
if (timestamp && ttl && timestamp + ttl > Date.now()) {
cacheData = lsService.getItem('sdkCompressedCacheData');
if (shouldUseSeedData()) {
cacheData = await carbonApi.getSeedData();
} else {
const { timestamp, ttl } = lsService.getItem('lastSdkCache') ?? {};
if (timestamp && ttl && timestamp + ttl > Date.now()) {
cacheData = lsService.getItem('sdkCompressedCacheData');
}
}
const initializer = carbonSDK.init(
CHAIN_ID,
Expand Down Expand Up @@ -86,6 +98,7 @@ export const SDKProvider: FC<Props> = ({ children }) => {
}, []);

useEffect(() => {
if (!isInitialized) return;
const invalidateQueriesByPair = (pair: TokenPair) => {
cache.invalidateQueries({
predicate: (query) =>
Expand Down Expand Up @@ -128,7 +141,7 @@ export const SDKProvider: FC<Props> = ({ children }) => {
Comlink.proxy(onCacheClearedCallback),
);
};
}, [cache]);
}, [cache, isInitialized]);

return (
<SdkContext.Provider
Expand Down
40 changes: 25 additions & 15 deletions src/services/carbonApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ interface MarketRate {
data: { USD: number };
}

const get = async <T>(
const getResponse = async (
endpoint: string,
params: object = {},
abortSignal?: AbortSignal,
): Promise<T> => {
) => {
const api = lsService.getItem('carbonApi') || config.carbonApi;
const url = new URL(api + endpoint);
for (const [key, value] of Object.entries(params)) {
Expand All @@ -35,15 +35,24 @@ const get = async <T>(
}
}
const response = await fetch(url, { signal: abortSignal });
const result = await response.json();
if (!response.ok) {
const result = await response.json();
const error = (result as { error?: string }).error;
throw new Error(
error ||
`Response was not okay. ${response.statusText} response received.`,
);
}
return result as T;
return response;
};

const getJSON = async <T>(
endpoint: string,
params: object = {},
abortSignal?: AbortSignal,
): Promise<T> => {
const res = await getResponse(endpoint, params, abortSignal);
return res.json();
};

const carbonApi = {
Expand All @@ -53,41 +62,42 @@ const carbonApi = {
return res.json();
},
getTokens: () => {
return get<Token[]>('tokens');
return getJSON<Token[]>('tokens');
},
getTokensMarketPrice: () => {
return get<Record<string, number>>('tokens/prices');
return getJSON<Record<string, number>>('tokens/prices');
},
getMarketRateHistory: async (
params: TokenPriceHistorySearch,
): Promise<TokenPriceHistoryResult[]> => {
return get<TokenPriceHistoryResult[]>('history/prices', params);
return getJSON<TokenPriceHistoryResult[]>('history/prices', params);
},
getMarketRate: async (address: string) => {
const params = { address, convert: ['USD'] };
const result = await get<MarketRate>('market-rate', params);
const result = await getJSON<MarketRate>('market-rate', params);
return result.data.USD;
},
getSimulator: async (
params: SimulatorAPIParams,
): Promise<SimulatorReturnNew> => {
return get<SimulatorReturnNew>('simulator/create', params);
return getJSON<SimulatorReturnNew>('simulator/create', params);
},
getAllStrategies: () => {
return get<StrategyAPIResult>('strategies');
return getJSON<StrategyAPIResult>('strategies');
},
getActivity: async (
params: QueryActivityParams,
abortSignal?: AbortSignal,
) => {
return get<ServerActivity[]>('activity', params, abortSignal);
return getJSON<ServerActivity[]>('activity', params, abortSignal);
},
getActivityMeta: async (params: QueryActivityParams) => {
return get<ServerActivityMeta>('activity/meta', params);
return getJSON<ServerActivityMeta>('activity/meta', params);
},
getTrending: () => get<Trending>('analytics/trending'),
getReward: (pair: string) => get<Reward>('merkle/data', { pair }),
getAllRewards: () => get<Reward[]>('merkle/all-data'),
getTrending: () => getJSON<Trending>('analytics/trending'),
getReward: (pair: string) => getJSON<Reward>('merkle/data', { pair }),
getAllRewards: () => getJSON<Reward[]>('merkle/all-data'),
getSeedData: () => getResponse('seed-data').then((res) => res.text()),
};

export { carbonApi };