Skip to content

Commit

Permalink
feat(core): ErrorClient types
Browse files Browse the repository at this point in the history
* ErrorClientDuplicatedTransaction
* ErrorClientRBFRejected
  • Loading branch information
Hanssen0 committed Sep 2, 2024
1 parent ec6f023 commit f5b5938
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 27 deletions.
8 changes: 8 additions & 0 deletions .changeset/funny-shrimps-grow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
"@ckb-ccc/core": patch
---

feat(core): ErrorClient types

- ErrorClientRBFRejected
- ErrorClientDuplicatedTransaction
24 changes: 24 additions & 0 deletions packages/core/src/client/clientTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -280,3 +280,27 @@ export class ErrorClientVerification extends ErrorClientBase {
this.scriptCodeHash = hexFrom(scriptCodeHash);
}
}

export class ErrorClientDuplicatedTransaction extends ErrorClientBase {
public readonly txHash: Hex;

constructor(origin: ErrorClientBaseLike, txHash: HexLike) {
super(origin);
this.txHash = hexFrom(txHash);
}
}

export class ErrorClientRBFRejected extends ErrorClientBase {
public readonly currentFee: Num;
public readonly leastFee: Num;

constructor(
origin: ErrorClientBaseLike,
currentFee: NumLike,
leastFee: NumLike,
) {
super(origin);
this.currentFee = numFrom(currentFee);
this.leastFee = numFrom(leastFee);
}
}
69 changes: 42 additions & 27 deletions packages/core/src/client/jsonRpc/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import {
ClientTransactionResponse,
ErrorClientBase,
ErrorClientBaseLike,
ErrorClientDuplicatedTransaction,
ErrorClientRBFRejected,
ErrorClientResolveUnknown,
ErrorClientVerification,
OutputsValidator,
Expand Down Expand Up @@ -50,6 +52,41 @@ async function transform(
return value;
}

const ERROR_PARSERS: [
string,
(error: ErrorClientBaseLike, match: RegExpMatchArray) => ErrorClientBase,
][] = [
[
"Resolve\\(Unknown\\(OutPoint\\((0x.*)\\)\\)\\)",
(error, match) =>
new ErrorClientResolveUnknown(error, OutPoint.fromBytes(match[1])),
],
[
"Verification\\(Error { kind: Script, inner: TransactionScriptError { source: (Inputs|Outputs)\\[([0-9]*)\\].(Lock|Type), cause: ValidationFailure: see error code (-?[0-9])* on page https://nervosnetwork\\.github\\.io/ckb-script-error-codes/by-(type|data)-hash/(.*)\\.html",
(error, match) =>
new ErrorClientVerification(
error,
match[3] === "Lock"
? "lock"
: match[1] === "Inputs"
? "inputType"
: "outputType",
match[2],
Number(match[4]),
match[5] === "data" ? "data" : "type",
match[6],
),
],
[
"Duplicated\\(Byte32\\((0x.*)\\)\\)",
(error, match) => new ErrorClientDuplicatedTransaction(error, match[1]),
],
[
'RBFRejected\\("Tx\'s current fee is ([0-9]*), expect it to >= ([0-9]*) to replace old txs"\\)',
(error, match) => new ErrorClientRBFRejected(error, match[1], match[2]),
],
];

/**
* An abstract class implementing JSON-RPC client functionality for a specific URL and timeout.
* Provides methods for sending transactions and building JSON-RPC payloads.
Expand Down Expand Up @@ -353,33 +390,11 @@ export abstract class ClientJsonRpc extends Client {
}
const err = errAny as ErrorClientBaseLike;

const unknownOutPointMatch = err.data.match(
new RegExp("Resolve\\(Unknown\\(OutPoint\\((0x.*)\\)\\)\\)"),
)?.[1];
if (unknownOutPointMatch) {
throw new ErrorClientResolveUnknown(
err,
OutPoint.fromBytes(unknownOutPointMatch),
);
}
const verificationFailedMatch = err.data.match(
new RegExp(
"Verification\\(Error { kind: Script, inner: TransactionScriptError { source: (Inputs|Outputs)\\[([0-9]*)\\].(Lock|Type), cause: ValidationFailure: see error code (-?[0-9])* on page https://nervosnetwork\\.github\\.io/ckb-script-error-codes/by-(type|data)-hash/(.*)\\.html",
),
);
if (verificationFailedMatch) {
throw new ErrorClientVerification(
err,
verificationFailedMatch[3] === "Lock"
? "lock"
: verificationFailedMatch[1] === "Inputs"
? "inputType"
: "outputType",
verificationFailedMatch[2],
Number(verificationFailedMatch[4]),
verificationFailedMatch[5] === "data" ? "data" : "type",
verificationFailedMatch[6],
);
for (const [regexp, builder] of ERROR_PARSERS) {
const match = err.data.match(regexp);
if (match) {
throw builder(err, match);
}
}

throw new ErrorClientBase(err);
Expand Down

0 comments on commit f5b5938

Please sign in to comment.