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

feat: Voting Operations | SPL Governance Integration #189

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
"@orca-so/whirlpools-sdk": "^0.13.12",
"@pythnetwork/hermes-client": "^1.3.0",
"@raydium-io/raydium-sdk-v2": "0.1.95-alpha",
"@solana/spl-governance": "^0.3.28",
"@solana/spl-token": "^0.4.9",
"@solana/web3.js": "^1.98.0",
"@sqds/multisig": "^2.1.3",
Expand Down
38 changes: 38 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions src/actions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ import launchPumpfunTokenAction from "./pumpfun/launchPumpfunToken";
import getWalletAddressAction from "./agent/getWalletAddress";
import flashOpenTradeAction from "./flash/flashOpenTrade";
import flashCloseTradeAction from "./flash/flashCloseTrade";
import manageVoteDelegationAction from "./realm/manageVotingDelegation";
import monitorVotingOutcomesAction from "./realm/manageVotingOutcomes";
import trackVotingPowerAction from "./realm/trackVotingPower";
import castGovernanceVoteAction from "./realm/castGovernanceVoteAction";

export const ACTIONS = {
WALLET_ADDRESS_ACTION: getWalletAddressAction,
Expand Down Expand Up @@ -65,6 +69,10 @@ export const ACTIONS = {
LAUNCH_PUMPFUN_TOKEN_ACTION: launchPumpfunTokenAction,
FLASH_OPEN_TRADE_ACTION: flashOpenTradeAction,
FLASH_CLOSE_TRADE_ACTION: flashCloseTradeAction,
MANAGE_VOTING_DELEGATION: manageVoteDelegationAction,
MANAGE_VOTING_OUTCOMES: monitorVotingOutcomesAction,
TRACK_VOTING_POWER: trackVotingPowerAction,
CAST_VOTE_ACTION: castGovernanceVoteAction,
};

export type { Action, ActionExample, Handler } from "../types/action";
71 changes: 71 additions & 0 deletions src/actions/realm/castGovernanceVoteAction.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import { PublicKey } from "@solana/web3.js";
import { Action } from "../../types/action";
import { z } from "zod";
import { SolanaAgentKit } from "../../agent";
import { castProposalVote } from "../../tools/realm";

const castGovernanceVoteAction: Action = {
name: "CAST_GOVERNANCE_VOTE",
similes: [
"vote on proposal",
"cast governance vote",
"submit proposal vote",
"vote on governance",
"vote on dao proposal",
],
description: `Cast a vote on a governance proposal in a Solana DAO.
Inputs ( input is a JSON string ):
realmAccount: string, eg "7nxQB..." (required) - The public key of the realm
proposalAccount: string, eg "8x2dR..." (required) - The public key of the proposal
voteType: string, either "yes" or "no" (required) - The type of vote to cast`,

examples: [
[
{
input: {
realmAccount: "7nxQB1nGrqk8WKXeFDR6ZUaQtYjV7HMsAGWgwtGHwmQU",
proposalAccount: "8x2dR8Mpzuz2YqyZyZjUbYWKSWesBo5jMx2Q9Y86udVk",
voteType: "yes",
},
output: {
status: "success",
signature: "2GjfL3N9E4cHp7WhDZRkx7oF2J9m3Sf5hT6zRHcVWUjp",
message: "Vote cast successfully",
},
explanation: "Cast a yes vote on a governance proposal",
},
],
],

schema: z.object({
realmAccount: z.string().min(32, "Invalid realm account address"),
proposalAccount: z.string().min(32, "Invalid proposal account address"),
voteType: z.enum(["yes", "no"], {
description: "Vote type must be either 'yes' or 'no'",
}),
}),

handler: async (agent: SolanaAgentKit, input: Record<string, any>) => {
try {
const signature = await castProposalVote(
agent,
new PublicKey(input.realmAccount),
new PublicKey(input.proposalAccount),
input.voteType,
);

return {
status: "success",
signature,
message: "Vote cast successfully",
};
} catch (error: any) {
return {
status: "error",
message: `Failed to cast vote: ${error.message}`,
};
}
},
};

export default castGovernanceVoteAction;
Empty file added src/actions/realm/index.ts
Empty file.
81 changes: 81 additions & 0 deletions src/actions/realm/manageVotingDelegation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import { PublicKey } from "@solana/web3.js";
import { Action } from "../../types/action";
import { z } from "zod";
import { manageVoteDelegation } from "../../tools/realm/manage_vote_delegation";
import { SolanaAgentKit } from "../../agent";

const manageVoteDelegationAction: Action = {
name: "MANAGE_VOTE_DELEGATION",
similes: [
"delegate governance votes",
"assign voting delegate",
"transfer voting rights",
"set voting delegate",
"delegate dao voting power",
],
description: `Delegate voting power to another wallet in a governance realm.

Inputs ( input is a JSON string ):
realm: string, eg "7nxQB..." (required) - The public key of the realm
governingTokenMint: string, eg "8x2dR..." (required) - The mint of the governing token
delegate: string, eg "9aUn5..." (required) - The wallet address to delegate voting power to`,

examples: [
[
{
input: {
realm: "7nxQB1nGrqk8WKXeFDR6ZUaQtYjV7HMsAGWgwtGHwmQU",
governingTokenMint: "8x2dR8Mpzuz2YqyZyZjUbYWKSWesBo5jMx2Q9Y86udVk",
delegate: "9aUn5swQzUTRanaaTwmszxiv89cvFwUCjEBv1vZCoT1u",
},
output: {
status: "success",
message: "Successfully delegated voting power",
signature: "2GjfL3N9E4cHp7WhDZRkx7oF2J9m3Sf5hT6zRHcVWUjp",
},
explanation: "Delegate governance voting power to another wallet",
},
],
],

schema: z.object({
realm: z.string().min(32, "Invalid realm address"),
governingTokenMint: z
.string()
.min(32, "Invalid governing token mint address"),
delegate: z.string().min(32, "Invalid delegate wallet address"),
}),
handler: async (agent: SolanaAgentKit, input: Record<string, any>) => {
try {
const signature = await manageVoteDelegation(
agent,
new PublicKey(input.realm),
new PublicKey(input.governingTokenMint),
new PublicKey(input.delegate),
);

return {
status: "success",
message: "Successfully delegated voting power",
signature,
};
} catch (error: any) {
let errorMessage = error.message;

// Handle specific error cases with user-friendly messages
if (error.message.includes("Account not found")) {
errorMessage =
"No token owner record found - you need to deposit tokens first";
} else if (error.message.includes("Invalid delegate")) {
errorMessage = "The provided delegate address is invalid";
}

return {
status: "error",
message: errorMessage,
};
}
},
};

export default manageVoteDelegationAction;
57 changes: 57 additions & 0 deletions src/actions/realm/manageVotingOutcomes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import { PublicKey } from "@solana/web3.js";
import { Action } from "../../types/action";
import { SolanaAgentKit } from "../../agent";
import { z } from "zod";
import { monitorVotingOutcomes } from "../../tools/realm/monitor_voting_outcomes";

const monitorVotingOutcomesAction: Action = {
name: "MONITOR_VOTING_OUTCOMES",
similes: [
"check voting results",
"view proposal outcomes",
"get vote results",
"show proposal status",
],
description: `Monitor and retrieve the voting outcomes of a proposal within a governance realm.`,
examples: [
[
{
input: {
realm: "ASNJL4uXNNiNuC7XibsKw5VfB3Ce9wNsXwqGsxqrNbF4",
},
output: {
status: "success",
proposalState: "Completed",
voteResults: {
yes: 1200,
no: 300,
abstain: 100,
},
},
explanation: "Get the voting results of a proposal after voting ends",
},
],
],
schema: z.object({
realm: z.string(),
}),
handler: async (agent: SolanaAgentKit, input: Record<string, any>) => {
console.log("Monitoring voting outcomes for realm:", input.realm);
const outcome = await monitorVotingOutcomes(
agent,
new PublicKey(input.realm),
);

return {
status: "success",
proposalState: outcome.account.state.toString(),
voteResults: {
yes: outcome.account.yesVotesCount,
no: outcome.account.noVotesCount,
abstain: outcome.account.abstainVoteWeight,
},
};
},
};

export default monitorVotingOutcomesAction;
48 changes: 48 additions & 0 deletions src/actions/realm/trackVotingPower.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { PublicKey } from "@solana/web3.js";
import { Action } from "../../types/action";
import { SolanaAgentKit } from "../../agent";
import { z } from "zod";
import { trackVotingPower } from "../../tools/realm/track_voting_power";

const trackVotingPowerAction: Action = {
name: "TRACK_VOTING_POWER",
similes: [
"check voting power",
"view voting power",
"get governance voting power",
"show voting power",
],
description: `Get the voting power of a given wallet in a specific governance realm.
If you provide the wallet's token address, the voting power will be calculated accordingly.`,
examples: [
[
{
input: {
realm: "6k4tvNjkL1shSmhFjdUzGFyQFC4ZKTpaXnKds1z9jEwF",
},
output: {
status: "success",
votingPower: 1500,
},
explanation:
"Get the voting power for a specific wallet and governance realm",
},
],
],
schema: z.object({
realm: z.string(),
}),
handler: async (agent: SolanaAgentKit, input: Record<string, any>) => {
const votingPower = await trackVotingPower(
agent,
new PublicKey(input.realmID),
);

return {
status: "success",
votingPower,
};
},
};

export default trackVotingPowerAction;
Loading