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: better error message for when type is not bigint #99

Merged
merged 5 commits into from
Jul 13, 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
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