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

# Solana Examples

> Working code examples for Solana integration with OneBalance, including transaction signing and cross-chain operations

This page provides complete, working code examples for integrating Solana with OneBalance. All examples use real data from our testing environment and are copy-paste ready.

## Prerequisites

Install the required dependencies:

<CodeGroup>
  ```bash npm theme={null}
  npm install @solana/web3.js bs58
  ```

  ```bash pnpm theme={null}
  pnpm add @solana/web3.js bs58
  ```

  ```bash yarn theme={null}
  yarn add @solana/web3.js bs58
  ```
</CodeGroup>

## Signing Utilities

Here's the signing function you'll need for Solana operations:

<Tabs>
  <Tab title="Browser Wallet Signing">
    For browser wallets like Phantom or Solflare:

    ```typescript theme={null}
    import { MessageV0, VersionedTransaction } from '@solana/web3.js';
    import bs58 from 'bs58';

    /**
     * Signs a Solana operation using a browser wallet
     * @param dataToSign - Base64 encoded data from quote response
     * @param wallet - Connected Solana wallet (Phantom, Solflare, etc.)
     * @returns Base58 encoded signature
     */
    async function signSolanaOperation(dataToSign: string, wallet: any): Promise<string> {
      const msgBuffer = Buffer.from(dataToSign, 'base64');
      const message = MessageV0.deserialize(msgBuffer);
      const transaction = new VersionedTransaction(message);
      
      const signedTx = await wallet.signTransaction(transaction);
      return bs58.encode(Buffer.from(signedTx.signatures[signedTx.signatures.length - 1]));
    }
    ```
  </Tab>

  <Tab title="Private Key Signing">
    For server-side operations or direct private key access:

    ```typescript theme={null}
    import { MessageV0, VersionedTransaction, PublicKey } from '@solana/web3.js';
    import bs58 from 'bs58';

    /**
     * Signs a Solana operation with a private key
     * @param accountAddress - Your Solana account address
     * @param privateKey - Your private key in base58 format
     * @param chainOp - Chain operation from quote response
     * @returns Signed chain operation
     */
    function signSolanaChainOperation(accountAddress: string, privateKey: string, chainOp: any) {
      const msgBuffer = Buffer.from(chainOp.dataToSign, 'base64');
      const message = MessageV0.deserialize(msgBuffer);
      const transaction = new VersionedTransaction(message);

      const decodedKey = bs58.decode(privateKey);
      transaction.sign([{
          publicKey: new PublicKey(accountAddress),
          secretKey: Buffer.from(decodedKey),
      }]);

      const signature = bs58.encode(Buffer.from(transaction.signatures[transaction.signatures.length - 1]));
      return { ...chainOp, signature };
    }
    ```
  </Tab>
</Tabs>

## Example 1: SOL → USDC (Same Chain)

The simplest Solana operation - swap SOL to USDC within Solana:

<Tabs>
  <Tab title="Request Quote">
    <CodeGroup>
      ```bash cURL theme={null}
      curl -X POST 'https://be.onebalance.io/api/v3/quote' \
        -H 'Content-Type: application/json' \
        -H 'x-api-key: 42bb629272001ee1163ca0dbbbc07bcbb0ef57a57baf16c4b1d4672db4562c11' \
        -d '{
          "from": {
            "accounts": [{
              "type": "solana",
              "accountAddress": "J5CCzBULFax899tcirb6wMbenQUd8whbaetG7EfSick5"
            }],
            "asset": {
              "assetId": "solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp/slip44:501"
            },
            "amount": "10000000"
          },
          "to": {
            "asset": {
              "assetId": "solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp/token:EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v"
            }
          }
        }'
      ```

      ```typescript TypeScript theme={null}
      const quoteRequest = {
        from: {
          accounts: [{
            type: "solana",
            accountAddress: "J5CCzBULFax899tcirb6wMbenQUd8whbaetG7EfSick5"
          }],
          asset: {
            assetId: "solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp/slip44:501"
          },
          amount: "10000000" // 0.01 SOL (9 decimals)
        },
        to: {
          asset: {
            assetId: "solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp/token:EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v"
          }
        }
      };

      const response = await fetch('https://be.onebalance.io/api/v3/quote', {
        method: 'POST',
        headers: {
          'Content-Type': 'application/json',
          'x-api-key': '42bb629272001ee1163ca0dbbbc07bcbb0ef57a57baf16c4b1d4672db4562c11'
        },
        body: JSON.stringify(quoteRequest)
      });

      const quote = await response.json();
      ```
    </CodeGroup>
  </Tab>

  <Tab title="Execute Quote">
    <CodeGroup>
      ```typescript TypeScript theme={null}
      // Sign the Solana operation
      const solanaOperation = quote.originChainsOperations.find(op => op.type === 'solana');
      const signature = await signSolanaOperation(solanaOperation.dataToSign, wallet);

      // Add signature to the quote
      const signedQuote = {
        ...quote,
        originChainsOperations: quote.originChainsOperations.map(op => 
          op.type === 'solana' ? { ...op, signature } : op
        )
      };

      // Execute the quote
      const executeResponse = await fetch('https://be.onebalance.io/api/v3/quote/execute', {
        method: 'POST',
        headers: {
          'Content-Type': 'application/json',
          'x-api-key': '42bb629272001ee1163ca0dbbbc07bcbb0ef57a57baf16c4b1d4672db4562c11'
        },
        body: JSON.stringify(signedQuote)
      });

      const result = await executeResponse.json();
      console.log('Swap completed:', result);
      ```
    </CodeGroup>

    <Check>
      Successfully swapped 0.01 SOL to \~1.63 USDC on Solana
    </Check>
  </Tab>
