forked from elizaOS/eliza
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbridge.ts
82 lines (73 loc) · 2.84 KB
/
bridge.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
import type { IAgentRuntime, Memory, State } from "@ai16z/eliza";
import { ChainId, executeRoute, getRoutes } from "@lifi/sdk";
import { BaseLifiAction } from "./baseLifi";
import { getChainConfigs, WalletProvider } from "../providers/wallet";
import { bridgeTemplate } from "../templates";
import type { BridgeParams, Transaction } from "../types";
export { bridgeTemplate };
export class BridgeAction extends BaseLifiAction {
async bridge(params: BridgeParams): Promise<Transaction> {
const walletClient = this.walletProvider.getWalletClient();
const [fromAddress] = await walletClient.getAddresses();
const routes = await getRoutes({
fromChainId: getChainConfigs(this.walletProvider.runtime)[
params.fromChain
].chainId as ChainId,
toChainId: getChainConfigs(this.walletProvider.runtime)[
params.toChain
].chainId as ChainId,
fromTokenAddress: params.fromToken,
toTokenAddress: params.toToken,
fromAmount: params.amount,
fromAddress: fromAddress,
toAddress: params.toAddress || fromAddress,
});
if (!routes.routes.length) throw new Error("No routes found");
const execution = await executeRoute(routes.routes[0]);
const process = execution.steps[0]?.execution?.process[0];
if (!process?.status || process.status === "FAILED") {
throw new Error("Transaction failed");
}
return {
hash: process.txHash as `0x${string}`,
from: fromAddress,
to: routes.routes[0].steps[0].estimate
.approvalAddress as `0x${string}`,
value: BigInt(params.amount),
chainId: getChainConfigs(this.walletProvider.runtime)[
params.fromChain
].chainId,
};
}
}
export const bridgeAction = {
name: "bridge",
description: "Bridge tokens between different chains",
handler: async (
runtime: IAgentRuntime,
message: Memory,
state: State,
options: any
) => {
const walletProvider = new WalletProvider(runtime);
const action = new BridgeAction(walletProvider);
return action.bridge(options);
},
template: bridgeTemplate,
validate: async (runtime: IAgentRuntime) => {
const privateKey = runtime.getSetting("EVM_PRIVATE_KEY");
return typeof privateKey === "string" && privateKey.startsWith("0x");
},
examples: [
[
{
user: "user",
content: {
text: "Bridge 1 ETH from Ethereum to Base",
action: "CROSS_CHAIN_TRANSFER",
},
},
],
],
similes: ["CROSS_CHAIN_TRANSFER", "CHAIN_BRIDGE", "MOVE_CROSS_CHAIN"],
}; // TODO: add more examples / similies