Skip to content

Commit d340ea6

Browse files
committed
Fix issues reported by mainnet_pat
- Export encodeArgument and isSignableUtxo - Slightly refactor automatic token selection to make sure it doesn't break when including P2PKH inputs
1 parent 075e139 commit d340ea6

File tree

4 files changed

+10
-15
lines changed

4 files changed

+10
-15
lines changed

packages/cashscript/src/Transaction.ts

+8-8
Original file line numberDiff line numberDiff line change
@@ -284,14 +284,14 @@ export class Transaction {
284284

285285
const allUtxos = await this.provider.getUtxos(this.address);
286286

287-
const manualTokenInputs = this.inputs.filter((input) => input.token);
288-
// This will throw if the amount is not enough
289-
if (manualTokenInputs.length > 0) {
290-
selectAllTokenUtxos(manualTokenInputs, this.outputs);
291-
}
287+
const tokenInputs = this.inputs.length > 0
288+
? this.inputs.filter((input) => input.token)
289+
: selectAllTokenUtxos(allUtxos, this.outputs);
292290

293-
const automaticTokenInputs = selectAllTokenUtxos(allUtxos, this.outputs);
294-
const tokenInputs = manualTokenInputs.length > 0 ? manualTokenInputs : automaticTokenInputs;
291+
// This throws if the manually selected inputs are not enough to cover the outputs
292+
if (this.inputs.length > 0) {
293+
selectAllTokenUtxos(this.inputs, this.outputs);
294+
}
295295

296296
if (this.tokenChange) {
297297
const tokenChangeOutputs = createFungibleTokenChangeOutputs(tokenInputs, this.outputs, this.address);
@@ -429,7 +429,7 @@ export class Transaction {
429429
bchUtxos.sort(utxoComparator).reverse();
430430

431431
// Add all automatically added token inputs to the transaction
432-
for (const utxo of automaticTokenInputs) {
432+
for (const utxo of tokenInputs) {
433433
this.inputs.push(utxo);
434434
satsAvailable += addPrecision(utxo.satoshis);
435435
if (!this.hardcodedFee) fee += addPrecision(contractInputSize * this.feePerByte);

packages/cashscript/src/index.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import SignatureTemplate from './SignatureTemplate.js';
33
export { SignatureTemplate };
44
export { Contract, ContractFunction } from './Contract.js';
55
export { Transaction } from './Transaction.js';
6-
export { Argument } from './Argument.js';
6+
export { Argument, encodeArgument } from './Argument.js';
77
export { Artifact, AbiFunction, AbiInput } from '@cashscript/utils';
88
export * as utils from '@cashscript/utils';
99
export {
@@ -12,6 +12,7 @@ export {
1212
SignatureAlgorithm,
1313
HashType,
1414
Network,
15+
isSignableUtxo,
1516
} from './interfaces.js';
1617
export * from './Errors.js';
1718
export {

packages/cashscript/test/Contract.test.ts

-2
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,6 @@ describe('Contract', () => {
8888
const p2sh20Instance = new Contract(p2pkhArtifact, [placeholder(20)], { provider, addressType: 'p2sh20' });
8989
const p2sh32Instance = new Contract(p2pkhArtifact, [placeholder(20)], { provider, addressType: 'p2sh32' });
9090

91-
console.log(p2sh20Instance.address, p2sh32Instance.address);
92-
9391
const P2SH20_ADDRESS_SIZE = 42;
9492
const P2SH32_ADDRESS_SIZE = 61;
9593
expect(p2sh20Instance.address.split(':')[1]).toHaveLength(P2SH20_ADDRESS_SIZE);

packages/cashscript/test/e2e/P2PKH-tokens.test.ts

-4
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,6 @@ describe('P2PKH-tokens', () => {
121121
.send();
122122

123123
const contractUtxos = await p2pkhInstance.getUtxos();
124-
console.log(contractUtxos);
125124
const [genesisUtxo] = contractUtxos.filter((utxo) => utxo.vout === 0 && utxo.satoshis > 2000);
126125

127126
if (!genesisUtxo) {
@@ -255,9 +254,6 @@ describe('P2PKH-tokens', () => {
255254
const nftUtxo = contractUtxos.find(isNftUtxo);
256255
const nonTokenUtxos = contractUtxos.filter(isNonTokenUtxo);
257256

258-
console.log(nftUtxo);
259-
console.log(contractUtxos);
260-
261257
if (!nftUtxo) {
262258
throw new Error('No token UTXO found with an NFT');
263259
}

0 commit comments

Comments
 (0)