Skip to content

Commit

Permalink
Merge branch '436-feature-complete-type-checks-on-token-transactions'…
Browse files Browse the repository at this point in the history
… into update-store-selector-and-load-from-hooks
  • Loading branch information
zzggo committed Feb 14, 2025
2 parents 8911eda + 982327a commit bfa5c39
Show file tree
Hide file tree
Showing 22 changed files with 760 additions and 527 deletions.
4 changes: 2 additions & 2 deletions e2e/utils/global.setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import {
// for user register and login
setup('setup new wallet or login if already registered', async ({ page, extensionId }) => {
// let playwright know this is going to be slow
// Wait up to 2 minutes to setup an account
setup.setTimeout(500_000);
// Wait up to 10 minutes to setup an account
setup.setTimeout(600_000);
// Create a new page and navigate to extension
// Navigate and wait for network to be idle

Expand Down
300 changes: 153 additions & 147 deletions src/background/controller/wallet.ts

Large diffs are not rendered by default.

229 changes: 97 additions & 132 deletions src/background/service/openapi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ const dataConfig: Record<string, OpenApiConfigValue> = {
params: ['transaction', 'message'],
},
get_transfers: {
path: '/v1/account/transfers',
path: '/api/v1/account/transfers',
method: 'get',
params: ['address', 'after', 'limit'],
},
Expand Down Expand Up @@ -368,6 +368,31 @@ const dataConfig: Record<string, OpenApiConfigValue> = {
method: 'get',
params: [],
},
get_ft_list: {
path: '/api/v3/fts',
method: 'get',
params: ['network', 'chain_type'],
},
get_nft_list: {
path: '/api/v3/nfts',
method: 'get',
params: ['network', 'chain_type'],
},
};

const defaultFlowToken = {
name: 'Flow',
address: '0x4445e7ad11568276',
contractName: 'FlowToken',
path: {
balance: '/public/flowTokenBalance',
receiver: '/public/flowTokenReceiver',
vault: '/storage/flowTokenVault',
},
logoURI:
'https://cdn.jsdelivr.net/gh/FlowFans/flow-token-list@main/token-registry/A.1654653399040a61.FlowToken/logo.svg',
decimals: 8,
symbol: 'flow',
};

const recordFetch = async (response, responseData, ...args: Parameters<typeof fetch>) => {
Expand Down Expand Up @@ -987,37 +1012,27 @@ class OpenApiService {
return data;
};

getNFTList = async (address: string, offset: number, limit: number) => {
const config = this.store.config.nft_list;
getNFTList = async (network: string) => {
const childType = await userWalletService.getActiveWallet();
let chainType = 'flow';
if (childType === 'evm') {
chainType = 'evm';
}

const nftList = await storage.getExpiry(`NFTList${network}${chainType}`);
if (nftList && nftList.length > 0) {
return nftList;
}
const config = this.store.config.get_nft_list;
const data = await this.sendRequest(config.method, config.path, {
address,
offset,
limit,
network,
chain_type: chainType,
});
return data;
};

// getNFTListV2 = async (address: string, offset: number, limit: number) => {
// const alchemyAPI = (await storage.get('alchemyAPI')) || false;
// const config = alchemyAPI
// ? this.store.config.nft_list_v2
// : this.store.config.nft_list_lilico_v2;
// const data = await this.sendRequest(config.method, config.path, {
// address,
// offset,
// limit,
// });
// return data;
// };
storage.setExpiry(`NFTList${network}${chainType}`, data, 600000);

// getNFTCollectionV2 = async (address: string) => {
// // const alchemyAPI = await storage.get('alchemyAPI') || false
// const config = this.store.config.nft_collections_lilico_v2;
// const data = await this.sendRequest(config.method, config.path, {
// address,
// });
// return data;
// };
return data;
};

getNFTMetadata = async (
address: string,
Expand Down Expand Up @@ -1230,11 +1245,17 @@ class OpenApiService {

getTransfers = async (address: string, after = '', limit: number) => {
const config = this.store.config.get_transfers;
const data = await this.sendRequest(config.method, config.path, {
address,
after,
limit,
});
const data = await this.sendRequest(
config.method,
config.path,
{
address,
after,
limit,
},
{},
WEB_NEXT_URL
);

return data;
};
Expand Down Expand Up @@ -1321,7 +1342,7 @@ class OpenApiService {
if (!network) {
network = await userWalletService.getNetwork();
}
const tokens = await this.getTokenListFromGithub(network);
const tokens = await this.getTokenList(network);
// const coins = await remoteFetch.flowCoins();
return tokens.find((item) => item.symbol.toLowerCase() === name.toLowerCase());
};
Expand All @@ -1331,15 +1352,15 @@ class OpenApiService {
network = await userWalletService.getNetwork();
}

const tokens = await this.getEvmListFromGithub(network);
const tokens = await this.getEvmList(network);

const tokenInfo = tokens.find((item) => item.symbol.toLowerCase() === name.toLowerCase());

if (tokenInfo && isValidEthereumAddress(tokenInfo.address)) {
return tokenInfo;
}

const freshTokens = await this.refreshEvmGitToken(network);
const freshTokens = await this.refreshEvmToken(network);
return freshTokens.find((item) => item.symbol.toLowerCase() === name.toLowerCase());
};

Expand Down Expand Up @@ -1372,20 +1393,19 @@ class OpenApiService {
return !!flags[featureFlag];
};

// @ts-ignore
getAllTokenInfo = async (fiterNetwork = true): Promise<TokenInfo[]> => {
getAllTokenInfo = async (filterNetwork = true): Promise<TokenInfo[]> => {
const network = await userWalletService.getNetwork();
const list = await this.getTokenListFromGithub(network);
return fiterNetwork ? list.filter((item) => item.address) : list;
const list = await this.getTokenList(network);
return filterNetwork ? list.filter((item) => item.address) : list;
};

getAllNft = async (fiterNetwork = true): Promise<NFTModel[]> => {
getAllNft = async (filterNetwork = true): Promise<NFTModel[]> => {
const list = await remoteFetch.nftCollection();
// const network = await userWalletService.getNetwork();
return list;
};

getAllNftV2 = async (fiterNetwork = true): Promise<NFTModel[]> => {
getAllNftV2 = async (filterNetwork = true): Promise<NFTModel[]> => {
const list = await remoteFetch.nftv2Collection();
// const network = await userWalletService.getNetwork();
return list;
Expand Down Expand Up @@ -1450,61 +1470,26 @@ class OpenApiService {
return balance;
};

fetchGitTokenList = async (network, chainType, childType) => {
const isProduction = process.env.NODE_ENV === 'production';
let url;

if (isProduction) {
url = `https://raw.githubusercontent.com/Outblock/token-list-jsons/outblock/jsons/${network}/${chainType}/default.json`;
} else if (
!isProduction &&
childType !== 'evm' &&
(network === 'testnet' || network === 'mainnet')
) {
url = `https://raw.githubusercontent.com/Outblock/token-list-jsons/outblock/jsons/${network}/${chainType}/dev.json`;
} else {
url = `https://raw.githubusercontent.com/Outblock/token-list-jsons/outblock/jsons/${network}/${chainType}/default.json`;
}
fetchFTList = async (network: string, chainType: string) => {
const config = this.store.config.get_ft_list;
const data = await this.sendRequest(
config.method,
config.path,
{
network,
chain_type: chainType,
},
{},
WEB_NEXT_URL
);

const response = await fetch(url);
const { tokens = [] } = await response.json();
const hasFlowToken = tokens.some((token) => token.symbol.toLowerCase() === 'flow');
if (!hasFlowToken) {
tokens.push({
name: 'Flow',
address: '0x4445e7ad11568276',
contractName: 'FlowToken',
path: {
balance: '/public/flowTokenBalance',
receiver: '/public/flowTokenReceiver',
vault: '/storage/flowTokenVault',
},
logoURI:
'https://cdn.jsdelivr.net/gh/FlowFans/flow-token-list@main/token-registry/A.1654653399040a61.FlowToken/logo.svg',
decimals: 8,
symbol: 'flow',
});
}
return tokens;
return data.tokens;
};

addFlowTokenIfMissing = (tokens) => {
const hasFlowToken = tokens.some((token) => token.symbol.toLowerCase() === 'flow');
if (!hasFlowToken) {
tokens.push({
name: 'Flow',
address: '0x4445e7ad11568276',
contractName: 'FlowToken',
path: {
balance: '/public/flowTokenBalance',
receiver: '/public/flowTokenReceiver',
vault: '/storage/flowTokenVault',
},
logoURI:
'https://cdn.jsdelivr.net/gh/FlowFans/flow-token-list@main/token-registry/A.1654653399040a61.FlowToken/logo.svg',
decimals: 8,
symbol: 'flow',
});
tokens.push(defaultFlowToken);
}
};

Expand Down Expand Up @@ -1535,82 +1520,62 @@ class OpenApiService {
});
};

getTokenListFromGithub = async (network) => {
getTokenList = async (network) => {
const childType = await userWalletService.getActiveWallet();
const chainType = childType === 'evm' ? 'evm' : 'flow';

const gitToken = await storage.getExpiry(`GitTokenList${network}${chainType}`);
if (gitToken) return gitToken;
const ftList = await storage.getExpiry(`TokenList${network}${chainType}`);
if (ftList) return ftList;

const tokens = await this.fetchGitTokenList(network, chainType, childType);
const tokens = await this.fetchFTList(network, chainType);

if (chainType === 'evm') {
const evmCustomToken = (await storage.get(`${network}evmCustomToken`)) || [];
this.mergeCustomTokens(tokens, evmCustomToken);
}

storage.setExpiry(`GitTokenList${network}${chainType}`, tokens, 600000);
storage.setExpiry(`TokenList${network}${chainType}`, tokens, 600000);
return tokens;
};

getEvmListFromGithub = async (network) => {
getEvmList = async (network) => {
const chainType = 'evm';

const gitToken = await storage.getExpiry(`GitTokenList${network}${chainType}`);
if (gitToken) return gitToken;
const ftList = await storage.getExpiry(`TokenList${network}${chainType}`);
if (ftList) return ftList;

const tokens = await this.fetchGitTokenList(network, chainType, chainType);
const tokens = await this.fetchFTList(network, chainType);

if (chainType === 'evm') {
const evmCustomToken = (await storage.get(`${network}evmCustomToken`)) || [];
this.mergeCustomTokens(tokens, evmCustomToken);
}

storage.setExpiry(`GitTokenList${network}${chainType}`, tokens, 600000);
storage.setExpiry(`TokenList${network}${chainType}`, tokens, 600000);
return tokens;
};

refreshEvmGitToken = async (network) => {
refreshEvmToken = async (network) => {
const chainType = 'evm';
let gitToken = await storage.getExpiry(`GitTokenList${network}${chainType}`);
if (!gitToken) gitToken = await this.fetchGitTokenList(network, chainType, 'evm');
let ftList = await storage.getExpiry(`TokenList${network}${chainType}`);
if (!ftList) ftList = await this.fetchFTList(network, chainType);

const evmCustomToken = (await storage.get(`${network}evmCustomToken`)) || [];
this.mergeCustomTokens(gitToken, evmCustomToken);
this.mergeCustomTokens(ftList, evmCustomToken);

storage.setExpiry(`GitTokenList${network}${chainType}`, gitToken, 600000);
storage.setExpiry(`TokenList${network}${chainType}`, ftList, 600000);

return gitToken;
return ftList;
};

refreshCustomEvmGitToken = async (network) => {
refreshCustomEvmToken = async (network) => {
const chainType = 'evm';
const gitToken = await this.fetchGitTokenList(network, chainType, 'evm');
const ftList = await this.fetchFTList(network, chainType);

const evmCustomToken = (await storage.get(`${network}evmCustomToken`)) || [];
this.mergeCustomTokens(gitToken, evmCustomToken);

storage.setExpiry(`GitTokenList${network}${chainType}`, gitToken, 600000);
};
this.mergeCustomTokens(ftList, evmCustomToken);

getNFTListFromGithub = async (network: string) => {
const childType = await userWalletService.getActiveWallet();
let chainType = 'flow';
if (childType === 'evm') {
chainType = 'evm';
}
const gitToken = await storage.getExpiry(`GitNFTList${network}${chainType}`);
if (gitToken && gitToken.length > 0) {
return gitToken;
} else {
const response = await fetch(
`https://raw.githubusercontent.com/Outblock/token-list-jsons/outblock/jsons/${network}/flow/nfts.json`
);
const res = await response.json();
const { data = {} } = res;
storage.setExpiry(`GitNFTList${network}${chainType}`, data, 600000);
return data;
}
storage.setExpiry(`TokenList${network}${chainType}`, ftList, 600000);
};

getEnabledTokenList = async (network = '') => {
Expand All @@ -1620,7 +1585,7 @@ class OpenApiService {
}
const address = await userWalletService.getCurrentAddress();

const tokenList = await this.getTokenListFromGithub(network);
const tokenList = await this.getTokenList(network);
let values;
const isChild = await userWalletService.getActiveWallet();
try {
Expand Down
2 changes: 1 addition & 1 deletion src/background/utils/remoteConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ class fetchRemoteConfig {
}
if (expire < now.getTime()) {
try {
const result = await openapi.getNFTListFromGithub(network);
const result = await openapi.getNFTList(network);
// fetch(`${baseURL}/fetchNFTList`);
// const result = await coins.json();
this.nftState[network].result = result;
Expand Down
2 changes: 1 addition & 1 deletion src/constant/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ export const INTERNAL_REQUEST_SESSION = {
export const INITIAL_OPENAPI_URL =
process.env.NODE_ENV === 'production' ? 'https://api.lilico.app' : 'https://dev.lilico.app';
export const WEB_NEXT_URL =
process.env.NODE_ENV === 'production' ? 'https://lilico.app' : 'https://test.lilico.app';
process.env.NODE_ENV === 'production' ? 'https://lilico.app' : process.env.API_BASE_URL;
// export const INITIAL_OPENAPI_URL = 'http://localhost:4747';

// export const INITIAL_OPENAPI_URL = process.env.INITIAL_OPENAPI_URL!;
Expand Down
Loading

0 comments on commit bfa5c39

Please sign in to comment.