Skip to content

Commit 3cd562a

Browse files
committed
Fix failing test that compains about token address for token change outputs
1 parent 5f0c320 commit 3cd562a

File tree

2 files changed

+22
-23
lines changed

2 files changed

+22
-23
lines changed

packages/cashscript/src/Contract.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ export class Contract {
3333
functions: Record<string, ContractFunction>;
3434
unlock: Record<string, ContractUnlocker>;
3535

36-
private redeemScript: Script;
37-
private provider: NetworkProvider;
36+
redeemScript: Script;
37+
provider: NetworkProvider;
3838
private addressType: 'p2sh20' | 'p2sh32';
3939

4040
constructor(
@@ -123,9 +123,7 @@ export class Contract {
123123
const unlocker = this.createUnlocker(abiFunction, selector)(...args);
124124

125125
return new Transaction(
126-
this.address,
127-
this.provider,
128-
this.redeemScript,
126+
this,
129127
unlocker,
130128
abiFunction,
131129
encodedArgs,

packages/cashscript/src/Transaction.ts

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import delay from 'delay';
88
import {
99
AbiFunction,
1010
placeholder,
11-
Script,
1211
scriptToBytecode,
1312
} from '@cashscript/utils';
1413
import deepEqual from 'fast-deep-equal';
@@ -36,10 +35,10 @@ import {
3635
getOutputSize,
3736
utxoTokenComparator,
3837
} from './utils.js';
39-
import NetworkProvider from './network/NetworkProvider.js';
4038
import SignatureTemplate from './SignatureTemplate.js';
4139
import { P2PKH_INPUT_SIZE } from './constants.js';
4240
import { TransactionBuilder } from './TransactionBuilder.js';
41+
import { Contract } from './Contract.js';
4342

4443
export class Transaction {
4544
private inputs: Utxo[] = [];
@@ -53,9 +52,7 @@ export class Transaction {
5352
private tokenChange: boolean = true;
5453

5554
constructor(
56-
private address: string,
57-
private provider: NetworkProvider,
58-
private redeemScript: Script,
55+
private contract: Contract,
5956
private unlocker: Unlocker,
6057
private abiFunction: AbiFunction,
6158
private args: (Uint8Array | SignatureTemplate)[],
@@ -148,10 +145,10 @@ export class Transaction {
148145
}
149146

150147
async build(): Promise<string> {
151-
this.locktime = this.locktime ?? await this.provider.getBlockHeight();
148+
this.locktime = this.locktime ?? await this.contract.provider.getBlockHeight();
152149
await this.setInputsAndOutputs();
153150

154-
const builder = new TransactionBuilder({ provider: this.provider });
151+
const builder = new TransactionBuilder({ provider: this.contract.provider });
155152

156153
this.inputs.forEach((utxo) => {
157154
if (isUtxoP2PKH(utxo)) {
@@ -173,11 +170,11 @@ export class Transaction {
173170
async send(raw?: true): Promise<TransactionDetails | string> {
174171
const tx = await this.build();
175172
try {
176-
const txid = await this.provider.sendRawTransaction(tx);
173+
const txid = await this.contract.provider.sendRawTransaction(tx);
177174
return raw ? await this.getTxDetails(txid, raw) : await this.getTxDetails(txid);
178175
} catch (e: any) {
179176
const reason = e.error ?? e.message;
180-
throw buildError(reason, meep(tx, this.inputs, this.redeemScript));
177+
throw buildError(reason, meep(tx, this.inputs, this.contract.redeemScript));
181178
}
182179
}
183180

@@ -188,7 +185,7 @@ export class Transaction {
188185
for (let retries = 0; retries < 1200; retries += 1) {
189186
await delay(500);
190187
try {
191-
const hex = await this.provider.getRawTransaction(txid);
188+
const hex = await this.contract.provider.getRawTransaction(txid);
192189

193190
if (raw) return hex;
194191

@@ -205,7 +202,7 @@ export class Transaction {
205202

206203
async meep(): Promise<string> {
207204
const tx = await this.build();
208-
return meep(tx, this.inputs, this.redeemScript);
205+
return meep(tx, this.inputs, this.contract.redeemScript);
209206
}
210207

211208
private async setInputsAndOutputs(): Promise<void> {
@@ -214,7 +211,9 @@ export class Transaction {
214211
}
215212

216213
// Fetched utxos are only used when no inputs are available, so only fetch in that case.
217-
const allUtxos: Utxo[] = this.inputs.length === 0 ? await this.provider.getUtxos(this.address) : [];
214+
const allUtxos: Utxo[] = this.inputs.length === 0
215+
? await this.contract.provider.getUtxos(this.contract.address)
216+
: [];
218217

219218
const tokenInputs = this.inputs.length > 0
220219
? this.inputs.filter((input) => input.token)
@@ -226,7 +225,9 @@ export class Transaction {
226225
}
227226

228227
if (this.tokenChange) {
229-
const tokenChangeOutputs = createFungibleTokenChangeOutputs(tokenInputs, this.outputs, this.address);
228+
const tokenChangeOutputs = createFungibleTokenChangeOutputs(
229+
tokenInputs, this.outputs, this.contract.tokenAddress,
230+
);
230231
this.outputs.push(...tokenChangeOutputs);
231232
}
232233

@@ -305,7 +306,7 @@ export class Transaction {
305306
commitment: unusedNft.commitment,
306307
},
307308
};
308-
const nftChangeOutput = { to: this.address, amount: BigInt(1000), token: tokenDetails };
309+
const nftChangeOutput = { to: this.contract.tokenAddress, amount: BigInt(1000), token: tokenDetails };
309310
this.outputs.push(nftChangeOutput);
310311
}
311312
}
@@ -318,13 +319,13 @@ export class Transaction {
318319

319320
// Create a placeholder preimage of the correct size
320321
const placeholderPreimage = this.abiFunction.covenant
321-
? placeholder(getPreimageSize(scriptToBytecode(this.redeemScript)))
322+
? placeholder(getPreimageSize(scriptToBytecode(this.contract.redeemScript)))
322323
: undefined;
323324

324325
// Create a placeholder input script for size calculation using the placeholder
325326
// arguments and correctly sized placeholder preimage
326327
const placeholderScript = createInputScript(
327-
this.redeemScript,
328+
this.contract.redeemScript,
328329
placeholderArgs,
329330
this.selector,
330331
placeholderPreimage,
@@ -389,12 +390,12 @@ export class Transaction {
389390

390391
// Account for the fee of adding a change output
391392
if (!this.hardcodedFee) {
392-
const changeOutputSize = getOutputSize({ to: this.address, amount: 0n });
393+
const changeOutputSize = getOutputSize({ to: this.contract.address, amount: 0n });
393394
change -= BigInt(changeOutputSize * this.feePerByte);
394395
}
395396

396397
// Add a change output if applicable
397-
const changeOutput = { to: this.address, amount: change };
398+
const changeOutput = { to: this.contract.address, amount: change };
398399
if (change >= this.minChange && change >= calculateDust(changeOutput)) {
399400
this.outputs.push(changeOutput);
400401
}

0 commit comments

Comments
 (0)