frakt-client
Version:
Client library for interacting with FRAKT solana program
475 lines (474 loc) • 31.6 kB
JavaScript
"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (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.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
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());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.decreaseStakingPool = exports.topupStakingPool = exports.initializePoolConfig = exports.createAndMintSomeTokens = exports.findAssociatedTokenAddress = exports.decodedStakeFRKTBuffersToUI = exports.STAKE_FRKT_ACCOUNT_DATA_LAYOUT = exports.initializeConfigs = exports.getAllProgramAccounts = exports.unstakeFRKT = exports.harvestFRKT = exports.stakeFRKT = exports.initializeCumulativeAccount = void 0;
const web3_js_1 = require("@solana/web3.js");
const spl_token_1 = require("@solana/spl-token");
const BufferLayout = __importStar(require("buffer-layout"));
const BN = require('bn.js');
//
/**
* Layout for a public key
*/
const publicKey = (property = 'publicKey') => {
return BufferLayout.blob(32, property);
};
const uint64 = (property = 'uint64') => {
return BufferLayout.blob(8, property);
};
function decodedPoolConfigBuffersToUI(decodedPoolConfigState, poolConfigAddress) {
return {
poolConfigAccountPubkey: poolConfigAddress.toBase58(),
is_initialized: Boolean(new BN(decodedPoolConfigState.is_initialized, 10, 'le').toNumber()),
farming_vault_owner_pda: new web3_js_1.PublicKey(decodedPoolConfigState.farming_vault_owner_pda).toBase58(),
farming_token_mint: new web3_js_1.PublicKey(decodedPoolConfigState.farming_token_mint).toBase58(),
farming_vault_token_account: new web3_js_1.PublicKey(decodedPoolConfigState.farming_vault_token_account).toBase58(),
farming_pool_vault_balance: new BN(decodedPoolConfigState.farming_pool_vault_balance, 10, 'le').toNumber(),
farming_tokens_per_second_per_point: new BN(decodedPoolConfigState.farming_tokens_per_second_per_point, 10, 'le').toNumber(),
version: new BN(decodedPoolConfigState.version, 10, 'le').toNumber(),
harvest_unlock_at: new BN(decodedPoolConfigState.harvest_unlock_at, 10, 'le').toNumber(),
};
}
function initializeCumulativeAccount({ programPubkey, userPublicKey, sendTxn }) {
return __awaiter(this, void 0, void 0, function* () {
const encoder = new TextEncoder();
const [cumulativePubkey] = yield web3_js_1.PublicKey.findProgramAddress([encoder.encode('cumulativeconfig')], programPubkey);
const cumulativeInstruction = new web3_js_1.TransactionInstruction({
programId: programPubkey,
data: Buffer.from(Uint8Array.of(11)),
keys: [
{ pubkey: userPublicKey, isSigner: true, isWritable: false },
{ pubkey: cumulativePubkey, isSigner: false, isWritable: true },
{ pubkey: web3_js_1.SystemProgram.programId, isSigner: false, isWritable: false },
{ pubkey: web3_js_1.SYSVAR_RENT_PUBKEY, isSigner: false, isWritable: false },
{ pubkey: web3_js_1.SYSVAR_CLOCK_PUBKEY, isSigner: false, isWritable: false },
{ pubkey: spl_token_1.ASSOCIATED_TOKEN_PROGRAM_ID, isSigner: false, isWritable: false },
],
});
const transaction = new web3_js_1.Transaction().add(cumulativeInstruction);
void (yield sendTxn(transaction));
});
}
exports.initializeCumulativeAccount = initializeCumulativeAccount;
function stakeFRKT({ amount, programPubkey, userPublicKey, mintPubkey, connection, sendTxn, stakeAccountGiven }) {
return __awaiter(this, void 0, void 0, function* () {
let stakeAccount;
const transaction = new web3_js_1.Transaction();
if (stakeAccountGiven == null) {
let seedStake = makeid(8);
const stakeAccounted = yield web3_js_1.PublicKey.createWithSeed(userPublicKey, seedStake, programPubkey);
const size = exports.STAKE_FRKT_ACCOUNT_DATA_LAYOUT.span;
const lamports = yield connection.getMinimumBalanceForRentExemption(size);
transaction.add(web3_js_1.SystemProgram.createAccountWithSeed({
fromPubkey: userPublicKey,
basePubkey: userPublicKey,
seed: seedStake,
newAccountPubkey: stakeAccounted,
lamports,
space: size,
programId: programPubkey,
}));
// await sendAndConfirmTransaction(connection, transaction, [payer]);
stakeAccount = stakeAccounted;
}
else {
stakeAccount = stakeAccountGiven;
}
const encoder = new TextEncoder();
const [farming_vault_owner_pubkey] = yield web3_js_1.PublicKey.findProgramAddress([encoder.encode('farmingvaultowner')], programPubkey);
const [pool_config_pubkey,] = yield web3_js_1.PublicKey.findProgramAddress([encoder.encode('poolconfig')], programPubkey);
const [cumulativePubkey] = yield web3_js_1.PublicKey.findProgramAddress([encoder.encode('cumulativeconfig')], programPubkey);
const associatedUserFarmingTokenAccountAddress = yield findAssociatedTokenAddress(userPublicKey, mintPubkey);
const associatedFarmingVaultTokenAddress = yield findAssociatedTokenAddress(farming_vault_owner_pubkey, mintPubkey);
const stakeInstruction = new web3_js_1.TransactionInstruction({
programId: programPubkey,
data: Buffer.from(Uint8Array.of(12, ...new BN(amount).toArray('le', 8))),
keys: [
{ pubkey: userPublicKey, isSigner: true, isWritable: false },
{ pubkey: associatedUserFarmingTokenAccountAddress, isSigner: false, isWritable: true },
{ pubkey: cumulativePubkey, isSigner: false, isWritable: true },
{ pubkey: stakeAccount, isSigner: false, isWritable: true },
{ pubkey: pool_config_pubkey, isSigner: false, isWritable: true },
{ pubkey: farming_vault_owner_pubkey, isSigner: false, isWritable: true },
{ pubkey: mintPubkey, isSigner: false, isWritable: true },
{ pubkey: associatedFarmingVaultTokenAddress, isSigner: false, isWritable: true },
{ pubkey: spl_token_1.TOKEN_PROGRAM_ID, isSigner: false, isWritable: false },
{ pubkey: web3_js_1.SYSVAR_RENT_PUBKEY, isSigner: false, isWritable: false },
{ pubkey: web3_js_1.SYSVAR_CLOCK_PUBKEY, isSigner: false, isWritable: false },
{ pubkey: web3_js_1.SystemProgram.programId, isSigner: false, isWritable: false },
{ pubkey: spl_token_1.ASSOCIATED_TOKEN_PROGRAM_ID, isSigner: false, isWritable: false },
],
});
transaction.add(stakeInstruction);
void (yield sendTxn(transaction));
});
}
exports.stakeFRKT = stakeFRKT;
function harvestFRKT({ programPubkey, userPublicKey, mintPubkey, sendTxn, connection, customerFRKTAccounts }) {
return __awaiter(this, void 0, void 0, function* () {
const encoder = new TextEncoder();
const [farming_vault_owner_pubkey] = yield web3_js_1.PublicKey.findProgramAddress([encoder.encode('farmingvaultowner')], programPubkey);
const [pool_config_pubkey,] = yield web3_js_1.PublicKey.findProgramAddress([encoder.encode('poolconfig')], programPubkey);
const [cumulativePubkey] = yield web3_js_1.PublicKey.findProgramAddress([encoder.encode('cumulativeconfig')], programPubkey);
const associatedUserFarmingTokenAccountAddress = yield findAssociatedTokenAddress(userPublicKey, mintPubkey);
const associatedFarmingVaultTokenAddress = yield findAssociatedTokenAddress(farming_vault_owner_pubkey, mintPubkey);
let transaction = new web3_js_1.Transaction();
var i = 0;
var tmp_i = 0;
while (customerFRKTAccounts.length > i) {
if (customerFRKTAccounts.length - i > 5) {
tmp_i = tmp_i + 5;
}
else {
tmp_i = tmp_i + (customerFRKTAccounts.length - i);
}
for (; i < tmp_i; ++i) {
const harvestInstruction = new web3_js_1.TransactionInstruction({
programId: programPubkey,
data: Buffer.from(Uint8Array.of(14)),
keys: [
{ pubkey: userPublicKey, isSigner: true, isWritable: false },
{ pubkey: associatedUserFarmingTokenAccountAddress, isSigner: false, isWritable: true },
{ pubkey: cumulativePubkey, isSigner: false, isWritable: true },
{ pubkey: customerFRKTAccounts[i], isSigner: false, isWritable: true },
{ pubkey: pool_config_pubkey, isSigner: false, isWritable: true },
{ pubkey: farming_vault_owner_pubkey, isSigner: false, isWritable: true },
{ pubkey: mintPubkey, isSigner: false, isWritable: true },
{ pubkey: associatedFarmingVaultTokenAddress, isSigner: false, isWritable: true },
{ pubkey: spl_token_1.TOKEN_PROGRAM_ID, isSigner: false, isWritable: false },
{ pubkey: web3_js_1.SYSVAR_RENT_PUBKEY, isSigner: false, isWritable: false },
{ pubkey: web3_js_1.SYSVAR_CLOCK_PUBKEY, isSigner: false, isWritable: false },
{ pubkey: web3_js_1.SystemProgram.programId, isSigner: false, isWritable: false },
{ pubkey: spl_token_1.ASSOCIATED_TOKEN_PROGRAM_ID, isSigner: false, isWritable: false },
],
});
transaction.add(harvestInstruction);
}
void (yield sendTxn(transaction));
yield new Promise(f => setTimeout(f, 1000));
transaction = new web3_js_1.Transaction();
}
});
}
exports.harvestFRKT = harvestFRKT;
function unstakeFRKT({ programPubkey, userPublicKey, mintPubkey, sendTxn, connection, customerFRKTAccounts }) {
return __awaiter(this, void 0, void 0, function* () {
const encoder = new TextEncoder();
const [farming_vault_owner_pubkey] = yield web3_js_1.PublicKey.findProgramAddress([encoder.encode('farmingvaultowner')], programPubkey);
const [pool_config_pubkey,] = yield web3_js_1.PublicKey.findProgramAddress([encoder.encode('poolconfig')], programPubkey);
const [cumulativePubkey] = yield web3_js_1.PublicKey.findProgramAddress([encoder.encode('cumulativeconfig')], programPubkey);
const associatedUserFarmingTokenAccountAddress = yield findAssociatedTokenAddress(userPublicKey, mintPubkey);
const associatedFarmingVaultTokenAddress = yield findAssociatedTokenAddress(farming_vault_owner_pubkey, mintPubkey);
let transaction = new web3_js_1.Transaction();
var i = 0;
var tmp_i = 0;
while (customerFRKTAccounts.length > i) {
if (customerFRKTAccounts.length - i > 5) {
tmp_i = tmp_i + 5;
}
else {
tmp_i = tmp_i + (customerFRKTAccounts.length - i);
}
for (; i < tmp_i; ++i) {
const unstakeInstruction = new web3_js_1.TransactionInstruction({
programId: programPubkey,
data: Buffer.from(Uint8Array.of(13)),
keys: [
{ pubkey: userPublicKey, isSigner: true, isWritable: false },
{ pubkey: associatedUserFarmingTokenAccountAddress, isSigner: false, isWritable: true },
{ pubkey: cumulativePubkey, isSigner: false, isWritable: true },
{ pubkey: customerFRKTAccounts[i], isSigner: false, isWritable: true },
{ pubkey: pool_config_pubkey, isSigner: false, isWritable: true },
{ pubkey: farming_vault_owner_pubkey, isSigner: false, isWritable: true },
{ pubkey: mintPubkey, isSigner: false, isWritable: true },
{ pubkey: associatedFarmingVaultTokenAddress, isSigner: false, isWritable: true },
{ pubkey: spl_token_1.TOKEN_PROGRAM_ID, isSigner: false, isWritable: false },
{ pubkey: web3_js_1.SYSVAR_RENT_PUBKEY, isSigner: false, isWritable: false },
{ pubkey: web3_js_1.SYSVAR_CLOCK_PUBKEY, isSigner: false, isWritable: false },
{ pubkey: web3_js_1.SystemProgram.programId, isSigner: false, isWritable: false },
{ pubkey: spl_token_1.ASSOCIATED_TOKEN_PROGRAM_ID, isSigner: false, isWritable: false },
],
});
transaction.add(unstakeInstruction);
}
void (yield sendTxn(transaction));
yield new Promise(f => setTimeout(f, 1000));
transaction = new web3_js_1.Transaction();
}
});
}
exports.unstakeFRKT = unstakeFRKT;
function getAllProgramAccounts(launchpdProgramPubKey, { connection }) {
return __awaiter(this, void 0, void 0, function* () {
let allAccounts = yield connection.getProgramAccounts(launchpdProgramPubKey, 'singleGossip');
const rawPoolConfigAccount = allAccounts.find((rawConfig) => rawConfig.account.data.length == POOL_CONFIG_ACCOUNT_DATA_LAYOUT.span);
const poolConfigAccount = rawPoolConfigAccount ? decodedPoolConfigBuffersToUI(POOL_CONFIG_ACCOUNT_DATA_LAYOUT.decode(rawPoolConfigAccount.account.data), rawPoolConfigAccount.pubkey) : null;
const stakeFRKTAccounts = allAccounts
.filter((rawStakeFRKT) => rawStakeFRKT.account.data.length == exports.STAKE_FRKT_ACCOUNT_DATA_LAYOUT.span)
.map(rawStakeFRKT => decodedStakeFRKTBuffersToUI(exports.STAKE_FRKT_ACCOUNT_DATA_LAYOUT.decode(rawStakeFRKT.account.data), rawStakeFRKT.pubkey));
const rawCumulativeAccount = allAccounts.find((rawCumulative) => rawCumulative.account.data.length == CUMULATIVE_ACCOUNT_DATA_LAYOUT.span);
const cumulativeAccount = rawCumulativeAccount ? decodedCumulativeBuffersToUI(CUMULATIVE_ACCOUNT_DATA_LAYOUT.decode(rawCumulativeAccount.account.data), rawCumulativeAccount.pubkey) : null;
return { poolConfigAccount, stakeFRKTAccounts, cumulativeAccount };
});
}
exports.getAllProgramAccounts = getAllProgramAccounts;
function initializeConfigs({ farmingMintPubkey, adminPubkey, programPubkey, sendTxn, connection }) {
return __awaiter(this, void 0, void 0, function* () {
const encoder = new TextEncoder();
const [cumulativePubkey] = yield web3_js_1.PublicKey.findProgramAddress([encoder.encode('cumulativeconfig')], programPubkey);
const [farming_vault_owner_pubkey] = yield web3_js_1.PublicKey.findProgramAddress([encoder.encode('farmingvaultowner')], programPubkey);
const [pool_config_pubkey,] = yield web3_js_1.PublicKey.findProgramAddress([encoder.encode('poolconfig')], programPubkey);
const associatedFarmingTokenVaultAddress = yield findAssociatedTokenAddress(farming_vault_owner_pubkey, farmingMintPubkey);
const associatedTokenVaultInfo = yield connection.getAccountInfo(associatedFarmingTokenVaultAddress, 'confirmed');
const InitializePoolConfigIx = new web3_js_1.TransactionInstruction({
programId: programPubkey,
keys: [
{ pubkey: adminPubkey, isSigner: true, isWritable: true },
{ pubkey: cumulativePubkey, isSigner: false, isWritable: true },
{ pubkey: pool_config_pubkey, isSigner: false, isWritable: true },
{ pubkey: farming_vault_owner_pubkey, isSigner: false, isWritable: true },
{ pubkey: farmingMintPubkey, isSigner: false, isWritable: true },
{ pubkey: associatedFarmingTokenVaultAddress, isSigner: false, isWritable: true },
{ pubkey: web3_js_1.SystemProgram.programId, isSigner: false, isWritable: false },
{ pubkey: web3_js_1.SYSVAR_RENT_PUBKEY, isSigner: false, isWritable: false },
{ pubkey: web3_js_1.SYSVAR_CLOCK_PUBKEY, isSigner: false, isWritable: false },
{ pubkey: spl_token_1.ASSOCIATED_TOKEN_PROGRAM_ID, isSigner: false, isWritable: false },
],
data: Buffer.from(Uint8Array.of(10)),
});
const transaction = new web3_js_1.Transaction();
if (associatedTokenVaultInfo === null) {
const createAssociatedTokenAccountIx = spl_token_1.Token.createAssociatedTokenAccountInstruction(spl_token_1.ASSOCIATED_TOKEN_PROGRAM_ID, spl_token_1.TOKEN_PROGRAM_ID, farmingMintPubkey, associatedFarmingTokenVaultAddress, farming_vault_owner_pubkey, adminPubkey);
transaction.add(createAssociatedTokenAccountIx);
}
// instructions.push(createStakeAccountIx)
transaction.add(InitializePoolConfigIx);
void (yield sendTxn(transaction));
});
}
exports.initializeConfigs = initializeConfigs;
exports.STAKE_FRKT_ACCOUNT_DATA_LAYOUT = BufferLayout.struct([
publicKey('stake_owner'),
publicKey('token_pubkey'),
publicKey('mint_pubkey'),
uint64('amount'),
uint64('staked_at'),
uint64('staked_at_cumulative'),
uint64('stake_end_at'),
uint64('version'),
BufferLayout.u8('is_initialized'),
publicKey('token_vault_pubkey'),
BufferLayout.u8('is_staked'),
uint64('last_harvested_at'),
]);
function decodedStakeFRKTBuffersToUI(decodedStakeState, stakeAddress) {
return {
stakeAccountPubkey: stakeAddress.toBase58(),
stake_owner: new web3_js_1.PublicKey(decodedStakeState.stake_owner).toBase58(),
token_pubkey: new web3_js_1.PublicKey(decodedStakeState.token_pubkey).toBase58(),
mint_pubkey: new web3_js_1.PublicKey(decodedStakeState.mint_pubkey).toBase58(),
amount: new BN(decodedStakeState.amount, 10, 'le').toNumber(),
staked_at: new BN(decodedStakeState.staked_at, 10, 'le').toNumber(),
staked_at_cumulative: new BN(decodedStakeState.staked_at_cumulative, 10, 'le').toNumber(),
stake_end_at: new BN(decodedStakeState.stake_end_at, 10, 'le').toNumber(),
version: new BN(decodedStakeState.version, 10, 'le').toNumber(),
is_initialized: new BN(decodedStakeState.is_initialized, 10, 'le').toNumber() > 0,
token_vault_pubkey: new web3_js_1.PublicKey(decodedStakeState.token_vault_pubkey).toBase58(),
is_staked: Boolean(new BN(decodedStakeState.is_staked, 10, 'le').toNumber()),
last_harvested_at: new BN(decodedStakeState.last_harvested_at, 10, 'le').toNumber(),
};
}
exports.decodedStakeFRKTBuffersToUI = decodedStakeFRKTBuffersToUI;
const CUMULATIVE_ACCOUNT_DATA_LAYOUT = BufferLayout.struct([
uint64("cumulative"),
uint64('last_time'),
uint64('apr'),
uint64('amount_of_staked'),
uint64('amount_of_staked_cumulative'),
BufferLayout.u8('is_initialized'),
uint64("old_cumulative"),
]);
function decodedCumulativeBuffersToUI(decodedCumulativeState, CumulativeAddress) {
return {
cumulative: new BN(decodedCumulativeState.cumulative, 10, 'le').toNumber(),
stakeAccountPubkey: CumulativeAddress.toBase58(),
last_time: new BN(decodedCumulativeState.last_time, 10, 'le').toNumber(),
apr: new BN(decodedCumulativeState.apr, 10, 'le').toNumber(),
amount_of_staked: new BN(decodedCumulativeState.amount_of_staked, 10, 'le').toNumber(),
amount_of_staked_cumulative: new BN(decodedCumulativeState.amount_of_staked_cumulative, 10, 'le').toNumber(),
is_initialized: new BN(decodedCumulativeState.is_initialized, 10, 'le').toNumber() > 0,
old_cumulative: new BN(decodedCumulativeState.old_cumulative, 10, "le").toNumber(),
};
}
function makeid(length) {
var result = [];
var characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
var charactersLength = characters.length;
for (var i = 0; i < length; i++) {
result.push(characters.charAt(Math.floor(Math.random() * charactersLength)));
}
return result.join('');
}
function findAssociatedTokenAddress(walletAddress, tokenMintAddress) {
return __awaiter(this, void 0, void 0, function* () {
return (yield web3_js_1.PublicKey.findProgramAddress([walletAddress.toBuffer(), spl_token_1.TOKEN_PROGRAM_ID.toBuffer(), tokenMintAddress.toBuffer()], spl_token_1.ASSOCIATED_TOKEN_PROGRAM_ID))[0];
});
}
exports.findAssociatedTokenAddress = findAssociatedTokenAddress;
const POOL_CONFIG_ACCOUNT_DATA_LAYOUT = BufferLayout.struct([
BufferLayout.u8('is_initialized'),
publicKey('farming_vault_owner_pda'),
publicKey('farming_token_mint'),
publicKey('farming_vault_token_account'),
uint64('farming_pool_vault_balance'),
uint64('farming_tokens_per_second_per_point'),
uint64('version'),
uint64('harvest_unlock_at'),
]);
function createAndMintSomeTokens(amountToMint, decimals, userPublicKey, sendTxn, { connection }) {
return __awaiter(this, void 0, void 0, function* () {
const mintSeed = makeid(8);
const mintAddress = yield web3_js_1.PublicKey.createWithSeed(userPublicKey, mintSeed, spl_token_1.TOKEN_PROGRAM_ID);
const createMintAccountIx = web3_js_1.SystemProgram.createAccountWithSeed({
fromPubkey: userPublicKey,
basePubkey: userPublicKey,
seed: mintSeed,
newAccountPubkey: mintAddress,
space: spl_token_1.MintLayout.span,
lamports: yield connection.getMinimumBalanceForRentExemption(spl_token_1.AccountLayout.span),
programId: spl_token_1.TOKEN_PROGRAM_ID,
});
// const associatedTokenAddress = await Token.getAssociatedTokenAddress(ASSOCIATED_TOKEN_PROGRAM_ID, TOKEN_PROGRAM_ID, mintAddress,
// userPublicKey)
const initializeMintIx = spl_token_1.Token.createInitMintInstruction(spl_token_1.TOKEN_PROGRAM_ID, mintAddress, decimals, userPublicKey, null);
const tokenAccAddress = yield findAssociatedTokenAddress(userPublicKey, mintAddress);
const createAssociatedTokenAccountIx = spl_token_1.Token.createAssociatedTokenAccountInstruction(spl_token_1.ASSOCIATED_TOKEN_PROGRAM_ID, spl_token_1.TOKEN_PROGRAM_ID, mintAddress, tokenAccAddress, userPublicKey, userPublicKey);
const mintToIx = spl_token_1.Token.createMintToInstruction(spl_token_1.TOKEN_PROGRAM_ID, mintAddress, tokenAccAddress, userPublicKey, [], amountToMint);
const transaction = new web3_js_1.Transaction().add(createMintAccountIx, initializeMintIx, createAssociatedTokenAccountIx, mintToIx);
void (yield sendTxn(transaction));
return { mintAddress: mintAddress.toBase58(), tokenAccAddress: tokenAccAddress.toBase58(), mintSeed };
});
}
exports.createAndMintSomeTokens = createAndMintSomeTokens;
function initializePoolConfig(farmingTokensPerSecondPerPoint, harvest_unlock_at, farmingMintPubkey, adminPubkey, programPubkey, sendTxn, { connection }) {
return __awaiter(this, void 0, void 0, function* () {
const encoder = new TextEncoder();
const [farming_vault_owner_pubkey] = yield web3_js_1.PublicKey.findProgramAddress([encoder.encode('farmingvaultowner')], programPubkey);
const [pool_config_pubkey,] = yield web3_js_1.PublicKey.findProgramAddress([encoder.encode('poolconfig')], programPubkey);
const associatedFarmingTokenVaultAddress = yield findAssociatedTokenAddress(farming_vault_owner_pubkey, farmingMintPubkey);
const associatedTokenVaultInfo = yield connection.getAccountInfo(associatedFarmingTokenVaultAddress, 'confirmed');
const InitializePoolConfigIx = new web3_js_1.TransactionInstruction({
programId: programPubkey,
keys: [
{ pubkey: adminPubkey, isSigner: true, isWritable: true },
{ pubkey: pool_config_pubkey, isSigner: false, isWritable: true },
{ pubkey: farming_vault_owner_pubkey, isSigner: false, isWritable: true },
{ pubkey: farmingMintPubkey, isSigner: false, isWritable: true },
{ pubkey: associatedFarmingTokenVaultAddress, isSigner: false, isWritable: true },
{ pubkey: web3_js_1.SystemProgram.programId, isSigner: false, isWritable: false },
{ pubkey: web3_js_1.SYSVAR_RENT_PUBKEY, isSigner: false, isWritable: false },
{ pubkey: web3_js_1.SYSVAR_CLOCK_PUBKEY, isSigner: false, isWritable: false },
{ pubkey: spl_token_1.ASSOCIATED_TOKEN_PROGRAM_ID, isSigner: false, isWritable: false },
],
data: Buffer.from(Uint8Array.of(7, ...new BN(farmingTokensPerSecondPerPoint).toArray('le', 8), ...new BN(harvest_unlock_at).toArray('le', 8))),
});
const transaction = new web3_js_1.Transaction();
if (associatedTokenVaultInfo === null) {
const createAssociatedTokenAccountIx = spl_token_1.Token.createAssociatedTokenAccountInstruction(spl_token_1.ASSOCIATED_TOKEN_PROGRAM_ID, spl_token_1.TOKEN_PROGRAM_ID, farmingMintPubkey, associatedFarmingTokenVaultAddress, farming_vault_owner_pubkey, adminPubkey);
transaction.add(createAssociatedTokenAccountIx);
}
// instructions.push(createStakeAccountIx)
transaction.add(InitializePoolConfigIx);
void (yield sendTxn(transaction));
});
}
exports.initializePoolConfig = initializePoolConfig;
function topupStakingPool({ topup_amount, adminPublicKey, farmingMintPubKey, programPubkey, sendTxn }) {
return __awaiter(this, void 0, void 0, function* () {
const encoder = new TextEncoder();
const [farming_vault_owner_pubkey] = yield web3_js_1.PublicKey.findProgramAddress([encoder.encode('farmingvaultowner')], programPubkey);
const [pool_config_pubkey,] = yield web3_js_1.PublicKey.findProgramAddress([encoder.encode('poolconfig')], programPubkey);
const [cumulativePubkey] = yield web3_js_1.PublicKey.findProgramAddress([encoder.encode('cumulativeconfig')], programPubkey);
const associatedAdminTokenAccountAddress = yield findAssociatedTokenAddress(adminPublicKey, farmingMintPubKey);
const associatedFarmingTokenVaultAddress = yield findAssociatedTokenAddress(farming_vault_owner_pubkey, farmingMintPubKey);
const TopupStakingPoolIx = new web3_js_1.TransactionInstruction({
programId: programPubkey,
keys: [
{ pubkey: adminPublicKey, isSigner: true, isWritable: true },
{ pubkey: associatedAdminTokenAccountAddress, isSigner: false, isWritable: true },
{ pubkey: cumulativePubkey, isSigner: false, isWritable: true },
{ pubkey: pool_config_pubkey, isSigner: false, isWritable: true },
{ pubkey: farming_vault_owner_pubkey, isSigner: false, isWritable: true },
{ pubkey: farmingMintPubKey, isSigner: false, isWritable: true },
{ pubkey: associatedFarmingTokenVaultAddress, isSigner: false, isWritable: true },
{ pubkey: spl_token_1.TOKEN_PROGRAM_ID, isSigner: false, isWritable: false },
{ pubkey: web3_js_1.SystemProgram.programId, isSigner: false, isWritable: false },
{ pubkey: web3_js_1.SYSVAR_RENT_PUBKEY, isSigner: false, isWritable: false },
{ pubkey: web3_js_1.SYSVAR_CLOCK_PUBKEY, isSigner: false, isWritable: false },
{ pubkey: spl_token_1.ASSOCIATED_TOKEN_PROGRAM_ID, isSigner: false, isWritable: false },
],
data: Buffer.from(Uint8Array.of(8, ...new BN(topup_amount).toArray('le', 8))),
});
const transaction = new web3_js_1.Transaction().add(TopupStakingPoolIx);
void (yield sendTxn(transaction));
});
}
exports.topupStakingPool = topupStakingPool;
function decreaseStakingPool({ decrease_amount, adminPublicKey, farmingMintPubKey, programPubkey, sendTxn }) {
return __awaiter(this, void 0, void 0, function* () {
const encoder = new TextEncoder();
const [farming_vault_owner_pubkey] = yield web3_js_1.PublicKey.findProgramAddress([encoder.encode('farmingvaultowner')], programPubkey);
const [pool_config_pubkey,] = yield web3_js_1.PublicKey.findProgramAddress([encoder.encode('poolconfig')], programPubkey);
const [cumulativePubkey] = yield web3_js_1.PublicKey.findProgramAddress([encoder.encode('cumulativeconfig')], programPubkey);
const associatedAdminTokenAccountAddress = yield findAssociatedTokenAddress(adminPublicKey, farmingMintPubKey);
const associatedFarmingTokenVaultAddress = yield findAssociatedTokenAddress(farming_vault_owner_pubkey, farmingMintPubKey);
const decreaseStakingPoolIx = new web3_js_1.TransactionInstruction({
programId: programPubkey,
keys: [
{ pubkey: adminPublicKey, isSigner: true, isWritable: true },
{ pubkey: associatedAdminTokenAccountAddress, isSigner: false, isWritable: true },
{ pubkey: cumulativePubkey, isSigner: false, isWritable: true },
{ pubkey: pool_config_pubkey, isSigner: false, isWritable: true },
{ pubkey: farming_vault_owner_pubkey, isSigner: false, isWritable: true },
{ pubkey: farmingMintPubKey, isSigner: false, isWritable: true },
{ pubkey: associatedFarmingTokenVaultAddress, isSigner: false, isWritable: true },
{ pubkey: spl_token_1.TOKEN_PROGRAM_ID, isSigner: false, isWritable: false },
{ pubkey: web3_js_1.SystemProgram.programId, isSigner: false, isWritable: false },
{ pubkey: web3_js_1.SYSVAR_RENT_PUBKEY, isSigner: false, isWritable: false },
{ pubkey: web3_js_1.SYSVAR_CLOCK_PUBKEY, isSigner: false, isWritable: false },
{ pubkey: spl_token_1.ASSOCIATED_TOKEN_PROGRAM_ID, isSigner: false, isWritable: false },
],
data: Buffer.from(Uint8Array.of(9, ...new BN(decrease_amount).toArray('le', 8))),
});
const transaction = new web3_js_1.Transaction().add(decreaseStakingPoolIx);
void (yield sendTxn(transaction));
});
}
exports.decreaseStakingPool = decreaseStakingPool;