Skip to content

Commit 45a975a

Browse files
committed
Use MockNetworkProvider in examples
1 parent 36b02bc commit 45a975a

File tree

9 files changed

+61
-29
lines changed

9 files changed

+61
-29
lines changed

examples/announcement.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,26 @@
1-
import { Contract, ElectrumNetworkProvider, Output, TransactionBuilder } from 'cashscript';
1+
import { Contract, Output, randomUtxo, TransactionBuilder } from 'cashscript';
22
import { compileFile } from 'cashc';
33
import { stringify } from '@bitauth/libauth';
44
import { URL } from 'url';
5+
import { MockNetworkProvider } from 'cashscript/dist';
56

67
// Compile the Announcement contract to an artifact object
78
const artifact = compileFile(new URL('announcement.cash', import.meta.url));
89

910
// Initialise a network provider for network operations on MAINNET
1011
const addressType = 'p2sh20';
11-
const provider = new ElectrumNetworkProvider();
12+
13+
// Once you're ready to send transactions on a real network (like chipnet or mainnet), use the ElectrumNetworkProvider
14+
// const provider = new ElectrumNetworkProvider();
15+
const provider = new MockNetworkProvider();
1216

1317
// Instantiate a new contract using the compiled artifact and network provider
1418
// AND providing the constructor parameters (none)
1519
const contract = new Contract(artifact, [], { provider, addressType });
1620

21+
// Add a mock UTXO to the mock network provider
22+
provider.addUtxo(contract.address, randomUtxo());
23+
1724
// Get contract balance & output address + balance
1825
console.log('contract address:', contract.address);
1926
const contractUtxos = await contract.getUtxos();

