A TypeScript wallet emulator that provides multiple operations on the Darklake DEX through a gRPC gateway API, including swap, add liquidity, and quote operations.
- Swap Operations: Execute token swaps through the gateway
- Add Liquidity: Add liquidity to pools and receive LP tokens
- Quote Operations: Get quotes for add liquidity operations
- Wallet Management: Load wallet from private key bytes
- Transaction Handling: Sign and broadcast transactions to Solana
- gRPC Integration: Modern gRPC-js implementation
- Multiple Networks: Support for devnet, testnet, and mainnet
- Modular Architecture: Clean separation of concerns with shared utilities
- Node.js 18+
- npm or yarn
- Solana CLI tools (optional, for key generation)
- gRPC server implementing the gateway.proto interface
- Install dependencies:
pnpm install- Build the project:
pnpm run build- Create a
.envfile with your configuration:
# Wallet Configuration
PRIVATE_KEY_BYTES="[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32]"
# gRPC Gateway Configuration
GATEWAY_HOST=localhost
GATEWAY_PORT=50051
# Swap Parameters
TOKEN_X_MINT="So11111111111111111111111111111111111111112" # SOL
TOKEN_Y_MINT="EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v" # USDC
INPUT_AMOUNT=1000
MIN_OUT=0
NETWORK=2- Create a private key:
# Generate a new keypair (optional)
solana-keygen new --outfile key.json
# Copy the array of bytes to PRIVATE_KEY_BYTES# Execute a token swap
pnpm run swap# Add liquidity to a pool
pnpm run add-liquidity
# Get a quote for adding liquidity
pnpm run quote-liquidity# Development mode (runs index.ts)
pnpm run dev
# Production mode
pnpm start
# Build only
pnpm run build- Load Wallet: The emulator loads a Solana keypair from the
PRIVATE_KEY_BYTESenvironment variable - Connect to gRPC Gateway: Establishes a gRPC connection to the gateway server
- Request Swap: Makes a gRPC call to the
Swapservice with swap parameters - Receive Transaction: Gets an unsigned Solana transaction from the gateway response
- Sign Transaction: Signs the transaction using the loaded wallet
- Submit Transaction: Sends the signed transaction via gRPC to the
SubmitSignedTransactionservice - Exit: The program exits after completion
| Variable | Description | Required | Default |
|---|---|---|---|
PRIVATE_KEY_BYTES |
JSON array of private key bytes | Yes | - |
GATEWAY_HOST |
gRPC gateway host address | Yes | localhost |
GATEWAY_PORT |
gRPC gateway port | Yes | 50051 |
TOKEN_X_MINT |
Token X mint address | Yes | - |
TOKEN_Y_MINT |
Token Y mint address | Yes | - |
INPUT_AMOUNT |
Amount of token X to swap | Yes | 1000 |
MIN_OUT |
Minimum output amount | Yes | 0 |
NETWORK |
Solana network (0=mainnet, 1=testnet, 2=devnet) | Yes | 2 |
The emulator expects a gRPC server implementing the following service (defined in proto/gateway.proto):
service SolanaGatewayService {
rpc CreateUnsignedTransaction(CreateUnsignedTransactionRequest) returns (CreateUnsignedTransactionResponse);
rpc SendSignedTransaction(SendSignedTransactionRequest) returns (SendSignedTransactionResponse);
}message CreateUnsignedTransactionRequest {
string user_address = 1;
string token_mint_x = 2;
string token_mint_y = 3;
uint64 amount_in = 4;
uint64 min_out = 5;
string tracking_id = 6;
Network network = 7;
bool is_swap_x_to_y = 8;
}message CreateUnsignedTransactionResponse {
string unsigned_transaction = 1; // Base64 encoded transaction
string order_id = 2;
}message SendSignedTransactionRequest {
string signed_transaction = 1; // Base64 encoded signed transaction
string order_id = 2;
}message SendSignedTransactionResponse {
bool success = 1;
}gateway-wallet/
├── src/
│ ├── index.ts # Main entry point with executeMain function
│ ├── swap.ts # Swap operations
│ ├── add-liquidity.ts # Add liquidity operations
│ ├── quote-add-liquidity.ts # Quote operations
│ ├── utils.ts # Shared utility functions
│ └── types.ts # TypeScript type definitions
├── proto/
│ └── darklake/
│ └── v1/
│ └── api.proto # gRPC service definition
├── package.json
├── tsconfig.json
└── README.md
- Keep your private key secure and never commit it to version control
- Use environment variables for sensitive configuration
- Consider using a dedicated wallet for testing operations
- Verify gRPC server endpoints before use
- Private key format error: Ensure
PRIVATE_KEY_BYTESis a valid JSON array of numbers - gRPC connection failed: Check your gateway host/port and ensure the gRPC server is running
- Transaction signing failed: Verify the private key is correct and has sufficient funds
- Invalid response format: Check that the gRPC server returns the expected message structure
- Network mismatch: Ensure the
NETWORKparameter matches your gateway configuration - Proto file not found: Ensure
proto/gateway.protoexists and is accessible
MIT