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

feat(core): Client.waitTransaction #69

Merged
merged 1 commit into from
Oct 10, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions .changeset/curvy-horses-arrive.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
"@ckb-ccc/core": patch
"@ckb-ccc/ccc": patch
"ckb-ccc": patch
"@ckb-ccc/connector": patch
"@ckb-ccc/connector-react": patch
---

feat(core): Client.waitTransaction
47 changes: 46 additions & 1 deletion packages/core/src/client/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
import { Zero } from "../fixedPoint/index.js";
import { Hex, HexLike, hexFrom } from "../hex/index.js";
import { Num, NumLike, numFrom, numMax } from "../num/index.js";
import { apply, reduceAsync } from "../utils/index.js";
import { apply, reduceAsync, sleep } from "../utils/index.js";
import { ClientCache } from "./cache/index.js";
import { ClientCacheMemory } from "./cache/memory.js";
import { ClientCollectableSearchKeyLike } from "./clientTypes.advanced.js";
Expand All @@ -26,6 +26,7 @@ import {
ClientIndexerSearchKeyLike,
ClientIndexerSearchKeyTransactionLike,
ClientTransactionResponse,
ErrorClientWaitTransactionTimeout,
OutputsValidator,
} from "./clientTypes.js";

Expand Down Expand Up @@ -567,4 +568,48 @@ export abstract class Client {
transaction: tx,
};
}

async waitTransaction(
txHash: HexLike,
confirmations: number = 0,
timeout: number = 30000,
interval: number = 2000,
): Promise<ClientTransactionResponse | undefined> {
const startTime = Date.now();
let tx: ClientTransactionResponse | undefined;

const getTx = async () => {
const res = await this.getTransaction(txHash);
if (
!res ||
res.blockNumber == null ||
["sent", "pending", "proposed"].includes(res.status)
) {
return undefined;
}

tx = res;
return res;
};

while (true) {
if (!tx) {
if (await getTx()) {
continue;
}
} else if (confirmations === 0) {
return tx;
} else if (
(await this.getTipHeader()).number - tx!.blockNumber! >=
confirmations
) {
return tx;
}

if (Date.now() - startTime + interval >= timeout) {
throw new ErrorClientWaitTransactionTimeout();
}
await sleep(interval);
}
}
}
6 changes: 6 additions & 0 deletions packages/core/src/client/clientTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -304,3 +304,9 @@ export class ErrorClientRBFRejected extends ErrorClientBase {
this.leastFee = numFrom(leastFee);
}
}

export class ErrorClientWaitTransactionTimeout extends Error {
constructor() {
super("Wait transaction timeout");
}
}
6 changes: 6 additions & 0 deletions packages/core/src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { NumLike, numFrom } from "../num/index.js";

/**
* A type safe way to apply a transformer on a value if it's not empty.
* @public
Expand Down Expand Up @@ -166,6 +168,10 @@ export async function reduceAsync<T, V>(
);
}

export function sleep(ms: NumLike) {
return new Promise((resolve) => setTimeout(resolve, Number(numFrom(ms))));
}

/**
* @public
*/
Expand Down
8 changes: 4 additions & 4 deletions packages/demo/src/app/connected/(tools)/IssueXUdtSus/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -167,10 +167,10 @@ export default function IssueXUdtSul() {
ccc.bytesFrom(ccc.hashTypeId(mintTx.inputs[0], 1)).slice(0, 20),
);
await mintTx.completeFeeBy(signer);
log(
"Transaction sent:",
explorerTransaction(await signer.sendTransaction(mintTx)),
);
const mintTxHash = await signer.sendTransaction(mintTx);
log("Transaction sent:", explorerTransaction(mintTxHash));
await signer.client.waitTransaction(mintTxHash);
log("Transaction committed:", explorerTransaction(mintTxHash));
}}
>
Issue
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -196,10 +196,10 @@ export default function IssueXUdtTypeId() {
ccc.bytesFrom(ccc.hashTypeId(mintTx.inputs[0], 2)).slice(0, 20),
);
await mintTx.completeFeeBy(signer);
log(
"Transaction sent:",
explorerTransaction(await signer.sendTransaction(mintTx)),
);
const mintTxHash = await signer.sendTransaction(mintTx);
log("Transaction sent:", explorerTransaction(mintTxHash));
await signer.client.waitTransaction(mintTxHash);
log("Transaction committed:", explorerTransaction(mintTxHash));
}}
>
Issue
Expand Down
16 changes: 8 additions & 8 deletions packages/demo/src/app/connected/(tools)/NervosDao/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -230,10 +230,10 @@ function DaoButton({ dao }: { dao: ccc.Cell }) {
}