examples/hodl_vault.cash

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ contract HodlVault(
1111
int minBlock,
1212
int priceTarget
1313
) {
14-
function spend(sig ownerSig, datasig oracleSig, bytes oracleMessage) {
14+
function spend(sig ownerSig, datasig oracleSig, bytes8 oracleMessage) {
1515
// message: { blockHeight, price }
1616
bytes4 blockHeightBin, bytes4 priceBin = oracleMessage.split(4);
1717
int blockHeight = int(blockHeightBin);

examples/hodl_vault.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { stringify } from '@bitauth/libauth';
2-
import { Contract, SignatureTemplate, ElectrumNetworkProvider, TransactionBuilder, Output } from 'cashscript';
2+
import { Contract, SignatureTemplate, TransactionBuilder, Output, MockNetworkProvider, randomUtxo } from 'cashscript';
33
import { compileFile } from 'cashc';
44
import { URL } from 'url';
55

@@ -14,14 +14,18 @@ import {
1414
// Compile the HodlVault contract to an artifact object
1515
const artifact = compileFile(new URL('hodl_vault.cash', import.meta.url));
1616

17-
// Initialise a network provider for network operations on CHIPNET
18-
const provider = new ElectrumNetworkProvider('chipnet');
17+
// Once you're ready to send transactions on a real network (like chipnet or mainnet), use the ElectrumNetworkProvider
18+
// const provider = new ElectrumNetworkProvider();
19+
const provider = new MockNetworkProvider();
1920

2021
// Instantiate a new contract using the compiled artifact and network provider
2122
// AND providing the constructor parameters
2223
const parameters = [alicePub, oraclePub, 100000n, 30000n];
2324
const contract = new Contract(artifact, parameters, { provider });
2425

26+
// Add a mock UTXO to the mock network provider
27+
provider.addUtxo(contract.address, randomUtxo());
28+
2529
// Get contract balance & output address + balance
2630
console.log('contract address:', contract.address);
2731
const contractUtxos = await contract.getUtxos();

examples/mecenas.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { stringify } from '@bitauth/libauth';
2-
import { Contract, ElectrumNetworkProvider, Output, TransactionBuilder } from 'cashscript';
2+
import { Contract, MockNetworkProvider, Output, randomUtxo, TransactionBuilder } from 'cashscript';
33
import { compileFile } from 'cashc';
44
import { URL } from 'url';
55

@@ -9,15 +9,19 @@ import { aliceAddress, alicePkh, bobPkh } from './common.js';
99
// Compile the Mecenas contract to an artifact object
1010
const artifact = compileFile(new URL('mecenas.cash', import.meta.url));
1111

12-
// Initialise a network provider for network operations on CHIPNET
13-
const provider = new ElectrumNetworkProvider('chipnet');
12+
// Once you're ready to send transactions on a real network (like chipnet or mainnet), use the ElectrumNetworkProvider
13+
// const provider = new ElectrumNetworkProvider();
14+
const provider = new MockNetworkProvider();
1415

1516
// Instantiate a new contract using the compiled artifact and network provider
1617
// AND providing the constructor parameters:
1718
// (recipient: alicePkh, funder: bobPkh, pledge: 10000)
1819
const pledgeAmount = 10_000n;
1920
const contract = new Contract(artifact, [alicePkh, bobPkh, pledgeAmount], { provider });
2021

22+
// Add a mock UTXO to the mock network provider
23+
provider.addUtxo(contract.address, randomUtxo());
24+
2125
// Get contract balance & output address + balance
2226
console.log('contract address:', contract.address);
2327
const contractUtxos = await contract.getUtxos();

examples/p2pkh.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { URL } from 'url';
22
import { compileFile } from 'cashc';
3-
import { ElectrumNetworkProvider, Contract, SignatureTemplate, TransactionBuilder } from 'cashscript';
3+
import { Contract, SignatureTemplate, TransactionBuilder, MockNetworkProvider, randomUtxo } from 'cashscript';
44
import { stringify } from '@bitauth/libauth';
55

66
// Import Alice's keys from common.ts
@@ -9,13 +9,17 @@ import { alicePkh, alicePriv, aliceAddress, alicePub } from './common.js';
99
// Compile the P2PKH contract to an artifact object
1010
const artifact = compileFile(new URL('p2pkh.cash', import.meta.url));
1111

12-
// Initialise a network provider for network operations on CHIPNET
13-
const provider = new ElectrumNetworkProvider('chipnet');
12+
// Once you're ready to send transactions on a real network (like chipnet or mainnet), use the ElectrumNetworkProvider
13+
// const provider = new ElectrumNetworkProvider();
14+
const provider = new MockNetworkProvider();
1415

1516
// Instantiate a new contract using the compiled artifact and network provider
1617
// AND providing the constructor parameters (pkh: alicePkh)
1718
const contract = new Contract(artifact, [alicePkh], { provider });
1819

20+
// Add a mock UTXO to the mock network provider
21+
provider.addUtxo(contract.address, randomUtxo());
22+
1923
// Get contract balance & output address + balance
2024
console.log('contract address:', contract.address);
2125
const contractUtxos = await contract.getUtxos();
@@ -52,4 +56,4 @@ if (changeAmount > 1000n) transactionBuilder.addOutput(changeOutput);
5256

5357
const tx = await transactionBuilder.send();
5458

55-
console.log('transaction details:', stringify(tx));
59+
console.log('transaction details:', stringify(tx));

examples/p2pkh.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { stringify } from '@bitauth/libauth';
22
import { compileFile } from 'cashc';
3-
import { ElectrumNetworkProvider, SignatureTemplate, Contract, TransactionBuilder, Output } from 'cashscript';
3+
import { SignatureTemplate, Contract, TransactionBuilder, Output, MockNetworkProvider, randomUtxo } from 'cashscript';
44
import { URL } from 'url';
55

66
// Import Alice's keys from common.ts
@@ -9,13 +9,17 @@ import { alicePkh, alicePriv, aliceAddress, alicePub } from './common.js';
99
// Compile the P2PKH contract to an artifact object
1010
const artifact = compileFile(new URL('p2pkh.cash', import.meta.url));
1111

12-
// Initialise a network provider for network operations on CHIPNET
13-
const provider = new ElectrumNetworkProvider('chipnet');
12+
// Once you're ready to send transactions on a real network (like chipnet or mainnet), use the ElectrumNetworkProvider
13+
// const provider = new ElectrumNetworkProvider();
14+
const provider = new MockNetworkProvider();
1415

1516
// Instantiate a new contract using the compiled artifact and network provider
1617
// AND providing the constructor parameters (pkh: alicePkh)
1718
const contract = new Contract(artifact, [alicePkh], { provider });
1819

20+
// Add a mock UTXO to the mock network provider
21+
provider.addUtxo(contract.address, randomUtxo());
22+
1923
// Get contract balance & output address + balance
2024
console.log('contract address:', contract.address);
2125
const contractUtxos = await contract.getUtxos();

examples/transfer_with_timeout.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { stringify } from '@bitauth/libauth';
22
import { compileFile } from 'cashc';
3-
import { Contract, ElectrumNetworkProvider, Output, SignatureTemplate, TransactionBuilder } from 'cashscript';
3+
import { Contract, MockNetworkProvider, Output, randomUtxo, SignatureTemplate, TransactionBuilder } from 'cashscript';
44
import { URL } from 'url';
55

66
// Import Bob and Alice's keys from common.ts
@@ -14,14 +14,18 @@ import {
1414
// Compile the TransferWithTimeout contract
1515
const artifact = compileFile(new URL('transfer_with_timeout.cash', import.meta.url));
1616

17-
// Initialise a network provider for network operations on CHIPNET
18-
const provider = new ElectrumNetworkProvider('chipnet');
17+
// Once you're ready to send transactions on a real network (like chipnet or mainnet), use the ElectrumNetworkProvider
18+
// const provider = new ElectrumNetworkProvider();
19+
const provider = new MockNetworkProvider();
1920

2021
// Instantiate a new contract using the compiled artifact and network provider
2122
// AND providing the constructor parameters:
2223
// { sender: alicePk, recipient: bobPk, timeout: 1000000 } - timeout is a past block
2324
const contract = new Contract(artifact, [alicePub, bobPub, 100000n], { provider });
2425

26+
// Add a mock UTXO to the mock network provider
27+
provider.addUtxo(contract.address, randomUtxo());
28+
2529
// Get contract balance & output address + balance
2630
console.log('contract address:', contract.address);
2731
const contractUtxos = await contract.getUtxos();

website/docs/basics/getting-started.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,6 @@ For a code example of how to generate key-pairs with Libauth, see the [CashScrip
119119

120120
With the instantiated contract, we can now get the contract address and get the contract balance and UTXOs in the following way:
121121

122-
123122
```javascript
124123
import { ElectrumNetworkProvider, Contract } from 'cashscript';
125124
import artifact from './TransferWithTimeout.json' with { type: 'json' };

website/docs/sdk/examples.md

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ We will break up the development of the smart contract application in 4 manageab
1414

1515
### Creating the keypairs
1616

17-
To put the `HodlVault.cash` contract to use in a TypeScript application, we have to use the CashScript SDK in combination with a BCH library such as [Libauth][libauth], [Mainnetjs][mainnetjs] or [BCHJS][bchjs]. These libraries are used to generate public/private keys for the contract participants.
17+
To put the `HodlVault.cash` contract to use in a TypeScript application, we have to use the CashScript SDK in combination with a BCH library such as [Libauth][libauth] or [Mainnetjs][mainnetjs]. These libraries are used to generate public/private keys for the contract participants.
18+
1819
In this example we'll use [Libauth][libauth] to generate the keys `alicePriv`, `alicePub`, `oracle` & `oraclePub`. Then we can use these keys to create the smart contract.
1920

2021
:::caution
@@ -57,11 +58,11 @@ export const oracleAddress = encodeCashAddress('bchtest', 'p2pkhWithTokens', ora
5758

5859
### Generating a Contract
5960

60-
For the networkprovider, we'll use the `ElectrumNetworkProvider` from the SDK and for `Simple Transaction Builder` for this example. Once you have a smart contract address you can send funds to it. To spend the Bitcoin cash locked in the contract you will have to satisfy the spending conditions on the contract.
61+
For development purposes, we'll use the `MockNetworkProvider` so you can simulate transactions in a 'mock' network environment. To do proper testing, you also need to add "mock" UTXOs to the `MockNetworkProvider`.
6162

6263
```ts title="hodl_vault.ts"
6364
import { stringify } from '@bitauth/libauth';
64-
import { Contract, SignatureTemplate, ElectrumNetworkProvider } from 'cashscript';
65+
import { Contract, SignatureTemplate, MockNetworkProvider, randomUtxo } from 'cashscript';
6566
import { compileFile } from 'cashc';
6667
import { URL } from 'url';
6768

@@ -74,14 +75,17 @@ import {
7475
// Compile the HodlVault contract to an artifact object
7576
const artifact = compileFile(new URL('hodl_vault.cash', import.meta.url));
7677

77-
// Initialise a network provider for network operations on CHIPNET
78-
const provider = new ElectrumNetworkProvider('chipnet');
78+
// Initialise a network provider for network operations on MockNet
79+
const provider = new MockNetworkProvider();
7980

8081
// Instantiate a new contract using the compiled artifact and network provider
8182
// AND providing the constructor parameters
8283
const parameters = [alicePub, oraclePub, 100000n, 30000n];
8384
const contract = new Contract(artifact, parameters, { provider });
8485

86+
// Add a mock UTXO to the network provider
87+
provider.addUtxo(contract.address, randomUtxo());
88+
8589
// Get contract balance & output address + balance
8690
console.log('contract address:', contract.address);
8791
console.log('contract balance:', await contract.getBalance());
@@ -121,7 +125,7 @@ Finally, we can put all of this together to create a working smart contract appl
121125

122126
```ts title="hodl_vault.ts"
123127
import { stringify } from '@bitauth/libauth';
124-
import { Contract, SignatureTemplate, ElectrumNetworkProvider } from 'cashscript';
128+
import { Contract, SignatureTemplate, MockNetworkProvider } from 'cashscript';
125129
import { compileFile } from 'cashc';
126130
import { URL } from 'url';
127131

@@ -136,14 +140,17 @@ import {
136140
// Compile the HodlVault contract to an artifact object
137141
const artifact = compileFile(new URL('hodl_vault.cash', import.meta.url));
138142

139-
// Initialise a network provider for network operations on CHIPNET
140-
const provider = new ElectrumNetworkProvider('chipnet');
143+
// Initialise a network provider for network operations on MockNet
144+
const provider = new MockNetworkProvider();
141145

142146
// Instantiate a new contract using the compiled artifact and network provider
143147
// AND providing the constructor parameters
144148
const parameters = [alicePub, oraclePub, 100000n, 30000n];
145149
const contract = new Contract(artifact, parameters, { provider });
146150

151+
// Add a mock UTXO to the network provider
152+
provider.addUtxo(contract.address, randomUtxo());
153+
147154
// Fetch contract utxos
148155
const contractUtxos = await contract.getUtxos();
149156

@@ -177,7 +184,6 @@ const transferDetails = await new TransactionBuilder({ provider })
177184
console.log(transferDetails);
178185
```
179186

180-
[bchjs]: https://bchjs.fullstack.cash/
181187
[mainnetjs]: https://mainnet.cash/
182188
[libauth]: https://libauth.org/
183189
[github-examples]: https://github.com/CashScript/cashscript/tree/master/examples

0 commit comments

Comments
 (0)