</Tabs>

## Example 2: SOL → USDC (Cross-Chain)

Swap SOL on Solana to USDC on Arbitrum - the most common cross-chain pattern:

<Tabs>
  <Tab title="Request Quote">
    <CodeGroup>
      ```bash cURL theme={null}
      curl -X POST 'https://be.onebalance.io/api/v3/quote' \
        -H 'Content-Type: application/json' \
        -H 'x-api-key: 42bb629272001ee1163ca0dbbbc07bcbb0ef57a57baf16c4b1d4672db4562c11' \
        -d '{
          "from": {
            "accounts": [{
              "type": "solana",
              "accountAddress": "J5CCzBULFax899tcirb6wMbenQUd8whbaetG7EfSick5"
            }],
            "asset": {
              "assetId": "solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp/slip44:501"
            },
            "amount": "10000000"
          },
          "to": {
            "asset": {
              "assetId": "eip155:42161/erc20:0xaf88d065e77c8cC2239327C5EDb3A432268e5831"
            },
            "account": "eip155:42161:0x895Cf62399bF1F8b88195E741b64278b41EB7F09"
          }
        }'
      ```

      ```typescript TypeScript theme={null}
      const crossChainQuote = {
        from: {
          accounts: [{
            type: "solana",
            accountAddress: "J5CCzBULFax899tcirb6wMbenQUd8whbaetG7EfSick5"
          }],
          asset: {
            assetId: "solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp/slip44:501"
          },
          amount: "10000000" // 0.01 SOL
        },
        to: {
          asset: {
            assetId: "eip155:42161/erc20:0xaf88d065e77c8cC2239327C5EDb3A432268e5831"
          },
          account: "eip155:42161:0x895Cf62399bF1F8b88195E741b64278b41EB7F09"
        }
      };

      const response = await fetch('https://be.onebalance.io/api/v3/quote', {
        method: 'POST',
        headers: {
          'Content-Type': 'application/json',
          'x-api-key': '42bb629272001ee1163ca0dbbbc07bcbb0ef57a57baf16c4b1d4672db4562c11'
        },
        body: JSON.stringify(crossChainQuote)
      });

      const quote = await response.json();
      ```
    </CodeGroup>
  </Tab>

  <Tab title="Quote Response">
    ```json theme={null}
    {
      "id": "0xcd3a5cfe80d1b84db755bfb8ebe0a617ff153cc48ab6d5ab28436386f06ce100",
      "accounts": [{
        "type": "solana",
        "accountAddress": "J5CCzBULFax899tcirb6wMbenQUd8whbaetG7EfSick5"
      }],
      "originToken": {
        "assetType": ["solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp/slip44:501"],
        "amount": "10000000",
        "fiatValue": "1.67"
      },
      "destinationToken": {
        "assetType": "eip155:42161/erc20:0xaf88d065e77c8cC2239327C5EDb3A432268e5831",
        "amount": "1570450", // ~1.57 USDC on Arbitrum
        "minimumAmount": "1516171",
        "fiatValue": "1.57",
        "recipientAccount": "eip155:42161:0x895Cf62399bF1F8b88195E741b64278b41EB7F09"
      },
      "fees": {
        "cumulativeUSD": "0.12"
      }
    }
    ```
  </Tab>

  <Tab title="Execute Quote">
    ```typescript theme={null}
    // Sign and execute the cross-chain operation
    const solanaOp = quote.originChainsOperations.find(op => op.type === 'solana');
    const signedOp = await signSolanaOperation(solanaOp.dataToSign, wallet);

    const executeRequest = {
      ...quote,
      originChainsOperations: [{ ...solanaOp, signature: signedOp }]
    };

    const result = await fetch('https://be.onebalance.io/api/v3/quote/execute', {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json',
        'x-api-key': '42bb629272001ee1163ca0dbbbc07bcbb0ef57a57baf16c4b1d4672db4562c11'
      },
      body: JSON.stringify(executeRequest)
    });

    const execution = await result.json();
    console.log('Cross-chain swap completed:', execution);
    ```

    <Check>
      Successfully swapped 0.01 SOL to 1.57 USDC and bridged to Arbitrum
    </Check>
  </Tab>
