UNPKG

pumpfun-swap-sdk

Version:

SDK for interacting with the PumpFun AMM on Solana

71 lines (70 loc) 4.68 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const chai = require('chai'); const { expect } = chai; const bn_js_1 = __importDefault(require("bn.js")); const sell_1 = require("../sdk/sell"); describe('sellBaseInput', () => { it('should compute final quote and minQuote correctly with typical inputs', () => { // Example pool reserves const baseReserve = new bn_js_1.default(1000000); // base tokens in pool const quoteReserve = new bn_js_1.default(2000000); // quote tokens in pool // The user wants to sell 50,000 base tokens const base = new bn_js_1.default(50000); // Slippage = 1% => the user will accept at least 99% of finalQuote const slippage = 1; // Fees (in BPS) // e.g. 30 => 0.30%, 20 => 0.20% const lpFeeBps = new bn_js_1.default(30); const protocolFeeBps = new bn_js_1.default(20); const result = (0, sell_1.sellBaseInputInternal)(base, slippage, baseReserve, quoteReserve, lpFeeBps, protocolFeeBps); console.log('Final quote received:', result.uiQuote.toString()); console.log('Min quote after slippage:', result.minQuote.toString()); // Replace these placeholder values with the actual results once you confirm them offline: // For example, if you do the math manually or from a reference, set them here: const expectedFinalQuote = new bn_js_1.default(94761); // Example placeholder const expectedMinQuote = new bn_js_1.default(93813); // Example placeholder expect(result.uiQuote.toString()).to.equal(expectedFinalQuote.toString(), 'Incorrect final quote'); expect(result.minQuote.toString()).to.equal(expectedMinQuote.toString(), 'Incorrect min quote'); }); it("should throw an error if 'base' is zero", () => { const base = new bn_js_1.default(0); const baseReserve = new bn_js_1.default(1000000); const quoteReserve = new bn_js_1.default(2000000); const slippage = 1; const lpFeeBps = new bn_js_1.default(30); const protocolFeeBps = new bn_js_1.default(20); expect(() => (0, sell_1.sellBaseInputInternal)(base, slippage, baseReserve, quoteReserve, lpFeeBps, protocolFeeBps)).to.throw("Invalid input: 'base' (base_amount_in) cannot be zero."); }); it("should throw an error if 'baseReserve' or 'quoteReserve' is zero", () => { const slippage = 1; const lpFeeBps = new bn_js_1.default(30); const protocolFeeBps = new bn_js_1.default(20); // baseReserve = 0 expect(() => (0, sell_1.sellBaseInputInternal)(new bn_js_1.default(1000), slippage, new bn_js_1.default(0), new bn_js_1.default(2000000), lpFeeBps, protocolFeeBps)).to.throw("Invalid input: 'baseReserve' or 'quoteReserve' cannot be zero."); // quoteReserve = 0 expect(() => (0, sell_1.sellBaseInputInternal)(new bn_js_1.default(1000), slippage, new bn_js_1.default(1000000), new bn_js_1.default(0), lpFeeBps, protocolFeeBps)).to.throw("Invalid input: 'baseReserve' or 'quoteReserve' cannot be zero."); }); it('should throw an error if lpFeeBps or protocolFeeBps is negative', () => { const base = new bn_js_1.default(10000); const baseReserve = new bn_js_1.default(1000000); const quoteReserve = new bn_js_1.default(2000000); const slippage = 1; expect(() => (0, sell_1.sellBaseInputInternal)(base, slippage, baseReserve, quoteReserve, new bn_js_1.default(-1), new bn_js_1.default(20))).to.throw('Fee basis points cannot be negative.'); expect(() => (0, sell_1.sellBaseInputInternal)(base, slippage, baseReserve, quoteReserve, new bn_js_1.default(30), new bn_js_1.default(-5))).to.throw('Fee basis points cannot be negative.'); }); it('should throw an error if fees exceed total output (finalQuote negative)', () => { // We want quoteAmountOut > 0 but finalQuote < 0 after subtracting fees. const base = new bn_js_1.default(1); const baseReserve = new bn_js_1.default(1); const quoteReserve = new bn_js_1.default(2); const slippage = 1; // Large fees (90% + 20% = 110% total) will exceed quoteAmountOut=1 const lpFeeBps = new bn_js_1.default(9000); // 90% const protocolFeeBps = new bn_js_1.default(2000); // 20% expect(() => (0, sell_1.sellBaseInputInternal)(base, slippage, baseReserve, quoteReserve, lpFeeBps, protocolFeeBps)).to.throw('Fees exceed total output; final quote is negative.'); }); });