|
| 1 | +import type { IAgentRuntime, Memory, State } from "@elizaos/core"; |
| 2 | +import { |
| 3 | + composeContext, |
| 4 | + generateObjectV2, |
| 5 | + ModelClass, |
| 6 | + elizaLogger, |
| 7 | +} from "@elizaos/core"; |
| 8 | + |
| 9 | +import { |
| 10 | + initLightningProvider, |
| 11 | + LightningProvider, |
| 12 | +} from "../providers/lightning"; |
| 13 | +import { PayResult } from "astra-lightning"; |
| 14 | +import { PayArgs } from "../types"; |
| 15 | +import { payInvoiceTemplate } from "../templates"; |
| 16 | + |
| 17 | +export { payInvoiceTemplate }; |
| 18 | + |
| 19 | +type ExtendedPayResult = PayResult & { outgoing_channel: string }; |
| 20 | +export class PayInvoiceAction { |
| 21 | + constructor(private lightningProvider: LightningProvider) { |
| 22 | + this.lightningProvider = lightningProvider; |
| 23 | + } |
| 24 | + |
| 25 | + async getAvalibleChannelId(): Promise<string> { |
| 26 | + const { channels } = await this.lightningProvider.getLndChannel(); |
| 27 | + const filteredActiveChannels = channels.filter( |
| 28 | + (channel) => channel.is_active === true, |
| 29 | + ); |
| 30 | + const sortedChannels = filteredActiveChannels.sort( |
| 31 | + (a, b) => b.local_balance - a.local_balance, |
| 32 | + ); |
| 33 | + if (sortedChannels.length > 0) { |
| 34 | + return sortedChannels[0].id; |
| 35 | + } |
| 36 | + return ""; |
| 37 | + } |
| 38 | + async payInvoice(params: PayArgs): Promise<ExtendedPayResult> { |
| 39 | + const outgoing_channel = await this.getAvalibleChannelId(); |
| 40 | + if (!outgoing_channel) { |
| 41 | + throw new Error("no avalible channel"); |
| 42 | + } |
| 43 | + const requestArgs = { |
| 44 | + outgoing_channel: outgoing_channel, |
| 45 | + ...params, |
| 46 | + }; |
| 47 | + const retPayInvoice = |
| 48 | + await this.lightningProvider.payInvoice(requestArgs); |
| 49 | + return { |
| 50 | + ...retPayInvoice, |
| 51 | + outgoing_channel: outgoing_channel, |
| 52 | + }; |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +export const payInvoiceAction = { |
| 57 | + name: "PAY_INVOICE", |
| 58 | + description: "Make a payment.", |
| 59 | + handler: async ( |
| 60 | + runtime: IAgentRuntime, |
| 61 | + _message: Memory, |
| 62 | + state: State, |
| 63 | + _options: any, |
| 64 | + callback?: any, |
| 65 | + ) => { |
| 66 | + elizaLogger.log("payInvoice action handler called"); |
| 67 | + const lightningProvider = await initLightningProvider(runtime); |
| 68 | + const action = new PayInvoiceAction(lightningProvider); |
| 69 | + |
| 70 | + // Compose bridge context |
| 71 | + const payInvoiceContext = composeContext({ |
| 72 | + state, |
| 73 | + template: payInvoiceTemplate, |
| 74 | + }); |
| 75 | + const content = await generateObjectV2({ |
| 76 | + runtime, |
| 77 | + context: payInvoiceContext, |
| 78 | + modelClass: ModelClass.LARGE, |
| 79 | + }); |
| 80 | + |
| 81 | + const payInvoiceOptions: PayArgs = { |
| 82 | + request: content.request, |
| 83 | + outgoing_channel: content.outgoing_channel, |
| 84 | + }; |
| 85 | + |
| 86 | + try { |
| 87 | + const payInvoiceResp = await action.payInvoice(payInvoiceOptions); |
| 88 | + elizaLogger.log("🚀 ~ payInvoiceResp:", payInvoiceResp); |
| 89 | + |
| 90 | + if (callback) { |
| 91 | + const text = ""; |
| 92 | + if (payInvoiceResp.is_confirmed) { |
| 93 | + callback({ |
| 94 | + text: `Successfully paid invoice ${content.request} from ${payInvoiceResp.outgoing_channel};\nAmount: ${payInvoiceResp.tokens};\nFee: ${payInvoiceResp.fee};\nPayment Hash: ${payInvoiceResp.id};`, |
| 95 | + content: { success: true }, |
| 96 | + }); |
| 97 | + } else { |
| 98 | + callback({ |
| 99 | + text: `Failed to payInvoice ${content.request} from ${content.outgoing_channel};\r\n Amount: ${payInvoiceResp.tokens};`, |
| 100 | + content: { |
| 101 | + success: false, |
| 102 | + }, |
| 103 | + }); |
| 104 | + } |
| 105 | + } |
| 106 | + return true; |
| 107 | + } catch (error) { |
| 108 | + elizaLogger.error("Error in payInvoice handler:", error); |
| 109 | + if (callback) { |
| 110 | + callback({ |
| 111 | + text: `Error: ${error.message || "An error occurred"}`, |
| 112 | + }); |
| 113 | + } |
| 114 | + return false; |
| 115 | + } |
| 116 | + }, |
| 117 | + template: payInvoiceTemplate, |
| 118 | + validate: async (runtime: IAgentRuntime) => { |
| 119 | + const cert = runtime.getSetting("LND_TLS_CERT"); |
| 120 | + const macaroon = runtime.getSetting("LND_MACAROON"); |
| 121 | + const socket = runtime.getSetting("LND_SOCKET"); |
| 122 | + return !!cert && !!macaroon && !!socket; |
| 123 | + }, |
| 124 | + examples: [ |
| 125 | + [ |
| 126 | + { |
| 127 | + user: "user", |
| 128 | + content: { |
| 129 | + text: "Pay invoice for lnbrc...", |
| 130 | + action: "PAY_INVOICE", |
| 131 | + }, |
| 132 | + }, |
| 133 | + ], |
| 134 | + ], |
| 135 | + similes: ["PAY_INVOICE", "MAKE_PAYMENT"], |
| 136 | +}; |
0 commit comments