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