@marinade.finance/kamino-sdk
Version:
337 lines • 15.2 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 (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__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.PoolState = void 0;
const web3_js_1 = require("@solana/web3.js");
const bn_js_1 = __importDefault(require("bn.js")); // eslint-disable-line @typescript-eslint/no-unused-vars
const borsh = __importStar(require("@project-serum/borsh")); // 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");
/**
* The pool state
*
* PDA of `[POOL_SEED, config, token_mint_0, token_mint_1]`
*
*/
class PoolState {
/** Bump to identify PDA */
bump;
ammConfig;
owner;
/** Token pair of the pool, where token_mint_0 address < token_mint_1 address */
tokenMint0;
tokenMint1;
/** Token pair vault */
tokenVault0;
tokenVault1;
/** observation account key */
observationKey;
/** mint0 and mint1 decimals */
mintDecimals0;
mintDecimals1;
/** The minimum number of ticks between initialized ticks */
tickSpacing;
/** The currently in range liquidity available to the pool. */
liquidity;
/** The current price of the pool as a sqrt(token_1/token_0) Q64.64 value */
sqrtPriceX64;
/** The current tick of the pool, i.e. according to the last tick transition that was run. */
tickCurrent;
/** the most-recently updated index of the observations array */
observationIndex;
observationUpdateDuration;
/**
* The fee growth as a Q64.64 number, i.e. fees of token_0 and token_1 collected per
* unit of liquidity for the entire life of the pool.
*/
feeGrowthGlobal0X64;
feeGrowthGlobal1X64;
/** The amounts of token_0 and token_1 that are owed to the protocol. */
protocolFeesToken0;
protocolFeesToken1;
/** The amounts in and out of swap token_0 and token_1 */
swapInAmountToken0;
swapOutAmountToken1;
swapInAmountToken1;
swapOutAmountToken0;
/**
* Bitwise representation of the state of the pool
* bit0, 1: disable open position and increase liquidity, 0: normal
* bit1, 1: disable decrease liquidity, 0: normal
* bit2, 1: disable collect fee, 0: normal
* bit3, 1: disable collect reward, 0: normal
* bit4, 1: disable swap, 0: normal
*/
status;
/** Leave blank for future use */
padding;
rewardInfos;
/** Packed initialized tick array state */
tickArrayBitmap;
/** except protocol_fee and fund_fee */
totalFeesToken0;
/** except protocol_fee and fund_fee */
totalFeesClaimedToken0;
totalFeesToken1;
totalFeesClaimedToken1;
fundFeesToken0;
fundFeesToken1;
openTime;
padding1;
padding2;
static discriminator = Buffer.from([247, 237, 227, 245, 215, 195, 222, 70]);
static layout = borsh.struct([
borsh.u8('bump'),
borsh.publicKey('ammConfig'),
borsh.publicKey('owner'),
borsh.publicKey('tokenMint0'),
borsh.publicKey('tokenMint1'),
borsh.publicKey('tokenVault0'),
borsh.publicKey('tokenVault1'),
borsh.publicKey('observationKey'),
borsh.u8('mintDecimals0'),
borsh.u8('mintDecimals1'),
borsh.u16('tickSpacing'),
borsh.u128('liquidity'),
borsh.u128('sqrtPriceX64'),
borsh.i32('tickCurrent'),
borsh.u16('observationIndex'),
borsh.u16('observationUpdateDuration'),
borsh.u128('feeGrowthGlobal0X64'),
borsh.u128('feeGrowthGlobal1X64'),
borsh.u64('protocolFeesToken0'),
borsh.u64('protocolFeesToken1'),
borsh.u128('swapInAmountToken0'),
borsh.u128('swapOutAmountToken1'),
borsh.u128('swapInAmountToken1'),
borsh.u128('swapOutAmountToken0'),
borsh.u8('status'),
borsh.array(borsh.u8(), 7, 'padding'),
borsh.array(types.RewardInfo.layout(), 3, 'rewardInfos'),
borsh.array(borsh.u64(), 16, 'tickArrayBitmap'),
borsh.u64('totalFeesToken0'),
borsh.u64('totalFeesClaimedToken0'),
borsh.u64('totalFeesToken1'),
borsh.u64('totalFeesClaimedToken1'),
borsh.u64('fundFeesToken0'),
borsh.u64('fundFeesToken1'),
borsh.u64('openTime'),
borsh.array(borsh.u64(), 25, 'padding1'),
borsh.array(borsh.u64(), 32, 'padding2'),
]);
constructor(fields) {
this.bump = fields.bump;
this.ammConfig = fields.ammConfig;
this.owner = fields.owner;
this.tokenMint0 = fields.tokenMint0;
this.tokenMint1 = fields.tokenMint1;
this.tokenVault0 = fields.tokenVault0;
this.tokenVault1 = fields.tokenVault1;
this.observationKey = fields.observationKey;
this.mintDecimals0 = fields.mintDecimals0;
this.mintDecimals1 = fields.mintDecimals1;
this.tickSpacing = fields.tickSpacing;
this.liquidity = fields.liquidity;
this.sqrtPriceX64 = fields.sqrtPriceX64;
this.tickCurrent = fields.tickCurrent;
this.observationIndex = fields.observationIndex;
this.observationUpdateDuration = fields.observationUpdateDuration;
this.feeGrowthGlobal0X64 = fields.feeGrowthGlobal0X64;
this.feeGrowthGlobal1X64 = fields.feeGrowthGlobal1X64;
this.protocolFeesToken0 = fields.protocolFeesToken0;
this.protocolFeesToken1 = fields.protocolFeesToken1;
this.swapInAmountToken0 = fields.swapInAmountToken0;
this.swapOutAmountToken1 = fields.swapOutAmountToken1;
this.swapInAmountToken1 = fields.swapInAmountToken1;
this.swapOutAmountToken0 = fields.swapOutAmountToken0;
this.status = fields.status;
this.padding = fields.padding;
this.rewardInfos = fields.rewardInfos.map((item) => new types.RewardInfo({ ...item }));
this.tickArrayBitmap = fields.tickArrayBitmap;
this.totalFeesToken0 = fields.totalFeesToken0;
this.totalFeesClaimedToken0 = fields.totalFeesClaimedToken0;
this.totalFeesToken1 = fields.totalFeesToken1;
this.totalFeesClaimedToken1 = fields.totalFeesClaimedToken1;
this.fundFeesToken0 = fields.fundFeesToken0;
this.fundFeesToken1 = fields.fundFeesToken1;
this.openTime = fields.openTime;
this.padding1 = fields.padding1;
this.padding2 = fields.padding2;
}
static async fetch(c, address) {
const info = await c.getAccountInfo(address);
if (info === null) {
return null;
}
if (!info.owner.equals(programId_1.PROGRAM_ID)) {
throw new Error("account doesn't belong to this program");
}
return this.decode(info.data);
}
static async fetchMultiple(c, addresses) {
const infos = await c.getMultipleAccountsInfo(addresses);
return infos.map((info) => {
if (info === null) {
return null;
}
if (!info.owner.equals(programId_1.PROGRAM_ID)) {
throw new Error("account doesn't belong to this program");
}
return this.decode(info.data);
});
}
static decode(data) {
if (!data.slice(0, 8).equals(PoolState.discriminator)) {
throw new Error('invalid account discriminator');
}
const dec = PoolState.layout.decode(data.slice(8));
return new PoolState({
bump: dec.bump,
ammConfig: dec.ammConfig,
owner: dec.owner,
tokenMint0: dec.tokenMint0,
tokenMint1: dec.tokenMint1,
tokenVault0: dec.tokenVault0,
tokenVault1: dec.tokenVault1,
observationKey: dec.observationKey,
mintDecimals0: dec.mintDecimals0,
mintDecimals1: dec.mintDecimals1,
tickSpacing: dec.tickSpacing,
liquidity: dec.liquidity,
sqrtPriceX64: dec.sqrtPriceX64,
tickCurrent: dec.tickCurrent,
observationIndex: dec.observationIndex,
observationUpdateDuration: dec.observationUpdateDuration,
feeGrowthGlobal0X64: dec.feeGrowthGlobal0X64,
feeGrowthGlobal1X64: dec.feeGrowthGlobal1X64,
protocolFeesToken0: dec.protocolFeesToken0,
protocolFeesToken1: dec.protocolFeesToken1,
swapInAmountToken0: dec.swapInAmountToken0,
swapOutAmountToken1: dec.swapOutAmountToken1,
swapInAmountToken1: dec.swapInAmountToken1,
swapOutAmountToken0: dec.swapOutAmountToken0,
status: dec.status,
padding: dec.padding,
rewardInfos: dec.rewardInfos.map((item /* eslint-disable-line @typescript-eslint/no-explicit-any */) => types.RewardInfo.fromDecoded(item)),
tickArrayBitmap: dec.tickArrayBitmap,
totalFeesToken0: dec.totalFeesToken0,
totalFeesClaimedToken0: dec.totalFeesClaimedToken0,
totalFeesToken1: dec.totalFeesToken1,
totalFeesClaimedToken1: dec.totalFeesClaimedToken1,
fundFeesToken0: dec.fundFeesToken0,
fundFeesToken1: dec.fundFeesToken1,
openTime: dec.openTime,
padding1: dec.padding1,
padding2: dec.padding2,
});
}
toJSON() {
return {
bump: this.bump,
ammConfig: this.ammConfig.toString(),
owner: this.owner.toString(),
tokenMint0: this.tokenMint0.toString(),
tokenMint1: this.tokenMint1.toString(),
tokenVault0: this.tokenVault0.toString(),
tokenVault1: this.tokenVault1.toString(),
observationKey: this.observationKey.toString(),
mintDecimals0: this.mintDecimals0,
mintDecimals1: this.mintDecimals1,
tickSpacing: this.tickSpacing,
liquidity: this.liquidity.toString(),
sqrtPriceX64: this.sqrtPriceX64.toString(),
tickCurrent: this.tickCurrent,
observationIndex: this.observationIndex,
observationUpdateDuration: this.observationUpdateDuration,
feeGrowthGlobal0X64: this.feeGrowthGlobal0X64.toString(),
feeGrowthGlobal1X64: this.feeGrowthGlobal1X64.toString(),
protocolFeesToken0: this.protocolFeesToken0.toString(),
protocolFeesToken1: this.protocolFeesToken1.toString(),
swapInAmountToken0: this.swapInAmountToken0.toString(),
swapOutAmountToken1: this.swapOutAmountToken1.toString(),
swapInAmountToken1: this.swapInAmountToken1.toString(),
swapOutAmountToken0: this.swapOutAmountToken0.toString(),
status: this.status,
padding: this.padding,
rewardInfos: this.rewardInfos.map((item) => item.toJSON()),
tickArrayBitmap: this.tickArrayBitmap.map((item) => item.toString()),
totalFeesToken0: this.totalFeesToken0.toString(),
totalFeesClaimedToken0: this.totalFeesClaimedToken0.toString(),
totalFeesToken1: this.totalFeesToken1.toString(),
totalFeesClaimedToken1: this.totalFeesClaimedToken1.toString(),
fundFeesToken0: this.fundFeesToken0.toString(),
fundFeesToken1: this.fundFeesToken1.toString(),
openTime: this.openTime.toString(),
padding1: this.padding1.map((item) => item.toString()),
padding2: this.padding2.map((item) => item.toString()),
};
}
static fromJSON(obj) {
return new PoolState({
bump: obj.bump,
ammConfig: new web3_js_1.PublicKey(obj.ammConfig),
owner: new web3_js_1.PublicKey(obj.owner),
tokenMint0: new web3_js_1.PublicKey(obj.tokenMint0),
tokenMint1: new web3_js_1.PublicKey(obj.tokenMint1),
tokenVault0: new web3_js_1.PublicKey(obj.tokenVault0),
tokenVault1: new web3_js_1.PublicKey(obj.tokenVault1),
observationKey: new web3_js_1.PublicKey(obj.observationKey),
mintDecimals0: obj.mintDecimals0,
mintDecimals1: obj.mintDecimals1,
tickSpacing: obj.tickSpacing,
liquidity: new bn_js_1.default(obj.liquidity),
sqrtPriceX64: new bn_js_1.default(obj.sqrtPriceX64),
tickCurrent: obj.tickCurrent,
observationIndex: obj.observationIndex,
observationUpdateDuration: obj.observationUpdateDuration,
feeGrowthGlobal0X64: new bn_js_1.default(obj.feeGrowthGlobal0X64),
feeGrowthGlobal1X64: new bn_js_1.default(obj.feeGrowthGlobal1X64),
protocolFeesToken0: new bn_js_1.default(obj.protocolFeesToken0),
protocolFeesToken1: new bn_js_1.default(obj.protocolFeesToken1),
swapInAmountToken0: new bn_js_1.default(obj.swapInAmountToken0),
swapOutAmountToken1: new bn_js_1.default(obj.swapOutAmountToken1),
swapInAmountToken1: new bn_js_1.default(obj.swapInAmountToken1),
swapOutAmountToken0: new bn_js_1.default(obj.swapOutAmountToken0),
status: obj.status,
padding: obj.padding,
rewardInfos: obj.rewardInfos.map((item) => types.RewardInfo.fromJSON(item)),
tickArrayBitmap: obj.tickArrayBitmap.map((item) => new bn_js_1.default(item)),
totalFeesToken0: new bn_js_1.default(obj.totalFeesToken0),
totalFeesClaimedToken0: new bn_js_1.default(obj.totalFeesClaimedToken0),
totalFeesToken1: new bn_js_1.default(obj.totalFeesToken1),
totalFeesClaimedToken1: new bn_js_1.default(obj.totalFeesClaimedToken1),
fundFeesToken0: new bn_js_1.default(obj.fundFeesToken0),
fundFeesToken1: new bn_js_1.default(obj.fundFeesToken1),
openTime: new bn_js_1.default(obj.openTime),
padding1: obj.padding1.map((item) => new bn_js_1.default(item)),
padding2: obj.padding2.map((item) => new bn_js_1.default(item)),
});
}
}
exports.PoolState = PoolState;
//# sourceMappingURL=PoolState.js.map