Skip to content

Commit

Permalink
Testnet tokens isolation (#668)
Browse files Browse the repository at this point in the history
* remove bcd

* add whitelist only for mainnet

* fix logs
  • Loading branch information
letier3110 authored Jun 9, 2022
1 parent 3bd393d commit 64aa321
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 111 deletions.
16 changes: 0 additions & 16 deletions src/lib/better-call-dev/base.ts

This file was deleted.

2 changes: 0 additions & 2 deletions src/lib/better-call-dev/index.ts

This file was deleted.

66 changes: 0 additions & 66 deletions src/lib/better-call-dev/tokens.ts

This file was deleted.

6 changes: 0 additions & 6 deletions src/lib/temple/activity/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import BigNumber from 'bignumber.js';

import { BcdTokenTransfer } from 'lib/better-call-dev';

export function tryParseTokenTransfers(
parameters: any,
destination: string,
Expand All @@ -26,10 +24,6 @@ export function toTokenId(contractAddress: string, tokenId: string | number = 0)
return `${contractAddress}_${tokenId}`;
}

export function getBcdTokenTransferId(tokenTrans: BcdTokenTransfer) {
return `${tokenTrans.hash}_${tokenTrans.nonce}`;
}

export function getTzktTokenTransferId(hash: string, nonce?: number) {
const nonceStr = nonce ? `_${nonce}` : '';
return `${hash}${nonceStr}`;
Expand Down
31 changes: 12 additions & 19 deletions src/lib/temple/front/sync-tokens.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import { useCallback, useEffect, useMemo, useRef } from 'react';
import { useCallback, useEffect, useRef } from 'react';

import BigNumber from 'bignumber.js';
import constate from 'constate';
import { trigger } from 'swr';

import { BCD_NETWORKS_NAMES, BcdNetwork } from 'lib/better-call-dev';
import {
useChainId,
useAccount,
Expand All @@ -24,6 +23,8 @@ import { getTokensMetadata } from 'lib/templewallet-api';
import { fetchWhitelistTokenSlugs } from 'lib/templewallet-api/whitelist-tokens';
import { getTokenBalances, getTokenBalancesCount, TzktAccountTokenBalance, TZKT_API_BASE_URLS } from 'lib/tzkt';

import { TempleChainId } from '../types';

export const [SyncTokensProvider] = constate(() => {
const chainId = useChainId(true)!;
const { publicKeyHash: accountPkh } = useAccount();
Expand All @@ -32,15 +33,9 @@ export const [SyncTokensProvider] = constate(() => {
useTokensMetadata();
const usdPrices = useUSDPrices();

const networkId = useMemo(
() => (isKnownChainId(chainId) ? BCD_NETWORKS_NAMES.get(chainId) : undefined) ?? null,
[chainId]
);

const sync = useCallback(async () => {
makeSync(
accountPkh,
networkId,
chainId,
allTokensBaseMetadataRef,
setTokensBaseMetadata,
Expand All @@ -50,7 +45,6 @@ export const [SyncTokensProvider] = constate(() => {
);
}, [
accountPkh,
networkId,
chainId,
allTokensBaseMetadataRef,
setTokensBaseMetadata,
Expand All @@ -64,17 +58,17 @@ export const [SyncTokensProvider] = constate(() => {
syncRef.current = sync;
}, [sync]);

const networkIdRef = useRef(networkId);
if (networkIdRef.current !== networkId) {
networkIdRef.current = networkId;
const networkIdRef = useRef(chainId);
if (networkIdRef.current !== chainId) {
networkIdRef.current = chainId;
}

useEffect(() => {
if (!networkId) {
if (!chainId) {
return;
}

const isTheSameNetwork = () => networkId === networkIdRef.current;
const isTheSameNetwork = () => chainId === networkIdRef.current;
let timeoutId: any;

const syncAndDefer = async () => {
Expand All @@ -92,7 +86,7 @@ export const [SyncTokensProvider] = constate(() => {
syncAndDefer();

return () => clearTimeout(timeoutId);
}, [networkId, accountPkh]);
}, [chainId, accountPkh]);
});

async function fetchTzktTokenBalances(chainId: string, address: string) {
Expand Down Expand Up @@ -130,22 +124,21 @@ async function fetchTzktTokenBalances(chainId: string, address: string) {

const makeSync = async (
accountPkh: string,
networkId: BcdNetwork | null,
chainId: string,
allTokensBaseMetadataRef: any,
setTokensBaseMetadata: any,
setTokensDetailedMetadata: any,
usdPrices: Record<string, string>,
fetchMetadata: any
) => {
if (!networkId) return;
const mainnet = networkId === 'mainnet';
if (!chainId) return;
const mainnet = chainId === TempleChainId.Mainnet;

const [tzktTokens, displayedFungibleTokens, displayedCollectibleTokens, whitelistTokenSlugs] = await Promise.all([
fetchTzktTokenBalances(chainId, accountPkh),
fetchDisplayedFungibleTokens(chainId, accountPkh),
fetchCollectibleTokens(chainId, accountPkh, true),
fetchWhitelistTokenSlugs()
fetchWhitelistTokenSlugs(chainId)
]);

const tzktTokensMap = new Map(
Expand Down
6 changes: 4 additions & 2 deletions src/lib/templewallet-api/whitelist-tokens.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import axios from 'axios';

import { TempleChainId } from 'lib/temple/types';

interface TokenListItem {
contractAddress: 'tez' | string;
fa2TokenId?: number;
Expand Down Expand Up @@ -30,11 +32,11 @@ const WHITELIST_TOKENS_BASE_URL = 'https://raw.githubusercontent.com/madfish-sol

const api = axios.create({ baseURL: WHITELIST_TOKENS_BASE_URL });

export const fetchWhitelistTokenSlugs = () =>
export const fetchWhitelistTokenSlugs = (chainId: string) =>
api
.get<WhitelistResponse>('tokens/quipuswap.whitelist.json')
.then(response =>
response.data.tokens
response.data.tokens && chainId === TempleChainId.Mainnet
? response.data.tokens
.map(token =>
token.contractAddress === 'tez' ? 'tez' : `${token.contractAddress}_${token.fa2TokenId ?? 0}`
Expand Down

0 comments on commit 64aa321

Please sign in to comment.