Skip to content

Commit

Permalink
fix contracts fetching and caching
Browse files Browse the repository at this point in the history
  • Loading branch information
devkudasov committed Sep 19, 2024
1 parent f99cd9f commit af92ba7
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 20 deletions.
8 changes: 2 additions & 6 deletions src/components/json-rpc-sign/json-rpc-sign.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,7 @@ import {EthereumSignInMessage} from '@app/helpers/ethereum-message-checker';
import {I18N} from '@app/i18n';
import {Fee} from '@app/models/fee';
import {Wallet} from '@app/models/wallet';
import {
JsonRpcMetadata,
PartialJsonRpcRequest,
VerifyAddressResponse,
} from '@app/types';
import {IContract, JsonRpcMetadata, PartialJsonRpcRequest} from '@app/types';

export interface JsonRpcSignProps {
isTransaction: boolean;
Expand All @@ -26,7 +22,7 @@ export interface JsonRpcSignProps {
request: PartialJsonRpcRequest;
metadata: JsonRpcMetadata;
wallet: Wallet;
verifyAddressResponse: VerifyAddressResponse | null;
verifyAddressResponse: IContract | null;
chainId?: number;
hideContractAttention?: boolean;
isAllowedDomain: boolean;
Expand Down
4 changes: 2 additions & 2 deletions src/components/json-rpc-sign/json-rpc-swap-transaction.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ import {Wallet} from '@app/models/wallet';
import {Balance} from '@app/services/balance';
import {Indexer, SushiPoolEstimateResponse} from '@app/services/indexer';
import {
IContract,
JsonRpcMetadata,
JsonRpcTransactionRequest,
VerifyAddressResponse,
} from '@app/types';
import {formatNumberString, openInAppBrowser, sleep} from '@app/utils';
import {STRINGS} from '@app/variables/common';
Expand All @@ -57,7 +57,7 @@ export interface JsonRpcSwapTransactionProps {
tx: Partial<JsonRpcTransactionRequest> | undefined;
parsedInput: ethers.utils.TransactionDescription | undefined;
chainId: string | number;
verifyAddressResponse: VerifyAddressResponse | null;
verifyAddressResponse: IContract | null;

onFeePress: () => void;
onError: () => void;
Expand Down
4 changes: 2 additions & 2 deletions src/components/json-rpc-sign/json-rpc-transaction-info.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ import {EthNetwork} from '@app/services';
import {Balance} from '@app/services/balance';
import {
AddressType,
IContract,
JsonRpcMetadata,
PartialJsonRpcRequest,
VerifyAddressResponse,
} from '@app/types';
import {
getTransactionFromJsonRpcRequest,
Expand All @@ -30,7 +30,7 @@ import {First} from '../ui';
interface JsonRpcTransactionInfoProps {
request: PartialJsonRpcRequest;
metadata: JsonRpcMetadata;
verifyAddressResponse: VerifyAddressResponse | null;
verifyAddressResponse: IContract | null;
chainId?: number;
hideContractAttention?: boolean;
fee?: Fee | null;
Expand Down
1 change: 1 addition & 0 deletions src/components/swap/swap-route-path-icons.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ export const SwapRoutePathIcons = observer(
if (!contract) {
return null;
}

return (
<React.Fragment key={`swap-route-path-item-${contract.id}`}>
<ImageWrapper
Expand Down
18 changes: 11 additions & 7 deletions src/helpers/whitelist.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ import {Token} from '@app/models/tokens';
import {VariablesString} from '@app/models/variables-string';
import {Wallet} from '@app/models/wallet';
import {Indexer} from '@app/services/indexer';
import {AddressType, VerifyAddressResponse} from '@app/types';
import {AddressType, IContract, VerifyAddressResponse} from '@app/types';
import {MAINNET_ETH_CHAIN_ID} from '@app/variables/common';

import {AddressUtils, NATIVE_TOKEN_ADDRESS} from './address-utils';

Expand Down Expand Up @@ -69,6 +70,9 @@ export class Whitelist {
force = false,
) {
provider = provider ?? Provider.selectedProvider;
const chainId = Provider.isAllNetworks
? MAINNET_ETH_CHAIN_ID
: provider.ethChainId;
const isWallet = Wallet.getAll().some(wallet =>
AddressUtils.equals(wallet.address, address),
);
Expand All @@ -77,7 +81,7 @@ export class Whitelist {
return {
address_type: AddressType.wallet,
id: AddressUtils.toHaqq(address),
} as VerifyAddressResponse;
} as IContract;
}

if (!provider.indexer || !address) {
Expand All @@ -88,7 +92,7 @@ export class Whitelist {
return Token.generateNativeTokenContracts()[0];
}

const key = `${CACHE_KEY}:${JSON.stringify(address)}:${provider.id}`;
const key = `${CACHE_KEY}:${JSON.stringify(address)}`;
let responseFromCache: CachedVerifyAddressResponse | null = null;
if (!force) {
try {
Expand All @@ -101,7 +105,7 @@ export class Whitelist {
responseFromCache?.cachedAt &&
responseFromCache.cachedAt + CACHE_LIFE_TIME > Date.now()
) {
return responseFromCache;
return responseFromCache.address[chainId];
}
}
} catch (err) {
Expand All @@ -112,7 +116,7 @@ export class Whitelist {
try {
const params: any[] = getParsedAddressList(address);
if (!Provider.isAllNetworks) {
params.push(Provider.selectedProvider.ethChainId);
params.push(provider.ethChainId);
}

const response = await jsonrpcRequest<VerifyAddressResponse | null>(
Expand All @@ -129,15 +133,15 @@ export class Whitelist {
VariablesString.set(key, responseForCache);
}

return response;
return response?.address[chainId] ?? null;
} catch (err) {
if (err instanceof JSONRPCError) {
Logger.captureException(err, 'Whitelist:verifyAddress', err.meta);
}
logger.error('verifyAddress', err);

if (responseFromCache) {
return responseFromCache;
return responseFromCache.address[chainId];
}

return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import {Balance} from '@app/services/balance';
import {EthSignErrorDataDetails} from '@app/services/eth-sign';
import {Indexer} from '@app/services/indexer';
import {SignJsonRpcRequest} from '@app/services/sign-json-rpc-request';
import {ModalType, VerifyAddressResponse} from '@app/types';
import {IContract, ModalType} from '@app/types';
import {
getTransactionFromJsonRpcRequest,
getUserAddressFromJRPCRequest,
Expand All @@ -37,7 +37,7 @@ export const JsonRpcSignScreen = memo(() => {
const [rejectLoading, setRejectLoading] = useState(false);
const [signLoading, setSignLoading] = useState(false);
const [verifyAddressResponse, setVerifyAddressResponse] =
useState<VerifyAddressResponse | null>(null);
useState<IContract | null>(null);
const [isLoading, setIsLoading] = useState(true);
const [phishingTxRequest, setPhisingTxRequest] =
useState<ethers.Transaction | null>(null);
Expand Down
4 changes: 3 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1407,7 +1407,9 @@ export enum AddressType {
unknown = 'unknown',
}

export type VerifyAddressResponse = IContract;
export type VerifyAddressResponse = {
address: Record<ChainId, IContract>;
};

export interface MobXStoreFromRealm {
realmSchemaName: string;
Expand Down

0 comments on commit af92ba7

Please sign in to comment.