Skip to content

Commit

Permalink
chore: review changes
Browse files Browse the repository at this point in the history
  • Loading branch information
work-kevin-flynn committed Nov 14, 2024
1 parent fac1c17 commit 445123b
Show file tree
Hide file tree
Showing 9 changed files with 56 additions and 60 deletions.
7 changes: 4 additions & 3 deletions packages/base/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { SendOptions, IContract, LoginStatusEnum } from '@portkey/types';
import { BaseGuardianItem } from '@portkey/did-ui-react';
import { IContract, LoginStatusEnum, SendOptions } from '@portkey/types';

export enum WalletStateEnum {
// Wallet plug-in detected
Expand Down Expand Up @@ -52,12 +53,12 @@ export interface ICallContractParams<T> {
args: T;
chainId?: TChainId;
sendOptions?: SendOptions;
guardiansApproved?: any[];
approvedGuardians?: BaseGuardianItem[];
}

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

Expand Down
12 changes: 6 additions & 6 deletions packages/bridge/src/bridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ import {
clearManagerReadonlyStatusInSideChain,
EE,
SET_GUARDIAN_APPROVAL_MODAL,
SET_GUARDIAN_LIST,
SET_GUARDIAN_APPROVAL_PAYLOAD,
} from './utils';

const { isPortkeyApp } = utils;
Expand Down Expand Up @@ -189,11 +189,11 @@ class Bridge {
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 { guardians, caHash, caAddress } = await this.getApprovalModalGuardians();
console.log('intg----getApprovalModalGuardians', guardians, caHash, caAddress);
const rs = await this.activeWallet?.callSendMethod({
...props,
guardiansApproved: guardians,
approvedGuardians: guardians,
});
console.log('intg---rs of callSendMethod', rs);
dispatch(setIsManagerReadOnlyStatus(false));
Expand All @@ -209,13 +209,13 @@ class Bridge {
}
};

getGuardianListFromGuardianApproveModal = async (): Promise<{
getApprovalModalGuardians = async (): Promise<{
guardians: any[];
caHash: string;
caAddress: string;
}> => {
return new Promise((resolve) => {
EE.once(SET_GUARDIAN_LIST, (result) => {
EE.once(SET_GUARDIAN_APPROVAL_PAYLOAD, (result) => {
resolve(result);
});
});
Expand Down
15 changes: 8 additions & 7 deletions packages/bridge/src/store.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { createSlice, configureStore, EnhancedStore } from '@reduxjs/toolkit';
import { TWalletInfo, WalletTypeEnum, TWalletError } from '@aelf-web-login/wallet-adapter-base';
import { TWalletError, TWalletInfo, WalletTypeEnum } from '@aelf-web-login/wallet-adapter-base';
import { BaseGuardianItem } from '@portkey/did-ui-react';
import { LoginStatusEnum } from '@portkey/types';
import { configureStore, createSlice, EnhancedStore } from '@reduxjs/toolkit';

export enum IsManagerReadOnlyStatusEnum {
INIT = 'INIT',
Expand All @@ -14,7 +15,7 @@ type TState = {
walletType: WalletTypeEnum;
loginError: TWalletError | null;
loginOnChainStatus: LoginStatusEnum;
approveGuardians: any[];
approvedGuardians: BaseGuardianItem[];
isManagerReadOnlyStatus: IsManagerReadOnlyStatusEnum;
};

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

Expand Down Expand Up @@ -56,8 +57,8 @@ const aelfWebLoginSlice = createSlice({
setLoginOnChainStatus: (state, action) => {
state.loginOnChainStatus = action.payload;
},
setApproveGuardians: (state, action) => {
state.approveGuardians = action.payload;
setApprovedGuardians: (state, action) => {
state.approvedGuardians = action.payload;
},
setIsManagerReadOnlyStatus: (state, action) => {
state.isManagerReadOnlyStatus = action.payload
Expand Down Expand Up @@ -104,7 +105,7 @@ export const {
setLoginError,
clearLoginError,
setLoginOnChainStatus,
setApproveGuardians,
setApprovedGuardians,
setIsManagerReadOnlyStatus,
} = aelfWebLoginSlice.actions;

Expand Down
7 changes: 2 additions & 5 deletions packages/bridge/src/ui.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -522,12 +522,9 @@ const SignInModal: React.FC<ISignInModalProps> = (props: ISignInModalProps) => {
const [showGuardianApprovalModal, setShowGuardianApprovalModal] = useState(false);

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

Expand Down
36 changes: 16 additions & 20 deletions packages/bridge/src/useTelegram.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,15 @@ const useTelegram = (
}
console.log('intg-----------onCreatePendingHandler,');
//TODO: only muti-guardian need to execute 76-82
const isManagerReadOnly = await getIsManagerReadOnly(
chainId,
createPendingInfo.didWallet?.caInfo.caHash,
createPendingInfo.walletInfo.address,
);
caAddressRef.current = createPendingInfo.didWallet?.caInfo.caAddress ?? '';
dispatch(setIsManagerReadOnlyStatus(isManagerReadOnly));
if (guardianList?.length && guardianList.length > 1) {
const isManagerReadOnly = await getIsManagerReadOnly(
chainId,
createPendingInfo.didWallet?.caInfo.caHash,
createPendingInfo.walletInfo.address,
);
caAddressRef.current = createPendingInfo.didWallet?.caInfo.caAddress ?? '';
dispatch(setIsManagerReadOnlyStatus(isManagerReadOnly));
}
bridgeInstance.onPortkeyAAWalletCreatePending(createPendingInfo);
}, []);
const [currentLifeCircle, setCurrentLifeCircle] = useState<
Expand Down Expand Up @@ -255,17 +257,7 @@ const useTelegram = (
} else {
setLoading(false);
if (isTelegramPlatform && enableAcceleration) {
// console.log('intg-----------more guardian', signResult.value.guardianList);
// setGuardianList(signResult.value.guardianList || []);
// setTimeout(() => {
// setApprovalVisible(true);
// ConfigProvider.setGlobalConfig({
// globalLoadingHandler: undefined,
// });
// }, 500);

const guardianListFromSignResult = signResult.value.guardianList ?? [];
const resetGuardianList = guardianListFromSignResult.map((ele: any) => {
const resetGuardianList = (signResult.value.guardianList ?? []).map((ele: any) => {
return {
...ele,
status: null,
Expand Down Expand Up @@ -335,8 +327,12 @@ const useTelegram = (
const onTGSignInApprovalSuccess = useCallback(
async (guardians: any[]) => {
EE.emit(SET_GUARDIAN_APPROVAL_MODAL, false);
EE.emit(SET_GUARDIAN_LIST, { guardians, caHash, caAddress: caAddressRef.current });
dispatch(setApproveGuardians(guardians));
EE.emit(SET_GUARDIAN_APPROVAL_PAYLOAD, {
guardians,
caHash,
caAddress: caAddressRef.current,
});
dispatch(setApprovedGuardians(guardians));
},
[EE, caHash],
);
Expand Down
18 changes: 9 additions & 9 deletions packages/bridge/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { TChainId, EventEmitter } from '@aelf-web-login/wallet-adapter-base';

export const EE = new EventEmitter();
export const SET_GUARDIAN_APPROVAL_MODAL = 'SET_GUARDIAN_APPROVAL_MODAL';
export const SET_GUARDIAN_LIST = 'SET_GUARDIAN_LIST';
export const SET_GUARDIAN_APPROVAL_PAYLOAD = 'SET_GUARDIAN_APPROVAL_PAYLOAD';

const getCaContractBase = async (chainId: TChainId) => {
const chainInfo = await getChainInfo(chainId);
Expand All @@ -24,44 +24,44 @@ const getCaContractBase = async (chainId: TChainId) => {
const clearManagerReadonlyStatusInMainChain = async (
caAddress = '',
caHash = '',
guardiansApproved?: any[],
approvedGuardians?: any[],
) => {
console.log(
'intg-----clearManagerReadonlyStatusInMainChain',
caAddress,
caHash,
guardiansApproved,
approvedGuardians,
);
if (!guardiansApproved || guardiansApproved.length === 0) {
if (!approvedGuardians || approvedGuardians.length === 0) {
return;
}
const ca = await getCaContractBase('AELF');
await ca.callSendMethod('RemoveReadOnlyManager', caAddress, {
caHash,
guardiansApproved: guardiansApproved,
approvedGuardians: approvedGuardians,
});
};

const clearManagerReadonlyStatusInSideChain = async (
chainId: TChainId,
caAddress = '',
caHash = '',
guardiansApproved?: any[],
approvedGuardians?: any[],
) => {
console.log(
'intg-----clearManagerReadonlyStatusInSideChain',
chainId,
caAddress,
caHash,
guardiansApproved,
approvedGuardians,
);
if (!guardiansApproved || guardiansApproved.length === 0) {
if (!approvedGuardians || approvedGuardians.length === 0) {
return;
}
const ca = await getCaContractBase(chainId);
await ca.callSendMethod('RemoveReadOnlyManager', caAddress, {
caHash,
guardiansApproved: guardiansApproved,
approvedGuardians: approvedGuardians,
});
};

Expand Down
6 changes: 3 additions & 3 deletions packages/react/src/index.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
/* eslint-disable @typescript-eslint/no-empty-function */
import React, { useCallback, useMemo, useState, useSyncExternalStore } from 'react';
import { initBridge, IConfigProps, IBridgeAPI } from '@aelf-web-login/wallet-adapter-bridge';
import {
ConnectedWallet,
enhancedLocalStorage,
PORTKEYAA,
} from '@aelf-web-login/wallet-adapter-base';
import { IBridgeAPI, IConfigProps, initBridge } from '@aelf-web-login/wallet-adapter-bridge';
import React, { useCallback, useMemo, useState, useSyncExternalStore } from 'react';

const HOOK_ERROR_MESSAGE =
'Must call the provided initialization method`init` method before using hooks.';
Expand Down Expand Up @@ -153,7 +153,7 @@ export function useConnectWallet() {
isConnected: isConnected,
loginError: stateFromStore.loginError,
loginOnChainStatus: stateFromStore.loginOnChainStatus,
approvedGuardians: stateFromStore.approveGuardians,
approvedGuardians: stateFromStore.approvedGuardians,
lock,
getAccountByChainId,
getWalletSyncIsCompleted,
Expand Down
3 changes: 2 additions & 1 deletion packages/starter/src/Demo/demo.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ const GRAPHQL_SERVER =
'https://dapp-aa-portkey-test.portkey.finance/aefinder-v2/api/app/graphql/portkey';
const CONNECT_SERVER = 'https://auth-aa-portkey-test.portkey.finance';
const SERVICE_SERVER = 'https://aa-portkey-test.portkey.finance';
const TELEGRAM_BOT_ID = '7781140664';
const TELEGRAM_BOT_ID = '8158866605';

const didConfig = {
graphQLUrl: GRAPHQL_SERVER,
Expand Down Expand Up @@ -151,6 +151,7 @@ const App: React.FC = () => {
const bridgeAPI = init(config);
return (
<WebLoginProvider bridgeAPI={bridgeAPI}>
123
<LoginDemo />
<Tabs defaultActiveKey="AccountDemo" items={items} />
</WebLoginProvider>
Expand Down
12 changes: 6 additions & 6 deletions packages/wallets/portkey-aa/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -435,7 +435,7 @@ export class PortkeyAAWallet extends BaseWalletAdapter {
methodName,
args,
sendOptions,
guardiansApproved = [],
approvedGuardians = [],
}: ISendOrViewAdapter<T>) {
const didWalletInfo = this._wallet!.extraInfo?.portkeyInfo;

Expand All @@ -449,7 +449,7 @@ export class PortkeyAAWallet extends BaseWalletAdapter {
const originChainId = didWalletInfo.chainId;
// use amount from result of managerApprove not from params
// dapp user may change amount at pop-up
const { amount, guardiansApproved, symbol } = (await managerApprove({
const { amount, approvedGuardians, symbol } = (await managerApprove({
originChainId,
targetChainId: chainId,
caHash: didWalletInfo.caInfo?.caHash,
Expand All @@ -466,15 +466,15 @@ export class PortkeyAAWallet extends BaseWalletAdapter {
{
caHash: didWalletInfo.caInfo?.caHash,
...args,
guardiansApproved,
approvedGuardians,
amount,
symbol,
},
sendOptions,
);
} else {
const params = {
guardiansApproved: guardiansApproved,
approvedGuardians: approvedGuardians,
caHash: didWalletInfo.caInfo?.caHash,
contractAddress: contractAddress,
methodName: methodName,
Expand Down Expand Up @@ -512,7 +512,7 @@ export class PortkeyAAWallet extends BaseWalletAdapter {
methodName,
args,
sendOptions,
guardiansApproved = [],
approvedGuardians = [],
}: ICallContractParams<T>) {
const enableAcceleration = this._config.enableAcceleration;
if (!this._wallet) {
Expand Down Expand Up @@ -553,7 +553,7 @@ export class PortkeyAAWallet extends BaseWalletAdapter {
const finalChainId = chainId || this._config.chainId;
const contract = await this.getContract(finalChainId);
const adapterProps = {
guardiansApproved,
approvedGuardians,
caContract: contract,
chainId: finalChainId,
contractAddress,
Expand Down

0 comments on commit 445123b

Please sign in to comment.