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: Proposal Management| SPL Governance #187

Open
wants to merge 1 commit 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
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"@bonfida/spl-name-service": "^3.0.7",
"@cks-systems/manifest-sdk": "0.1.59",
"@coral-xyz/anchor": "0.29",
"@coral-xyz/borsh": "^0.30.1",
"@langchain/core": "^0.3.26",
"@langchain/groq": "^0.1.2",
"@langchain/langgraph": "^0.2.36",
Expand All @@ -45,10 +46,11 @@
"@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",
"@tensor-oss/tensorswap-sdk": "^4.5.0",
"@sqds/multisig": "^2.1.3",
"@tiplink/api": "^0.3.1",
"ai": "^4.0.22",
"bn.js": "^5.2.1",
Expand Down
41 changes: 41 additions & 0 deletions pnpm-lock.yaml

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

49 changes: 48 additions & 1 deletion src/agent/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import { Connection, Keypair, PublicKey } from "@solana/web3.js";
import {
Connection,
Keypair,
PublicKey,
TransactionInstruction,
} from "@solana/web3.js";
import { BN } from "@coral-xyz/anchor";
import bs58 from "bs58";
import Decimal from "decimal.js";
Expand Down Expand Up @@ -92,6 +97,13 @@ import { create_proposal } from "../tools/squads_multisig/create_proposal";
import { approve_proposal } from "../tools/squads_multisig/approve_proposal";
import { execute_transaction } from "../tools/squads_multisig/execute_proposal";
import { reject_proposal } from "../tools/squads_multisig/reject_proposal";
import {
createGovernanceProposal,
cancelGovernanceProposal,
executeGovernanceProposal,
getGovernanceProposalState,
} from "../tools/proposals_governance";
import { ProposalState } from "@solana/spl-governance";