</Tabs>

## Example 3: USDC (Solana) → USDC (Arbitrum)

Transfer USDC from Solana to Arbitrum:

<CodeGroup>
  ```bash cURL theme={null}
  curl -X POST 'https://be.onebalance.io/api/v3/quote' \
    -H 'Content-Type: application/json' \
    -H 'x-api-key: 42bb629272001ee1163ca0dbbbc07bcbb0ef57a57baf16c4b1d4672db4562c11' \
    -d '{
      "from": {
        "accounts": [{
          "type": "solana",
          "accountAddress": "J5CCzBULFax899tcirb6wMbenQUd8whbaetG7EfSick5"
        }],
        "asset": {
          "assetId": "solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp/token:EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v"
        },
        "amount": "1000000"
      },
      "to": {
        "asset": {
          "assetId": "eip155:42161/erc20:0xaf88d065e77c8cC2239327C5EDb3A432268e5831"
        },
        "account": "eip155:42161:0x895Cf62399bF1F8b88195E741b64278b41EB7F09"
      }
    }'
  ```

  ```typescript TypeScript theme={null}
  const usdcTransfer = {
    from: {
      accounts: [{
        type: "solana",
        accountAddress: "J5CCzBULFax899tcirb6wMbenQUd8whbaetG7EfSick5"
      }],
      asset: {
        assetId: "solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp/token:EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v"
      },
      amount: "1000000" // 1 USDC (6 decimals)
    },
    to: {
      asset: {
        assetId: "eip155:42161/erc20:0xaf88d065e77c8cC2239327C5EDb3A432268e5831"
      },
      account: "eip155:42161:0x895Cf62399bF1F8b88195E741b64278b41EB7F09"
    }
  };

  const response = await fetch('https://be.onebalance.io/api/v3/quote', {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json',
      'x-api-key': '42bb629272001ee1163ca0dbbbc07bcbb0ef57a57baf16c4b1d4672db4562c11'
    },
    body: JSON.stringify(usdcTransfer)
  });

  const quote = await response.json();
  ```
</CodeGroup>

<Info>
  This operation only requires a Solana account since funds come entirely from Solana. The recipient is specified in `to.account`.
</Info>

## Example 4: Aggregated USDC → SOL (Multi-Account)

Use aggregated USDC balance from both EVM and Solana accounts to buy SOL:

<Tabs>
  <Tab title="Multi-Account Request">
    <CodeGroup>
      ```bash cURL theme={null}
      curl -X POST 'https://be.onebalance.io/api/v3/quote' \
        -H 'Content-Type: application/json' \
        -H 'x-api-key: 42bb629272001ee1163ca0dbbbc07bcbb0ef57a57baf16c4b1d4672db4562c11' \
        -d '{
          "from": {
            "accounts": [
              {
                "type": "kernel-v3.1-ecdsa",
                "accountAddress": "0xd4f5A60c9b500f875ADf757BC3027A4424079E05",
                "deploymentType": "ERC4337",
                "signerAddress": "0x9b747cC14A5672a7166b4eccdc92d7F4003f8081"
              },
              {
                "type": "solana",
                "accountAddress": "J5CCzBULFax899tcirb6wMbenQUd8whbaetG7EfSick5"
              }
            ],
            "asset": {
              "assetId": "ob:usdc"
            },
            "amount": "10000000"
          },
          "to": {
            "asset": {
              "assetId": "solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp/slip44:501"
            }
          }
        }'
      ```

      ```typescript TypeScript theme={null}
      const aggregatedSwap = {
        from: {
          accounts: [
            {
              type: "kernel-v3.1-ecdsa",
              accountAddress: "0xd4f5A60c9b500f875ADf757BC3027A4424079E05",
              deploymentType: "ERC4337",
              signerAddress: "0x9b747cC14A5672a7166b4eccdc92d7F4003f8081"
            },
            {
              type: "solana",
              accountAddress: "J5CCzBULFax899tcirb6wMbenQUd8whbaetG7EfSick5"
            }
          ],
          asset: {
            assetId: "ob:usdc" // Aggregated USDC across chains
          },
          amount: "10000000" // 10 USDC
        },
        to: {
          asset: {
            assetId: "solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp/slip44:501"
          }
        }
      };

      const response = await fetch('https://be.onebalance.io/api/v3/quote', {
        method: 'POST',
        headers: {
          'Content-Type': 'application/json',
          'x-api-key': '42bb629272001ee1163ca0dbbbc07bcbb0ef57a57baf16c4b1d4672db4562c11'
        },
        body: JSON.stringify(aggregatedSwap)
      });

      const quote = await response.json();
      ```
    </CodeGroup>
  </Tab>

  <Tab title="Multi-Account Signing">
    This operation involves both EVM and Solana accounts:

    ```typescript theme={null}
    async function signMultiAccountQuote(quote: any, solanaWallet: any, evmSigner: any) {
      const signedOperations = await Promise.all(
        quote.originChainsOperations.map(async (operation: any) => {
          if (operation.type === 'solana') {
            const signature = await signSolanaOperation(operation.dataToSign, solanaWallet);
            return { ...operation, signature };
          } else {
            // Sign EVM operation with typed data
            const signature = await evmSigner.signTypedData(operation.typedDataToSign);
            return { 
              ...operation, 
              userOp: { ...operation.userOp, signature }
            };
          }
        })
      );
      
      return {
        ...quote,
        originChainsOperations: signedOperations
      };
    }

    // Execute the multi-account operation
    const signedQuote = await signMultiAccountQuote(quote, solanaWallet, evmSigner);

    const result = await fetch('https://be.onebalance.io/api/v3/quote/execute', {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json',
        'x-api-key': '42bb629272001ee1163ca0dbbbc07bcbb0ef57a57baf16c4b1d4672db4562c11'
      },
      body: JSON.stringify(signedQuote)
    });

    const execution = await result.json();
    console.log('Aggregated swap completed:', execution);
    ```

    <Check>
      Used aggregated USDC from multiple chains to purchase SOL
    </Check>
  </Tab>
