Skip to content

Commit a747598

Browse files
committed
Updated example and readme
Signed-off-by: Robert Gogete <gogeterobert@yahoo.com>
1 parent 36110cc commit a747598

File tree

2 files changed

+125
-58
lines changed

2 files changed

+125
-58
lines changed

README.md

Lines changed: 66 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,90 @@
1-
# DIG SDK v2 Example: Coin Indexer with L1PeerService
1+
# DIG SDK v2 – Chia Coin Indexer Example
22

3-
This example demonstrates how to use the CoinIndexer and L1PeerService to sync and monitor coins from the Chia blockchain using the DIG SDK v2.
3+
This project demonstrates how to use the DIG SDK v2 to index, monitor, and interact with Chia blockchain coins using the `CoinIndexer`, `ChiaWallet`, `ChiaColdWallet`, and `WalletService` abstractions.
44

55
## Prerequisites
66

77
- **Node.js** (v18+ recommended)
88
- **npm** (v9+ recommended)
99
- Access to the Chia testnet or mainnet (for real blockchain data)
10-
- The following files in your project root:
11-
- `ca.crt` and `ca.key` (Chia network certificates, required for peer connections)
12-
13-
## Setup
10+
## Setup for local example
1411

1512
1. **Install dependencies**
1613

1714
```sh
1815
npm install
1916
```
2017

21-
2. **Build the project**
22-
23-
```sh
24-
npm run build
25-
```
26-
27-
3. **Configure your wallet**
18+
2. **Configure your wallets**
2819

2920
- Open `test/L1PeerServiceWithIndexer.example.ts`.
30-
- Set `testnetMnemonic` to your testnet mnemonic phrase.
31-
- Set `testnetWalletAddress` to a unique name for your wallet (e.g., `"dev"`).
32-
- Ensure `ca.crt` and `ca.key` are present in your project root, or update the paths in the example file.
21+
- Set `testnetMnemonic` to your testnet mnemonic phrase (for hot wallet).
22+
- Set `testnetWalletAddressName` to a unique name for your wallet (e.g., `"dev"`).
23+
- Set `testnetWalletAddress` to your Chia address (for cold wallet).
24+
- Optionally, set `coldWalletName` for your cold wallet entry.
25+
26+
3. **Run the example**
3327

34-
4. **Run the example**
3528
```sh
3629
npx ts-node ./test/L1PeerServiceWithIndexer.example.ts
3730
```
3831

39-
- The script will:
40-
- Connect to the Chia testnet using L1PeerService.
41-
- Create or load a wallet and add it to the local database and keyring.
42-
- Make a transaction by sending txch
43-
- Run the indexer which will fetch all coins for the wallet.
44-
- Periodically print all coins in the local database, showing their status and wallet association.
32+
The script will:
33+
- Connect to the Chia testnet using the CoinIndexer and peer services.
34+
- Create or load a hot wallet (mnemonic-based) and a cold wallet (address-based) using `WalletService`.
35+
- Instantiate `ChiaWallet` and `ChiaColdWallet` wrappers for event-driven coin monitoring.
36+
- Subscribe to coin creation and spend events for both wallet types.
37+
- Periodically print all coins in the local database, showing their status and wallet association.
38+
39+
## Key Concepts
40+
41+
- **CoinIndexer**: Listens to the Chia blockchain, ingests new blocks, and emits events for coin creation and spending.
42+
- **ChiaWallet**: Hot wallet abstraction (mnemonic-based) that emits events for coins belonging to the wallet.
43+
- **ChiaColdWallet**: Cold wallet abstraction (address-based) for monitoring coins without private keys.
44+
- **WalletService**: Utility for creating, loading, and managing wallets and cold wallets.
45+
46+
## Example Usage
47+
48+
See `test/L1PeerServiceWithIndexer.example.ts` for a full example. Key steps:
49+
50+
```typescript
51+
import { CoinIndexer } from "../src/infrastructure/Workers/CoinIndexer/CoinIndexer";
52+
import { WalletService } from "../src/application/services/WalletService";
53+
import { ChiaWallet } from "../src/infrastructure/types/ChiaWallet";
54+
import { ChiaColdWallet } from "../src/infrastructure/types/ChiaColdWallet";
55+
// ...
56+
57+
const coinIndexer = new CoinIndexer();
58+
await coinIndexer.start();
59+
60+
const wallet = await WalletService.createWallet("dev", testnetMnemonic);
61+
const coldWallet = await WalletService.createColdWallet("dev-cold", testnetWalletAddress);
62+
63+
const chiaWallet = new ChiaWallet(wallet, coinIndexer);
64+
const chiaColdWallet = new ChiaColdWallet(coldWallet, coinIndexer);
65+
66+
chiaWallet.on("chiaCoinCreated", (coin) => console.log("[ChiaWallet] Coin created", coin));
67+
chiaColdWallet.on("chiaCoinCreated", (coin) => console.log("[ChiaColdWallet] Coin created", coin));
68+
// ...
69+
```
4570

