Update CI/Cargo.toml references to 0.0.122
[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 LDKRecord record) {
8         LDKStr mod = Record_get_module_path(&record);
9         LDKStr str = Record_get_args(&record);
10         printf("%.*s:%d - %.*s\n", (int)mod.len, mod.chars, Record_get_line(&record), (int)str.len, str.chars);
11         Str_free(str);
12         Str_free(mod);
13         Record_free(record);
14 }
15
16 uint32_t get_fee(const void *this_arg, LDKConfirmationTarget target) {
17         if (target == LDKConfirmationTarget_AnchorChannelFee || target == LDKConfirmationTarget_MinAllowedAnchorChannelRemoteFee) {
18                 return 253;
19         } else {
20                 return 507;
21         }
22 }
23
24 void broadcast_txn(const void *this_arg, LDKCVec_TransactionZ txn) {
25         //TODO
26         CVec_TransactionZ_free(txn);
27 }
28
29 LDKCResult_ChannelMonitorUpdateStatusNoneZ add_channel_monitor(const void *this_arg, LDKOutPoint funding_txo, LDKChannelMonitor monitor) {
30         return CResult_ChannelMonitorUpdateStatusNoneZ_ok(ChannelMonitorUpdateStatus_completed());
31 }
32 LDKChannelMonitorUpdateStatus update_channel_monitor(const void *this_arg, LDKOutPoint funding_txo, const LDKChannelMonitorUpdate *monitor) {
33         return ChannelMonitorUpdateStatus_completed();
34 }
35 LDKCVec_C3Tuple_OutPointCVec_MonitorEventZPublicKeyZZ monitors_pending_monitor_events(const void *this_arg) {
36         LDKCVec_C3Tuple_OutPointCVec_MonitorEventZPublicKeyZZ empty_htlc_vec = {
37                 .data = NULL,
38                 .datalen = 0,
39         };
40         return empty_htlc_vec;
41 }
42
43 void never_handle_event(const void *this_arg, const struct LDKEvent event) {
44         // Note that we never actually generate any events to handle in the code below.
45         assert(false);
46 }
47
48 LDKCResult_RouteLightningErrorZ do_find_route(const void *this_arg, LDKPublicKey payer, const LDKRouteParameters *route_params, LDKCVec_ChannelDetailsZ *first_hops, const LDKInFlightHtlcs inflight_htlcs) {
49         LDKStr reason = { .chars = (const unsigned char*)"", .len = 0, .chars_is_owned = false };
50         return CResult_RouteLightningErrorZ_err(LightningError_new(reason, ErrorAction_ignore_error()));
51 }
52
53 LDKCResult_RouteLightningErrorZ do_find_route_with_id(const void *this_arg, LDKPublicKey payer, const LDKRouteParameters *route_params, LDKCVec_ChannelDetailsZ *first_hops, const LDKInFlightHtlcs inflight_htlcs, struct LDKThirtyTwoBytes payment_hash, struct LDKThirtyTwoBytes payment_id) {
54         LDKStr reason = { .chars = (const unsigned char*)"", .len = 0, .chars_is_owned = false };
55         return CResult_RouteLightningErrorZ_err(LightningError_new(reason, ErrorAction_ignore_error()));
56 }
57
58 int main() {
59         uint8_t node_seed[32];
60         memset(node_seed, 0, 32);
61
62         LDKNetwork net = LDKNetwork_Bitcoin;
63
64         LDKLogger logger = {
65                 .this_arg = NULL,
66                 .log = print_log,
67                 .free = NULL,
68         };
69
70         LDKFeeEstimator fee_est = {
71                 .this_arg = NULL,
72                 .get_est_sat_per_1000_weight = get_fee,
73                 .free = NULL
74         };
75
76         LDKWatch mon = {
77                 .this_arg = NULL,
78                 .watch_channel = add_channel_monitor,
79                 .update_channel = update_channel_monitor,
80                 .release_pending_monitor_events = monitors_pending_monitor_events,
81                 .free = NULL,
82         };
83
84         LDKBroadcasterInterface broadcast = {
85                 .this_arg = NULL,
86                 .broadcast_transactions = broadcast_txn,
87                 .free = NULL,
88         };
89
90         LDKRouter router = {
91                 .this_arg = NULL,
92                 .find_route = do_find_route,
93                 .find_route_with_id = do_find_route_with_id,
94                 .free = NULL,
95         };
96
97         LDKKeysManager keys = KeysManager_new(&node_seed, 0, 0);
98         LDKEntropySource entropy_source = KeysManager_as_EntropySource(&keys);
99         LDKNodeSigner node_signer = KeysManager_as_NodeSigner(&keys);
100         LDKSignerProvider signer_provider = KeysManager_as_SignerProvider(&keys);
101
102         LDKUserConfig config = UserConfig_default();
103         LDKThirtyTwoBytes chain_tip;
104         memset(&chain_tip, 0, 32);
105         LDKChainParameters chain = ChainParameters_new(net, BestBlock_new(chain_tip, 0));
106         LDKChannelManager cm = ChannelManager_new(fee_est, mon, broadcast, router, logger, entropy_source, node_signer, signer_provider, config, chain, 42);
107
108         LDKCVec_ChannelDetailsZ channels = ChannelManager_list_channels(&cm);
109         assert((unsigned long)channels.data < 4096); // There's an offset, but it should still be an offset against null in the 0 page
110         assert(channels.datalen == 0);
111         CVec_ChannelDetailsZ_free(channels);
112
113         LDKEventsProvider prov = ChannelManager_as_EventsProvider(&cm);
114         // Check that no events were generated by asserting if any events are passed to never_handle_event.
115         LDKEventHandler handler = { .handle_event = never_handle_event, .free = NULL };
116         (prov.process_pending_events)(prov.this_arg, handler);
117
118         ChannelManager_free(cm);
119         KeysManager_free(keys);
120
121         // Check that passing empty vecs to rust doesn't blow it up:
122         LDKCVec_MonitorEventZ empty_htlc_vec = {
123                 .data = NULL,
124                 .datalen = 0,
125         };
126         CVec_MonitorEventZ_free(empty_htlc_vec);
127 }