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

CU-86dt27bzj - Create a WcSdk method to wipe all requests from a dapp #113

Merged
merged 1 commit into from
Apr 17, 2024

Conversation

LeonardoDizConde
Copy link
Contributor

No description provided.

@melanke
Copy link
Collaborator

melanke commented Apr 16, 2024

const dappPage = DAPP_REACT
const walletPage = WALLET_REACT
await connectReactDappToNewReactAccount(context, dappPage, walletPage)
await dappPage.page.waitForLoadState('networkidle') // Wait to load request

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of using waitForLoadState('networkidle') and awaitSeconds(5), consider using waitForSelector or waitForFunction to ensure the specific element or condition is ready before proceeding.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems like a good suggestion to use this waitForSelector to avoid the loop.

await connectReactDappToNewReactAccount(context, dappPage, walletPage)
await dappPage.page.waitForLoadState('networkidle') // Wait to load request
await dappPage.awaitSeconds(5) // Wait for 5 seconds
for (let i = 0; i <= 3; i++) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Avoid using a loop without a clear exit condition. If the 'hello-world__sign-transaction' button is not available, this could result in an infinite loop.

for (let i = 0; i <= 3; i++) {
// Click on Sign Transaction button 3 times
await dappPage.page.getByTestId('hello-world__sign-transaction').click()
await dappPage.awaitSeconds(0.5)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Avoid using arbitrary wait times like awaitSeconds(0.5). It's better to wait for specific elements or conditions to be met.

await dappPage.awaitSeconds(0.5)
}
await dappPage.page.getByTestId('hello-world__wipe-methods').click() // Click on Wipe Methods button
await dappPage.awaitSeconds(2) // Wait for 2 seconds

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Avoid using arbitrary wait times like awaitSeconds(2). It's better to wait for specific elements or conditions to be met.

await dappPage.page.getByTestId('hello-world__wipe-methods').click() // Click on Wipe Methods button
await dappPage.awaitSeconds(2) // Wait for 2 seconds
const pendingRequests = await walletPage.page.getByTestId('default-card__pending-request').all()
expect(pendingRequests.length).toBe(0) // Verify if the response had a return

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The comment // Verify if the response had a return is not clear. Please provide a more descriptive comment.

@@ -377,4 +387,27 @@ export class WcWalletSDK {
this.requests = filteredRequests
}
}

private async wipeRequests({ session, request }: TAdapterMethodParam): Promise<string[]> {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider renaming the method wipeRequests to something more descriptive, like wipeAllDappRequests, to clearly indicate its purpose.

@@ -377,4 +387,27 @@ export class WcWalletSDK {
this.requests = filteredRequests
}
}

private async wipeRequests({ session, request }: TAdapterMethodParam): Promise<string[]> {
const result: string[] = []

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The result variable could be named more descriptively. Consider renaming it to wipedRequests or clearedRequests.

(dappRequest: TSessionRequest) => dappRequest.topic === session.topic && dappRequest.id !== request.id,
) ?? []) as TSessionRequest[]

for (const dappRequest of dappRequests) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider using Promise.all with Array.map for handling multiple promises. This could potentially improve the performance by executing the promises concurrently.

