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