Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

State dump modifications #5382

Merged
merged 7 commits into from
Mar 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,6 @@
"ethereumjs-wallet": "1.0.1",
"events": "3.3.0",
"fast-text-encoding": "1.0.4",
"flatted": "3.2.7",
"global": "4.4.0",
"grapheme-splitter": "1.0.4",
"graphql-request": "5.0.0",
Expand Down
51 changes: 48 additions & 3 deletions src/screens/Diagnostics/helpers/createAndShareStateDumpFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,56 @@ import { APP_STATE_DUMP_FILE_NAME } from '@/screens/Diagnostics/constants';
import RNShare from 'react-native-share';
import { IS_ANDROID } from '@/env';
import { logger, RainbowError } from '@/logger';
import { stringify } from 'flatted';
import { getAllActiveSessions } from '@/walletConnect';

// function partially developed by ChatGPT that helps remove and trace cyclic references in javascript objects
function cyclicReplacer() {
const seenObjects = new Map<object, string[]>(); // Tracks objects and their paths

function replacer(key: string | undefined, value: any, path: string[] = []): any {
if (typeof value === 'object' && value !== null) {
// Determine the new path
const newPath = key !== undefined ? path.concat(key) : path;

if (seenObjects.has(value)) {
// Construct a string representation of the path to the cyclic reference
const keys = seenObjects.get(value)?.filter(key => !!key);
let path = keys?.shift();
keys?.forEach(key => {
if (key) {
if (!isNaN(Number(key))) {
path += `[${key}]`;
} else {
path += `.${key}`;
}
}
});
return `Cyclic reference to ${path}`;
}

seenObjects.set(value, newPath);

// Recursively handle nested objects and arrays
const valueCopy: { [key: string]: any } = Array.isArray(value) ? [] : {};
for (const k of Object.keys(value)) {
valueCopy[k] = replacer(k, value[k], newPath);
}
return valueCopy;
}
return value;
}

return (key: string, value: any): any => replacer(key, value);
}

export async function createAndShareStateDumpFile() {
const appState = store.getState();
const stringifiedState = stringify(appState);
const reduxState = store.getState();
const walletConnectV2Sessions = await getAllActiveSessions();
const state = {
reduxState,
walletConnectV2: { sessions: walletConnectV2Sessions },
};
const stringifiedState = JSON.stringify(state, cyclicReplacer());

const documentsFilePath = `${RNFS.DocumentDirectoryPath}/${APP_STATE_DUMP_FILE_NAME}`;
try {
Expand Down
5 changes: 0 additions & 5 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -10547,11 +10547,6 @@ flat@^5.0.2:
resolved "https://registry.yarnpkg.com/flat/-/flat-5.0.2.tgz#8ca6fe332069ffa9d324c327198c598259ceb241"
integrity sha512-b6suED+5/3rTpUBdG1gupIl8MPFCAMA0QXwmljLhvCUKcUvdE4gWky9zpuGCcXHOsz4J9wPGNWq6OKpmIzz3hQ==

flatted@3.2.7:
version "3.2.7"
resolved "https://registry.yarnpkg.com/flatted/-/flatted-3.2.7.tgz#609f39207cb614b89d0765b477cb2d437fbf9787"
integrity sha512-5nqDSxl8nn5BSNxyR3n4I6eDmbolI6WT+QqR547RwxQapgjQBmtktdP+HTBb/a/zLsbzERTONyUB5pefh5TtjQ==

flatted@^3.2.9:
version "3.3.1"
resolved "https://registry.yarnpkg.com/flatted/-/flatted-3.3.1.tgz#21db470729a6734d4997002f439cb308987f567a"
Expand Down