> ## Documentation Index
> Fetch the complete documentation index at: https://docs.glam.systems/llms.txt
> Use this file to discover all available pages before exploring further.

# Code Examples

## Vault Setup

Refer to CLI docs to [create the vault](https://docs.glam.systems/cli/general#create-a-new-product), [enable integrations](https://docs.glam.systems/cli/vault/integrations/configuration) and [manage delegates](https://docs.glam.systems/cli/delegate).

## Project Setup

Install `@glamsystems/glam-sdk` using your preferred package manager (npm, pnpm, yarn etc). An example TypeScript project using the GLAM SDK [can be found on Github](https://github.com/glamsystems/glam-sdk/tree/main/examples/nodejs-app).

## Solana Basics

Key building blocks used across programs and integrations:

* [**Program Derived Address (PDA)**](http://%20solana.com/docs/core/pda) – A deterministic address controlled by a program, often used for vaults or state accounts.
* [**Mint**](http://%20solana.com/developers/courses/tokens-and-nfts/token-program#token-mint) – Defines a token’s supply, decimals, and authorities.
* [**Associated Token Account (ATA)**](http://%20spl.solana.com/associated-token-account) – Standardized token holding account for each user-token pair.
* **Token Standards**
  * [Tokens on Solana](https://solana.com/docs/tokens) – High-level overview.
  * [Token Program](https://spl.solana.com/token) – Original SPL token implementation.
  * [Token-2022](https://spl.solana.com/token-2022) – Newer standard with added features like transfer hooks and confidentiality.

## Initialize a GlamClient

Build GlamClient from environment variables:

```javascript theme={null}
import { GlamClient} from "@glamsystems/glam-sdk";

// Make sure the following env variables are available:
// ANCHOR_PROVIDER_URL=<solana_rpc_url>
// ANCHOR_WALLET=<path_to_keypair>
const glamClient = new GlamClient();
```

Alternatively you can create a GlamClient instance from RPC URL and wallet keypair programmatically:

```javascript lines  theme={null}
import NodeWallet from "@coral-xyz/anchor/dist/cjs/nodewallet";
import { AnchorProvider } from "@coral-xyz/anchor";
import { Connection, Keypair } from "@solana/web3.js";
import { GlamClient} from "@glamsystems/glam-sdk";

const rpcConnection = new Connection("solana_rpc_url");
const loadWallet = (path: string) => {
  let payer = Keypair.fromSecretKey(
    Buffer.from(
      JSON.parse(require("fs").readFileSync(path, { encoding: "utf-8" })),
    ),
  );
  return new NodeWallet(payer);
};

const glamClient = new GlamClient({
  provider: new AnchorProvider(
    rpcConnection,
    loadWallet("/path/to/wallet/keypair.json"),
    { commitment: "confirmed" },
  ),
  cluster: ClusterNetwork.Mainnet,
});
```

## Transaction Options

All glam sdk methods that initiate transactions accept a param `txOptions` which can be used to customize priority fees, simulation behavior, lookup tables, etc. It’s recommended to enable simulation and put a max cap on the priority fee:

```javascript theme={null}
const txOptions = {
  simulate: true,
  maxFeeLamports: 100_000, // 0.0001 SOL
  useMaxFee: true
}
```

If you want to enable a dynamic priority fee, use the `getPriorityFeeMicroLamports` callback function:

```javascript theme={null}
const txOptions = {
  simulate: true,
  getPriorityFeeMicroLamports, // a function that returns priority fee micro lamports
}
```

## Kamino

### Initialize User Metadata

```javascript theme={null}
      try {
        const txSig = await glamClient.kaminoLending.initUserMetadata(
          txOptions,
        );
        console.log(`Initialized Kamino user:`, txSig);
      } catch (e) {
        console.error(parseTxError(e));
        throw e;
      }
```

### Deposit

```javascript theme={null}
      try {
        const txSig = await glamClient.kaminoLending.deposit(
          statePda,
          lendingMarket,
          asset, // mint pubkey
          amount, // BN
          txOptions,
        );
        console.log(`Deposit ${amount} ${asset} to Kamino from vault:`, txSig);
      } catch (e) {
        console.error(parseTxError(e));
        throw e;
      }
```

### Borrow

```javascript theme={null}
      try {
        const txSig = await glamClient.kaminoLending.borrow(
          statePda,
          lendingMarket,
          asset, // mint pubkey
          amount, // BN
          txOptions,
        );
        console.log(`Borrowed ${amount} ${asset} from Kamino:`, txSig);
      } catch (e) {
        console.error(parseTxError(e));
        throw e;
      }
```

### Repay

```javascript theme={null}
      try {
        const txSig = await glamClient.kaminoLending.repay(
          statePda,
          lendingMarket,
          asset,
          amount, // BN
          txOptions,
        );
        console.log(`Repaid ${amount} ${asset} to Kamino:`, txSig);
      } catch (e) {
        console.error(parseTxError(e));
        throw e;
      }
```

### Withdraw

```javascript theme={null}
      try {
        const txSig = await glamClient.kaminoLending.withdraw(
          statePda,
          lendingMarket,
          asset, // mint pubkey
          amount, // BN
          txOptions,
        );
        console.log(`Withdraw ${amount} ${asset} from Kamino:`, txSig);
      } catch (e) {
        console.error(parseTxError(e));
        throw e;
      }
```

### Claim Rewards

> [!NOTE]
>
> Claiming rewards from Kamino refers to harvesting current farm incentives. Future KMNO token distributions are not yet available and are planned for a later version.

```javascript theme={null}
      try {
        const txSig = await glamClient.kaminoFarm.harvest([farmState], txOptions);
        console.log(`Harvested farm rewards:`, txSig);
      } catch (e) {
        console.error(parseTxError(e));
        throw e;
      }
```

## Jupiter Swap

Create quote params:

```javascript theme={null}
import { QuoteParams} from "@glamsystems/glam-sdk";

const quoteParams = {
  inputMint,
  outputMint,
  amount,
  swapMode: "ExactIn",
  slippageBps,
  asLegacyTransaction: false,
} as QuoteParams;
```

Execute the swap:

```javascript theme={null}
try {
  const txSig = await glamClient.jupiterSwap.swap(
    statePda,
    { quoteParams },
    txOptions,
  );
  console.log(`Swapped ${amount} ${from} to ${to}: ${txSig}`);
} catch (e) {
  console.error(parseTxError(e));
}
```


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.