|
| 1 | +/// MintLightning interface for backend |
| 2 | +const Self = @This(); |
| 3 | + |
| 4 | +const std = @import("std"); |
| 5 | +const core = @import("../lib.zig"); |
| 6 | + |
| 7 | +const Channel = @import("../../channels/channels.zig").Channel; |
| 8 | +const Amount = core.amount.Amount; |
| 9 | +const PaymentQuoteResponse = core.lightning.PaymentQuoteResponse; |
| 10 | +const CreateInvoiceResponse = core.lightning.CreateInvoiceResponse; |
| 11 | +const PayInvoiceResponse = core.lightning.PayInvoiceResponse; |
| 12 | +const MeltQuoteBolt11Request = core.nuts.nut05.MeltQuoteBolt11Request; |
| 13 | +const Settings = core.lightning.Settings; |
| 14 | +const MintMeltSettings = core.lightning.MintMeltSettings; |
| 15 | +const FeeReserve = core.mint.FeeReserve; |
| 16 | +const MintQuoteState = core.nuts.nut04.QuoteState; |
| 17 | + |
| 18 | +// _type: type, |
| 19 | +allocator: std.mem.Allocator, |
| 20 | +ptr: *anyopaque, |
| 21 | + |
| 22 | +deinitFn: *const fn (ptr: *anyopaque) void, |
| 23 | +getSettingsFn: *const fn (ptr: *anyopaque) Settings, |
| 24 | +waitAnyInvoiceFn: *const fn (ptr: *anyopaque) anyerror!Channel(std.ArrayList(u8)).Rx, |
| 25 | +getPaymentQuoteFn: *const fn (ptr: *anyopaque, alloc: std.mem.Allocator, melt_quote_request: MeltQuoteBolt11Request) anyerror!PaymentQuoteResponse, |
| 26 | +payInvoiceFn: *const fn (ptr: *anyopaque, alloc: std.mem.Allocator, melt_quote: core.mint.MeltQuote, partial_msats: ?Amount, max_fee_msats: ?Amount) anyerror!PayInvoiceResponse, |
| 27 | +checkInvoiceStatusFn: *const fn (ptr: *anyopaque, request_lookup_id: []const u8) anyerror!MintQuoteState, |
| 28 | +createInvoiceFn: *const fn (ptr: *anyopaque, gpa: std.mem.Allocator, amount: Amount, unit: core.nuts.CurrencyUnit, description: []const u8, unix_expiry: u64) anyerror!CreateInvoiceResponse, |
| 29 | + |
| 30 | +pub fn initFrom(comptime T: type, allocator: std.mem.Allocator, value: T) !Self { |
| 31 | + const gen = struct { |
| 32 | + pub fn getSettings(pointer: *anyopaque) Settings { |
| 33 | + const self: *T = @ptrCast(@alignCast(pointer)); |
| 34 | + return self.getSettings(); |
| 35 | + } |
| 36 | + |
| 37 | + pub fn waitAnyInvoice(pointer: *anyopaque) anyerror!Channel(std.ArrayList(u8)).Rx { |
| 38 | + const self: *T = @ptrCast(@alignCast(pointer)); |
| 39 | + return self.waitAnyInvoice(); |
| 40 | + } |
| 41 | + |
| 42 | + pub fn getPaymentQuote(pointer: *anyopaque, arena: std.mem.Allocator, melt_quote_request: MeltQuoteBolt11Request) anyerror!PaymentQuoteResponse { |
| 43 | + const self: *T = @ptrCast(@alignCast(pointer)); |
| 44 | + return self.getPaymentQuote(arena, melt_quote_request); |
| 45 | + } |
| 46 | + |
| 47 | + pub fn payInvoice(pointer: *anyopaque, arena: std.mem.Allocator, melt_quote: core.mint.MeltQuote, partial_msats: ?Amount, max_fee_msats: ?Amount) !PayInvoiceResponse { |
| 48 | + const self: *T = @ptrCast(@alignCast(pointer)); |
| 49 | + return self.payInvoice(arena, melt_quote, partial_msats, max_fee_msats); |
| 50 | + } |
| 51 | + |
| 52 | + pub fn checkInvoiceStatus(pointer: *anyopaque, request_lookup_id: []const u8) !MintQuoteState { |
| 53 | + const self: *T = @ptrCast(@alignCast(pointer)); |
| 54 | + return self.checkInvoiceStatus(request_lookup_id); |
| 55 | + } |
| 56 | + |
| 57 | + pub fn createInvoice(pointer: *anyopaque, arena: std.mem.Allocator, amount: Amount, unit: core.nuts.CurrencyUnit, description: []const u8, unix_expiry: u64) !CreateInvoiceResponse { |
| 58 | + const self: *T = @ptrCast(@alignCast(pointer)); |
| 59 | + return self.createInvoice(arena, amount, unit, description, unix_expiry); |
| 60 | + } |
| 61 | + |
| 62 | + pub fn deinit(pointer: *anyopaque) void { |
| 63 | + if (std.meta.hasFn(T, "deinit")) { |
| 64 | + const self: *T = @ptrCast(@alignCast(pointer)); |
| 65 | + self.deinit(); |
| 66 | + } |
| 67 | + } |
| 68 | + }; |
| 69 | + |
| 70 | + const ptr = try allocator.create(T); |
| 71 | + ptr.* = value; |
| 72 | + |
| 73 | + return .{ |
| 74 | + // ._type = T, |
| 75 | + .allocator = allocator, |
| 76 | + .ptr = ptr, |
| 77 | + .getSettingsFn = gen.getSettings, |
| 78 | + .waitAnyInvoiceFn = gen.waitAnyInvoice, |
| 79 | + .getPaymentQuoteFn = gen.getPaymentQuote, |
| 80 | + .payInvoiceFn = gen.payInvoice, |
| 81 | + .checkInvoiceStatusFn = gen.checkInvoiceStatus, |
| 82 | + .createInvoiceFn = gen.createInvoice, |
| 83 | + .deinitFn = gen.deinit, |
| 84 | + }; |
| 85 | +} |
| 86 | + |
| 87 | +pub fn deinit(self: Self) void { |
| 88 | + self.deinitFn(self.ptr); |
| 89 | + // clearing pointer |
| 90 | + // self.allocator.destroy(@as(self._type, @ptrCast(self.ptr))); |
| 91 | +} |
| 92 | + |
| 93 | +pub fn getSettings(self: Self) Settings { |
| 94 | + return self.getSettingsFn(self.ptr); |
| 95 | +} |
| 96 | + |
| 97 | +pub fn waitAnyInvoice(self: Self) !Channel(std.ArrayList(u8)).Rx { |
| 98 | + return self.waitAnyInvoiceFn(self.ptr); |
| 99 | +} |
| 100 | + |
| 101 | +pub fn getPaymentQuote(self: Self, arena: std.mem.Allocator, melt_quote_request: MeltQuoteBolt11Request) !PaymentQuoteResponse { |
| 102 | + return self.getPaymentQuoteFn(self.ptr, arena, melt_quote_request); |
| 103 | +} |
| 104 | + |
| 105 | +pub fn payInvoice(self: Self, arena: std.mem.Allocator, melt_quote: core.mint.MeltQuote, partial_msats: ?Amount, max_fee_msats: ?Amount) !PayInvoiceResponse { |
| 106 | + return self.payInvoiceFn(self.ptr, arena, melt_quote, partial_msats, max_fee_msats); |
| 107 | +} |
| 108 | + |
| 109 | +pub fn checkInvoiceStatus(self: Self, request_lookup_id: []const u8) !MintQuoteState { |
| 110 | + return self.checkInvoiceStatusFn(self.ptr, request_lookup_id); |
| 111 | +} |
| 112 | + |
| 113 | +pub fn createInvoice(self: Self, arena: std.mem.Allocator, amount: Amount, unit: core.nuts.CurrencyUnit, description: []const u8, unix_expiry: u64) !CreateInvoiceResponse { |
| 114 | + return self.createInvoiceFn(self.ptr, arena, amount, unit, description, unix_expiry); |
| 115 | +} |
0 commit comments