Skip to content

Commit fc0dc5c

Browse files
committed
modify b2 plugin
1 parent 065160d commit fc0dc5c

18 files changed

+837
-48
lines changed
+150
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
import {
2+
Action,
3+
ActionExample,
4+
IAgentRuntime,
5+
generateObjectDeprecated,
6+
Memory,
7+
State,
8+
HandlerCallback,
9+
elizaLogger,
10+
composeContext,
11+
ModelClass,
12+
} from "@elizaos/core";
13+
import { getTxReceipt, sendNativeAsset, sendToken, depositBTC } from "../utils";
14+
import { Address, Hash } from "viem";
15+
import { validateB2NetworkConfig } from "../environment";
16+
import { stakeTemplate } from "../templates";
17+
import { WalletProvider } from "../providers";
18+
import { StakeParams } from "../types";
19+
import { initWalletProvider } from "../providers";
20+
import { FARM_ADDRESS } from "../utils/constants";
21+
22+
// Exported for tests
23+
export class StakeAction {
24+
25+
constructor(private walletProvider: WalletProvider) {}
26+
27+
async stake(params: StakeParams): Promise<Hash> {
28+
try {
29+
let balance = await this.walletProvider.getNativeBalance(this.walletProvider.getAddress());
30+
if ( balance == 0 ) {
31+
throw new Error(`The total cost (gas * gas fee + value) of executing this transaction exceeds the balance of the account.`);
32+
}
33+
let txHash = await depositBTC(
34+
this.walletProvider,
35+
FARM_ADDRESS,
36+
params.amount,
37+
);
38+
return txHash;
39+
} catch(error) {
40+
console.log(`Stake failed: ${error.message}`);
41+
throw new Error(`Stake failed: ${error.message}`);
42+
// throw new Error(`Stake failed: The total cost (gas * gas fee + value) of executing this transaction exceeds the balance of the account.`)
43+
}
44+
}
45+
46+
async txReceipt(tx: Hash) {
47+
const receipt = await getTxReceipt(this.walletProvider, tx);
48+
if (receipt.status === "success") {
49+
return true;
50+
} else {
51+
return false;
52+
}
53+
}
54+
55+
async buildStakeDetails(
56+
state: State,
57+
runtime: IAgentRuntime,
58+
wp: WalletProvider
59+
): Promise<StakeParams> {
60+
const context = composeContext({
61+
state,
62+
template: stakeTemplate,
63+
});
64+
65+
const stakeDetails = (await generateObjectDeprecated({
66+
runtime,
67+
context,
68+
modelClass: ModelClass.SMALL,
69+
})) as StakeParams;
70+
71+
return stakeDetails;
72+
}
73+
}
74+
75+
export const stakeAction: Action = {
76+
name: "STAKE",
77+
similes: [
78+
"STAKE_BTC_ON_B2",
79+
"STAKE_NATIVE_BTC_ON_B2",
80+
"DEPOSIT_BTC_ON_B2",
81+
"DEPOSIT_NATIVE_BTC_ON_B2",
82+
],
83+
validate: async (runtime: IAgentRuntime, _message: Memory) => {
84+
await validateB2NetworkConfig(runtime);
85+
return true;
86+
},
87+
description:
88+
"stake native btc.",
89+
handler: async (
90+
runtime: IAgentRuntime,
91+
message: Memory,
92+
state: State,
93+
_options: { [key: string]: unknown },
94+
callback?: HandlerCallback
95+
) => {
96+
elizaLogger.log("Starting STAKE handler...");
97+
98+
// Initialize or update state
99+
if (!state) {
100+
state = (await runtime.composeState(message)) as State;
101+
} else {
102+
state = await runtime.updateRecentMessageState(state);
103+
}
104+
105+
console.log("stake action handler called");
106+
const walletProvider = await initWalletProvider(runtime);
107+
const action = new StakeAction(walletProvider);
108+
109+
// Compose stake context
110+
const paramOptions = await action.buildStakeDetails(
111+
state,
112+
runtime,
113+
walletProvider
114+
);
115+
116+
elizaLogger.debug("Stake paramOptions:", paramOptions);
117+
118+
let txHash = await action.stake(paramOptions);
119+
if (txHash) {
120+
let result = await action.txReceipt(txHash);
121+
if (result) {
122+
callback?.({
123+
text: "stake successful",
124+
content: { success: true, txHash: txHash },
125+
});
126+
} else {
127+
callback?.({
128+
text: "stake failed",
129+
content: { error: "Stake failed" },
130+
});
131+
}
132+
} else {
133+
callback?.({
134+
text: "stake failed",
135+
content: { error: "Stake failed" },
136+
});
137+
}
138+
return true;
139+
},
140+
examples: [
141+
[
142+
{
143+
user: "{{user1}}",
144+
content: {
145+
text: "Stake 1 B2-BTC",
146+
},
147+
},
148+
],
149+
] as ActionExample[][],
150+
};

packages/plugin-b2/src/actions/transfer.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ export class TransferAction {
5252
}
5353
}
5454

