mirror of
https://github.com/d0zingcat/solana-agent-kit.git
synced 2026-05-21 23:26:45 +00:00
feat: drift perp trade, account creation, withdraw, and deposit
This commit is contained in:
59
src/actions/drift/createDriftUserAccount.ts
Normal file
59
src/actions/drift/createDriftUserAccount.ts
Normal file
@@ -0,0 +1,59 @@
|
||||
import { z } from "zod";
|
||||
import type { Action } from "../../types";
|
||||
import { createDriftUserAccount } from "../../tools";
|
||||
|
||||
const createDriftUserAccountAction: Action = {
|
||||
name: "CREATE_DRIFT_USER_ACCOUNT",
|
||||
similes: [
|
||||
"create drift account",
|
||||
"create drift user account",
|
||||
"create user account on drift",
|
||||
],
|
||||
description: "Create a new user account on Drift protocol",
|
||||
examples: [
|
||||
[
|
||||
{
|
||||
input: {
|
||||
amount: 100,
|
||||
symbol: "SOL",
|
||||
},
|
||||
output: {
|
||||
status: "success",
|
||||
message: "User account created with 100 SOL successfully deposited",
|
||||
account: "4xKpN2...",
|
||||
},
|
||||
explanation: "Create a new user account with 100 SOL",
|
||||
},
|
||||
],
|
||||
],
|
||||
schema: z.object({
|
||||
amount: z.number().positive().describe("Amount of the token to deposit"),
|
||||
symbol: z.string().describe("Symbol of the token to deposit"),
|
||||
}),
|
||||
handler: async (agent, input) => {
|
||||
try {
|
||||
const res = await createDriftUserAccount(
|
||||
agent,
|
||||
input.amount,
|
||||
input.symbol,
|
||||
);
|
||||
|
||||
return {
|
||||
status: "success",
|
||||
message:
|
||||
res.message ??
|
||||
`User account created with ${input.amount} ${input.symobl} successfully deposited.`,
|
||||
account: res.account,
|
||||
signature: res.txSignature,
|
||||
};
|
||||
} catch (e) {
|
||||
return {
|
||||
status: "error",
|
||||
// @ts-expect-error - error message is a string
|
||||
message: `Failed to create user account: ${e.message}`,
|
||||
};
|
||||
}
|
||||
},
|
||||
};
|
||||
|
||||
export default createDriftUserAccountAction;
|
||||
@@ -1,6 +1,5 @@
|
||||
import { z } from "zod";
|
||||
import type { Action } from "../../types";
|
||||
import type { SolanaAgentKit } from "../../agent";
|
||||
import { depositIntoVault } from "../../tools/drift_vault";
|
||||
|
||||
const depositIntoDriftVaultAction: Action = {
|
||||
@@ -31,7 +30,7 @@ const depositIntoDriftVaultAction: Action = {
|
||||
.positive()
|
||||
.describe("The amount in tokens you'd like to deposit into the vault"),
|
||||
}),
|
||||
handler: async (agent: SolanaAgentKit, input) => {
|
||||
handler: async (agent, input) => {
|
||||
try {
|
||||
const tx = await depositIntoVault(
|
||||
agent,
|
||||
|
||||
84
src/actions/drift/depositToDriftUserAccount.ts
Normal file
84
src/actions/drift/depositToDriftUserAccount.ts
Normal file
@@ -0,0 +1,84 @@
|
||||
import { z } from "zod";
|
||||
import type { SolanaAgentKit } from "../../agent";
|
||||
import type { Action } from "../../types";
|
||||
import { depositToDriftUserAccount } from "../../tools";
|
||||
|
||||
const depositToDriftUserAccountAction: Action = {
|
||||
name: "DEPOSIT_TO_DRIFT_USER_ACCOUNT",
|
||||
description: "Deposit funds into your drift user account",
|
||||
similes: [
|
||||
"deposit into drift user account",
|
||||
"add funds to drift user account",
|
||||
"add funds to my drift account",
|
||||
],
|
||||
examples: [
|
||||
[
|
||||
{
|
||||
input: {
|
||||
amount: 100,
|
||||
symbol: "usdc",
|
||||
},
|
||||
output: {
|
||||
status: "success",
|
||||
message: "Funds deposited successfully",
|
||||
signature:
|
||||
"2nFeP7taii3wGVgrWk4YiLMPmhtu3Zg9iXCUu4zGBDadwunHw8reXFxRWT7khbFsQ9JT3zK4RYDLNDFDRYvM3wJk",
|
||||
},
|
||||
explanation: "Deposit 100 USDC into your drift user account",
|
||||
},
|
||||
],
|
||||
[
|
||||
{
|
||||
input: {
|
||||
amount: 100,
|
||||
symbol: "USDC",
|
||||
address: "2nFeP7taii3wGVgrWk4YiLMPmhtu3Zg9iXCUu4zGBD",
|
||||
},
|
||||
output: {
|
||||
status: "success",
|
||||
message: "Funds deposited successfully",
|
||||
signature:
|
||||
"2nFeP7taii3wGVgrWk4YiLMPmhtu3Zg9iXCUu4zGBDadwunHw8reXFxRWT7khbFsQ9JT3zK4RYDLNDFDRYvM3wJk",
|
||||
},
|
||||
explanation: "Deposit 100 USDC into a drift user account",
|
||||
},
|
||||
],
|
||||
],
|
||||
schema: z.object({
|
||||
amount: z
|
||||
.number()
|
||||
.positive()
|
||||
.describe(
|
||||
"The amount in tokens you'd like to deposit into your drift user account",
|
||||
),
|
||||
symbol: z
|
||||
.string()
|
||||
.toUpperCase()
|
||||
.describe("The symbol of the token you'd like to deposit"),
|
||||
address: z.string().optional().describe("The drift user account address"),
|
||||
}),
|
||||
handler: async (agent: SolanaAgentKit, input) => {
|
||||
try {
|
||||
const tx = await depositToDriftUserAccount(
|
||||
agent,
|
||||
input.amount as number,
|
||||
input.symbol as string,
|
||||
input.address,
|
||||
);
|
||||
|
||||
return {
|
||||
status: "success",
|
||||
message: "Funds deposited successfully",
|
||||
signature: tx,
|
||||
};
|
||||
} catch (e) {
|
||||
return {
|
||||
status: "error",
|
||||
// @ts-expect-error - error message
|
||||
message: `Failed to deposit funds: ${e.message}`,
|
||||
};
|
||||
}
|
||||
},
|
||||
};
|
||||
|
||||
export default depositToDriftUserAccountAction;
|
||||
53
src/actions/drift/doesUserHaveDriftAccount.ts
Normal file
53
src/actions/drift/doesUserHaveDriftAccount.ts
Normal file
@@ -0,0 +1,53 @@
|
||||
import { z } from "zod";
|
||||
import { doesUserHaveDriftAccount } from "../../tools";
|
||||
import type { Action } from "../../types";
|
||||
|
||||
export const doesUserHaveDriftAccountAction: Action = {
|
||||
name: "DOES_USER_HAVE_DRIFT_ACCOUNT",
|
||||
description: "Check if a user has a Drift account",
|
||||
similes: [
|
||||
"check if user has drift account",
|
||||
"check if user has account on drift",
|
||||
"do I have an account on drift",
|
||||
],
|
||||
examples: [
|
||||
[
|
||||
{
|
||||
input: {},
|
||||
output: {
|
||||
status: "success",
|
||||
message: "Nice! You have a Drift account",
|
||||
account: "4xKpN2...",
|
||||
},
|
||||
explanation: "Check if a user has a Drift account",
|
||||
},
|
||||
],
|
||||
],
|
||||
schema: z.object({}),
|
||||
handler: async (agent) => {
|
||||
try {
|
||||
const res = await doesUserHaveDriftAccount(agent);
|
||||
|
||||
if (!res.hasAccount) {
|
||||
return {
|
||||
status: "error",
|
||||
message: "You do not have a Drift account",
|
||||
};
|
||||
}
|
||||
|
||||
return {
|
||||
status: "success",
|
||||
message: "Nice! You have a Drift account",
|
||||
account: res.account,
|
||||
};
|
||||
} catch (e) {
|
||||
return {
|
||||
status: "error",
|
||||
// @ts-expect-error - error message is a string
|
||||
message: `Failed to check if you have a Drift account: ${e.message}`,
|
||||
};
|
||||
}
|
||||
},
|
||||
};
|
||||
|
||||
export default doesUserHaveDriftAccountAction;
|
||||
79
src/actions/drift/tradePerpAccount.ts
Normal file
79
src/actions/drift/tradePerpAccount.ts
Normal file
@@ -0,0 +1,79 @@
|
||||
import { z } from "zod";
|
||||
import type { Action } from "../../types";
|
||||
import { driftPerpTrade } from "../../tools";
|
||||
|
||||
export const tradeDriftPerpAccountAction: Action = {
|
||||
name: "TRADE_DRIFT_PERP_ACCOUNT",
|
||||
similes: [
|
||||
"trade drift perp account",
|
||||
"trade drift perp",
|
||||
"trade drift perpetual account",
|
||||
"trade perp account",
|
||||
"trade account",
|
||||
],
|
||||
description: "Trade a perpetual account on Drift protocol",
|
||||
examples: [
|
||||
[
|
||||
{
|
||||
input: {
|
||||
amount: 100,
|
||||
symbol: "SOL",
|
||||
action: "long",
|
||||
type: "market",
|
||||
},
|
||||
output: {
|
||||
status: "success",
|
||||
message: "Trade successful",
|
||||
},
|
||||
explanation: "Open a $100 long position on SOL.",
|
||||
},
|
||||
],
|
||||
[
|
||||
{
|
||||
input: {
|
||||
amount: 50,
|
||||
symbol: "BTC",
|
||||
action: "short",
|
||||
type: "limit",
|
||||
price: 50000,
|
||||
},
|
||||
output: {
|
||||
status: "success",
|
||||
message: "Trade successful",
|
||||
},
|
||||
explanation: "$50 short position on BTC at $50,000.",
|
||||
},
|
||||
],
|
||||
],
|
||||
schema: z.object({
|
||||
amount: z.number().positive(),
|
||||
symbol: z.string().min(3).max(10),
|
||||
action: z.enum(["long", "short"]),
|
||||
type: z.enum(["market", "limit"]),
|
||||
price: z.number().positive().optional(),
|
||||
}),
|
||||
handler: async (agent, input) => {
|
||||
try {
|
||||
const signature = await driftPerpTrade(agent, {
|
||||
action: input.action,
|
||||
amount: input.amount,
|
||||
symbol: input.symbol,
|
||||
type: input.type,
|
||||
price: input.price,
|
||||
});
|
||||
|
||||
return {
|
||||
status: "success",
|
||||
signature: signature,
|
||||
};
|
||||
} catch (e) {
|
||||
return {
|
||||
status: "error",
|
||||
// @ts-expect-error - error message is a string
|
||||
message: `Failed to trade perp account: ${e.message}`,
|
||||
};
|
||||
}
|
||||
},
|
||||
};
|
||||
|
||||
export default tradeDriftPerpAccountAction;
|
||||
65
src/actions/drift/withdrawFromDriftAccount.ts
Normal file
65
src/actions/drift/withdrawFromDriftAccount.ts
Normal file
@@ -0,0 +1,65 @@
|
||||
import { z } from "zod";
|
||||
import type { Action } from "../../types";
|
||||
import { withdrawFromDriftUserAccount } from "../../tools";
|
||||
|
||||
const withdrawFromDriftAccountAction: Action = {
|
||||
name: "WITHDRAW_FROM_DRIFT_ACCOUNT",
|
||||
description: "Withdraw funds from your drift account",
|
||||
similes: [
|
||||
"withdraw from drift account",
|
||||
"withdraw funds from drift account",
|
||||
"withdraw funds from my drift account",
|
||||
],
|
||||
examples: [
|
||||
[
|
||||
{
|
||||
input: {
|
||||
amount: 100,
|
||||
symbol: "usdc",
|
||||
},
|
||||
output: {
|
||||
status: "success",
|
||||
message: "Funds withdrawn successfully",
|
||||
signature:
|
||||
"2nFeP7taii3wGVgrWk4YiLMPmhtu3Zg9iXCUu4zGBDadwunHw8reXFxRWT7khbFsQ9JT3zK4RYDLNDFDRYvM3wJk",
|
||||
},
|
||||
explanation: "Withdraw 100 USDC from your drift account",
|
||||
},
|
||||
],
|
||||
],
|
||||
schema: z.object({
|
||||
amount: z
|
||||
.number()
|
||||
.positive()
|
||||
.describe(
|
||||
"The amount in tokens you'd like to withdraw from your drift account",
|
||||
),
|
||||
symbol: z
|
||||
.string()
|
||||
.toUpperCase()
|
||||
.describe("The symbol of the token you'd like to withdraw"),
|
||||
}),
|
||||
handler: async (agent, input) => {
|
||||
try {
|
||||
const tx = await withdrawFromDriftUserAccount(
|
||||
agent,
|
||||
input.amount,
|
||||
input.symbol,
|
||||
);
|
||||
|
||||
return {
|
||||
status: "success",
|
||||
message: "Funds withdrawn successfully",
|
||||
signature: tx,
|
||||
};
|
||||
} catch (e) {
|
||||
return {
|
||||
status: "error",
|
||||
// @ts-expect-error - error message is a string
|
||||
message: `Failed to withdraw funds: ${e.message}`,
|
||||
};
|
||||
}
|
||||
},
|
||||
};
|
||||
|
||||
export default withdrawFromDriftAccountAction;
|
||||
@@ -37,6 +37,11 @@ import requestWithdrawalFromVaultAction from "./drift/requestWithdrawalFromVault
|
||||
import withdrawFromVaultAction from "./drift/withdrawFromVault";
|
||||
import tradeDelegatedDriftVaultAction from "./drift/tradeDelegatedDriftVault";
|
||||
import vaultInfoAction from "./drift/vaultInfo";
|
||||
import createDriftUserAccountAction from "./drift/createDriftUserAccount";
|
||||
import tradeDriftPerpAccountAction from "./drift/tradePerpAccount";
|
||||
import doesUserHaveDriftAccountAction from "./drift/doesUserHaveDriftAccount";
|
||||
import depositToDriftUserAccountAction from "./drift/depositToDriftUserAccount";
|
||||
import withdrawFromDriftAccountAction from "./drift/withdrawFromDriftAccount";
|
||||
|
||||
export const ACTIONS = {
|
||||
WALLET_ADDRESS_ACTION: getWalletAddressAction,
|
||||
@@ -79,6 +84,11 @@ export const ACTIONS = {
|
||||
WITHDRAW_FROM_DRIFT_VAULT_ACTION: withdrawFromVaultAction,
|
||||
TRADE_DELEGATED_DRIFT_VAULT_ACTION: tradeDelegatedDriftVaultAction,
|
||||
DRIFT_VAULT_INFO_ACTION: vaultInfoAction,
|
||||
CREATE_DRIFT_USER_ACCOUNT_ACTION: createDriftUserAccountAction,
|
||||
TRADE_DRIFT_PERP_ACCOUNT_ACTION: tradeDriftPerpAccountAction,
|
||||
DOES_USER_HAVE_DRIFT_ACCOUNT_ACTION: doesUserHaveDriftAccountAction,
|
||||
DEPOSIT_TO_DRIFT_USER_ACCOUNT_ACTION: depositToDriftUserAccountAction,
|
||||
WITHDRAW_FROM_DRIFT_ACCOUNT_ACTION: withdrawFromDriftAccountAction,
|
||||
};
|
||||
|
||||
export type { Action, ActionExample, Handler } from "../types/action";
|
||||
|
||||
Reference in New Issue
Block a user