/**
* Main class for interacting with Solana blockchain
Expand Down Expand Up @@ -655,4 +667,39 @@ export class SolanaAgentKit {
): Promise<string> {
return execute_transaction(this, transactionIndex);
}

async createGovernanceProposal(
realm: PublicKey,
governance: PublicKey,
name: string,
description: string,
instructions: TransactionInstruction[],
): Promise<string> {
return createGovernanceProposal(
this,
realm,
governance,
name,
description,
instructions,
);
}

async cancelGovernanceProposal(proposal: PublicKey): Promise<string> {
return cancelGovernanceProposal(this, proposal);
}

async executeGovernanceProposal(proposal: PublicKey): Promise<string> {
return executeGovernanceProposal(this, proposal);
}

async getGovernanceProposalState(proposal: PublicKey): Promise<{
state: ProposalState;
name: string;
description: string;
yesVotes: number;
noVotes: number;
}> {
return getGovernanceProposalState(this, proposal);
}
}
158 changes: 158 additions & 0 deletions src/langchain/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
CreateSingleOptions,
StoreInitOptions,
} from "@3land/listings-sdk/dist/types/implementation/implementationTypes";
import { TransactionInstruction } from "@solana/web3.js";

export class SolanaBalanceTool extends Tool {
name = "solana_balance";
Expand Down Expand Up @@ -2687,6 +2688,159 @@ export class SolanaExecuteProposal2by2Multisig extends Tool {
}
}
}
export class SolanaCreateGovernanceProposalTool extends Tool {
name = "create_governance_proposal";
description = `Creates a new governance proposal in a DAO.

Inputs (JSON string):
- realm: string, the realm public key (required)
- governance: string, the governance account public key (required)
- name: string, name of the proposal (required)
- description: string, description of the proposal (required)
- instructions: string[], array of serialized instructions (required)`;

constructor(private solanaKit: SolanaAgentKit) {
super();
}

protected async _call(input: string): Promise<string> {
try {
const params = JSON.parse(input);
const instructions = params.instructions.map((i: string) => {
const decoded = Buffer.from(i, "base64");
// Deserialize the instruction data
const data = decoded.slice(0, -32); // Last 32 bytes are programId
const programId = new PublicKey(decoded.slice(-32));

// Note: You might need to adjust this depending on your exact serialization format
// This assumes the instruction data contains keys and their metadata
return new TransactionInstruction({
programId,
keys: [], // You'll need to populate this based on your serialized format
data,
});
});

const signature = await this.solanaKit.createGovernanceProposal(
new PublicKey(params.realm),
new PublicKey(params.governance),
params.name,
params.description,
instructions,
);

return JSON.stringify({
status: "success",
message: "Governance proposal created successfully",
signature,
});
} catch (error: any) {
return JSON.stringify({
status: "error",
message: error.message,
code: error.code || "CREATE_GOVERNANCE_PROPOSAL_ERROR",
});
}
}
}
export class SolanaCancelGovernanceProposalTool extends Tool {
name = "cancel_governance_proposal";
description = `Cancel an existing governance proposal in a DAO.

Inputs (JSON string):
- proposal: string, the proposal public key (required)`;

constructor(private solanaKit: SolanaAgentKit) {
super();
}

protected async _call(input: string): Promise<string> {
try {
const params = JSON.parse(input);
const signature = await this.solanaKit.cancelGovernanceProposal(
new PublicKey(params.proposal),
);

return JSON.stringify({
status: "success",
message: "Governance proposal cancelled successfully",
signature,
});
} catch (error: any) {
return JSON.stringify({
status: "error",
message: error.message,
code: error.code || "CANCEL_GOVERNANCE_PROPOSAL_ERROR",
});
}
}
}

export class SolanaExecuteGovernanceProposalTool extends Tool {
name = "execute_governance_proposal";
description = `Execute an existing governance proposal in a DAO.

Inputs (JSON string):
- proposal: string, the proposal public key (required)`;

constructor(private solanaKit: SolanaAgentKit) {
super();
}

protected async _call(input: string): Promise<string> {
try {
const params = JSON.parse(input);
const signature = await this.solanaKit.executeGovernanceProposal(
new PublicKey(params.proposal),
);

return JSON.stringify({
status: "success",
message: "Governance proposal executed successfully",
signature,
});
} catch (error: any) {
return JSON.stringify({
status: "error",
message: error.message,
code: error.code || "EXECUTE_GOVERNANCE_PROPOSAL_ERROR",
});
}
}
}

export class SolanaGetGovernanceProposalStateTool extends Tool {
name = "get_governance_proposal_state";
description = `Get the current state of an existing governance proposal in a DAO.

Inputs (JSON string):
- proposal: string, the proposal public key (required)`;

constructor(private solanaKit: SolanaAgentKit) {
super();
}

protected async _call(input: string): Promise<string> {
try {
const params = JSON.parse(input);
const state = await this.solanaKit.getGovernanceProposalState(
new PublicKey(params.proposal),
);

return JSON.stringify({
status: "success",
message: "Governance proposal state fetched successfully",
state,
});
} catch (error: any) {
return JSON.stringify({
status: "error",
message: error.message,
code: error.code || "GET_GOVERNANCE_PROPOSAL_STATE_ERROR",
});
}
}
}

export function createSolanaTools(solanaKit: SolanaAgentKit) {
return [
Expand Down Expand Up @@ -2755,5 +2909,9 @@ export function createSolanaTools(solanaKit: SolanaAgentKit) {
new SolanaApproveProposal2by2Multisig(solanaKit),
new SolanaRejectProposal2by2Multisig(solanaKit),
new SolanaExecuteProposal2by2Multisig(solanaKit),
new SolanaCreateGovernanceProposalTool(solanaKit),
new SolanaCancelGovernanceProposalTool(solanaKit),
new SolanaExecuteGovernanceProposalTool(solanaKit),
new SolanaGetGovernanceProposalStateTool(solanaKit),
];
}
Loading