Quickstart

A 10 minute overview on how to interact with Squads Protocol using Typescript.

Set up your workspace (1 minute)

Let's set up a new Typescript project. First, create a new directory.

mkdir squads_quickstart
cd squads_quickstart

Now, create a new file named tsconfig.json

{
  "compilerOptions": {
    "module": "commonjs",
    "target": "es6",
    "esModuleInterop": true
  }
}

Add a package.json file and add this content

{
  "scripts": {
    "test": "yarn run ts-mocha -p tsconfig.json -t 1000000 ./main.ts"
  },
  "dependencies": {
    "@solana/web3.js": "^1.73.0",
    "@sqds/multisig": "^1.9.0"
  },
  "devDependencies": {
    "@types/chai": "^4.3.3",
    "@types/mocha": "^9.1.1",
    "chai": "^4.3.6",
    "mocha": "^10.0.0",
    "ts-mocha": "^10.0.0",
    "typescript": "^4.8.3"
  }
}

Here, the main takeaway is that we are going to use the @sqds/sdk package.

Last but not least, create a main.ts file at the same location, and add this code:

import * as multisig from "@sqds/multisig";
import { Connection, Keypair, LAMPORTS_PER_SOL } from "@solana/web3.js";

const { Permission, Permissions } = multisig.types;
const connection = new Connection("http://localhost:8899", "confirmed");

Let's get to the code (8 minutes)

Create a multisig (2 minutes)

Let's set up a testing flow in which we're going to create a multisig, propose a new transaction, vote on that transaction and execute it.

First, let's add our first step: Setting up the multisig members and creating the multisig.

describe("Interacting with the Squads V4 SDK", () => {
    const creator = Keypair.generate();
    const secondMember = Keypair.generate();
    before(async () => {
        const airdropSignature = await connection.requestAirdrop(
            creator.publicKey,
            1 * LAMPORTS_PER_SOL
        );
        await connection.confirmTransaction(airdropSignature);
    });

    const createKey = Keypair.generate().publicKey;

    // Derive the multisig account PDA
    const [multisigPda] = multisig.getMultisigPda({
        createKey,
    });

    it("Create a new multisig", async () => {
        // Create the multisig
        const signature = await multisig.rpc.multisigCreate({
            connection,
            // One time random Key
            createKey,
            // The creator & fee payer
            creator,
            multisigPda,
            configAuthority: creator.publicKey,
            timeLock: 0,
            members: [{
                    key: creator.publicKey,
                    permissions: Permissions.all(),
                },
                {
                    key: secondMember.publicKey,
                    // This permission means that the user will only be able to vote on transactions
                    permissions: Permissions.fromPermissions([Permission.Vote]),
                },
            ],
            // This means that there needs to be 2 votes for a transaction proposal to be approved
            threshold: 2,
        });
        console.log("Multisig created: ", signature);
    });
});

Create transaction proposal (2 minutes)

Now, let's create a transaction proposal. We want the multisig to send 0.1 SOL to the creator. For purposes of this tutorial, we first have to send that amount to the multisig, and can then create a message containing the instruction that needs to be executed.

it("Create a transaction proposal", async () => {
    const [vaultPda, vaultBump] = multisig.getVaultPda({
        multisigPda,
        index: 0,
    });
    const instruction = SystemProgram.transfer(
    // The transfer is being signed from the Squads Vault, that is why we use the VaultPda
        vaultPda,
        creator.publicKey,
        1 * LAMPORTS_PER_SOL
    );
    // This message contains the instructions that the transaction is going to execute
    const transferMessage = new TransactionMessage({
        payerKey: vaultPda,
        recentBlockhash: (await connection.getLatestBlockhash()).blockhash,
        instructions: [instruction],
    });
    // This is the first transaction in the multisig
    const transactionIndex = 1n;
    const signature1 = await multisig.rpc.vaultTransactionCreate({
        connection,
        feePayer: creator,
        multisigPda,
        transactionIndex,
        creator: creator.publicKey,
        vaultIndex: 1,
        ephemeralSigners: 0,
        transactionMessage: transferMessage,
        memo: "Transfer 0.1 SOL to creator",
    });

    console.log("Transaction created: ", signature1);
    
    const signature2 = await multisig.rpc.proposalCreate({
        connection,
        feePayer: members.voter,
        multisigPda,
        transactionIndex,
        creator: feePayer,
    });
    
    console.log("Transaction proposal created: ", signature2);
    
});

Vote on the transaction proposal (2 minutes)

Let's now vote on the transaction proposal we just made using the two Keypairs we created at the start of this tutorial: creator and secondMember.

 it("Vote on the created proposal", async () => {
     const transactionIndex = 1n;
     multisig.rpc.proposalApprove({
         connection,
         feePayer: creator,
         multisigPda,
         transactionIndex,
         member: creator.publicKey,
     });

     multisig.rpc.proposalApprove({
         connection,
         feePayer: creator,
         multisigPda,
         transactionIndex,
         member: secondMember.publicKey,
         signers: [creator, secondMember],
     });
 });

Execute the transaction (2 minutes)

Now the most important part, actually executing that transaction we proposed.

it("Execute the proposal", async () => {
    const transactionIndex = 1n;
    const [proposalPda] = multisig.getProposalPda({
        multisigPda,
        transactionIndex,
    });
    const signature = await multisig.rpc.vaultTransactionExecute({
        connection,
        feePayer: creator,
        multisigPda,
        transactionIndex,
        member: creator.publicKey,
        signers: [creator],
    });
    console.log("Transaction executed: ", signature);
});

Start a local validator (1 minute)

Now that you have a completed flow, let's actually execute these transactions on chain. For the purpose of this tutorial, we are going to do so on a local Solana instance.

If you do not yet have the Solana CLI installed, please do so by reading the following guide.

Now, start up a local validator with the Squads V4 program preloaded.

solana-test-validator --url m -c SQDS4ep65T869zMMBKyuUq6aD6EgTu8psMjkvj52pCf

Execute the script

Okay, let's execute the script and see what happens.

yarn test

Make sure you are using the Nodee.js version 16. If not, run nvm use 16 inside your terminal.

Visualize your transactions

Once the tests have passed, you can go to the Solana Explorer and visualize your transactions by pasting their signature in the search bar.

What can I do from here?

Last updated