topic: dappRequest.topic,
response: formatJsonRpcError(dappRequest.id, {
code: 1,
message: 'rejected by the dapp',

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The error code 1 is not descriptive. Consider using a constant with a descriptive name or a standard error code if available.

}),
})
} finally {
this.requests = this.requests.filter(({ id }) => id !== dappRequest.id)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The requests array is being updated inside a loop. This might lead to performance issues for large arrays. Consider collecting all the ids to be removed in an array and then use Array.filter once after the loop.

const dappPage = DAPP_REACT
const walletPage = WALLET_REACT
await connectReactDappToNewReactAccount(context, dappPage, walletPage)
await dappPage.page.waitForLoadState('networkidle') // Wait to load request
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems like a good suggestion to use this waitForSelector to avoid the loop.

try {
await wcSdk.wipeRequests()
} catch (error) {
console.log(error)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you can remove this try-catch with the console.log.
It's not supposed to fail here, so allow the app to crash when it fails.

@@ -43,6 +43,7 @@ export class WcWalletSDK {
private _proposals: TSessionProposal[] = []
private _requests: TSessionRequest[] = []
private _status: EStatus = EStatus.NOT_STARTED
private _permissibleWalletRequestMethods: Record<string, (args: TAdapterMethodParam) => Promise<any>>
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can call it nonAdapterMethods, it's not pretty, but it's very clear.

@@ -203,26 +205,26 @@ packages:
typescript:
optional: true

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider using the latest version of '@eslint-community/regexpp' for better performance and security.

'@typescript-eslint/utils': 6.7.4(eslint@8.50.0)(typescript@5.2.2)
'@typescript-eslint/visitor-keys': 6.7.4
'@eslint-community/regexpp': 4.10.0
'@typescript-eslint/parser': 6.21.0(eslint@8.57.0)(typescript@5.4.5)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Update '@typescript-eslint/parser' to the latest version to leverage new features and improvements.

'@typescript-eslint/visitor-keys': 6.7.4
'@eslint-community/regexpp': 4.10.0
'@typescript-eslint/parser': 6.21.0(eslint@8.57.0)(typescript@5.4.5)
'@typescript-eslint/scope-manager': 6.21.0

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider updating '@typescript-eslint/scope-manager' to the latest version for better performance and stability.

'@eslint-community/regexpp': 4.10.0
'@typescript-eslint/parser': 6.21.0(eslint@8.57.0)(typescript@5.4.5)
'@typescript-eslint/scope-manager': 6.21.0
'@typescript-eslint/type-utils': 6.21.0(eslint@8.57.0)(typescript@5.4.5)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Update '@typescript-eslint/type-utils' to the latest version to leverage new features and improvements.

'@typescript-eslint/parser': 6.21.0(eslint@8.57.0)(typescript@5.4.5)
'@typescript-eslint/scope-manager': 6.21.0
'@typescript-eslint/type-utils': 6.21.0(eslint@8.57.0)(typescript@5.4.5)
'@typescript-eslint/utils': 6.21.0(eslint@8.57.0)(typescript@5.4.5)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider updating '@typescript-eslint/utils' to the latest version for better performance and stability.


const executionTime = performance.now() - startExecutionTime
result = await adapterMethod.apply(blockchainOptions.adapter, [{ request, session }])
} else throw new Error('Invalid request method')

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The error message 'Invalid request method' is not informative enough. Consider providing more details about the error.

result = await adapterMethod.apply(blockchainOptions.adapter, [{ request, session }])
} else throw new Error('Invalid request method')

const executionTime = Math.abs(performance.now() - startExecutionTime)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider using performance.now() directly in the subtraction to avoid the use of Math.abs(). This will make the code cleaner and easier to read.

}),
)

this.requests = this.requests.filter((dappRequest: TSessionRequest) => dappRequest.topic !== session.topic)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The this.requests filter operation is outside the Promise.all block. This might lead to a race condition where the requests are not properly wiped if the promises have not yet resolved. Consider moving this line inside the finally block of the Promise.all block.

@@ -63,4 +63,9 @@ export type TAdapterMethodParam = {
session: TSession
}

export enum ResponseErrorCode {
REJECT = 1,

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider adding a brief description for each enum member to clarify their purpose and usage.

@@ -63,4 +63,9 @@ export type TAdapterMethodParam = {
session: TSession
}

export enum ResponseErrorCode {
REJECT = 1,
DISCONNECT = 5900,

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The error code DISCONNECT seems to be arbitrary. If there's a specific reason for choosing 5900, it would be better to document it somewhere outside the code.

@melanke melanke merged commit 3f41979 into main Apr 17, 2024
1 of 2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants