ZetaClientWrapper is a Node.js module that provides a convenient interface for interacting with the Zeta Markets SDK. It simplifies the process of initializing the client, managing connections, and executing trades on the Solana blockchain.
Before you begin, ensure you have met the following requirements:
- Node.js (v14 or later)
- npm (v6 or later)
- A Solana wallet with some SOL for transaction fees
- Access to a Solana RPC endpoint
- A Helius RPC URL for priority fees (optional)
-
Clone the repository:
git clone https://github.com/yourusername/zeta-client-wrapper.git -
Navigate to the project directory:
cd zeta-client-wrapper -
Install the dependencies:
npm install
-
Create a
.envfile in the root directory of the project. -
Add the following environment variables to the
.envfile:RPC_ENDPOINT_1=your_primary_rpc_endpoint RPC_ENDPOINT_2=your_secondary_rpc_endpoint (optional) RPC_WS_ENDPOINT_2=your_secondary_websocket_endpoint (optional) RPC_ENDPOINT_3=your_additional_rpc_endpoint (optional) RPC_WS_ENDPOINT_3=your_additional_websocket_endpoint (optional) HELIUS_RPC=your_helius_rpc_url KEYPAIR_FILE_PATH=path_to_your_solana_keypair_file REDIS_HOST=your_redis_host REDIS_PORT=your_redis_port REDIS_PASSWORD=your_redis_passwordReplace the placeholders with your actual values.
Note: The RPC_ENDPOINT_1 is required, while RPC_ENDPOINT_2, RPC_WS_ENDPOINT_2, RPC_ENDPOINT_3, and RPC_WS_ENDPOINT_3 are optional additional connections.
The ZetaClientWrapper supports multiple RPC connections, with the primary connection being mandatory and additional connections being optional. Here's how to use it:
import { ZetaClientWrapper } from './zeta-api-v4.js';
async function main() {
const zetaClient = new ZetaClientWrapper();
try {
// Initialize the client
await zetaClient.initialize();
// The primary connection (RPC_ENDPOINT_1) is now set up
console.log('Primary connection established');
// Check if additional connections are available
if (zetaClient.connection_2) {
console.log('Additional connection 2 is available');
}
if (zetaClient.connection_3) {
console.log('Additional connection 3 is available');
}
// Open a long position
const txid = await zetaClient.openPositionWithTPSLVersioned('long');
console.log(`Position opened. Transaction ID: ${txid}`);
// Get current position
const position = await zetaClient.getPosition(zetaClient.activeMarket);
console.log('Current position:', position);
// Cancel all orders
await zetaClient.cancelAllOrders(zetaClient.activeMarket);
console.log('All orders cancelled');
} catch (error) {
console.error('Error:', error);
}
}
main();In this example, the initialize() method sets up all available connections based on the provided environment variables. The primary connection (RPC_ENDPOINT_1) is required, while the additional connections (RPC_ENDPOINT_2 and RPC_ENDPOINT_3) are optional.
The optional connections can be used for specific purposes like load balancing, failover, or dedicated operations, providing flexibility in your application architecture.
initialize(): Initializes the ZetaClientWrapper, setting up connections and loading the exchange. It establishes the primary connection and optional additional connections if the corresponding environment variables are set.openPositionWithTPSLVersioned(direction, marketIndex): Opens a position with take-profit and stop-loss orders.getPosition(marketIndex): Retrieves the current position for the specified market.cancelAllOrders(marketIndex): Cancels all orders for the specified market.updateSettings(newSettings): Updates the trading settings (leverage, take-profit, stop-loss).fetchSettings(): Retrieves the current trading settings from Redis.
The ZetaClientWrapper uses a logger to record errors and important information. Make sure to handle errors appropriately in your application:
try {
// Your code here
} catch (error) {
logger.error('An error occurred:', error);
}Contributions to the ZetaClientWrapper are welcome. Please follow these steps:
- Fork the repository
- Create a new branch (
git checkout -b feature/your-feature-name) - Make your changes
- Commit your changes (
git commit -am 'Add some feature') - Push to the branch (
git push origin feature/your-feature-name) - Create a new Pull Request
MIT License
Copyright (c) 2024 BOP BOP BEEP BOOP BOP
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.