Skip to content

A TypeScript example project demonstrating how to interact with the Deriverse DEX on Solana. This example shows how to connect to the Deriverse exchange, manage client accounts, deposit funds and execute spot trading operations.

Notifications You must be signed in to change notification settings

deriverse/kit-example

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Deriverse Kit Example

A TypeScript example project demonstrating how to interact with the Deriverse decentralized trading protocol on Solana. This example shows how to connect to the Deriverse exchange, manage client accounts, deposit funds, and execute spot trading operations.

Overview

This project uses the @deriverse/kit library to:

  • Initialize and connect to the Deriverse trading engine
  • Set up client accounts for trading
  • Deposit USDC tokens for trading
  • Place and cancel spot orders for SOL/USDC trading pairs
  • Interact with Solana's devnet environment

Prerequisites

  • Node.js (v20 or higher)
  • TypeScript
  • A Solana wallet keypair file
  • Access to Solana devnet

Installation

  1. Clone this repository:
git clone https://github.com/deriverse/kit-example.git
cd kit-example
  1. Install dependencies:
npm install
  1. Set up environment variables by copying .env.example to .env:
cp .env.example .env

Environment Configuration

Create a .env file with the following variables:

KEYPAIR_FILENAME=../keys/new_user.json
PROGRAM_ID=Drvrseg8AQLP8B96DBGmHRjFGviFNYTkHueY9g3k27Gu
RPC_HTTP=https://api.devnet.solana.com
RPC_WS=wss://api.devnet.solana.com
VERSION=12
TOKEN_MINT_A=9pan9bMn5HatX4EJdBwg9VgCa7Uz5HL8N1m5D3NdXejP #Wrapped SOL (Token 2022)
TOKEN_MINT_B=A2Pz6rVyXuadFkKnhMXd1w9xgSrZd8m8sEGpuGuyFhaj #Fake Deriverse Devnet USDC (SPL Token)

Environment Variables Explained

  • KEYPAIR_FILENAME: Path to your Solana wallet keypair JSON file
  • PROGRAM_ID: Deriverse program ID on Solana
  • RPC_HTTP: Solana RPC HTTP endpoint (devnet)
  • RPC_WS: Solana RPC WebSocket endpoint (devnet)
  • VERSION: Deriverse Protocol Data Structure version
  • TOKEN_MINT_A: Wrapped SOL token mint address (Asset token)
  • TOKEN_MINT_B: USDC token mint address (Currency token)

Wallet Setup

  1. Generate a new Solana keypair or use an existing one:
solana-keygen new --outfile keys/new_user.json
  1. Fund your wallet with devnet SOL:
solana airdrop 2 <your-wallet-address> --url devnet
  1. Make sure you have some devnet USDC tokens for trading.

Building and Running

  1. Build the TypeScript project:
npm run build
  1. Run the example:
node build/index.js

What the Example Does

The example script performs the following operations:

  1. Initialize Engine: Connects to the Deriverse trading protocol
  2. Client Setup: Creates or loads an existing client account
  3. Token Validation: Validates the trading pair (SOL/USDC)
  4. Fund Deposit: Deposits 100 USDC if the account balance is insufficient
  5. Order Management:
    • Cancels existing bid orders (if any)
    • Places a new bid order at 99% of market price

Trading Logic

  • Asset Token: Wrapped SOL (TOKEN_MINT_A)
  • Currency Token: USDC (TOKEN_MINT_B)
  • Order Type: Limit orders
  • Strategy: Places bid orders 1% below market price
  • Quantity: Calculates quantity based on $100 worth of the asset

Code Structure

├── src/
│   └── index.ts          # Main application logic
├── package.json          # Project dependencies and scripts
├── tsconfig.json         # TypeScript configuration
└── .env.example          # Environment variables template

Dependencies

  • @deriverse/kit: Core library for Deriverse protocol interaction
  • @solana/kit: Solana Web3.js v2 toolkit
  • dotenv: Environment variable management
  • TypeScript: Static typing for JavaScript

Network Information

This example is configured for Solana Devnet:

  • RPC: https://api.devnet.solana.com
  • WebSocket: wss://api.devnet.solana.com

Troubleshooting

Common Issues

  1. Invalid Token Mint: Ensure TOKEN_MINT_A and TOKEN_MINT_B are valid devnet addresses
  2. Insufficient Funds: Make sure your wallet has enough SOL for transaction fees
  3. Keypair Loading Error: Check the KEYPAIR_FILENAME path and file format

Debug Tips

  • Check console output for Client ID and transaction signatures
  • Monitor your wallet and Deriverse Account balances before and after running the script
  • Verify environment variables are correctly set

Useful Resources

About

A TypeScript example project demonstrating how to interact with the Deriverse DEX on Solana. This example shows how to connect to the Deriverse exchange, manage client accounts, deposit funds and execute spot trading operations.

Resources

Stars

Watchers

Forks

Contributors 2

  •  
  •