Skip to content

Commit

Permalink
[SDK] Update smart wallet test (#4845)
Browse files Browse the repository at this point in the history
## Problem solved

Short description of the bug fixed or feature added

<!-- start pr-codex -->

---

## PR-Codex overview
This PR introduces a new `sleep` function in `sleep.ts` for delaying asynchronous operations and replaces existing inline sleep implementations across various files with this unified function.

### Detailed summary
- Added `sleep` function in `packages/thirdweb/src/utils/sleep.ts` for delaying async operations.
- Removed duplicate `sleep` functions from `packages/thirdweb/src/rpc/watchBlockNumber.ts` and `packages/thirdweb/src/wallets/in-app/web/utils/iFrameCommunication/IframeCommunicator.ts`.
- Updated calls to `sleep` in `packages/thirdweb/src/wallets/smart/smart-wallet-integration-v07.test.ts` and `packages/thirdweb/src/wallets/in-app/web/utils/iFrameCommunication/IframeCommunicator.ts` to use the new function.

> ✨ Ask PR-Codex anything about this PR by commenting with `/codex {your question}`

<!-- end pr-codex -->
  • Loading branch information
kien-ngo committed Sep 28, 2024
1 parent 90b5495 commit 6c26012
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 25 deletions.
9 changes: 1 addition & 8 deletions packages/thirdweb/src/rpc/watchBlockNumber.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { Chain } from "../chains/types.js";
import type { ThirdwebClient } from "../client/client.js";
import { sleep } from "../utils/sleep.js";
import { eth_blockNumber } from "./actions/eth_blockNumber.js";
import { getRpcClient } from "./rpc.js";

Expand Down Expand Up @@ -138,14 +139,6 @@ function createBlockNumberPoller(
};
}

/**
* TODO: document
* @internal
*/
function sleep(ms: number) {
return new Promise((resolve) => setTimeout(resolve, ms));
}

const existingPollers = new Map<
number,
ReturnType<typeof createBlockNumberPoller>
Expand Down
8 changes: 8 additions & 0 deletions packages/thirdweb/src/utils/sleep.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/**
* Delay an async thread
* @param ms Sleep time in millisecond
* @internal
*/
export function sleep(ms: number) {
return new Promise((resolve) => setTimeout(resolve, ms));
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { sleep } from "../../../../../utils/sleep.js";

type IFrameCommunicatorProps = {
link: string;
baseUrl: string;
Expand All @@ -6,12 +8,6 @@ type IFrameCommunicatorProps = {
onIframeInitialize?: () => void;
};

function sleep(seconds: number) {
return new Promise((resolve) => {
setTimeout(resolve, seconds * 1000);
});
}

const iframeBaseStyle = {
height: "100%",
width: "100%",
Expand Down Expand Up @@ -152,12 +148,12 @@ export class IframeCommunicator<T extends { [key: string]: any }> {
showIframe?: boolean;
}) {
while (!isIframeLoaded.get(this.iframe.src)) {
await sleep(this.POLLING_INTERVAL_SECONDS);
await sleep(this.POLLING_INTERVAL_SECONDS * 1000);
}
if (showIframe) {
this.iframe.style.display = "block";
// magic number to let the display render before performing the animation of the modal in
await sleep(0.005);
await sleep(0.005 * 1000);
}

const channel = new MessageChannel();
Expand All @@ -168,7 +164,7 @@ export class IframeCommunicator<T extends { [key: string]: any }> {
channel.port1.close();
if (showIframe) {
// magic number to let modal fade out before hiding it
await sleep(0.1);
await sleep(0.1 * 1000);
this.iframe.style.display = "none";
}
if (!data.success) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import { sendBatchTransaction } from "../../transaction/actions/send-batch-trans
import { waitForReceipt } from "../../transaction/actions/wait-for-tx-receipt.js";
import { getAddress } from "../../utils/address.js";
import { isContractDeployed } from "../../utils/bytecode/is-contract-deployed.js";
import { sleep } from "../../utils/sleep.js";
import type { Account, Wallet } from "../interfaces/wallet.js";
import { generateAccount } from "../utils/generateAccount.js";
import { ENTRYPOINT_ADDRESS_v0_7 } from "./lib/constants.js";
Expand Down Expand Up @@ -235,15 +236,17 @@ describe.runIf(process.env.TW_SECRET_KEY).sequential(
}),
account: newSmartAccount,
}),
sendAndConfirmTransaction({
transaction: claimTo({
contract,
quantity: 1n,
to: newSmartAccount.address,
tokenId: 0n,
sleep(1000).then(() =>
sendAndConfirmTransaction({
transaction: claimTo({
contract,
quantity: 1n,
to: newSmartAccount.address,
tokenId: 0n,
}),
account: newSmartAccount,
}),
account: newSmartAccount,
}),
),
]);
expect(txs.length).toEqual(2);
expect(txs.every((t) => t.transactionHash.length === 66)).toBe(true);
Expand Down

0 comments on commit 6c26012

Please sign in to comment.