@kamino-finance/kliquidity-sdk
Version:
Typescript SDK for interacting with the Kamino Liquidity (kliquidity) protocol
210 lines • 9.23 kB
JavaScript
"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 });
exports.Whirlpool = void 0;
/* eslint-disable @typescript-eslint/no-unused-vars */
const kit_1 = require("@solana/kit");
/* eslint-enable @typescript-eslint/no-unused-vars */
const bn_js_1 = __importDefault(require("bn.js")); // eslint-disable-line @typescript-eslint/no-unused-vars
const borsh = __importStar(require("@coral-xyz/borsh")); // eslint-disable-line @typescript-eslint/no-unused-vars
const utils_1 = require("../utils"); // eslint-disable-line @typescript-eslint/no-unused-vars
const types = __importStar(require("../types")); // eslint-disable-line @typescript-eslint/no-unused-vars
const programId_1 = require("../programId");
class Whirlpool {
whirlpoolsConfig;
whirlpoolBump;
tickSpacing;
tickSpacingSeed;
feeRate;
protocolFeeRate;
liquidity;
sqrtPrice;
tickCurrentIndex;
protocolFeeOwedA;
protocolFeeOwedB;
tokenMintA;
tokenVaultA;
feeGrowthGlobalA;
tokenMintB;
tokenVaultB;
feeGrowthGlobalB;
rewardLastUpdatedTimestamp;
rewardInfos;
static discriminator = Buffer.from([
63, 149, 209, 12, 225, 128, 99, 9,
]);
static layout = borsh.struct([
(0, utils_1.borshAddress)("whirlpoolsConfig"),
borsh.array(borsh.u8(), 1, "whirlpoolBump"),
borsh.u16("tickSpacing"),
borsh.array(borsh.u8(), 2, "tickSpacingSeed"),
borsh.u16("feeRate"),
borsh.u16("protocolFeeRate"),
borsh.u128("liquidity"),
borsh.u128("sqrtPrice"),
borsh.i32("tickCurrentIndex"),
borsh.u64("protocolFeeOwedA"),
borsh.u64("protocolFeeOwedB"),
(0, utils_1.borshAddress)("tokenMintA"),
(0, utils_1.borshAddress)("tokenVaultA"),
borsh.u128("feeGrowthGlobalA"),
(0, utils_1.borshAddress)("tokenMintB"),
(0, utils_1.borshAddress)("tokenVaultB"),
borsh.u128("feeGrowthGlobalB"),
borsh.u64("rewardLastUpdatedTimestamp"),
borsh.array(types.WhirlpoolRewardInfo.layout(), 3, "rewardInfos"),
]);
constructor(fields) {
this.whirlpoolsConfig = fields.whirlpoolsConfig;
this.whirlpoolBump = fields.whirlpoolBump;
this.tickSpacing = fields.tickSpacing;
this.tickSpacingSeed = fields.tickSpacingSeed;
this.feeRate = fields.feeRate;
this.protocolFeeRate = fields.protocolFeeRate;
this.liquidity = fields.liquidity;
this.sqrtPrice = fields.sqrtPrice;
this.tickCurrentIndex = fields.tickCurrentIndex;
this.protocolFeeOwedA = fields.protocolFeeOwedA;
this.protocolFeeOwedB = fields.protocolFeeOwedB;
this.tokenMintA = fields.tokenMintA;
this.tokenVaultA = fields.tokenVaultA;
this.feeGrowthGlobalA = fields.feeGrowthGlobalA;
this.tokenMintB = fields.tokenMintB;
this.tokenVaultB = fields.tokenVaultB;
this.feeGrowthGlobalB = fields.feeGrowthGlobalB;
this.rewardLastUpdatedTimestamp = fields.rewardLastUpdatedTimestamp;
this.rewardInfos = fields.rewardInfos.map((item) => new types.WhirlpoolRewardInfo({ ...item }));
}
static async fetch(rpc, address, programId = programId_1.PROGRAM_ID) {
const info = await (0, kit_1.fetchEncodedAccount)(rpc, address);
if (!info.exists) {
return null;
}
if (info.programAddress !== programId) {
throw new Error("account doesn't belong to this program");
}
return this.decode(Buffer.from(info.data));
}
static async fetchMultiple(rpc, addresses, programId = programId_1.PROGRAM_ID) {
const infos = await (0, kit_1.fetchEncodedAccounts)(rpc, addresses);
return infos.map((info) => {
if (!info.exists) {
return null;
}
if (info.programAddress !== programId) {
throw new Error("account doesn't belong to this program");
}
return this.decode(Buffer.from(info.data));
});
}
static decode(data) {
if (!data.slice(0, 8).equals(Whirlpool.discriminator)) {
throw new Error("invalid account discriminator");
}
const dec = Whirlpool.layout.decode(data.slice(8));
return new Whirlpool({
whirlpoolsConfig: dec.whirlpoolsConfig,
whirlpoolBump: dec.whirlpoolBump,
tickSpacing: dec.tickSpacing,
tickSpacingSeed: dec.tickSpacingSeed,
feeRate: dec.feeRate,
protocolFeeRate: dec.protocolFeeRate,
liquidity: dec.liquidity,
sqrtPrice: dec.sqrtPrice,
tickCurrentIndex: dec.tickCurrentIndex,
protocolFeeOwedA: dec.protocolFeeOwedA,
protocolFeeOwedB: dec.protocolFeeOwedB,
tokenMintA: dec.tokenMintA,
tokenVaultA: dec.tokenVaultA,
feeGrowthGlobalA: dec.feeGrowthGlobalA,
tokenMintB: dec.tokenMintB,
tokenVaultB: dec.tokenVaultB,
feeGrowthGlobalB: dec.feeGrowthGlobalB,
rewardLastUpdatedTimestamp: dec.rewardLastUpdatedTimestamp,
rewardInfos: dec.rewardInfos.map((item /* eslint-disable-line @typescript-eslint/no-explicit-any */) => types.WhirlpoolRewardInfo.fromDecoded(item)),
});
}
toJSON() {
return {
whirlpoolsConfig: this.whirlpoolsConfig,
whirlpoolBump: this.whirlpoolBump,
tickSpacing: this.tickSpacing,
tickSpacingSeed: this.tickSpacingSeed,
feeRate: this.feeRate,
protocolFeeRate: this.protocolFeeRate,
liquidity: this.liquidity.toString(),
sqrtPrice: this.sqrtPrice.toString(),
tickCurrentIndex: this.tickCurrentIndex,
protocolFeeOwedA: this.protocolFeeOwedA.toString(),
protocolFeeOwedB: this.protocolFeeOwedB.toString(),
tokenMintA: this.tokenMintA,
tokenVaultA: this.tokenVaultA,
feeGrowthGlobalA: this.feeGrowthGlobalA.toString(),
tokenMintB: this.tokenMintB,
tokenVaultB: this.tokenVaultB,
feeGrowthGlobalB: this.feeGrowthGlobalB.toString(),
rewardLastUpdatedTimestamp: this.rewardLastUpdatedTimestamp.toString(),
rewardInfos: this.rewardInfos.map((item) => item.toJSON()),
};
}
static fromJSON(obj) {
return new Whirlpool({
whirlpoolsConfig: (0, kit_1.address)(obj.whirlpoolsConfig),
whirlpoolBump: obj.whirlpoolBump,
tickSpacing: obj.tickSpacing,
tickSpacingSeed: obj.tickSpacingSeed,
feeRate: obj.feeRate,
protocolFeeRate: obj.protocolFeeRate,
liquidity: new bn_js_1.default(obj.liquidity),
sqrtPrice: new bn_js_1.default(obj.sqrtPrice),
tickCurrentIndex: obj.tickCurrentIndex,
protocolFeeOwedA: new bn_js_1.default(obj.protocolFeeOwedA),
protocolFeeOwedB: new bn_js_1.default(obj.protocolFeeOwedB),
tokenMintA: (0, kit_1.address)(obj.tokenMintA),
tokenVaultA: (0, kit_1.address)(obj.tokenVaultA),
feeGrowthGlobalA: new bn_js_1.default(obj.feeGrowthGlobalA),
tokenMintB: (0, kit_1.address)(obj.tokenMintB),
tokenVaultB: (0, kit_1.address)(obj.tokenVaultB),
feeGrowthGlobalB: new bn_js_1.default(obj.feeGrowthGlobalB),
rewardLastUpdatedTimestamp: new bn_js_1.default(obj.rewardLastUpdatedTimestamp),
rewardInfos: obj.rewardInfos.map((item) => types.WhirlpoolRewardInfo.fromJSON(item)),
});
}
}
exports.Whirlpool = Whirlpool;
//# sourceMappingURL=Whirlpool.js.map