Add missing documentation to c_types module contents
[ldk-c-bindings] / lightning-c-bindings / demo.c
1 #include "include/rust_types.h"
2 #include "include/lightning.h"
3
4 #include <assert.h>
5 #include <stdio.h>
6 #include <string.h>
7
8 void print_log(const void *this_arg, const char *record) {
9         printf("%s", record);
10 }
11
12 uint32_t get_fee(const void *this_arg, LDKConfirmationTarget target) {
13         if (target == LDKConfirmationTarget_Background) {
14                 return 253;
15         } else {
16                 return 507;
17         }
18 }
19
20 void broadcast_tx(const void *this_arg, LDKTransaction tx) {
21         //TODO
22         Transaction_free(tx);
23 }
24
25 LDKCResult_NoneChannelMonitorUpdateErrZ add_channel_monitor(const void *this_arg, LDKOutPoint funding_txo, LDKChannelMonitor monitor) {
26         return CResult_NoneChannelMonitorUpdateErrZ_ok();
27 }
28 LDKCResult_NoneChannelMonitorUpdateErrZ update_channel_monitor(const void *this_arg, LDKOutPoint funding_txo, LDKChannelMonitorUpdate monitor) {
29         return CResult_NoneChannelMonitorUpdateErrZ_ok();
30 }
31 LDKCVec_MonitorEventZ monitors_pending_monitor_events(const void *this_arg) {
32         LDKCVec_MonitorEventZ empty_htlc_vec = {
33                 .data = NULL,
34                 .datalen = 0,
35         };
36         return empty_htlc_vec;
37 }
38
39 int main() {
40         uint8_t node_seed[32];
41         memset(node_seed, 0, 32);
42
43         LDKNetwork net = LDKNetwork_Bitcoin;
44
45         LDKLogger logger = {
46                 .this_arg = NULL,
47                 .log = print_log,
48                 .free = NULL,
49         };
50
51         LDKFeeEstimator fee_est = {
52                 .this_arg = NULL,
53                 .get_est_sat_per_1000_weight = get_fee,
54                 .free = NULL
55         };
56
57         LDKWatch mon = {
58                 .this_arg = NULL,
59                 .watch_channel = add_channel_monitor,
60                 .update_channel = update_channel_monitor,
61                 .release_pending_monitor_events = monitors_pending_monitor_events,
62                 .free = NULL,
63         };
64
65         LDKBroadcasterInterface broadcast = {
66                 broadcast.this_arg = NULL,
67                 broadcast.broadcast_transaction = broadcast_tx,
68                 .free = NULL,
69         };
70
71         LDKKeysManager keys = KeysManager_new(&node_seed, 0, 0);
72         LDKKeysInterface keys_source = KeysManager_as_KeysInterface(&keys);
73
74         LDKUserConfig config = UserConfig_default();
75         LDKThirtyTwoBytes chain_tip;
76         memset(&chain_tip, 0, 32);
77         LDKChainParameters chain = ChainParameters_new(net, chain_tip, 0);
78         LDKChannelManager cm = ChannelManager_new(fee_est, mon, broadcast, logger, keys_source, config, chain);
79
80         LDKCVec_ChannelDetailsZ channels = ChannelManager_list_channels(&cm);
81         assert((unsigned long)channels.data < 4096); // There's an offset, but it should still be an offset against null in the 0 page
82         assert(channels.datalen == 0);
83         CVec_ChannelDetailsZ_free(channels);
84
85         LDKEventsProvider prov = ChannelManager_as_EventsProvider(&cm);
86         LDKCVec_EventZ events = (prov.get_and_clear_pending_events)(prov.this_arg);
87         assert((unsigned long)events.data < 4096); // There's an offset, but it should still be an offset against null in the 0 page
88         assert(events.datalen == 0);
89
90         ChannelManager_free(cm);
91         KeysManager_free(keys);
92
93         // Check that passing empty vecs to rust doesn't blow it up:
94         LDKCVec_MonitorEventZ empty_htlc_vec = {
95                 .data = NULL,
96                 .datalen = 0,
97         };
98         CVec_MonitorEventZ_free(empty_htlc_vec);
99 }