Skip to content

Commit 9c0dc88

Browse files
committed
Remove old transaction builder and remove related occurrances in tests
1 parent cac756f commit 9c0dc88

13 files changed

Lines changed: 47 additions & 2007 deletions

File tree

packages/cashscript/README.md

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,23 @@ Using the CashScript SDK, you can import contract artifact files, create new ins
4343
console.log('contract address:', contract.address);
4444
console.log('contract balance:', await contract.getBalance());
4545

46-
// Call the spend function with the owner's signature
47-
// And use it to send 0. 000 100 00 BCH back to the contract's address
48-
const txDetails = await contract.functions
49-
.spend(pk, new SignatureTemplate(keypair))
50-
.to(contract.address, 10000)
46+
const transactionBuilder = new TransactionBuilder({ provider });
47+
const contractUtxos = await contract.getUtxos();
48+
49+
const sendAmount = 10_000n;
50+
const destinationAddress = '... some address ...';
51+
52+
// Calculate the change amount, accounting for a miner fee of 1000 satoshis
53+
const changeAmount = contractUtxos[0].satoshis - sendAmount - 1000n;
54+
55+
// Construct a transaction with the transaction builder
56+
const txDetails = await transactionBuilder
57+
// Add a contract input that spends from the contract using the 'spend' function
58+
.addInput(contractUtxos[0], contract.unlock.spend(pk, new SignatureTemplate(keypair)))
59+
// Add an output that sends 0. 000 100 00 BCH back to the destination address
60+
.addOutput({ to: destinationAddress, amount: sendAmount })
61+
// Add a change output that sends the change back to the contract's address
62+
.addOutput({ to: contract.address, amount: changeAmount })
5163
.send();
5264

5365
console.log(txDetails);

packages/cashscript/src/Contract.ts

Lines changed: 2 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,8 @@ import {
1010
Script,
1111
scriptToBytecode,
1212
} from '@cashscript/utils';
13-
import { Transaction } from './Transaction.js';
1413
import {
15-
ConstructorArgument, encodeFunctionArgument, encodeConstructorArguments, encodeFunctionArguments, FunctionArgument,
14+
ConstructorArgument, encodeFunctionArgument, encodeConstructorArguments, FunctionArgument,
1615
} from './Argument.js';
1716
import {
1817
Unlocker, ContractOptions, GenerateUnlockingBytecodeOptions, Utxo, AddressType, ContractUnlocker,
@@ -29,12 +28,10 @@ export class Contract<
2928
TArtifact extends Artifact = Artifact,
3029
TResolved extends {
3130
constructorInputs: ConstructorArgument[];
32-
functions: Record<string, any>;
3331
unlock: Record<string, any>;
3432
}
3533
= {
3634
constructorInputs: ParamsToTuple<TArtifact['constructorInputs']>;
37-
functions: AbiToFunctionMap<TArtifact['abi'], Transaction>;
3835
unlock: AbiToFunctionMap<TArtifact['abi'], Unlocker>;
3936
},
4037
> {
@@ -44,10 +41,7 @@ export class Contract<
4441
bytecode: string;
4542
bytesize: number;
4643
opcount: number;
47-
48-
functions: TResolved['functions'];
4944
unlock: TResolved['unlock'];
50-
5145
redeemScript: Script;
5246
public provider: NetworkProvider;
5347
public addressType: AddressType;
@@ -79,21 +73,7 @@ export class Contract<
7973

8074
this.redeemScript = generateRedeemScript(asmToScript(this.artifact.bytecode), this.encodedConstructorArgs);
8175

82-
// Populate the functions object with the contract's functions
83-
// (with a special case for single function, which has no "function selector")
84-
this.functions = {};
85-
if (artifact.abi.length === 1) {
86-
const f = artifact.abi[0];
87-
// @ts-ignore TODO: see if we can use generics to make TypeScript happy
88-
this.functions[f.name] = this.createFunction(f);
89-
} else {
90-
artifact.abi.forEach((f, i) => {
91-
// @ts-ignore TODO: see if we can use generics to make TypeScript happy
92-
this.functions[f.name] = this.createFunction(f, i);
93-
});
94-
}
95-
96-
// Populate the functions object with the contract's functions
76+
// Populate the 'unlock' object with the contract's functions
9777
// (with a special case for single function, which has no "function selector")
9878
this.unlock = {};
9979
if (artifact.abi.length === 1) {
@@ -124,27 +104,6 @@ export class Contract<
124104
return this.provider.getUtxos(this.address);
125105
}
126106

127-
private createFunction(abiFunction: AbiFunction, selector?: number): ContractFunction {
128-
return (...args: FunctionArgument[]) => {
129-
if (abiFunction.inputs.length !== args.length) {
130-
throw new Error(`Incorrect number of arguments passed to function ${abiFunction.name}. Expected ${abiFunction.inputs.length} arguments (${abiFunction.inputs.map((input) => input.type)}) but got ${args.length}`);
131-
}
132-
133-
// Encode passed args (this also performs type checking)
134-
const encodedArgs = encodeFunctionArguments(abiFunction, args);
135-
136-
const unlocker = this.createUnlocker(abiFunction, selector)(...args);
137-
138-
return new Transaction(
139-
this,
140-
unlocker,
141-
abiFunction,
142-
encodedArgs,
143-
selector,
144-
);
145-
};
146-
}
147-
148107
private createUnlocker(abiFunction: AbiFunction, selector?: number): ContractFunctionUnlocker {
149108
return (...args: FunctionArgument[]) => {
150109
if (abiFunction.inputs.length !== args.length) {
@@ -179,5 +138,4 @@ export class Contract<
179138
}
180139
}
181140

182-
export type ContractFunction = (...args: FunctionArgument[]) => Transaction;
183141
type ContractFunctionUnlocker = (...args: FunctionArgument[]) => ContractUnlocker;

0 commit comments

Comments
 (0)