Skip to content

Commit b3b48dc

Browse files
committed
Use MockNetworkProvider in HodlVault example
1 parent 36b02bc commit b3b48dc

File tree

2 files changed

+15
-10
lines changed

2 files changed

+15
-10
lines changed

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)