UNPKG

orca-clmm-agent

Version:

Orca Whirlpool clmm library for automated position management

90 lines (89 loc) 3.98 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const dotenv = __importStar(require("dotenv")); const orca_1 = require("../orca"); const solana_1 = require("../solana"); const solana_2 = require("../solana"); const bs58_1 = __importDefault(require("bs58")); const kit_1 = require("@solana/kit"); const okx_1 = require("../okx"); dotenv.config(); const rpcUrl = process.env.RPC_URL || "https://api.mainnet-beta.solana.com"; console.log(`Using RPC URL: ${rpcUrl}`); process.on("unhandledRejection", (reason, promise) => { console.error("Unhandled Rejection at:", promise, "reason:", reason); }); const rpc = (0, kit_1.createSolanaRpc)((0, kit_1.mainnet)(rpcUrl)); // --------------------------------------------------------------------------- // Example: Calculate divergence (impermanent) loss for a hypothetical position // --------------------------------------------------------------------------- // Parameters: // p - Current price (tokenB per tokenA) // p_i - Initial price when the position was opened // p_a - Lower price bound of the range // p_b - Upper price bound of the range // depositA - Amount of token A deposited // depositB - Amount of token B deposited // // Here we assume the current price moved to 1.2, it was 1.0 at entry, // the position range spans from 0.8 to 1.5 and we originally deposited // 1000 units of token A and 1200 units of token B. const dlExample = (0, orca_1.divergenceLoss)(1, 1.0, 0.8, 1.5, 1000, 0.3586); //console.log("Divergence loss example:", dlExample); async function main() { const bytes = await (0, solana_2.loadKeypairFromFile)("./examples/keypair.json"); const byteArray = new Uint8Array(bytes); const privateKey = bs58_1.default.encode(byteArray); const wallet = await (0, kit_1.createKeyPairSignerFromBytes)(bytes); const balances = await (0, solana_1.fetchNonZeroTokenBalances)(wallet.address); const USDCBalance = balances.find((b) => b.address === solana_1.USDC_MINT_ADDRESS); if (!USDCBalance) throw new Error("No USDC balance found"); const okxSwapInstruction = await (0, okx_1.getOkxSwapInstruction)({ chainIndex: "501", // SOL chainId: "1", amount: USDCBalance.balance.uiAmount.toString(), fromTokenAddress: solana_1.USDC_MINT_ADDRESS, toTokenAddress: "", slippage: "100", userWalletAddress: wallet.address, }); console.log("OKX Swap Instruction:", okxSwapInstruction); } main();