# Solana

## Getting Started <a href="#getting-started" id="getting-started"></a>

You can use two different options to implement staking for Everstake validator.

**Option 1: REST API**

You can use REST API to call methods which are described in [Swagger](https://wallet-sdk-api.everstake.one/swagger/#/Solana) with detailed examples (both [v1](https://wallet-sdk-api.everstake.one/swagger/#/Solana) and [v2](https://wallet-sdk-api.everstake.one/swagger/#/Solana-v2) are supported).

```
https://wallet-sdk-api.everstake.one
```

**Option 2: JavaScript library**

You can install and import Wallet SDK for Javascript.

{% hint style="warning" %}
Choose the version according to the Solana version you're working with
{% endhint %}

{% tabs %}
{% tab title="Solana v1" %}

```
@everstake/wallet-sdk-solana@^1.x.x
```

{% endtab %}

{% tab title="Solana v2" %}

```
@everstake/wallet-sdk-solana@^2.x.x
```

{% endtab %}
{% endtabs %}

#### Step. 1: Installing the Library <a href="#step-1-installing-the-library" id="step-1-installing-the-library"></a>

Install the npm library or yarn by copying the code below.

{% tabs %}
{% tab title="npm" %}

```sh
$ npm install @everstake/wallet-sdk-solana
```

{% endtab %}

{% tab title="yarn" %}

```sh
$ yarn add @everstake/wallet-sdk-solana
```

{% endtab %}
{% endtabs %}

#### Step. 2: Import Wallet SDK <a href="#step-2-import-wallet-sdk" id="step-2-import-wallet-sdk"></a>

After installing the app, you can import module of needed blockchain (Ethereum, Aptos, Solana, Cosmos, Polygon are available) and use the SDK:

**Import ES6**

```typescript
// import module
import { Solana } from "@everstake/wallet-sdk-solana";
```

**Import ES5**

```typescript
// import module
const { Solana } = require("@everstake/wallet-sdk-solana");

// Select network: "mainnet-beta" | "devnet"
const network = "mainnet-beta";

// Create SDK instance
const solanaClient = new Solana(network);
```

## Delegate <a href="#delegate-solana" id="delegate-solana"></a>

The <mark style="color:yellow;">`delegate`</mark> namespace contains method used for sending transactions on delegation. The unique method to the <mark style="color:yellow;">`delegate`</mark> namespace is:

* <mark style="color:yellow;">`createAccount(address, lamports)`</mark>: Create Account for Delegate user tokens.
* <mark style="color:yellow;">`delegate(address, lamports, stakeAccount)`</mark>: Delegate user tokens.

**Delegate Code Example**

```typescript
// Import SDK
import { Solana } from "@everstake/wallet-sdk-solana";

// Select network: "mainnet-beta" | "devnet"
const network = "mainnet-beta";

// Create SDK instance
const solanaClient = new Solana(network);

// User wallet address.
const address = "exampleCnpYVG5ukLQsfusLqMGFRm5kJwcNhVdA3NS94";

// The amount that the user delegates.
const lamports = 1000000000; // min value 0.01 (SOL)

// partner ID
const source = "99";

// return createStakeAccountTx - Raw Transaction and stakeAccount - address
const createAccount = await solanaClient.createAccount(
  address,
  lamports,
  source
);
// { result: createStakeAccountTx, stakeAccount }

const delegate = await solanaClient.delegate(
  address,
  lamports,
  createAccount.result.stakeAccount
);
console.log(delegate); // Raw Transaction Object
```

## Stake <a href="#stake-solana" id="stake-solana"></a>

The <mark style="color:yellow;">`stake`</mark> namespace contains method used for sending transactions on stake. This method includes createAccount and delegation together. The unique method to the <mark style="color:yellow;">`stake`</mark> namespace is:

* <mark style="color:yellow;">`stake(address, lamports, source)`</mark>: Stake user tokens.

**Stake Code Example**

```typescript
// Import SDK
import { Solana } from "@everstake/wallet-sdk-solana";

// User wallet address.
const address = "exampleCnpYVG5ukLQsfusLqMGFRm5kJwcNhVdA3NS94";

// The amount that the user delegates.
const lamports = 1000000000; // min value 0.01 (SOL)

// Your source identificator
const source = "99";

// Select network: "mainnet-beta" | "devnet"
const network = "mainnet-beta";

// Create SDK instance
const solanaClient = new Solana(network);

const stake = await solanaClient.stake(address, lamports, source);
console.log(stake); // Raw Transaction Object
```

## Deactivate <a href="#deactivate-solana" id="deactivate-solana"></a>

The <mark style="color:yellow;">`deactivate`</mark> namespace contains method used for deactivation of the user stake account. The unique method to the <mark style="color:yellow;">`deactivate`</mark> namespace is:

* <mark style="color:yellow;">`deactivate(address, stakeAccountPublicKey)`</mark>: Deactivate user stake account.

**Deactivate Code Example**

```typescript
// Import SDK
import { Solana } from "@everstake/wallet-sdk-solana";

// Select network: "mainnet-beta" | "devnet"
const network = "mainnet-beta";

// Create SDK instance
const solanaClient = new Solana(network);

// You can get Stake Account Public Key and all user delegations.
// User wallet address.
const address = "exampleCnpYVG5ukLQsfusLqMGFRm5kJwcNhVdA3NS94";
// Get all user delegations.
const getDelegations = await solanaClient.getDelegations(address);

// User Stake Account Public Key, which should be deactivated
const stakeAccountPublicKey =
  getDelegations.result[0].data.pubkey ||
  "AidgPiAZMLbqtNdGzt5CfBbtMjvFugnCcr116PXpCCnm";

// deactivateResp - return Raw Transaction
const deactivateResp = await solanaClient.deactivate(
  address,
  stakeAccountPublicKey
);
console.log(deactivateResp); // Raw Transaction Object
```

## Unstake <a href="#unstake-solana" id="unstake-solana"></a>

The <mark style="color:yellow;">`unstake`</mark> namespace contains methods used for sending transactions to unstake tokens. The unique method to the <mark style="color:yellow;">`unstake`</mark> namespace is:

* <mark style="color:yellow;">`unstake(address, lamports)`</mark>: Unstake user tokens.

**Unstake Code Example**

```typescript
// Import SDK
import { Solana } from "@everstake/wallet-sdk-solana";

// User wallet address.
const address = "exampleCnpYVG5ukLQsfusLqMGFRm5kJwcNhVdA3NS94";

// The amount that the user wants to unstake.
const lamports = 500000000; // min value 0.01 (SOL)

// Select network: "mainnet-beta" | "devnet"
const network = "mainnet-beta";

// Create SDK instance
const solanaClient = new Solana(network);

const unstake = await solanaClient.unstake(address, lamports);
console.log(unstake); // Raw Transaction Object
```

## Claim <a href="#claim-solana" id="claim-solana"></a>

The <mark style="color:yellow;">`claim`</mark> namespace contains methods used for claiming rewards from staking accounts. The unique method to the <mark style="color:yellow;">`claim`</mark> namespace is:

* <mark style="color:yellow;">`claim(address)`</mark>: Claim rewards for a staking account.

**Claim Code Example**

```typescript
// Import SDK
import { Solana } from "@everstake/wallet-sdk-solana";

// User wallet address.
const address = "exampleCnpYVG5ukLQsfusLqMGFRm5kJwcNhVdA3NS94";

// Select network: "mainnet-beta" | "devnet"
const network = "mainnet-beta";

// Create SDK instance
const solanaClient = new Solana(network);

const claim = await solanaClient.claim(address);
console.log(claim); // Raw Transaction Object
```

## Withdraw <a href="#withdraw-solana" id="withdraw-solana"></a>

The <mark style="color:yellow;">`withdraw`</mark> namespace contains methods used for sending transactions. The unique method to the <mark style="color:yellow;">`withdraw`</mark> namespace is:

<mark style="color:yellow;">`withdraw(address, stakeAccountPublicKey, stakeBalance)`</mark>: Withdraw user's tokens.

**Withdraw Code Example**

```typescript
// Import SDK
import { Solana } from "@everstake/wallet-sdk-solana";

// Select network: "mainnet-beta" | "devnet"
const network = "mainnet-beta";

// Create SDK instance
const solanaClient = new Solana(network);

// You can get Stake Account Public Key and all user delegations.
// User wallet address.
const address = "exampleCnpYVG5ukLQsfusLqMGFRm5kJwcNhVdA3NS94";
// Get all user delegations.
const getDelegations = await solanaClient.getDelegations(address);

// User Stake Account Public Key, which should be withdrawn
const stakeAccountPublicKey =
  getDelegations.result[0].data.pubkey ||
  "AidgPiAZMLbqtNdGzt5CfBbtMjvFugnCcr116PXpCCnm";

// User Stake Account Balance, which should be withdraw
const stakeBalance =
  getDelegations.result[0].data.parsed.stake.stake || "2300000"; // String, value in lamports (0.0023 SOL * 1000000000)

// Withdraw user's tokens - return Raw Transaction
const withdrawResp = await solanaClient.withdraw(
  address,
  stakeAccountPublicKey,
  stakeBalance
);
console.log(withdrawResp); // Raw Transaction Object
```

## Getting Info <a href="#getting-info-solana" id="getting-info-solana"></a>

The <mark style="color:yellow;">`get`</mark> namespace contains method used for getting info. The unique methods to the <mark style="color:yellow;">`get`</mark> namespace is:

* <mark style="color:yellow;">`getDelegations(address)`</mark>: Get all user delegations.

**Getting Info Code Example**

```typescript
// Import SDK
import { Solana } from "@everstake/wallet-sdk-solana";

// Select network: "mainnet-beta" | "devnet"
const network = "mainnet-beta";

// Create SDK instance
const solanaClient = new Solana(network);

// User wallet address.
const address = "CnpYVG5ukLQsfusLqMGFRm5kJwcNhVdA3NS94example";

// Get all user delegations.
const getDelegationsResult = await solanaClient.getDelegations(address);
console.log(getDelegationsResult); // see response example below
```

**Response Example**

```typescript
getDelegationsResult = {
  result: [
    {
      account: null,
      data: {
        parsed: {
          info: {
            meta: {
              authorized: {
                staker: "CnpYVG5ukLQsfusLqMGFRm5kJwcNhVdA3NS94example",
                withdrawer: "CnpYVG5ukLQsfusLqMGFRm5kJwcNhVdA3NS94example",
              },
              lockup: {
                custodian: "11111111111111111111111111111111",
                epoch: 0,
                unixTimestamp: 0,
              },
              rentExemptReserve: "2282880",
            },
            stake: {
              creditsObserved: 140045079,
              delegation: {
                activationEpoch: "399",
                deactivationEpoch: "400",
                stake: "2300000",
                voter: "9QU2QSxhb24FUX3Tu2FpczXjpK3VYrvRudywSZaM29mF",
                warmupCooldownRate: 0.25,
              },
            },
          },
          type: "delegated",
        },
        program: "stake",
        space: 200,
      },
      executable: false,
      lamports: 4582880,
      owner: "Stake11111111111111111111111111111111111111",
      rentEpoch: 0,
      pubkey: "CTZ4dbobNAec3fikKLFaXscPNqUzLmdLTzhNxrz4RTBh",
    },
  ],
};
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.everstake.one/integrations/everstake-products/wallet-sdk/protocols/solana.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
