@kamino-finance/scope-sdk
Version:
Scope Oracle SDK
176 lines • 7.13 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.Custody = 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 Custody {
pool;
mint;
tokenAccount;
decimals;
isStable;
oracle;
pricing;
permissions;
targetRatioBps;
assets;
fundingRateState;
bump;
tokenAccountBump;
static discriminator = Buffer.from([
1, 184, 48, 81, 93, 131, 63, 145,
]);
static layout = borsh.struct([
(0, utils_1.borshAddress)("pool"),
(0, utils_1.borshAddress)("mint"),
(0, utils_1.borshAddress)("tokenAccount"),
borsh.u8("decimals"),
borsh.bool("isStable"),
types.OracleParams.layout("oracle"),
types.PricingParams.layout("pricing"),
types.Permissions.layout("permissions"),
borsh.u64("targetRatioBps"),
types.Assets.layout("assets"),
types.FundingRateState.layout("fundingRateState"),
borsh.u8("bump"),
borsh.u8("tokenAccountBump"),
]);
constructor(fields) {
this.pool = fields.pool;
this.mint = fields.mint;
this.tokenAccount = fields.tokenAccount;
this.decimals = fields.decimals;
this.isStable = fields.isStable;
this.oracle = new types.OracleParams({ ...fields.oracle });
this.pricing = new types.PricingParams({ ...fields.pricing });
this.permissions = new types.Permissions({ ...fields.permissions });
this.targetRatioBps = fields.targetRatioBps;
this.assets = new types.Assets({ ...fields.assets });
this.fundingRateState = new types.FundingRateState({
...fields.fundingRateState,
});
this.bump = fields.bump;
this.tokenAccountBump = fields.tokenAccountBump;
}
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(Custody.discriminator)) {
throw new Error("invalid account discriminator");
}
const dec = Custody.layout.decode(data.slice(8));
return new Custody({
pool: dec.pool,
mint: dec.mint,
tokenAccount: dec.tokenAccount,
decimals: dec.decimals,
isStable: dec.isStable,
oracle: types.OracleParams.fromDecoded(dec.oracle),
pricing: types.PricingParams.fromDecoded(dec.pricing),
permissions: types.Permissions.fromDecoded(dec.permissions),
targetRatioBps: dec.targetRatioBps,
assets: types.Assets.fromDecoded(dec.assets),
fundingRateState: types.FundingRateState.fromDecoded(dec.fundingRateState),
bump: dec.bump,
tokenAccountBump: dec.tokenAccountBump,
});
}
toJSON() {
return {
pool: this.pool,
mint: this.mint,
tokenAccount: this.tokenAccount,
decimals: this.decimals,
isStable: this.isStable,
oracle: this.oracle.toJSON(),
pricing: this.pricing.toJSON(),
permissions: this.permissions.toJSON(),
targetRatioBps: this.targetRatioBps.toString(),
assets: this.assets.toJSON(),
fundingRateState: this.fundingRateState.toJSON(),
bump: this.bump,
tokenAccountBump: this.tokenAccountBump,
};
}
static fromJSON(obj) {
return new Custody({
pool: (0, kit_1.address)(obj.pool),
mint: (0, kit_1.address)(obj.mint),
tokenAccount: (0, kit_1.address)(obj.tokenAccount),
decimals: obj.decimals,
isStable: obj.isStable,
oracle: types.OracleParams.fromJSON(obj.oracle),
pricing: types.PricingParams.fromJSON(obj.pricing),
permissions: types.Permissions.fromJSON(obj.permissions),
targetRatioBps: new bn_js_1.default(obj.targetRatioBps),
assets: types.Assets.fromJSON(obj.assets),
fundingRateState: types.FundingRateState.fromJSON(obj.fundingRateState),
bump: obj.bump,
tokenAccountBump: obj.tokenAccountBump,
});
}
}
exports.Custody = Custody;
//# sourceMappingURL=Custody.js.map