Skip to content

Commit

Permalink
feat: better error message for when type is not bigint (#99)
Browse files Browse the repository at this point in the history
* feat: update error message for utxo.ts to explicitly tell the issue with types

* feat: better error message for when the type is wrong

* fix: linter issues
  • Loading branch information
dogebonker authored Jul 13, 2024
1 parent bae4ac5 commit d4d9247
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 4 deletions.
5 changes: 4 additions & 1 deletion src/transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -518,7 +518,10 @@ export class Transaction {
): psbt.TransactionOutput {
let { amount, script } = o;
if (amount === undefined) amount = cur?.amount;
if (typeof amount !== 'bigint') throw new Error('amount must be bigint sats');
if (typeof amount !== 'bigint')
throw new Error(
`Wrong amount type, should be of type bigint in sats, but got ${amount} of type ${typeof amount}`
);
if (typeof script === 'string') script = hex.decode(script);
if (script === undefined) script = cur?.script;
let res: psbt.PSBTKeyMapKeys<typeof psbt.PSBTOutput> = { ...cur, ...o, amount, script };
Expand Down
20 changes: 17 additions & 3 deletions src/utxo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,12 @@ function getScript(o: Output, opts: TxOpts = {}, network = NETWORK) {
script = OutScript.encode(Address(network).decode(o.address));
}
if (!script) throw new Error('Estimator: wrong output script');
if (typeof o.amount !== 'bigint') throw new Error(`Estimator: wrong output amount=${o.amount}`);
if (typeof o.amount !== 'bigint')
throw new Error(
`Estimator: wrong output amount=${
o.amount
}, should be of type bigint but got ${typeof o.amount}.`
);
if (script && !opts.allowUnknownOutputs && OutScript.decode(script).type === 'unknown') {
throw new Error(
'Estimator: unknown output script type, there is a chance that input is unspendable. Pass allowUnknownOutputs=true, if you sure'
Expand Down Expand Up @@ -333,9 +338,18 @@ export class _Estimator {
private opts: EstimatorOpts
) {
if (typeof opts.feePerByte !== 'bigint')
throw new Error(`Estimator: wrong feePerByte=${opts.feePerByte}`);
throw new Error(
`Estimator: wrong feePerByte=${
opts.feePerByte
}, should be of type bigint but got ${typeof opts.feePerByte}.`
);
if (opts.dust) {
if (typeof opts.dust !== 'bigint') throw new Error(`Estimator: wrong dust=${opts.dust}`);
if (typeof opts.dust !== 'bigint')
throw new Error(
`Estimator: wrong dust=${
opts.dust
}, should be of type bigint but got ${typeof opts.dust}.`
);
this.dust = opts.dust;
}
if (opts.requiredInputs !== undefined && !Array.isArray(opts.requiredInputs))
Expand Down

0 comments on commit d4d9247

Please sign in to comment.