</Tabs>

<Info>
  This example shows OneBalance's power - using funds from multiple blockchains in a single operation.
</Info>

## Check Balances

Always verify your balances before creating quotes:

<CodeGroup>
  ```bash cURL - Single Account theme={null}
  curl -X GET 'https://be.onebalance.io/api/v3/balances/aggregated-balance?account=solana:J5CCzBULFax899tcirb6wMbenQUd8whbaetG7EfSick5' \
    -H 'x-api-key: 42bb629272001ee1163ca0dbbbc07bcbb0ef57a57baf16c4b1d4672db4562c11'
  ```

  ```bash cURL - Multi-Account theme={null}
  curl -X GET 'https://be.onebalance.io/api/v3/balances/aggregated-balance?account=eip155:42161:0xfe52613d747E20F2f62e0A5cC36B0DFAe771C442,solana:J5CCzBULFax899tcirb6wMbenQUd8whbaetG7EfSick5' \
    -H 'x-api-key: 42bb629272001ee1163ca0dbbbc07bcbb0ef57a57baf16c4b1d4672db4562c11'
  ```

  ```typescript TypeScript theme={null}
  // Check Solana account balance
  const balance = await fetch(
    'https://be.onebalance.io/api/v3/balances/aggregated-balance?account=solana:J5CCzBULFax899tcirb6wMbenQUd8whbaetG7EfSick5',
    {
      headers: { 'x-api-key': '42bb629272001ee1163ca0dbbbc07bcbb0ef57a57baf16c4b1d4672db4562c11' }
    }
  );

  const balanceData = await balance.json();
  console.log('Available assets:', balanceData.balanceByAggregatedAsset);
  ```
</CodeGroup>

## Quick Error Handling

<CodeGroup>
  ```typescript Validate Account theme={null}
  import { PublicKey } from '@solana/web3.js';

  // Ensure Solana address is valid before making requests
  try {
    new PublicKey(accountAddress);
  } catch (error) {
    throw new Error('Invalid Solana address format');
  }
  ```

  ```typescript Check Quote Status   theme={null}
  // Monitor transaction progress
  const statusResponse = await fetch(
    `https://be.onebalance.io/api/v3/status/get-execution-status?quoteId=${quote.id}`,
    { headers: { 'x-api-key': 'YOUR_API_KEY' } }
  );
  const status = await statusResponse.json();
  console.log('Transaction status:', status.status.status);
  ```
</CodeGroup>

## Next Steps

<CardGroup cols={2}>
  <Card title="Contract Calls" icon="code" href="/guides/solana/contract-calls">
    Use Solana assets to fund smart contract calls on EVM chains
  </Card>

  <Card title="Troubleshooting" icon="wrench" href="/guides/solana/troubleshooting">
    Common issues and solutions for Solana integration
  </Card>

  <Card title="API Reference" icon="book" href="/api-reference/quotes/get-quote-v3">
    Complete API documentation for v3 endpoints
  </Card>

  <Card title="Solana Overview" icon="info" href="/guides/solana/overview">
    Concepts and setup guide for Solana integration
  </Card>
</CardGroup>

<Note>
  For additional support, use the **Intercom chat widget** in the bottom right corner, join our [Discord](https://discord.com/invite/vHkw7rpdT8), or reach out via [support](mailto:support@onebalance.io).
</Note>