4671
## Notes
4772

48-
- The example will run indefinitely, printing coin data every 10 seconds. Stop it with `Ctrl+C`.
49-
- You can adjust the polling interval or other parameters in the example script.
73+
- The example runs indefinitely, printing coin data every 10 seconds. Stop it with `Ctrl+C`.
74+
- You can adjust the polling interval and other parameters in the example script.
75+
- The codebase supports both testnet and mainnet. Set `config.BLOCKCHAIN_NETWORK` as needed.
76+
77+
## Testing
78+
79+
Run all tests:
80+
81+
```sh
82+
npm test
83+
```
84+
85+
## Project Structure
86+
87+
- `src/` – SDK source code
88+
- `test/` – Example and test scripts
89+
- `ca.crt`, `ca.key` – Chia network certificates (required for peer connections)
90+

test/L1PeerServiceWithIndexer.example.ts

Lines changed: 59 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -6,54 +6,80 @@ import { WalletService } from '../src/application/services/WalletService';
66
import config from '../src/config';
77
import { BlockchainNetwork } from '../src/config/types/BlockchainNetwork';
88
import { Wallet } from '../src/application/types/Wallet';
9+
import { ColdWallet } from '../src/application/types/ColdWallet';
910
import { CoinRecord, CoinSpend } from '@dignetwork/chia-block-listener';
1011