// Sign and send the transaction
log(
"Transaction sent:",
explorerTransaction(await signer.sendTransaction(tx)),
);
const txHash = await signer.sendTransaction(tx);
log("Transaction sent:", explorerTransaction(txHash));
await signer.client.waitTransaction(txHash);
log("Transaction committed:", explorerTransaction(txHash));
})();
}}
className={`align-center ${isNew ? "text-yellow-400" : "text-orange-400"}`}
Expand Down Expand Up @@ -400,10 +400,10 @@ export default function Transfer() {
await tx.completeFeeBy(signer, feeRate);

// Sign and send the transaction
log(
"Transaction sent:",
explorerTransaction(await signer.sendTransaction(tx)),
);
const txHash = await signer.sendTransaction(tx);
log("Transaction sent:", explorerTransaction(txHash));
await signer.client.waitTransaction(txHash);
log("Transaction committed:", explorerTransaction(txHash));
}}
>
Deposit
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,10 @@ function ClaimButton({ cell, lock }: { cell: ccc.Cell; lock: ccc.Script }) {
await tx.completeInputsByCapacity(signer);
await tx.completeFeeChangeToOutput(signer, 0);

log(
"Transaction sent:",
explorerTransaction(await signer.sendTransaction(tx)),
);
const txHash = await signer.sendTransaction(tx);
log("Transaction sent:", explorerTransaction(txHash));
await signer.client.waitTransaction(txHash);
log("Transaction committed:", explorerTransaction(txHash));
})();
}}
className="align-center text-yellow-400"
Expand Down Expand Up @@ -149,10 +149,10 @@ export default function TimeLockedTransfer() {
await tx.completeInputsByCapacity(signer);
await tx.completeFeeBy(signer);

log(
"Transaction sent:",
explorerTransaction(await signer.sendTransaction(tx)),
);
const txHash = await signer.sendTransaction(tx);
log("Transaction sent:", explorerTransaction(txHash));
await signer.client.waitTransaction(txHash);
log("Transaction committed:", explorerTransaction(txHash));
}, [
signer,
amount,
Expand Down
8 changes: 4 additions & 4 deletions packages/demo/src/app/connected/(tools)/Transfer/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -106,10 +106,10 @@ export default function Transfer() {
await tx.completeFeeBy(signer, feeRate);

// Sign and send the transaction
log(
"Transaction sent:",
explorerTransaction(await signer.sendTransaction(tx)),
);
const txHash = await signer.sendTransaction(tx);
log("Transaction sent:", explorerTransaction(txHash));
await signer.client.waitTransaction(txHash);
log("Transaction committed:", explorerTransaction(txHash));
}}
>
Transfer
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,10 +113,10 @@ export default function TransferLumos() {
tx.outputsData[0] = ccc.hexFrom(dataBytes);

// Sign and send the transaction
log(
"Transaction sent:",
explorerTransaction(await signer.sendTransaction(tx)),
);
const txHash = await signer.sendTransaction(tx);
log("Transaction sent:", explorerTransaction(txHash));
await signer.client.waitTransaction(txHash);
log("Transaction committed:", explorerTransaction(txHash));
}}
>
Transfer
Expand Down
8 changes: 4 additions & 4 deletions packages/demo/src/app/connected/(tools)/TransferXUdt/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,10 @@ export default function TransferXUdt() {
await tx.completeFeeBy(signer);

// Sign and send the transaction
log(
"Transaction sent:",
explorerTransaction(await signer.sendTransaction(tx)),
);
const txHash = await signer.sendTransaction(tx);
log("Transaction sent:", explorerTransaction(txHash));
await signer.client.waitTransaction(txHash);
log("Transaction committed:", explorerTransaction(txHash));
}}
>
Transfer
Expand Down