@renec-foundation/redex-sdk
Version:
Typescript SDK to interact with Orca's Whirlpool program.
225 lines (224 loc) • 7.27 kB
JavaScript
;
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 __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
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;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.ParsableMintInfo = exports.ParsableTokenInfo = exports.ParsableFeeTier = exports.ParsableTickArray = exports.ParsablePosition = exports.ParsableWhirlpool = exports.ParsableWhirlpoolsConfig = void 0;
const spl_token_1 = require("@solana/spl-token");
const web3_js_1 = require("@solana/web3.js");
const public_1 = require("../../types/public");
const anchor_1 = require("@project-serum/anchor");
const WhirlpoolIDL = __importStar(require("../../artifacts/whirlpool.json"));
const common_sdk_1 = require("@orca-so/common-sdk");
/**
* @category Parsables
*/
let ParsableWhirlpoolsConfig = class ParsableWhirlpoolsConfig {
constructor() { }
static parse(data) {
if (!data) {
return null;
}
try {
return parseAnchorAccount(public_1.AccountName.WhirlpoolsConfig, data);
}
catch (e) {
console.error(`error while parsing WhirlpoolsConfig: ${e}`);
return null;
}
}
};
ParsableWhirlpoolsConfig = __decorate([
staticImplements()
], ParsableWhirlpoolsConfig);
exports.ParsableWhirlpoolsConfig = ParsableWhirlpoolsConfig;
/**
* @category Parsables
*/
let ParsableWhirlpool = class ParsableWhirlpool {
constructor() { }
static parse(data) {
if (!data) {
return null;
}
try {
return parseAnchorAccount(public_1.AccountName.Whirlpool, data);
}
catch (e) {
console.error(`error while parsing Whirlpool: ${e}`);
return null;
}
}
};
ParsableWhirlpool = __decorate([
staticImplements()
], ParsableWhirlpool);
exports.ParsableWhirlpool = ParsableWhirlpool;
/**
* @category Parsables
*/
let ParsablePosition = class ParsablePosition {
constructor() { }
static parse(data) {
if (!data) {
return null;
}
try {
return parseAnchorAccount(public_1.AccountName.Position, data);
}
catch (e) {
console.error(`error while parsing Position: ${e}`);
return null;
}
}
};
ParsablePosition = __decorate([
staticImplements()
], ParsablePosition);
exports.ParsablePosition = ParsablePosition;
/**
* @category Parsables
*/
let ParsableTickArray = class ParsableTickArray {
constructor() { }
static parse(data) {
if (!data) {
return null;
}
try {
return parseAnchorAccount(public_1.AccountName.TickArray, data);
}
catch (e) {
console.error(`error while parsing TickArray: ${e}`);
return null;
}
}
};
ParsableTickArray = __decorate([
staticImplements()
], ParsableTickArray);
exports.ParsableTickArray = ParsableTickArray;
/**
* @category Parsables
*/
let ParsableFeeTier = class ParsableFeeTier {
constructor() { }
static parse(data) {
if (!data) {
return null;
}
try {
return parseAnchorAccount(public_1.AccountName.FeeTier, data);
}
catch (e) {
console.error(`error while parsing FeeTier: ${e}`);
return null;
}
}
};
ParsableFeeTier = __decorate([
staticImplements()
], ParsableFeeTier);
exports.ParsableFeeTier = ParsableFeeTier;
/**
* @category Parsables
*/
let ParsableTokenInfo = class ParsableTokenInfo {
constructor() { }
static parse(data) {
if (!data) {
return null;
}
try {
return common_sdk_1.TokenUtil.deserializeTokenAccount(data);
}
catch (e) {
console.error(`error while parsing TokenAccount: ${e}`);
return null;
}
}
};
ParsableTokenInfo = __decorate([
staticImplements()
], ParsableTokenInfo);
exports.ParsableTokenInfo = ParsableTokenInfo;
/**
* @category Parsables
*/
let ParsableMintInfo = class ParsableMintInfo {
constructor() { }
static parse(data) {
if (!data) {
return null;
}
try {
const buffer = spl_token_1.MintLayout.decode(data);
const mintInfo = {
mintAuthority: buffer.mintAuthorityOption === 0 ? null : new web3_js_1.PublicKey(buffer.mintAuthority),
supply: spl_token_1.u64.fromBuffer(buffer.supply),
decimals: buffer.decimals,
isInitialized: buffer.isInitialized !== 0,
freezeAuthority: buffer.freezeAuthority === 0 ? null : new web3_js_1.PublicKey(buffer.freezeAuthority),
};
return mintInfo;
}
catch (e) {
console.error(`error while parsing MintInfo: ${e}`);
return null;
}
}
};
ParsableMintInfo = __decorate([
staticImplements()
], ParsableMintInfo);
exports.ParsableMintInfo = ParsableMintInfo;
/**
* Class decorator to define an interface with static methods
* Reference: https://github.com/Microsoft/TypeScript/issues/13462#issuecomment-295685298
*/
function staticImplements() {
return (constructor) => {
constructor;
};
}
const WhirlpoolCoder = new anchor_1.BorshAccountsCoder(WhirlpoolIDL);
function parseAnchorAccount(accountName, data) {
const discriminator = anchor_1.BorshAccountsCoder.accountDiscriminator(accountName);
if (discriminator.compare(data.slice(0, 8))) {
console.error("incorrect account name during parsing");
return null;
}
try {
return WhirlpoolCoder.decode(accountName, data);
}
catch (_e) {
console.error("unknown account name during parsing");
return null;
}
}