UNPKG

@orca-so/whirlpool-sdk

Version:

Whirlpool SDK for the Orca protocol.

226 lines (225 loc) 12.7 kB
"use strict"; 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 __rest = (this && this.__rest) || function (s, e) { var t = {}; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p]; if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) { if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]]; } return t; }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.OrcaAdmin = void 0; const common_sdk_1 = require("@orca-so/common-sdk"); const web3_js_1 = require("@solana/web3.js"); const tiny_invariant_1 = __importDefault(require("tiny-invariant")); const address_1 = require("../utils/address"); const whirlpool_client_sdk_1 = require("@orca-so/whirlpool-client-sdk"); class OrcaAdmin { constructor(dal) { this.dal = dal; } getInitPoolTx(param) { const { provider, initSqrtPrice, tokenMintA, tokenMintB, tickSpacing } = param; const { programId, whirlpoolsConfig: whirlpoolConfigKey } = this.dal; const ctx = whirlpool_client_sdk_1.WhirlpoolContext.withProvider(provider, programId); const client = new whirlpool_client_sdk_1.WhirlpoolClient(ctx); const whirlpoolPda = (0, whirlpool_client_sdk_1.getWhirlpoolPda)(programId, whirlpoolConfigKey, (0, address_1.toPubKey)(tokenMintA), (0, address_1.toPubKey)(tokenMintB), tickSpacing); const feeTierPda = (0, whirlpool_client_sdk_1.getFeeTierPda)(programId, whirlpoolConfigKey, tickSpacing); const tx = client.initPoolTx({ initSqrtPrice, whirlpoolConfigKey, tokenMintA: (0, address_1.toPubKey)(tokenMintA), tokenMintB: (0, address_1.toPubKey)(tokenMintB), whirlpoolPda, tokenVaultAKeypair: web3_js_1.Keypair.generate(), tokenVaultBKeypair: web3_js_1.Keypair.generate(), tickSpacing, feeTierKey: feeTierPda.publicKey, funder: provider.wallet.publicKey, }); return { tx, address: whirlpoolPda.publicKey }; } /*** Fee ***/ getCollectProtocolFeesTx(param) { return __awaiter(this, void 0, void 0, function* () { const { provider, poolAddress } = param; const { programId, whirlpoolsConfig } = this.dal; const ctx = whirlpool_client_sdk_1.WhirlpoolContext.withProvider(provider, programId); const client = new whirlpool_client_sdk_1.WhirlpoolClient(ctx); const whirlpool = yield this.dal.getPool(poolAddress, true); (0, tiny_invariant_1.default)(!!whirlpool, "OrcaAdmin - whirlpool does not exist"); const [ataA, ataB] = yield (0, common_sdk_1.resolveOrCreateATAs)(provider.connection, provider.wallet.publicKey, [{ tokenMint: whirlpool.tokenMintA }, { tokenMint: whirlpool.tokenMintB }], () => this.dal.getAccountRentExempt()); const _a = ataA, { address: tokenDestinationA } = _a, createTokenAAtaIx = __rest(_a, ["address"]); const _b = ataB, { address: tokenDestinationB } = _b, createTokenBAtaIx = __rest(_b, ["address"]); const collectFeesIx = client .collectProtocolFeesTx({ whirlpoolsConfig, whirlpool: (0, address_1.toPubKey)(poolAddress), collectProtocolFeesAuthority: provider.wallet.publicKey, tokenVaultA: whirlpool.tokenVaultA, tokenVaultB: whirlpool.tokenVaultB, tokenDestinationA: (0, address_1.toPubKey)(tokenDestinationA), tokenDestinationB: (0, address_1.toPubKey)(tokenDestinationB), }) .compressIx(false); return new whirlpool_client_sdk_1.TransactionBuilder(provider) .addInstruction(createTokenAAtaIx) .addInstruction(createTokenBAtaIx) .addInstruction(collectFeesIx); }); } getSetFeeAuthorityTx(param) { const { provider, newFeeAuthority } = param; const { programId, whirlpoolsConfig } = this.dal; const ctx = whirlpool_client_sdk_1.WhirlpoolContext.withProvider(provider, programId); const client = new whirlpool_client_sdk_1.WhirlpoolClient(ctx); return client.setFeeAuthorityTx({ whirlpoolsConfig, feeAuthority: provider.wallet.publicKey, newFeeAuthority: (0, address_1.toPubKey)(newFeeAuthority), }); } getSetFeeRateTx(param) { return __awaiter(this, void 0, void 0, function* () { const { provider, feeRate, poolAddress } = param; const { programId, whirlpoolsConfig } = this.dal; const ctx = whirlpool_client_sdk_1.WhirlpoolContext.withProvider(provider, programId); const client = new whirlpool_client_sdk_1.WhirlpoolClient(ctx); const whirlpoolsConfigAccount = yield this.dal.getConfig(whirlpoolsConfig, true); (0, tiny_invariant_1.default)(!!whirlpoolsConfigAccount, `OrcaAdmin - Whirlpool config doesn't exist ${whirlpoolsConfig.toBase58()}`); return client.setFeeRateIx({ whirlpool: (0, address_1.toPubKey)(poolAddress), whirlpoolsConfig, feeAuthority: whirlpoolsConfigAccount.feeAuthority, feeRate, }); }); } getSetProtocolFeeRateTx(param) { return __awaiter(this, void 0, void 0, function* () { const { provider, protocolFeeRate, poolAddress } = param; const { programId, whirlpoolsConfig } = this.dal; const ctx = whirlpool_client_sdk_1.WhirlpoolContext.withProvider(provider, programId); const client = new whirlpool_client_sdk_1.WhirlpoolClient(ctx); const whirlpoolsConfigAccount = yield this.dal.getConfig(whirlpoolsConfig, true); (0, tiny_invariant_1.default)(!!whirlpoolsConfigAccount, `OrcaAdmin - Whirlpool config doesn't exist ${whirlpoolsConfig.toBase58()}`); return client.setProtocolFeeRateIx({ whirlpool: (0, address_1.toPubKey)(poolAddress), whirlpoolsConfig, feeAuthority: whirlpoolsConfigAccount.feeAuthority, protocolFeeRate, }); }); } getSetCollectProtocolFeesAuthorityTx(param) { const { provider, newCollectProtocolFeesAuthority } = param; const { programId, whirlpoolsConfig } = this.dal; const ctx = whirlpool_client_sdk_1.WhirlpoolContext.withProvider(provider, programId); const client = new whirlpool_client_sdk_1.WhirlpoolClient(ctx); return client.setCollectProtocolFeesAuthorityTx({ whirlpoolsConfig, collectProtocolFeesAuthority: provider.wallet.publicKey, newCollectProtocolFeesAuthority: (0, address_1.toPubKey)(newCollectProtocolFeesAuthority), }); } /*** Reward ***/ getInitRewardTx(param) { const { provider, rewardAuthority, poolAddress, rewardMint, rewardIndex } = param; const ctx = whirlpool_client_sdk_1.WhirlpoolContext.withProvider(provider, this.dal.programId); const client = new whirlpool_client_sdk_1.WhirlpoolClient(ctx); (0, tiny_invariant_1.default)(rewardIndex < whirlpool_client_sdk_1.NUM_REWARDS, "invalid rewardIndex"); const rewardVaultKeypair = web3_js_1.Keypair.generate(); const tx = client.initializeRewardTx({ rewardAuthority: (0, address_1.toPubKey)(rewardAuthority), funder: provider.wallet.publicKey, whirlpool: (0, address_1.toPubKey)(poolAddress), rewardMint: (0, address_1.toPubKey)(rewardMint), rewardVaultKeypair, rewardIndex, }); return { tx, rewardVault: rewardVaultKeypair.publicKey }; } getSetRewardAuthorityTx(param) { const { provider, poolAddress, newRewardAuthority, rewardIndex } = param; const ctx = whirlpool_client_sdk_1.WhirlpoolContext.withProvider(provider, this.dal.programId); const client = new whirlpool_client_sdk_1.WhirlpoolClient(ctx); (0, tiny_invariant_1.default)(rewardIndex < whirlpool_client_sdk_1.NUM_REWARDS, "invalid rewardIndex"); return client.setRewardAuthorityTx({ whirlpool: (0, address_1.toPubKey)(poolAddress), rewardAuthority: provider.wallet.publicKey, newRewardAuthority: (0, address_1.toPubKey)(newRewardAuthority), rewardIndex, }); } getSetRewardEmissionsTx(param) { var _a; return __awaiter(this, void 0, void 0, function* () { const { provider, poolAddress, rewardIndex, emissionsPerSecondX64 } = param; const ctx = whirlpool_client_sdk_1.WhirlpoolContext.withProvider(provider, this.dal.programId); const client = new whirlpool_client_sdk_1.WhirlpoolClient(ctx); (0, tiny_invariant_1.default)(rewardIndex < whirlpool_client_sdk_1.NUM_REWARDS, "invalid rewardIndex"); const whirlpool = yield this.dal.getPool(poolAddress, true); const rewardVault = (_a = whirlpool === null || whirlpool === void 0 ? void 0 : whirlpool.rewardInfos[rewardIndex]) === null || _a === void 0 ? void 0 : _a.vault; (0, tiny_invariant_1.default)(!!rewardVault, "reward vault doeos not exist"); return client.setRewardEmissionsTx({ rewardAuthority: provider.wallet.publicKey, whirlpool: (0, address_1.toPubKey)(poolAddress), rewardIndex, emissionsPerSecondX64, rewardVault, }); }); } getSetRewardAuthorityBySuperAuthorityTx(param) { const { provider, poolAddress, newRewardAuthority, rewardIndex } = param; const { programId, whirlpoolsConfig } = this.dal; const ctx = whirlpool_client_sdk_1.WhirlpoolContext.withProvider(provider, programId); const client = new whirlpool_client_sdk_1.WhirlpoolClient(ctx); (0, tiny_invariant_1.default)(rewardIndex < whirlpool_client_sdk_1.NUM_REWARDS, "invalid rewardIndex"); return client.setRewardAuthorityBySuperAuthorityTx({ whirlpoolsConfig, whirlpool: (0, address_1.toPubKey)(poolAddress), rewardEmissionsSuperAuthority: provider.wallet.publicKey, newRewardAuthority: (0, address_1.toPubKey)(newRewardAuthority), rewardIndex, }); } getSetRewardSuperAuthorityTx(param) { const { provider, newRewardSuperAuthority } = param; const { programId, whirlpoolsConfig } = this.dal; const ctx = whirlpool_client_sdk_1.WhirlpoolContext.withProvider(provider, programId); const client = new whirlpool_client_sdk_1.WhirlpoolClient(ctx); return client.setRewardEmissionsSuperAuthorityTx({ whirlpoolsConfig, rewardEmissionsSuperAuthority: provider.wallet.publicKey, newRewardEmissionsSuperAuthority: (0, address_1.toPubKey)(newRewardSuperAuthority), }); } getSetRewardEmissionsBySuperAuthorityTx(param) { const { provider, rewardEmissionsSuperAuthority, newRewardEmissionsSuperAuthority } = param; const { programId, whirlpoolsConfig } = this.dal; const ctx = whirlpool_client_sdk_1.WhirlpoolContext.withProvider(provider, programId); const client = new whirlpool_client_sdk_1.WhirlpoolClient(ctx); return client.setRewardEmissionsSuperAuthorityTx({ whirlpoolsConfig, rewardEmissionsSuperAuthority: (0, address_1.toPubKey)(rewardEmissionsSuperAuthority), newRewardEmissionsSuperAuthority: (0, address_1.toPubKey)(newRewardEmissionsSuperAuthority), }); } } exports.OrcaAdmin = OrcaAdmin;