@admin-jigsaw/jigsaw-sdk
Version:
Returns predefined data for Jigsaw platform and exposes functionality to retrieve the necessary data
268 lines (267 loc) • 14 kB
JavaScript
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __generator = (this && this.__generator) || function (thisArg, body) {
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
function verb(n) { return function (v) { return step([n, v]); }; }
function step(op) {
if (f) throw new TypeError("Generator is already executing.");
while (g && (g = 0, op[0] && (_ = 0)), _) try {
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
if (y = 0, t) op = [op[0] & 2, t.value];
switch (op[0]) {
case 0: case 1: t = op; break;
case 4: _.label++; return { value: op[1], done: false };
case 5: _.label++; y = op[1]; op = [0]; continue;
case 7: op = _.ops.pop(); _.trys.pop(); continue;
default:
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
if (t[2]) _.ops.pop();
_.trys.pop(); continue;
}
op = body.call(thisArg, _);
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
}
};
import { beforeAll, describe, expect, it } from "vitest";
import { JigsawClient } from "../core";
import { COLLATERALS } from "../constants/coinsConstants";
import { PENDLE_STRATEGIES } from "../constants/strategiesConstants";
var client;
var holdingAddress = "0xB9b734bfed02D357FFFa8DBa0cd20E9E6C65CD26";
describe("JigsawClient Tests", function () {
var strategyAddress = PENDLE_STRATEGIES[COLLATERALS.LBTC.symbol].general.stratAddress;
beforeAll(function () {
client = new JigsawClient();
});
it("should get the current gas fee", function () { return __awaiter(void 0, void 0, void 0, function () {
var gasPrice;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, client.getCurrentGasFee()];
case 1:
gasPrice = _a.sent();
expect(gasPrice).toBeTruthy();
expect(parseFloat(gasPrice)).toBeGreaterThan(0);
return [2 /*return*/];
}
});
}); });
it("should return an empty array if the user has no strategies", function () { return __awaiter(void 0, void 0, void 0, function () {
var emptyHolding, strategies;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
emptyHolding = "0x0000000000000000000000000000000000000000";
return [4 /*yield*/, client.getUserStrategies(emptyHolding)];
case 1:
strategies = _a.sent();
expect(strategies).toBeInstanceOf(Array);
expect(strategies.length).toBe(0);
return [2 /*return*/];
}
});
}); });
it("should return the dictionary of strategies", function () {
var strategies = client.getAllStrategies();
expect(Array.isArray(strategies)).toBe(true);
expect(strategies.length).toBeGreaterThan(0);
});
it("should retrieve the strategy info for an existing strategy", function () { return __awaiter(void 0, void 0, void 0, function () {
var strategyInfo;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, client.getStrategyInfo(strategyAddress)];
case 1:
strategyInfo = _a.sent();
if (strategyInfo !== null) {
expect(strategyInfo).toBeTruthy();
expect(Array.isArray(strategyInfo)).toBe(true);
expect(strategyInfo.length).toBe(3);
}
else {
expect(strategyInfo).toBeNull();
}
return [2 /*return*/];
}
});
}); });
it("should return the user's strategies as an array of addresses", function () { return __awaiter(void 0, void 0, void 0, function () {
var strategies;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, client.getUserStrategies(holdingAddress)];
case 1:
strategies = _a.sent();
expect(strategies).toBeInstanceOf(Array);
if (strategies.length > 0) {
// Check that each strategy is an address
expect(strategies[0]).toMatch(/^0x[a-fA-F0-9]{40}$/);
}
return [2 /*return*/];
}
});
}); });
it("should return user's strategies filtered by asset symbol", function () { return __awaiter(void 0, void 0, void 0, function () {
var assetSymbol, strategies, getCollateralMethod_1;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
assetSymbol = COLLATERALS.wstETH.symbol;
return [4 /*yield*/, client.getUserStrategies(holdingAddress, assetSymbol)];
case 1:
strategies = _a.sent();
expect(strategies).toBeInstanceOf(Array);
if (strategies.length > 0) {
getCollateralMethod_1 = client.getCollateralByStrategy.bind(client);
expect(strategies.every(function (strategy) { var _a; return ((_a = getCollateralMethod_1(strategy)) === null || _a === void 0 ? void 0 : _a.symbol) === assetSymbol; })).toBe(true);
}
return [2 /*return*/];
}
});
}); });
it("should return an empty array when filtering by an incorrect asset symbol", function () { return __awaiter(void 0, void 0, void 0, function () {
var assetSymbol, strategies;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
assetSymbol = "NON_EXISTENT_ASSET";
return [4 /*yield*/, client.getUserStrategies(holdingAddress, assetSymbol)];
case 1:
strategies = _a.sent();
expect(strategies).toBeInstanceOf(Array);
expect(strategies.length).toBe(0);
return [2 /*return*/];
}
});
}); });
it("should return liquidation data for user's strategies", function () { return __awaiter(void 0, void 0, void 0, function () {
var result, error_1;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
_a.trys.push([0, 2, , 3]);
return [4 /*yield*/, client.getUserLiquidationInfo(holdingAddress, COLLATERALS.LBTC.symbol)];
case 1:
result = _a.sent();
expect(result).toBeTruthy();
expect(result.strategies).toBeInstanceOf(Array);
expect(result.callData).toBeInstanceOf(Array);
expect(result.strategies.length).toBe(result.callData.length);
// If there are results, check that they match our expectations
if (result.strategies.length > 0) {
// Verify strategies are addresses
expect(result.strategies[0]).toMatch(/^0x[a-fA-F0-9]{40}$/);
// Verify that each callData is a string
expect(typeof result.callData[0]).toBe("string");
}
return [3 /*break*/, 3];
case 2:
error_1 = _a.sent();
console.error("Error fetching liquidation info:", error_1);
expect(error_1).toBeFalsy(); // Ensures test fails if there's an error
return [3 /*break*/, 3];
case 3: return [2 /*return*/];
}
});
}); });
it("should return empty arrays when no matching strategies found", function () { return __awaiter(void 0, void 0, void 0, function () {
var nonExistentSymbol, result;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
nonExistentSymbol = "NON_EXISTENT_ASSET";
return [4 /*yield*/, client.getUserLiquidationInfo(holdingAddress, nonExistentSymbol)];
case 1:
result = _a.sent();
expect(result).toEqual({ strategies: [], callData: [] });
return [2 /*return*/];
}
});
}); });
// New test to verify the getCollateralByStrategy helper method
it("should correctly identify collateral for a strategy", function () {
// Using TypeScript type assertion to access private method
var getCollateralMethod = client.getCollateralByStrategy.bind(client);
// Test an AAVE strategy
var aaveStrategy = "0xaE7b7A1c6C4d859e19301ccAc2C6eD28A4C51288";
var aaveCollateral = getCollateralMethod(aaveStrategy);
expect(aaveCollateral).not.toBeNull();
expect(aaveCollateral.symbol).toBe("WETH");
// Test a Pendle strategy with the new structure
var pendleStrategy = PENDLE_STRATEGIES[COLLATERALS.rswETH.symbol].general.stratAddress;
var pendleCollateral = getCollateralMethod(pendleStrategy);
expect(pendleCollateral).not.toBeNull();
expect(pendleCollateral.symbol).toBe("rswETH");
});
});
describe("JigsawClient Error Handling Tests", function () {
var client;
var invalidHolding = "0x0000000000000000000000000000000000000000";
var invalidStrategy = "0x0000000000000000000000000000000000000000";
beforeAll(function () {
client = new JigsawClient();
});
it("should return an empty array when failing to fetch user strategies", function () { return __awaiter(void 0, void 0, void 0, function () {
var strategies;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, client.getUserStrategies(invalidHolding)];
case 1:
strategies = _a.sent();
expect(strategies).toBeInstanceOf(Array);
expect(strategies.length).toBe(0); // Instead of expecting an error, we expect an empty array
return [2 /*return*/];
}
});
}); });
it("should return null when failing to fetch liquidation info", function () { return __awaiter(void 0, void 0, void 0, function () {
var liquidationInfo;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, client.getUserLiquidationInfo(invalidHolding, invalidStrategy)];
case 1:
liquidationInfo = _a.sent();
expect(liquidationInfo).toBeNull(); // Instead of an error, return null
return [2 /*return*/];
}
});
}); });
it("should return null when failing to fetch strategy info", function () { return __awaiter(void 0, void 0, void 0, function () {
var strategyInfo;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, client.getStrategyInfo(invalidStrategy)];
case 1:
strategyInfo = _a.sent();
expect(strategyInfo).toStrictEqual([BigInt(0), false, false]);
return [2 /*return*/];
}
});
}); });
it("should return '0' when failing to fetch gas price", function () { return __awaiter(void 0, void 0, void 0, function () {
var brokenClient, gasPrice;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
brokenClient = new JigsawClient("https://invalid-rpc-url.com");
return [4 /*yield*/, brokenClient.getCurrentGasFee()];
case 1:
gasPrice = _a.sent();
expect(gasPrice).toBe("0"); // Ensures it returns "0" instead of throwing an error
return [2 /*return*/];
}
});
}); });
});