Skip to content

Commit cac756f

Browse files
committed
Make provider a required option in the Contract constructor
1 parent a981e02 commit cac756f

5 files changed

Lines changed: 43 additions & 36 deletions

File tree

packages/cashscript/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ Using the CashScript SDK, you can import contract artifact files, create new ins
3737
const provider = new ElectrumNetworkProvider('mainnet');
3838

3939
// Create a new P2PKH contract with constructor arguments: { pkh: pkh }
40-
const contract = new Contract(P2PKH, [pkh], provider);
40+
const contract = new Contract(P2PKH, [pkh], { provider });
4141

4242
// Get contract balance & output address + balance
4343
console.log('contract address:', contract.address);

packages/cashscript/src/Contract.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ import {
2222
addressToLockScript, createInputScript, createSighashPreimage, scriptToAddress,
2323
} from './utils.js';
2424
import SignatureTemplate from './SignatureTemplate.js';
25-
import { ElectrumNetworkProvider } from './network/index.js';
2625
import { ParamsToTuple, AbiToFunctionMap } from './types/type-inference.js';
2726
import semver from 'semver';
2827

@@ -57,10 +56,10 @@ export class Contract<
5756
constructor(
5857
public artifact: TArtifact,
5958
constructorArgs: TResolved['constructorInputs'],
60-
private options?: ContractOptions,
59+
private options: ContractOptions,
6160
) {
62-
this.provider = this.options?.provider ?? new ElectrumNetworkProvider();
63-
this.addressType = this.options?.addressType ?? 'p2sh32';
61+
this.provider = this.options.provider;
62+
this.addressType = this.options.addressType ?? 'p2sh32';
6463

6564
const expectedProperties = ['abi', 'bytecode', 'constructorInputs', 'contractName', 'compiler'];
6665
if (!expectedProperties.every((property) => property in artifact)) {

packages/cashscript/src/interfaces.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ export interface TransactionDetails extends Transaction {
158158
}
159159

160160
export interface ContractOptions {
161-
provider?: NetworkProvider,
161+
provider: NetworkProvider,
162162
addressType?: AddressType,
163163
}
164164

packages/cashscript/test/types/Contract.types.test.ts

Lines changed: 37 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* eslint-disable */
2-
import { Artifact, Contract, SignatureTemplate, Transaction, Unlocker } from 'cashscript';
2+
import { Artifact, Contract, MockNetworkProvider, SignatureTemplate, Transaction, Unlocker } from 'cashscript';
33
import p2pkhArtifact from '../fixture/p2pkh.artifact';
44
import p2pkhArtifactJsonNotConst from '../fixture/p2pkh.json' with { type: 'json' };
55
import announcementArtifact from '../fixture/announcement.artifact';
@@ -32,41 +32,48 @@ interface ManualArtifactType extends Artifact {
3232
]
3333
}
3434

35+
// Create a MockNetworkProvider for the tests
36+
const provider = new MockNetworkProvider();
37+
3538
// describe('P2PKH contract | single constructor input | single function (2 args)')
3639
{
3740
// describe('Constructor arguments')
3841
{
3942
// it('should not give type errors when using correct constructor inputs')
40-
new Contract(p2pkhArtifact, [alicePkh]);
41-
new Contract(p2pkhArtifact, [binToHex(alicePkh)]);
43+
new Contract(p2pkhArtifact, [alicePkh], { provider });
44+
new Contract(p2pkhArtifact, [binToHex(alicePkh)], { provider });
4245

4346
// it('should give type errors when using empty constructor inputs')
4447
// @ts-expect-error
45-
new Contract(p2pkhArtifact, []);
48+
new Contract(p2pkhArtifact, [], { provider });
4649

4750
// it('should give type errors when using incorrect constructor input type')
4851
// @ts-expect-error
49-
new Contract(p2pkhArtifact, [1000n]);
52+
new Contract(p2pkhArtifact, [1000n], { provider });
5053

5154
// it('should give type errors when using incorrect constructor input length')
5255
// @ts-expect-error
53-
new Contract(p2pkhArtifact, [alicePkh, 1000n]);
56+
new Contract(p2pkhArtifact, [alicePkh, 1000n], { provider });
5457

5558
// it('should not perform type checking when cast to any')
56-
new Contract(p2pkhArtifact as any, [alicePkh, 1000n]);
59+
new Contract(p2pkhArtifact as any, [alicePkh, 1000n], { provider });
5760

5861
// it('should not perform type checking when cannot infer type')
5962
// Note: would be very nice if it *could* infer the type from static json
60-
new Contract(p2pkhArtifactJsonNotConst, [alicePkh, 1000n]);
63+
new Contract(p2pkhArtifactJsonNotConst, [alicePkh, 1000n], { provider });
6164

6265
// it('should perform type checking when manually specifying a type
6366
// @ts-expect-error
64-
new Contract<ManualArtifactType>(p2pkhArtifactJsonNotConst as any, [alicePkh, 1000n]);
67+
new Contract<ManualArtifactType>(p2pkhArtifactJsonNotConst as any, [alicePkh, 1000n], { provider });
68+
69+
// it('requires a provider to be passed')
70+
// @ts-expect-error
71+
new Contract(p2pkhArtifact, [alicePkh]);
6572
}
6673

6774
// describe('Contract unlockers')
6875
{
69-
const contract = new Contract(p2pkhArtifact, [alicePkh]);
76+
const contract = new Contract(p2pkhArtifact, [alicePkh], { provider });
7077

7178
// it('should not give type errors when using correct function inputs')
7279
contract.unlock.spend(alicePub, new SignatureTemplate(alicePriv));
@@ -86,14 +93,14 @@ interface ManualArtifactType extends Artifact {
8693
contract.unlock.spend(alicePub);
8794

8895
// it('should not perform type checking when cast to any')
89-
const contractAsAny = new Contract(p2pkhArtifact as any, [alicePkh, 1000n]);
96+
const contractAsAny = new Contract(p2pkhArtifact as any, [alicePkh, 1000n], { provider });
9097
contractAsAny.unlock.notAFunction();
9198
contractAsAny.unlock.spend();
9299
contractAsAny.unlock.spend(1000n, true);
93100

94101
// it('should not perform type checking when cannot infer type')
95102
// Note: would be very nice if it *could* infer the type from static json
96-
const contractFromUnknown = new Contract(p2pkhArtifactJsonNotConst, [alicePkh, 1000n]);
103+
const contractFromUnknown = new Contract(p2pkhArtifactJsonNotConst, [alicePkh, 1000n], { provider });
97104
contractFromUnknown.unlock.notAFunction();
98105
contractFromUnknown.unlock.spend();
99106
contractFromUnknown.unlock.spend(1000n, true);
@@ -109,7 +116,7 @@ interface ManualArtifactType extends Artifact {
109116

110117
// describe('Contract functions')
111118
{
112-
const contract = new Contract(p2pkhArtifact, [alicePkh]);
119+
const contract = new Contract(p2pkhArtifact, [alicePkh], { provider });
113120

114121
// it('should not give type errors when using correct function inputs')
115122
contract.functions.spend(alicePub, new SignatureTemplate(alicePriv)).build();
@@ -129,14 +136,14 @@ interface ManualArtifactType extends Artifact {
129136
contract.functions.spend(alicePub);
130137

131138
// it('should not perform type checking when cast to any')
132-
const contractAsAny = new Contract(p2pkhArtifact as any, [alicePkh, 1000n]);
139+
const contractAsAny = new Contract(p2pkhArtifact as any, [alicePkh, 1000n], { provider });
133140
contractAsAny.functions.notAFunction().build();
134141
contractAsAny.functions.spend();
135142
contractAsAny.functions.spend(1000n, true);
136143

137144
// it('should not perform type checking when cannot infer type')
138145
// Note: would be very nice if it *could* infer the type from static json
139-
const contractFromUnknown = new Contract(p2pkhArtifactJsonNotConst, [alicePkh, 1000n]);
146+
const contractFromUnknown = new Contract(p2pkhArtifactJsonNotConst, [alicePkh, 1000n], { provider });
140147
contractFromUnknown.functions.notAFunction().build();
141148
contractFromUnknown.functions.spend();
142149
contractFromUnknown.functions.spend(1000n, true);
@@ -152,7 +159,7 @@ interface ManualArtifactType extends Artifact {
152159

153160
// describe('Contract unlockers')
154161
{
155-
const contract = new Contract(p2pkhArtifact, [alicePkh]);
162+
const contract = new Contract(p2pkhArtifact, [alicePkh], { provider });
156163

157164
// it('should not give type errors when using correct function inputs')
158165
contract.unlock.spend(alicePub, new SignatureTemplate(alicePriv)).generateLockingBytecode();
@@ -172,14 +179,14 @@ interface ManualArtifactType extends Artifact {
172179
contract.unlock.spend(alicePub);
173180

174181
// it('should not perform type checking when cast to any')
175-
const contractAsAny = new Contract(p2pkhArtifact as any, [alicePkh, 1000n]);
182+
const contractAsAny = new Contract(p2pkhArtifact as any, [alicePkh, 1000n], { provider });
176183
contractAsAny.unlock.notAFunction().generateLockingBytecode();
177184
contractAsAny.unlock.spend();
178185
contractAsAny.unlock.spend(1000n, true);
179186

180187
// it('should not perform type checking when cannot infer type')
181188
// Note: would be very nice if it *could* infer the type from static json
182-
const contractFromUnknown = new Contract(p2pkhArtifactJsonNotConst, [alicePkh, 1000n]);
189+
const contractFromUnknown = new Contract(p2pkhArtifactJsonNotConst, [alicePkh, 1000n], { provider });
183190
contractFromUnknown.unlock.notAFunction().generateLockingBytecode();
184191
contractFromUnknown.unlock.spend();
185192
contractFromUnknown.unlock.spend(1000n, true);
@@ -199,21 +206,21 @@ interface ManualArtifactType extends Artifact {
199206
// describe('Constructor arguments')
200207
{
201208
// it('should not give type errors when using correct constructor inputs')
202-
new Contract(announcementArtifact, []);
209+
new Contract(announcementArtifact, [], { provider });
203210

204211
// it('should give type errors when using incorrect constructor input length')
205212
// @ts-expect-error
206-
new Contract(announcementArtifact, [1000n]);
213+
new Contract(announcementArtifact, [1000n], { provider });
207214

208215
// it('should give type errors when passing in completely incorrect type')
209216
// @ts-expect-error
210-
new Contract(announcementArtifact, 'hello');
217+
new Contract(announcementArtifact, 'hello', { provider });
211218
}
212219

213220
// describe('Contract unlockers')
214221
{
215222
// it('should not give type errors when using correct function inputs')
216-
const contract = new Contract(announcementArtifact, []);
223+
const contract = new Contract(announcementArtifact, [], { provider });
217224

218225
// it('should not give type errors when using correct function inputs')
219226
contract.unlock.announce();
@@ -230,7 +237,7 @@ interface ManualArtifactType extends Artifact {
230237
// describe('Contract functions')
231238
{
232239
// it('should not give type errors when using correct function inputs')
233-
const contract = new Contract(announcementArtifact, []);
240+
const contract = new Contract(announcementArtifact, [], { provider });
234241

235242
// it('should not give type errors when using correct function inputs')
236243
contract.functions.announce();
@@ -250,17 +257,17 @@ interface ManualArtifactType extends Artifact {
250257
// describe('Constructor arguments')
251258
{
252259
// it('should not give type errors when using correct constructor inputs')
253-
new Contract(hodlVaultArtifact, [alicePub, binToHex(oraclePub), 1000n, 1000n]);
260+
new Contract(hodlVaultArtifact, [alicePub, binToHex(oraclePub), 1000n, 1000n], { provider });
254261

255262
// it('should give type errors when using too few constructor inputs')
256263
// @ts-expect-error
257-
new Contract(hodlVaultArtifact, [alicePub, binToHex(oraclePub)]);
264+
new Contract(hodlVaultArtifact, [alicePub, binToHex(oraclePub)], { provider });
258265

259266
// it('should give type errors when using incorrect constructor input type')
260267
// @ts-expect-error
261-
new Contract(hodlVaultArtifact, [alicePub, binToHex(oraclePub), 1000n, 'hello']);
268+
new Contract(hodlVaultArtifact, [alicePub, binToHex(oraclePub), 1000n, 'hello'], { provider });
262269
// @ts-expect-error
263-
new Contract(hodlVaultArtifact, [alicePub, binToHex(oraclePub), true, 1000n]);
270+
new Contract(hodlVaultArtifact, [alicePub, binToHex(oraclePub), true, 1000n], { provider });
264271
}
265272
}
266273

@@ -270,12 +277,12 @@ interface ManualArtifactType extends Artifact {
270277
// describe('Constructor arguments')
271278
{
272279
// it('should not give type errors when using correct constructor inputs')
273-
new Contract(transferWithTimeoutArtifact, [alicePub, bobPub, 100_000n]);
280+
new Contract(transferWithTimeoutArtifact, [alicePub, bobPub, 100_000n], { provider });
274281
}
275282

276283
// describe('Contract unlockers')
277284
{
278-
const contract = new Contract(transferWithTimeoutArtifact, [alicePub, bobPub, 100_000n]);
285+
const contract = new Contract(transferWithTimeoutArtifact, [alicePub, bobPub, 100_000n], { provider });
279286

280287
// it('should not give type errors when using correct function inputs')
281288
contract.unlock.transfer(new SignatureTemplate(alicePriv));
@@ -300,7 +307,7 @@ interface ManualArtifactType extends Artifact {
300307

301308
// describe('Contract functions')
302309
{
303-
const contract = new Contract(transferWithTimeoutArtifact, [alicePub, bobPub, 100_000n]);
310+
const contract = new Contract(transferWithTimeoutArtifact, [alicePub, bobPub, 100_000n], { provider });
304311

305312
// it('should not give type errors when using correct function inputs')
306313
contract.functions.transfer(new SignatureTemplate(alicePriv));

website/docs/releases/release-notes.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ title: Release Notes
66

77
#### CashScript SDK
88
- :boom: **BREAKING**: Set `updateUtxoSet` to `true` by default for `MockNetworkProvider`.
9+
- :boom: **BREAKING**: Make `provider` a required option in `Contract` constructor.
910

1011
## v0.11.5
1112

0 commit comments

Comments
 (0)