Skip to content

Commit 4f7ccb6

Browse files
committed
refactor: fetching firstAddress before showing nano contract request modal
1 parent e40c331 commit 4f7ccb6

File tree

6 files changed

+55
-65
lines changed

6 files changed

+55
-65
lines changed

src/actions/index.js

+9
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ export const types = {
7272
REOWN_SET_MODAL: 'REOWN_SET_MODAL',
7373
REOWN_SET_SESSIONS: 'REOWN_SET_SESSIONS',
7474
REOWN_SET_CONNECTION_STATE: 'REOWN_SET_CONNECTION_STATE',
75+
REOWN_SET_FIRST_ADDRESS: 'REOWN_SET_FIRST_ADDRESS',
7576
REOWN_NEW_NANOCONTRACT_STATUS_LOADING: 'REOWN_NEW_NANOCONTRACT_STATUS_LOADING',
7677
REOWN_NEW_NANOCONTRACT_STATUS_READY: 'REOWN_NEW_NANOCONTRACT_STATUS_READY',
7778
REOWN_NEW_NANOCONTRACT_STATUS_SUCCESS: 'REOWN_NEW_NANOCONTRACT_STATUS_SUCCESS',
@@ -744,6 +745,14 @@ export const setReownSessions = (payload) => ({
744745
payload,
745746
});
746747

748+
/**
749+
* Set the Reown first address
750+
*/
751+
export const setReownFirstAddress = (payload) => ({
752+
type: types.REOWN_SET_FIRST_ADDRESS,
753+
payload,
754+
});
755+
747756
/**
748757
* Set the Reown connection state
749758
* @param {string} state - One of the REOWN_CONNECTION_STATE values

src/components/Reown/ReownModal.js

+10-39
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* LICENSE file in the root directory of this source tree.
66
*/
77

8-
import React, { useEffect, useState } from 'react';
8+
import React, { useEffect } from 'react';
99
import { useDispatch, useSelector } from 'react-redux';
1010
import { types } from '../../actions';
1111
import { getGlobalWallet } from '../../modules/wallet';
@@ -25,48 +25,20 @@ export const ReownModalTypes = {
2525

2626
export function ReownModal({ manageDomLifecycle, data, type, onAcceptAction, onRejectAction }) {
2727
const modalDomId = 'reownModal';
28-
const dispatch = useDispatch();
29-
const [firstAddress, setFirstAddress] = useState('');
3028

3129
useEffect(() => {
3230
manageDomLifecycle(`#${modalDomId}`);
3331
}, []);
3432

35-
useEffect(() => {
36-
if (data?.data?.blueprintId) {
37-
dispatch({
38-
type: types.BLUEPRINT_FETCH_REQUESTED,
39-
payload: data.data.blueprintId
40-
});
41-
}
42-
}, [data?.data?.blueprintId]);
43-
44-
useEffect(() => {
45-
const loadFirstAddress = async () => {
46-
const wallet = getGlobalWallet();
47-
if (wallet.isReady()) {
48-
const address = await wallet.getAddressAtIndex(0);
49-
setFirstAddress(address);
50-
}
33+
const handleAccept = (acceptedData) => {
34+
const ncData = {
35+
blueprintId: acceptedData.blueprintId,
36+
method: acceptedData.method,
37+
args: acceptedData.args,
38+
actions: acceptedData.actions,
39+
caller: acceptedData.caller,
5140
};
52-
loadFirstAddress();
53-
}, []);
54-
55-
const handleAccept = () => {
56-
if (type === ReownModalTypes.SEND_NANO_CONTRACT_TX) {
57-
// Process the nano contract transaction
58-
// Create a new object with the same properties
59-
const ncData = {
60-
blueprintId: data.data.blueprintId,
61-
method: data.data.method,
62-
args: data.data.args,
63-
actions: data.data.actions,
64-
caller: firstAddress
65-
};
66-
onAcceptAction(ncData);
67-
} else {
68-
onAcceptAction(data);
69-
}
41+
onAcceptAction(ncData);
7042
};
7143

7244
const handleReject = () => {
@@ -87,8 +59,7 @@ export function ReownModal({ manageDomLifecycle, data, type, onAcceptAction, onR
8759
case ReownModalTypes.SEND_NANO_CONTRACT_TX:
8860
return (
8961
<SendNanoContractTxModal
90-
data={data}
91-
firstAddress={firstAddress}
62+
data={data}
9263
onAccept={handleAccept}
9364
onReject={handleReject}
9465
/>

src/components/Reown/modals/SendNanoContractTxModal.js

+17-19
Original file line numberDiff line numberDiff line change
@@ -12,33 +12,18 @@ import { types, setNewNanoContractStatusReady } from '../../../actions';
1212
import helpers from '../../../utils/helpers';
1313
import nanoUtils from '../../../utils/nanoContracts';
1414
import { NanoContractActions } from '../NanoContractActions';
15-
import { getGlobalWallet } from '../../../modules/wallet';
1615
import AddressList from '../../AddressList';
1716
import { NANO_UPDATE_ADDRESS_LIST_COUNT } from '../../../constants';
1817

19-
export function SendNanoContractTxModal({ data, firstAddress, onAccept, onReject }) {
18+
export function SendNanoContractTxModal({ data, onAccept, onReject }) {
2019
const dispatch = useDispatch();
2120
const blueprintInfo = useSelector((state) => state.blueprintsData[data?.data?.blueprintId]);
2221
const nanoContracts = useSelector((state) => state.nanoContracts);
2322
const decimalPlaces = useSelector((state) => state.serverInfo.decimalPlaces);
23+
const firstAddress = useSelector((state) => state.reown.firstAddress);
2424
const [selectedAddress, setSelectedAddress] = useState(firstAddress);
2525
const [isSelectingAddress, setIsSelectingAddress] = useState(false);
2626

27-
// Ensure we have a valid address by loading the first one if needed
28-
useEffect(() => {
29-
const ensureValidAddress = async () => {
30-
if (!selectedAddress) {
31-
const wallet = getGlobalWallet();
32-
if (wallet.isReady()) {
33-
const address = await wallet.getAddressAtIndex(0);
34-
setSelectedAddress(address);
35-
}
36-
}
37-
};
38-
39-
ensureValidAddress();
40-
}, [selectedAddress]);
41-
4227
// Reset state when component unmounts
4328
useEffect(() => {
4429
return () => {
@@ -47,6 +32,15 @@ export function SendNanoContractTxModal({ data, firstAddress, onAccept, onReject
4732
};
4833
}, [dispatch]);
4934

35+
useEffect(() => {
36+
if (data?.data?.blueprintId) {
37+
dispatch({
38+
type: types.BLUEPRINT_FETCH_REQUESTED,
39+
payload: data.data.blueprintId
40+
});
41+
}
42+
}, [data?.data?.blueprintId]);
43+
5044
const renderArgumentsSection = () => {
5145
if (!data?.data?.args || !blueprintInfo) {
5246
return null;
@@ -101,8 +95,12 @@ export function SendNanoContractTxModal({ data, firstAddress, onAccept, onReject
10195
};
10296

10397
const handleAccept = () => {
104-
// Pass the selected address to the onAccept callback
105-
onAccept(selectedAddress);
98+
// Create a new object with all the data and the selected address
99+
const ncData = {
100+
...data.data,
101+
caller: selectedAddress,
102+
};
103+
onAccept(ncData);
106104
};
107105

108106
// Address selection mode content

src/reducers/index.js

+1
Original file line numberDiff line numberDiff line change
@@ -451,6 +451,7 @@ const rootReducer = (state = initialState, action) => {
451451
case types.REOWN_CREATE_TOKEN_STATUS_READY:
452452
case types.REOWN_CREATE_TOKEN_STATUS_SUCCESSFUL:
453453
case types.REOWN_CREATE_TOKEN_STATUS_FAILED:
454+
case types.REOWN_SET_FIRST_ADDRESS:
454455
return {
455456
...state,
456457
reown: reownReducer(state.reown, action),

src/reducers/reown.js

+6
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ const initialState = {
1717
onAcceptAction: null,
1818
onRejectAction: null,
1919
},
20+
firstAddress: null,
2021
sessions: {},
2122
connectionState: REOWN_CONNECTION_STATE.IDLE,
2223
nanoContractStatus: BASE_STATUS.READY,
@@ -25,6 +26,11 @@ const initialState = {
2526

2627
export default function reownReducer(state = initialState, action) {
2728
switch (action.type) {
29+
case types.REOWN_SET_FIRST_ADDRESS:
30+
return {
31+
...state,
32+
firstAddress: action.payload,
33+
};
2834
case types.REOWN_SET_CLIENT:
2935
return {
3036
...state,

src/sagas/reown.js

+12-7
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ import {
5252
setCreateTokenStatusFailed,
5353
showGlobalModal,
5454
hideGlobalModal,
55+
setReownFirstAddress,
5556
} from '../actions';
5657
import { checkForFeatureFlag, getNetworkSettings, retryHandler, showPinScreenForResult } from './helpers';
5758
import { logger } from '../utils/logger';
@@ -146,6 +147,9 @@ function* init() {
146147
yield call(refreshActiveSessions, true);
147148
yield fork(requestsListener);
148149

150+
const wallet = getGlobalWallet();
151+
const firstAddress = yield call(() => wallet.getAddressAtIndex(0));
152+
yield put(setReownFirstAddress(firstAddress));
149153
} catch (error) {
150154
log.debug('Error on init: ', error);
151155
yield put(onExceptionCaptured(error));
@@ -409,7 +413,6 @@ export function* processRequest(action) {
409413
}
410414
}));
411415
} catch (e) {
412-
console.log('Error on processRequest: ', e);
413416
let shouldAnswer = true;
414417
switch (e.constructor) {
415418
case SendNanoContractTxError: {
@@ -652,10 +655,7 @@ export function* handleDAppRequest({ payload }, modalType) {
652655

653656
yield put(showGlobalModal(MODAL_TYPES.REOWN, {
654657
type: modalType,
655-
data: {
656-
data,
657-
dapp,
658-
},
658+
data: { data, dapp },
659659
onAcceptAction: accept,
660660
onRejectAction: denyCb,
661661
}));
@@ -700,6 +700,12 @@ export function* onSignOracleDataRequest(action) {
700700
* @param {Object} action - The action containing the request payload
701701
*/
702702
export function* onSendNanoContractTxRequest(action) {
703+
const wallet = getGlobalWallet();
704+
705+
if (!wallet.isReady()) {
706+
yield take(types.WALLET_STATE_READY);
707+
}
708+
703709
yield* handleDAppRequest(action, ReownModalTypes.SEND_NANO_CONTRACT_TX);
704710
}
705711

@@ -734,7 +740,6 @@ export function* onWalletReset() {
734740
* @param {Object} action - The action containing the session proposal
735741
*/
736742
export function* onSessionProposal(action) {
737-
log.debug('Got session proposal', action);
738743
const { id, params } = action.payload;
739744

740745
try {
@@ -750,7 +755,7 @@ export function* onSessionProposal(action) {
750755
const requiredMethods = get(params, 'requiredNamespaces.hathor.methods', []);
751756
const availableMethods = values(AVAILABLE_METHODS);
752757
const unsupportedMethods = requiredMethods.filter(method => !availableMethods.includes(method));
753-
758+
754759
if (unsupportedMethods.length > 0) {
755760
log.error('Unsupported methods requested:', unsupportedMethods);
756761
// Set connection state to FAILED

0 commit comments

Comments
 (0)