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