1112
async function main() {
12-
const testnetWalletAddressName = 'dev'; // Replace with your actual address
13+
const testnetWalletAddressName = 'dev'; // Replace with your actual address name
1314
const testnetMnemonic = ''; // Replace with your actual mnemonic
1415
const testnetWalletAddress = 'txch1fw0lql9h6n9e23yzq8ewg0hnjw2gcftayzrnj6rxlx0q6w7x6klsfy5z5f'; // Replace with your actual address
1516

1617
config.BLOCKCHAIN_NETWORK = BlockchainNetwork.TESTNET;
17-
18-
let coinIndexer = new CoinIndexer();
18+
19+
const coinIndexer = new CoinIndexer();
1920
coinIndexer.start();
2021

21-
// const addresses = await WalletService.getWallets();
22+
// Use WalletService to create or load a hot wallet
23+
let wallet: Wallet | undefined;
24+
let coldWallet: ColdWallet | undefined;
25+
const addresses = await WalletService.getWallets();
26+
27+
// Hot wallet
28+
if (!addresses.map((address) => address.name).includes(testnetWalletAddressName)) {
29+
const created = await WalletService.createWallet(testnetWalletAddressName, testnetMnemonic);
30+
if ('mnemonic' in created) {
31+
wallet = created;
32+
console.log(`Address ${testnetWalletAddressName} added to DB and keyring.`);
33+
}
34+
} else {
35+
const loaded = await WalletService.loadWallet(testnetWalletAddressName);
36+
if (loaded && 'mnemonic' in loaded) {
37+
wallet = loaded;
38+
console.log(`Address ${testnetWalletAddressName} loading existing.`);
39+
}
40+
}
41+
42+
// Cold wallet
43+
const coldWalletName = 'dev-cold'; // Replace with your cold wallet name
44+
if (!addresses.map((address) => address.name).includes(coldWalletName)) {
45+
const created = await WalletService.createColdWallet(coldWalletName, testnetWalletAddress);
46+
if ('address' in created) {
47+
coldWallet = created;
48+
console.log(`Cold wallet ${coldWalletName} added to DB.`);
49+
}
50+
} else {
51+
const loaded = await WalletService.loadWallet(coldWalletName);
52+
if (loaded && 'address' in loaded) {
53+
coldWallet = loaded;
54+
console.log(`Cold wallet ${coldWalletName} loading existing.`);
55+
}
56+
}
57+
58+
// Instantiate ChiaWallet and ChiaColdWallet using the loaded wallet entities and CoinIndexer
59+
const chiaWallet = wallet ? new ChiaWallet(wallet, coinIndexer) : undefined;
60+
const chiaColdWallet = coldWallet ? new ChiaColdWallet(coldWallet, coinIndexer) : undefined;
2261

23-
// let wallet: Wallet;
24-
// if (!addresses.map((address) => address.name).includes(testnetWalletAddressName)) {
25-
// wallet = await WalletService.createWallet(testnetWalletAddressName, testnetMnemonic);
26-
// console.log(`Address ${testnetWalletAddressName} added to DB and keyring.`);
27-
// } else {
28-
// wallet = await WalletService.loadWallet(testnetWalletAddressName);
29-
// console.log(`Address ${testnetWalletAddressName} loading existing.`);
30-
// }
31-
const coldWallet = new ChiaColdWallet(testnetWalletAddress, coinIndexer);
32-
const chiaWallet = new ChiaWallet(testnetMnemonic, coinIndexer);
3362

3463
// Subscribe to ChiaWallet events
35-
chiaWallet.on(ChiaWalletEventNames.CoinCreated, (coin: CoinRecord) => {
36-
console.log(`[ChiaWallet] Coin created: hash ${coin.puzzleHash}, Amount ${coin.amount}`);
37-
});
38-
chiaWallet.on(ChiaWalletEventNames.SpendCreated, (spend: CoinSpend) => {
39-
console.log(`[ChiaWallet] Coin spent: hash ${spend.coin.puzzleHash}, Amount ${spend.coin.amount}`);
40-
});
64+
if (chiaWallet) {
65+
chiaWallet.on(ChiaWalletEventNames.CoinCreated, (coin: CoinRecord) => {
66+
console.log(`[ChiaWallet] Coin created: hash ${coin.puzzleHash}, Amount ${coin.amount}`);
67+
});
68+
chiaWallet.on(ChiaWalletEventNames.SpendCreated, (spend: CoinSpend) => {
69+
console.log(`[ChiaWallet] Coin spent: hash ${spend.coin.puzzleHash}, Amount ${spend.coin.amount}`);
70+
});
71+
}
4172

4273
// Subscribe to ChiaColdWallet events
43-
coldWallet.on(ChiaColdWalletEventNames.CoinCreated, (coin: CoinRecord) => {
44-
console.log(`[ChiaColdWallet] Coin created: hash ${coin.puzzleHash}, Amount ${coin.amount}`);
45-
});
46-
coldWallet.on(ChiaColdWalletEventNames.SpendCreated, (spend: CoinSpend) => {
47-
console.log(`[ChiaColdWallet] Coin spent: hash ${spend.coin.puzzleHash}, Amount ${spend.coin.amount}`);
48-
});
74+
if (chiaColdWallet) {
75+
chiaColdWallet.on(ChiaColdWalletEventNames.CoinCreated, (coin: CoinRecord) => {
76+
console.log(`[ChiaColdWallet] Coin created: hash ${coin.puzzleHash}, Amount ${coin.amount}`);
77+
});
78+
chiaColdWallet.on(ChiaColdWalletEventNames.SpendCreated, (spend: CoinSpend) => {
79+
console.log(`[ChiaColdWallet] Coin spent: hash ${spend.coin.puzzleHash}, Amount ${spend.coin.amount}`);
80+
});
81+
}
4982

50-
// Still log CoinIndexer events for reference
51-
coinIndexer.onCoinCreated((coin: CoinRecord) => {
52-
console.log(`[CoinIndexer] Coin created: hash ${coin.puzzleHash}, Amount ${coin.amount}`);
53-
});
54-
coinIndexer.onSpendCreated((event: CoinSpend) => {
55-
console.log(`[CoinIndexer] Coin spent: hash ${event.coin.puzzleHash}, Amount ${event.coin.amount}`);
56-
});
5783
coinIndexer.onNewBlockIngested((event) => {
5884
console.log(`[CoinIndexer] New block ingested: Height ${event.height}, Weight ${event.weight}, HeaderHash ${event.headerHash.toString('hex')}`);
5985
if (event.timestamp) {

0 commit comments

Comments
 (0)