Skip to content

0xProject/0x-solana-example

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

7 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

0x Solana Swap Example

This TypeScript project demonstrates how to interact with the 0x Swap API for Solana to get a quote, build swap instructions, and send a swap transaction.

Example Output

➜  0x-solana-example git:(main) βœ— npm run index

> 0x-solana-example@1.0.0 index
> tsx src/index.ts

πŸ’° Quote received
βœ… Simulation succeeded
✍️ Transaction sent with signature: 3ofwWgzDzZRpPGNQTvAKSY4o3FMPT2y7fjCw6UtTAMTnvHj26TPaYVJWM88v28jNeey5CdX9sWGcVfPYfQNQsSBa
βœ… Transaction confirmed: https://solscan.io/tx/3ofwWgzDzZRpPGNQTvAKSY4o3FMPT2y7fjCw6UtTAMTnvHj26TPaYVJWM88v28jNeey5CdX9sWGcVfPYfQNQsSBa

What It Does

This script performs the following steps:

  1. Loads environment variables from .env (0x API key, private key, RPC URL).
  2. Fetches a swap quote from the 0x /swap-instructions endpoint.

If you have provided a private key:

  1. Builds and signs a transaction from the instructions.
  2. Simulates the transaction to catch errors before sending.
  3. Sends a transaction to the Solana blockchain to execute the swap.

Getting Started

1. Clone the Repository

git clone https://github.com/0xProject/0x-solana-example.git
cd 0x-solana-example

2. Install Dependencies

npm install

3. Create a .env File

Copy the example from .env.example into a new .env file:

Variable Name Description Required Default Value
ZEROEX_API_KEY API Key for use with 0x Solana API. βœ… None
PRIVATE_KEY Must be base58-encoded. ❌ Random generated
RPC_URL Valid Solana RPC HTTP endpoint. For example, get a free RPC key and endpoint from Helius. ❌ https://api.mainnet-beta.solana.com

4. Run the Example

npm run index

πŸ“ Notes

  • The amount and token mint addresses are hardcoded for simplicity (SOL β†’ USDC), but can be parameterized.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published