Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: plugin-lightning #2959

Merged
merged 3 commits into from
Jan 29, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/plugin-lightning/src/actions/createInvoice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export const createInvoiceAction = {
runtime: IAgentRuntime,
_message: Memory,
state: State,
_options: any,
_options: Record<string, unknown>,
callback?: (response: {
text: string;
content?: { success: boolean; invoice?: string };
Expand Down
28 changes: 21 additions & 7 deletions packages/plugin-lightning/src/actions/payInvoice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
import type { PayResult } from "astra-lightning";
import type { PayArgs } from "../types";
import { payInvoiceTemplate } from "../templates";
import { z } from "zod";

export { payInvoiceTemplate };

Expand Down Expand Up @@ -54,15 +55,26 @@ export class PayInvoiceAction {
}
}

// Define the schema type
const payInvoiceSchema = z.object({
request: z.string(),
outgoing_channel: z.string()
});

type PayInvoiceContent = z.infer<typeof payInvoiceSchema>;

export const payInvoiceAction = {
name: "PAY_INVOICE",
description: "Make a payment.",
handler: async (
runtime: IAgentRuntime,
_message: Memory,
state: State,
_options: any,
callback?: any
_options: Record<string, unknown>,
callback?: (response: {
text: string;
content?: { success: boolean };
}) => void
) => {
elizaLogger.log("payInvoice action handler called");
const lightningProvider = await initLightningProvider(runtime);
Expand All @@ -76,28 +88,30 @@ export const payInvoiceAction = {
const content = await generateObject({
runtime,
context: payInvoiceContext,
schema: payInvoiceSchema as z.ZodType,
modelClass: ModelClass.LARGE,
});

const payInvoiceContent = content.object as PayInvoiceContent;

const payInvoiceOptions: PayArgs = {
request: content.request,
outgoing_channel: content.outgoing_channel,
request: payInvoiceContent.request,
outgoing_channel: payInvoiceContent.outgoing_channel,
};

try {
const payInvoiceResp = await action.payInvoice(payInvoiceOptions);
elizaLogger.log("🚀 ~ payInvoiceResp:", payInvoiceResp);

if (callback) {
const text = "";
if (payInvoiceResp.is_confirmed) {
callback({
text: `Successfully paid invoice ${content.request} from ${payInvoiceResp.outgoing_channel};\nAmount: ${payInvoiceResp.tokens};\nFee: ${payInvoiceResp.fee};\nPayment Hash: ${payInvoiceResp.id};`,
text: `Successfully paid invoice ${payInvoiceContent.request} from ${payInvoiceResp.outgoing_channel};\nAmount: ${payInvoiceResp.tokens};\nFee: ${payInvoiceResp.fee};\nPayment Hash: ${payInvoiceResp.id};`,
content: { success: true },
});
} else {
callback({
text: `Failed to payInvoice ${content.request} from ${content.outgoing_channel};\r\n Amount: ${payInvoiceResp.tokens};`,
text: `Failed to payInvoice ${payInvoiceContent.request} from ${payInvoiceContent.outgoing_channel};\r\n Amount: ${payInvoiceResp.tokens};`,
content: {
success: false,
},
Expand Down
Loading