Skip to content

Commit

Permalink
feat: initial implementation of send tokens RPC handling
Browse files Browse the repository at this point in the history
  • Loading branch information
andreabadesso committed Feb 12, 2025
1 parent 13dbe5b commit b6da3b2
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 51 deletions.
32 changes: 32 additions & 0 deletions src/actions/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,10 @@ export const types = {
SHOW_GLOBAL_MODAL: 'SHOW_GLOBAL_MODAL',
HIDE_GLOBAL_MODAL: 'HIDE_GLOBAL_MODAL',
SERVER_INFO_UPDATED: 'SERVER_INFO_UPDATED',
REOWN_SEND_TX_STATUS_LOADING: 'REOWN_SEND_TX_STATUS_LOADING',
REOWN_SEND_TX_STATUS_READY: 'REOWN_SEND_TX_STATUS_READY',
REOWN_SEND_TX_STATUS_SUCCESS: 'REOWN_SEND_TX_STATUS_SUCCESS',
REOWN_SEND_TX_STATUS_FAILURE: 'REOWN_SEND_TX_STATUS_FAILURE',
};

/**
Expand Down Expand Up @@ -847,3 +851,31 @@ export const showGlobalModal = (modalType, modalProps = {}) => ({
export const hideGlobalModal = () => ({
type: types.HIDE_GLOBAL_MODAL
});

/**
* Set send transaction status to loading
*/
export const setSendTxStatusLoading = () => ({
type: types.REOWN_SEND_TX_STATUS_LOADING,
});

/**
* Set send transaction status to ready
*/
export const setSendTxStatusReady = () => ({
type: types.REOWN_SEND_TX_STATUS_READY,
});

/**
* Set send transaction status to success
*/
export const setSendTxStatusSuccess = () => ({
type: types.REOWN_SEND_TX_STATUS_SUCCESS,
});

/**
* Set send transaction status to failure
*/
export const setSendTxStatusFailure = () => ({
type: types.REOWN_SEND_TX_STATUS_FAILURE,
});
25 changes: 25 additions & 0 deletions src/reducers/reown.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const initialState = {
connectionFailed: false,
nanoContractStatus: 'ready', // 'ready' | 'loading' | 'success' | 'failure'
createTokenStatus: 'ready', // 'ready' | 'loading' | 'success' | 'failure'
sendTxStatus: 'ready', // 'ready' | 'loading' | 'success' | 'failure'
};

export default function reownReducer(state = initialState, action) {
Expand Down Expand Up @@ -99,6 +100,30 @@ export default function reownReducer(state = initialState, action) {
createTokenStatus: 'failure',
};

case types.REOWN_SEND_TX_STATUS_LOADING:
return {
...state,
sendTxStatus: 'loading',
};

case types.REOWN_SEND_TX_STATUS_READY:
return {
...state,
sendTxStatus: 'ready',
};

case types.REOWN_SEND_TX_STATUS_SUCCESS:
return {
...state,
sendTxStatus: 'success',
};

case types.REOWN_SEND_TX_STATUS_FAILURE:
return {
...state,
sendTxStatus: 'failure',
};

default:
return state;
}
Expand Down
78 changes: 27 additions & 51 deletions src/sagas/reown.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ import {
setCreateTokenStatusReady,
setCreateTokenStatusSuccessful,
setCreateTokenStatusFailed,
setSendTxStatusLoading,
setSendTxStatusReady,
setSendTxStatusSuccess,
setSendTxStatusFailure,
showGlobalModal,
hideGlobalModal,
} from '../actions';
Expand Down Expand Up @@ -342,6 +346,10 @@ export function* processRequest(action) {
case RpcResponseTypes.CreateTokenResponse:
yield put(setCreateTokenStatusSuccessful());
break;
case RpcResponseTypes.SendTransactionResponse:
yield put(setSendTxStatusSuccess());
yield put(showGlobalModal(MODAL_TYPES.TRANSACTION_FEEDBACK, { isLoading: false, isError: false }));
break;
default:
break;
}
Expand All @@ -354,60 +362,28 @@ export function* processRequest(action) {
result: response,
}
}));
} catch (e) {
log.debug('Error on processRequest: ', e);
let shouldAnswer = true;
switch (e.constructor) {
case SendNanoContractTxError: {
yield put(setNewNanoContractStatusFailure());
yield put(showGlobalModal(MODAL_TYPES.NANO_CONTRACT_FEEDBACK, { isLoading: false, isError: true }));

const retry = yield call(
retryHandler,
types.REOWN_NEW_NANOCONTRACT_RETRY,
types.REOWN_NEW_NANOCONTRACT_RETRY_DISMISS,
);

if (retry) {
shouldAnswer = false;
yield* processRequest(action);
}
} break;
case CreateTokenError: {
yield put(setCreateTokenStatusFailed());

const retry = yield call(
retryHandler,
types.REOWN_CREATE_TOKEN_RETRY,
types.REOWN_CREATE_TOKEN_RETRY_DISMISS,
);

if (retry) {
shouldAnswer = false;
yield* processRequest(action);
}
} break;
default:
break;
} catch (err) {
if (err instanceof SendNanoContractTxError) {
yield put(setNewNanoContractStatusFailure());
yield put(showGlobalModal(MODAL_TYPES.NANO_CONTRACT_FEEDBACK, { isLoading: false, isError: true }));
} else if (err instanceof CreateTokenError) {
yield put(setCreateTokenStatusFailed());
} else {
yield put(setSendTxStatusFailure());
yield put(showGlobalModal(MODAL_TYPES.TRANSACTION_FEEDBACK, { isLoading: false, isError: true }));
}

if (shouldAnswer) {
try {
yield call(() => walletKit.respondSessionRequest({
topic: payload.topic,
response: {
id: payload.id,
jsonrpc: '2.0',
error: {
code: ERROR_CODES.USER_REJECTED_METHOD,
message: 'Rejected by the user',
},
},
}));
} catch (error) {
log.error('Error rejecting response on sessionRequest', error);
yield call(() => walletKit.respondSessionRequest({
topic: payload.topic,
response: {
id: payload.id,
jsonrpc: '2.0',
error: {
code: -32000,
message: err.message,
},
}
}
}));
}
}

Expand Down

0 comments on commit b6da3b2

Please sign in to comment.