Skip to Content

Anchor

Using Anchor with @solana/kit, Kite, and Codama

We could write a whole guide to using Anchor with Kite, but we thought it would be better to show you.

QuickNode has an entire YouTube video covering using Anchor and Solana Kit, that also uses Kite:

Here’s a full Anchor program - anchor-escrow-2025 - that uses Kite, Kit and Codama.

There are two ways TypeScript/JavaScript is used to interact with Anchor programs:

  • TypeScript based tests
  • Solana wallets

Either way, follow the steps below:

1. Make a client for your Anchor program using Codama

Check out create-codama-client.ts from the anchor-escrow-2025 repo. Codama will read the Anchor program’s IDL and create a TypeScript client for it, called dist/js-client.js, which is what we import.

2. Use the Codama client to interact with your Anchor program

The Codama client includes automatically generated functions to send instructions for each instruction handler in your Anchor program.

Check out example.test.ts from the anchor-escrow-2025 repo as an example.

Send instructions to your program’s instruction handlers

import * as programClient from "../dist/js-client";

We import the entire client as programClient, which allows us to call all the program’s instruction handlers.

The Escrow program has instruction handlers called makeOffer, takeOffer, refundOffer. Codama will create functions called getMakeOfferInstructionAsync, getTakeOfferInstructionAsync, getRefundOfferInstructionAsync, which we can use to crrate an instruction for each of these instruction handlers.

For example, if your program has a createBid instruction handler, Codama will create a getCreateBidInstructionAsync function for it. If it has addBet and settleBet instruction handlers, Codama will create getAddBetInstructionAsync and getSettleBetInstructionAsync functions for them. You get the idea.

const takeOfferInstruction = await programClient.getTakeOfferInstructionAsync({ taker: bob, maker: alice.address, tokenMintA, tokenMintB, takerTokenAccountA: bobTokenAccountA, makerTokenAccountB: aliceTokenAccountB, offer: testOffer, vault: testVault, tokenProgram: TOKEN_EXTENSIONS_PROGRAM, });

We can then send the instructions to the network using Kite’s sendTransactionFromInstructions function.

await connection.sendTransactionFromInstructions({ feePayer: alice, instructions: [takeOfferInstruction], });

Go check the full anchor-escrow-2025 tests for more examples. We make accounts for our users, create tokens (using Solana Kite), and then use the Escrow program’s instruction handlers via programClient.

Fetch and decode your program’s accounts

Your unique Anchor data accounts - ie, the Structs you make in Rust - can be fetched and decoded using Kite’s getAccountsFactory function. This will create a get<AccountName>Accounts function for each of your program’s accounts. See the Data Accounts page.

3. Use the wallet adapter to connect to Wallet Apps in your browser

For Next/React, you can allow Solana Wallet apps (like Phantom or Solflare) to connect to your app with using @solana/react. There’s a full example in the React App Example repo. I’ll add an example repo here soon, unless you add one first.

Last updated on