55-
async transferTxReceipt(tx: Hash) {
55+
async txReceipt(tx: Hash) {
5656
const receipt = await getTxReceipt(this.walletProvider, tx);
5757
if (receipt.status === "success") {
5858
return true;
@@ -87,7 +87,7 @@ export const transferAction: Action = {
8787
"TRANSFER_TOKEN_ON_B2",
8888
"TRANSFER_TOKENS_ON_B2",
8989
"SEND_TOKENS_ON_B2",
90-
"SEND_AVAX_ON_B2",
90+
"SEND_B2BTC_ON_B2",
9191
"PAY_ON_B2",
9292
],
9393
validate: async (runtime: IAgentRuntime, _message: Memory) => {
@@ -127,7 +127,7 @@ export const transferAction: Action = {
127127

128128
let tx = await action.transfer(paramOptions);
129129
if (tx) {
130-
let result = await action.transferTxReceipt(tx.hash);
130+
let result = await action.txReceipt(tx.hash);
131131
if (result) {
132132
callback?.({
133133
text: "transfer successful",
+150
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
import {
2+
Action,
3+
ActionExample,
4+
IAgentRuntime,
5+
generateObjectDeprecated,
6+
Memory,
7+
State,
8+
HandlerCallback,
9+
elizaLogger,
10+
composeContext,
11+
ModelClass,
12+
} from "@elizaos/core";
13+
import { getTxReceipt, unstake } from "../utils";
14+
import { Hash } from "viem";
15+
import { validateB2NetworkConfig } from "../environment";
16+
import { unstakeTemplate } from "../templates";
17+
import { WalletProvider } from "../providers";
18+
import { UnstakeParams } from "../types";
19+
import { initWalletProvider } from "../providers";
20+
import { FARM_ADDRESS } from "../utils/constants";
21+
22+
// Exported for tests
23+
export class UnstakeAction {
24+
25+
constructor(private walletProvider: WalletProvider) {}
26+
27+
async unstake(params: UnstakeParams): Promise<Hash> {
28+
try {
29+
let balance = await this.walletProvider.getNativeBalance(this.walletProvider.getAddress());
30+
if ( balance == 0 ) {
31+
throw new Error(`The total cost (gas * gas fee + value) of executing this transaction exceeds the balance of the account.`);
32+
}
33+
let txHash = await unstake(
34+
this.walletProvider,
35+
FARM_ADDRESS,
36+
params.amount,
37+
);
38+
return txHash;
39+
} catch(error) {
40+
console.log(`Unstake failed: ${error.message}`);
41+
throw new Error(`Unstake failed: ${error.message}`);
42+
// throw new Error(`Unstake failed: The total cost (gas * gas fee + value) of executing this transaction exceeds the balance of the account.`)
43+
}
44+
}
45+
46+
async txReceipt(tx: Hash) {
47+
const receipt = await getTxReceipt(this.walletProvider, tx);
48+
if (receipt.status === "success") {
49+
return true;
50+
} else {
51+
return false;
52+
}
53+
}
54+
55+
async buildUnstakeDetails(
56+
state: State,
57+
runtime: IAgentRuntime,
58+
wp: WalletProvider
59+
): Promise<UnstakeParams> {
60+
const context = composeContext({
61+
state,
62+
template: unstakeTemplate,
63+
});
64+
65+
const unstakeDetails = (await generateObjectDeprecated({
66+
runtime,
67+
context,
68+
modelClass: ModelClass.SMALL,
69+
})) as UnstakeParams;
70+
71+
return unstakeDetails;
72+
}
73+
}
74+
75+
export const unstakeAction: Action = {
76+
name: "UNSTAKE",
77+
similes: [
78+
"UNSTAKE_BTC_ON_B2",
79+
"UNSTAKE_NATIVE_BTC_ON_B2",
80+
"UNSTAKE_BTC_ON_B2",
81+
"UNSTAKE_NATIVE_BTC_ON_B2",
82+
],
83+
validate: async (runtime: IAgentRuntime, _message: Memory) => {
84+
await validateB2NetworkConfig(runtime);
85+
return true;
86+
},
87+
description:
88+
"unstake native btc.",
89+
handler: async (
90+
runtime: IAgentRuntime,
91+
message: Memory,
92+
state: State,
93+
_options: { [key: string]: unknown },
94+
callback?: HandlerCallback
95+
) => {
96+
elizaLogger.log("Starting UNSTAKE handler...");
97+
98+
// Initialize or update state
99+
if (!state) {
100+
state = (await runtime.composeState(message)) as State;
101+
} else {
102+
state = await runtime.updateRecentMessageState(state);
103+
}
104+
105+
console.log("unstake action handler called");
106+
const walletProvider = await initWalletProvider(runtime);
107+
const action = new UnstakeAction(walletProvider);
108+
109+
// Compose unstake context
110+
const paramOptions = await action.buildUnstakeDetails(
111+
state,
112+
runtime,
113+
walletProvider
114+
);
115+
116+
elizaLogger.debug("Unstake paramOptions:", paramOptions);
117+
118+
let txHash = await action.unstake(paramOptions);
119+
if (txHash) {
120+
let result = await action.txReceipt(txHash);
121+
if (result) {
122+
callback?.({
123+
text: "unstake successful",
124+
content: { success: true, txHash: txHash },
125+
});
126+
} else {
127+
callback?.({
128+
text: "unstake failed",
129+
content: { error: "Unstake failed" },
130+
});
131+
}
132+
} else {
133+
callback?.({
134+
text: "unstake failed",
135+
content: { error: "Unstake failed" },
136+
});
137+
}
138+
return true;
139+
},
140+
examples: [
141+
[
142+
{
143+
user: "{{user1}}",
144+
content: {
145+
text: "Unstake 1 B2-BTC",
146+
},
147+
},
148+
],
149+
] as ActionExample[][],
150+
};

0 commit comments

Comments
 (0)