Skip to content

Commit

Permalink
feat: te acceleration for multi-guardian
Browse files Browse the repository at this point in the history
  • Loading branch information
aelf-lxy committed Nov 13, 2024
1 parent 6e5179f commit f6ab4f3
Show file tree
Hide file tree
Showing 19 changed files with 446 additions and 303 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
"devDependencies": {
"@babel/parser": "^7.24.0",
"@biomejs/biome": "^1.4.1",
"@changesets/changelog-git": "^0.2.1-alpha.3",
"@changesets/changelog-git": "^0.2.0",
"@changesets/cli": "^2.27.1",
"@ianvs/prettier-plugin-sort-imports": "^4.1.1",
"@testing-library/react": "^14.1.2",
Expand Down
6 changes: 3 additions & 3 deletions packages/base/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@
"@babel/core": "^7.24.7",
"@babel/preset-env": "^7.24.7",
"@babel/preset-typescript": "^7.24.7",
"@portkey/did-ui-react": "^2.14.0-alpha.6",
"@portkey/types": "^2.14.0-alpha.6",
"@portkey/did-ui-react": "^2.15.9",
"@portkey/types": "^2.15.9",
"@types/jest": "^29.5.12",
"babel-jest": "^29.7.0",
"jest": "^29.7.0",
Expand All @@ -44,7 +44,7 @@
"typescript": "^5.3.3"
},
"peerDependencies": {
"@portkey/did-ui-react": "^2.14.0-alpha.6"
"@portkey/did-ui-react": "^2.15.9"
},
"publishConfig": {
"registry": "https://registry.npmjs.org",
Expand Down
2 changes: 2 additions & 0 deletions packages/base/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,12 @@ export interface ICallContractParams<T> {
args: T;
chainId?: TChainId;
sendOptions?: SendOptions;
guardiansApproved?: any[];
}

export interface ISendOrViewAdapter<T> extends ICallContractParams<T> {
caContract: IContract;
guardiansApproved: any[];
type?: string;
}

Expand Down
9 changes: 5 additions & 4 deletions packages/bridge/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,22 +29,23 @@
},
"dependencies": {
"@aelf-web-login/wallet-adapter-base": "workspace:*",
"@portkey/services": "^2.14.0-alpha.6",
"@portkey/services": "^2.15.9",
"@reduxjs/toolkit": "^2.2.3",
"react": "^18.0.0",
"react-dom": "^18.0.0"
},
"devDependencies": {
"@portkey/types": "^2.14.0-alpha.6",
"@portkey/did-ui-react": "^2.14.0-alpha.6",
"@portkey/types": "^2.15.9",
"@portkey/did-ui-react": "^2.15.9",
"@portkey/utils": "^2.15.9",
"@types/react": "^18.3.1",
"@types/react-dom": "^18.3.0",
"antd": "4.24.14",
"father": "^4.3.8",
"typescript": "^5.3.3"
},
"peerDependencies": {
"@portkey/did-ui-react": "^2.14.0-alpha.6"
"@portkey/did-ui-react": "^2.15.9"
},
"publishConfig": {
"registry": "https://registry.npmjs.org",
Expand Down
53 changes: 50 additions & 3 deletions packages/bridge/src/bridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,19 @@ import {
setLoginError,
clearLoginError,
setLoginOnChainStatus,
store,
IsManagerReadOnlyStatusEnum,
setIsManagerReadOnlyStatus,
} from './store';
import { CreatePendingInfo, DIDWalletInfo, TelegramPlatform } from '@portkey/did-ui-react';
import { IBaseConfig } from '.';
import {
clearManagerReadonlyStatusInMainChain,
clearManagerReadonlyStatusInSideChain,
EE,
SET_GUARDIAN_APPROVAL_MODAL,
SET_GUARDIAN_LIST,
} from './utils';

const { isPortkeyApp } = utils;
let isDisconnectClicked = false;
Expand All @@ -41,9 +51,11 @@ class Bridge {
private _logoutReject: (arg: boolean) => void;
private _eventMap: Record<keyof IWalletAdapterEvents, any> = {} as IWalletAdapterEvents;
private _noCommonBaseModal: boolean;
private _sideChainId: TChainId;

constructor(wallets: WalletAdapter[], { noCommonBaseModal = false }: IBaseConfig) {
constructor(wallets: WalletAdapter[], { sideChainId, noCommonBaseModal = false }: IBaseConfig) {
this._noCommonBaseModal = noCommonBaseModal;
this._sideChainId = sideChainId;
this._wallets = wallets;
this._activeWallet = undefined;
this._loginResolve = () => {};
Expand Down Expand Up @@ -170,8 +182,43 @@ class Bridge {
) {
return null as R;
}
const rs = await this.activeWallet?.callSendMethod(props);
return rs as R;
const { isManagerReadOnlyStatus } = store.getState();
if (
this.isAAWallet &&
isManagerReadOnlyStatus === IsManagerReadOnlyStatusEnum.TRUE &&
props.methodName !== 'Approve'
) {
EE.emit(SET_GUARDIAN_APPROVAL_MODAL, true);
const { guardians, caHash, caAddress } = await this.getGuardianListFromGuardianApproveModal();
console.log('intg----getGuardianListFromGuardianApproveModal', guardians, caHash, caAddress);
const rs = await this.activeWallet?.callSendMethod({
...props,
guardiansApproved: guardians,
});
console.log('intg---rs of callSendMethod', rs);
dispatch(setIsManagerReadOnlyStatus(false));
if (props.chainId === 'AELF') {
clearManagerReadonlyStatusInSideChain(this._sideChainId, caAddress, caHash, guardians);
} else {
clearManagerReadonlyStatusInMainChain(caAddress, caHash, guardians);
}
return rs as R;
} else {
const rs = await this.activeWallet?.callSendMethod(props);
return rs as R;
}
};

getGuardianListFromGuardianApproveModal = async (): Promise<{
guardians: any[];
caHash: string;
caAddress: string;
}> => {
return new Promise((resolve) => {
EE.once(SET_GUARDIAN_LIST, (result) => {
resolve(result);
});
});
};

sendMultiTransaction = async <T>(
Expand Down
1 change: 1 addition & 0 deletions packages/bridge/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ type BaseConfigProviderProps = Omit<React.ComponentProps<typeof PortkeyProvider>
export interface IBaseConfig {
networkType: NetworkEnum;
chainId: TChainId;
sideChainId: TChainId;
keyboard?: boolean;
design?: SignInDesignEnum;
iconSrcForSocialDesign?: string;
Expand Down
20 changes: 20 additions & 0 deletions packages/bridge/src/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,20 @@ import { createSlice, configureStore, EnhancedStore } from '@reduxjs/toolkit';
import { TWalletInfo, WalletTypeEnum, TWalletError } from '@aelf-web-login/wallet-adapter-base';
import { LoginStatusEnum } from '@portkey/types';

export enum IsManagerReadOnlyStatusEnum {
INIT = 'INIT',
TRUE = 'TRUE',
FALSE = 'FALSE',
}

type TState = {
walletInfo: TWalletInfo;
isLocking: boolean;
walletType: WalletTypeEnum;
loginError: TWalletError | null;
loginOnChainStatus: LoginStatusEnum;
approveGuardians: any[];
isManagerReadOnlyStatus: IsManagerReadOnlyStatusEnum;
};

const initialState: TState = {
Expand All @@ -16,6 +24,8 @@ const initialState: TState = {
walletType: WalletTypeEnum.unknown,
loginError: null,
loginOnChainStatus: LoginStatusEnum.INIT,
approveGuardians: [],
isManagerReadOnlyStatus: IsManagerReadOnlyStatusEnum.INIT,
};

const aelfWebLoginSlice = createSlice({
Expand Down Expand Up @@ -46,6 +56,14 @@ const aelfWebLoginSlice = createSlice({
setLoginOnChainStatus: (state, action) => {
state.loginOnChainStatus = action.payload;
},
setApproveGuardians: (state, action) => {
state.approveGuardians = action.payload;
},
setIsManagerReadOnlyStatus: (state, action) => {
state.isManagerReadOnlyStatus = action.payload
? IsManagerReadOnlyStatusEnum.TRUE
: IsManagerReadOnlyStatusEnum.FALSE;
},
},
});

Expand Down Expand Up @@ -86,6 +104,8 @@ export const {
setLoginError,
clearLoginError,
setLoginOnChainStatus,
setApproveGuardians,
setIsManagerReadOnlyStatus,
} = aelfWebLoginSlice.actions;

export type AppDispatch = typeof store.dispatch;
Expand Down
43 changes: 21 additions & 22 deletions packages/bridge/src/ui.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import {
enhancedLocalStorage,
PORTKEYAA,
OperationTypeEnum,
// EventEmitter,
} from '@aelf-web-login/wallet-adapter-base';
import { Bridge } from './bridge';
import {
Expand All @@ -26,6 +25,7 @@ import { IBaseConfig } from '.';
import { Modal, Button, Typography, Drawer } from 'antd';
import useTelegram from './useTelegram';
import './ui.css';
import { EE, SET_GUARDIAN_APPROVAL_MODAL } from './utils';

export interface IConfirmLogoutDialogProps {
title: string;
Expand Down Expand Up @@ -77,8 +77,6 @@ interface ISignInModalProps {
}
const { isMobile } = utils;

// const EE = new EventEmitter();

const ConfirmLogoutDialog = (props: Partial<IConfirmLogoutDialogProps>) => {
const { title, subTitle, okTxt, cancelTxt, visible, onOk, onCancel, width, mobileWidth } = {
...defaultProps,
Expand Down Expand Up @@ -241,8 +239,6 @@ const SignInModal: React.FC<ISignInModalProps> = (props: ISignInModalProps) => {
handleTelegram,
currentLifeCircle,
guardianList,
approvalVisible,
setApprovalVisible,
caHash,
originChainId,
onTGSignInApprovalSuccess,
Expand All @@ -253,6 +249,7 @@ const SignInModal: React.FC<ISignInModalProps> = (props: ISignInModalProps) => {
baseConfig.networkType,
bridgeInstance,
setIsShowWrapper,
EE,
);
const filteredWallets = wallets.filter((ele) => ele.name !== PORTKEYAA);
const isMobileDevice = isMobile();
Expand All @@ -273,7 +270,10 @@ const SignInModal: React.FC<ISignInModalProps> = (props: ISignInModalProps) => {
const isToggleAccountRef = useRef(false);

useEffect(() => {
console.log('----------------------2', TelegramPlatform.isTelegramPlatform());
console.log(
'----------------------the current environment is telegram:',
TelegramPlatform.isTelegramPlatform(),
);
if (!TelegramPlatform.isTelegramPlatform()) {
return;
}
Expand Down Expand Up @@ -301,7 +301,7 @@ const SignInModal: React.FC<ISignInModalProps> = (props: ISignInModalProps) => {
if (enableAcceleration) {
ConfigProvider.setGlobalConfig({
globalLoadingHandler: {
onSetLoading: (loadingInfo) => {
onSetLoading: (loadingInfo: any) => {
console.log(loadingInfo, 'loadingInfo===');
},
},
Expand All @@ -314,7 +314,7 @@ const SignInModal: React.FC<ISignInModalProps> = (props: ISignInModalProps) => {
if (enableAcceleration) {
ConfigProvider.setGlobalConfig({
globalLoadingHandler: {
onSetLoading: (loadingInfo) => {
onSetLoading: (loadingInfo: any) => {
console.log(loadingInfo, 'loadingInfo===');
},
},
Expand Down Expand Up @@ -519,17 +519,17 @@ const SignInModal: React.FC<ISignInModalProps> = (props: ISignInModalProps) => {
);
}, [onForgetPinHandler]);

// const [showGuardianApprovalModal, setShowGuardianApprovalModal] = useState(false);
const [showGuardianApprovalModal, setShowGuardianApprovalModal] = useState(false);

// useEffect(() => {
// const hander = (args: boolean) => {
// setShowGuardianApprovalModal(args);
// };
// EE.on('SET_GLOBAL_LOADING_1', hander);
// return () => {
// EE.off('SET_GLOBAL_LOADING_1', hander);
// };
// }, []);
useEffect(() => {
const hander = (isShow: boolean) => {
setShowGuardianApprovalModal(isShow);
};
EE.on(SET_GUARDIAN_APPROVAL_MODAL, hander);
return () => {
EE.off(SET_GUARDIAN_APPROVAL_MODAL, hander);
};
}, []);

return (
// <PortkeyProvider networkType={baseConfig.networkType} theme="dark">
Expand Down Expand Up @@ -575,17 +575,16 @@ const SignInModal: React.FC<ISignInModalProps> = (props: ISignInModalProps) => {

{guardianList?.length && (
<GuardianApprovalModal
open={approvalVisible}
isAsyncVerify
open={showGuardianApprovalModal}
networkType={baseConfig.networkType}
caHash={caHash}
originChainId={originChainId}
targetChainId={baseConfig.chainId}
guardianList={guardianList}
operationType={OperationTypeEnum.communityRecovery}
operationDetails={getOperationDetails(OperationTypeEnum.communityRecovery)}
onClose={() => setApprovalVisible(false)}
onBack={() => setApprovalVisible(false)}
onClose={() => EE.emit(SET_GUARDIAN_APPROVAL_MODAL, false)}
onBack={() => EE.emit(SET_GUARDIAN_APPROVAL_MODAL, false)}
onApprovalSuccess={onTGSignInApprovalSuccess}
/>
)}
Expand Down
Loading

0 comments on commit f6ab4f3

Please sign in to comment.