Skip to content

Commit

Permalink
Merge branch 'feat/update-rpc' into tmp/20241226
Browse files Browse the repository at this point in the history
  • Loading branch information
cs1707 committed Dec 26, 2024
2 parents 8427298 + 18a5f63 commit 1f3d264
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 22 deletions.
47 changes: 43 additions & 4 deletions src/background/controller/wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1783,12 +1783,43 @@ export class WalletController extends BaseController {
getGasAccountSig = gasAccountService.getGasAccountSig;
setGasAccountSig = gasAccountService.setGasAccountSig;

setCustomRPC = RPCService.setRPC;
removeCustomRPC = RPCService.removeCustomRPC;
setCustomRPC = (chainEnum: CHAINS_ENUM, url: string) => {
RPCService.setRPC(chainEnum, url);
const chain = findChain({
enum: chainEnum,
});
if (chain?.isTestnet && RPCService.hasCustomRPC(chainEnum)) {
customTestnetService.setCustomRPC({ chainId: chain.id, url: url });
}
};
removeCustomRPC = (chainEnum: CHAINS_ENUM) => {
RPCService.removeCustomRPC(chainEnum);
const chain = findChain({
enum: chainEnum,
});
if (chain?.isTestnet) {
customTestnetService.removeCustomRPC(chain.id);
}
};
getAllCustomRPC = RPCService.getAllRPC;
getCustomRpcByChain = RPCService.getRPCByChain;
pingCustomRPC = RPCService.ping;
setRPCEnable = RPCService.setRPCEnable;
setRPCEnable = (chainEnum: CHAINS_ENUM, enable: boolean) => {
RPCService.setRPCEnable(chainEnum, enable);
const chain = findChain({
enum: chainEnum,
});
if (chain?.isTestnet) {
if (enable) {
customTestnetService.setCustomRPC({
chainId: chain.id,
url: RPCService.getRPCByChain(chainEnum).url,
});
} else {
customTestnetService.removeCustomRPC(chain.id);
}
}
};
validateRPC = async (url: string, chainId: number) => {
const chain = findChain({
id: chainId,
Expand Down Expand Up @@ -4758,7 +4789,15 @@ export class WalletController extends BaseController {
return res;
};
updateCustomTestnet = customTestnetService.update;
removeCustomTestnet = customTestnetService.remove;
removeCustomTestnet = (chainId: number) => {
const chain = findChain({
id: chainId,
});
if (chain?.enum) {
RPCService.removeCustomRPC(chain.enum);
}
customTestnetService.remove(chainId);
};
getCustomTestnetList = customTestnetService.getList;

getCustomTestnetNonce = async ({
Expand Down
52 changes: 37 additions & 15 deletions src/background/service/customTestnet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,7 @@ import { createPersistStore, withTimeout } from 'background/utils';
import { BigNumber } from 'bignumber.js';
import { intToHex } from 'ethereumjs-util';
import { omit, sortBy } from 'lodash';
import {
Client,
createClient,
defineChain,
erc20Abi,
http,
isAddress,
} from 'viem';
import { createClient, defineChain, erc20Abi, http, isAddress } from 'viem';
import {
estimateGas,
getBalance,
Expand All @@ -24,6 +17,8 @@ import {
} from 'viem/actions';
import { http as axios } from '../utils/http';
import { matomoRequestEvent } from '@/utils/matomo-request';
import RPCService, { RPCServiceStore } from './rpc';
import { storage } from '../webapi';

const MAX_READ_CONTRACT_TIME = 8000;

Expand Down Expand Up @@ -89,7 +84,7 @@ class CustomTestnetService {
customTokenList: [],
};

chains: Record<string, Client> = {};
chains: Record<string, ReturnType<typeof createClientByChain>> = {};

logos: Record<
string,
Expand All @@ -100,16 +95,22 @@ class CustomTestnetService {
> = {};

init = async () => {
const storage = await createPersistStore<CutsomTestnetServiceStore>({
const storageCache = await createPersistStore<CutsomTestnetServiceStore>({
name: 'customTestnet',
template: {
customTestnet: {},
customTokenList: [],
},
});
this.store = storage || this.store;
this.store = storageCache || this.store;
const rpcStorage: RPCServiceStore = await storage.get('rpc');
Object.values(this.store.customTestnet).forEach((chain) => {
const client = createClientByChain(chain);
const config =
rpcStorage.customRPC[chain.enum] &&
rpcStorage.customRPC[chain.enum]?.enable
? { ...chain, rpcUrl: rpcStorage.customRPC[chain.enum].url }
: chain;
const client = createClientByChain(config);
this.chains[chain.id] = client;
});
this.syncChainList();
Expand Down Expand Up @@ -176,12 +177,14 @@ class CustomTestnetService {
},
};
}

const testnetChain = createTestnetChain(chain);
this.store.customTestnet = {
...this.store.customTestnet,
[chain.id]: createTestnetChain(chain),
[chain.id]: testnetChain,
};
this.chains[chain.id] = createClientByChain(chain);
if (!RPCService.hasCustomRPC(testnetChain.enum)) {
this.chains[chain.id] = createClientByChain(chain);
}
this.syncChainList();

if (this.getList().length) {
Expand Down Expand Up @@ -677,6 +680,25 @@ class CustomTestnetService {
return {};
}
};

setCustomRPC = ({ chainId, url }: { chainId: number; url: string }) => {
const client = this.getClient(chainId);
if (client) {
this.chains[chainId] = createClientByChain({
...this.store.customTestnet[chainId],
rpcUrl: url,
});
}
};

removeCustomRPC = (chainId: number) => {
const client = this.getClient(chainId);
if (client) {
this.chains[chainId] = createClientByChain(
this.store.customTestnet[chainId]
);
}
};
}

export const customTestnetService = new CustomTestnetService();
Expand Down
4 changes: 4 additions & 0 deletions src/ui/views/CommonPopup/AssetList/TestnetChainList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ export const TestnetChainList = ({
// );
// }

if (!chainList?.length) {
return null;
}

return (
<div
className={clsx(
Expand Down
4 changes: 2 additions & 2 deletions src/ui/views/CustomRPC/index.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import ThemeIcon from '@/ui/component/ThemeMode/ThemeIcon';
import { useRabbyDispatch, useRabbySelector } from '@/ui/store';
import { findChain, findChainByEnum } from '@/utils/chain';
import { findChain, findChainByEnum, getTestnetChainList } from '@/utils/chain';
import { matomoRequestEvent } from '@/utils/matomo-request';
import { CHAINS_ENUM } from '@debank/common';
import { Button, Switch, message } from 'antd';
Expand Down Expand Up @@ -345,7 +345,7 @@ const CustomRPC = () => {
onChange={handleChainChanged}
onCancel={handleCancelSelectChain}
showRPCStatus
hideTestnetTab
hideTestnetTab={!getTestnetChainList().length}
/>
<EditRPCModal
visible={rpcModalVisible}
Expand Down
7 changes: 6 additions & 1 deletion src/ui/views/SendToken/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1263,7 +1263,12 @@ const SendToken = () => {
let tokenFromOrder: TokenItem | null = null;

const lastTimeToken = await wallet.getLastTimeSendToken(account.address);
if (lastTimeToken) {
if (
lastTimeToken &&
findChain({
serverId: lastTimeToken.chain,
})
) {
setCurrentToken(lastTimeToken);
} else {
const { firstChain } = await dispatch.chains.getOrderedChainList({
Expand Down

0 comments on commit 1f3d264

Please sign in to comment.