frakt-client
Version:
Client library for interacting with FRAKT solana program
178 lines (177 loc) • 11.7 kB
JavaScript
;
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.initializeConfigs = exports.approxed = exports.getAllProgramAccounts = exports.initializeCumulativeAccount = exports.unstakeFRKT = exports.getTokenBalance = exports.harvestFRKT = exports.stakeFRKT = exports.topupStakingPool = exports.initializePoolConfig = exports.createAndMintTokens = exports.establishConnectionDevnet = exports.establishConnection = void 0;
const index_stake = __importStar(require("./index"));
const web3_js_1 = require("@solana/web3.js");
const spl_token_1 = require("@solana/spl-token");
const BN = require('bn.js');
function establishConnection() {
return __awaiter(this, void 0, void 0, function* () {
const rpcUrl = 'http://127.0.0.1:8899';
const connection = new web3_js_1.Connection(rpcUrl, 'confirmed');
const version = yield connection.getVersion();
console.log('Connection to cluster established:', rpcUrl, version);
return connection;
});
}
exports.establishConnection = establishConnection;
function establishConnectionDevnet() {
return __awaiter(this, void 0, void 0, function* () {
const rpcUrl = 'https://api.devnet.solana.com';
const connection = new web3_js_1.Connection(rpcUrl, 'processed');
const version = yield connection.getVersion();
console.log('Connection to cluster established:', rpcUrl, version);
return connection;
});
}
exports.establishConnectionDevnet = establishConnectionDevnet;
function createAndMintTokens(amount, connection, adminKeypair, someMan) {
return __awaiter(this, void 0, void 0, function* () {
let sendTxnAdmin = (txn) => __awaiter(this, void 0, void 0, function* () { return void connection.sendTransaction(txn, [adminKeypair]); });
const result = yield index_stake.createAndMintSomeTokens(amount, 8, adminKeypair.publicKey, sendTxnAdmin, { connection });
yield new Promise(f => setTimeout(f, 1400));
if (someMan != null) {
const receiverTokenAccAddress = yield index_stake.findAssociatedTokenAddress(someMan.publicKey, new web3_js_1.PublicKey(result.mintAddress));
const createAssociatedTokenAccountIx = spl_token_1.Token.createAssociatedTokenAccountInstruction(spl_token_1.ASSOCIATED_TOKEN_PROGRAM_ID, spl_token_1.TOKEN_PROGRAM_ID, new web3_js_1.PublicKey(result.mintAddress), receiverTokenAccAddress, someMan.publicKey, adminKeypair.publicKey);
const instruction = spl_token_1.Token.createTransferInstruction(spl_token_1.TOKEN_PROGRAM_ID, new web3_js_1.PublicKey(result.tokenAccAddress), receiverTokenAccAddress, adminKeypair.publicKey, // new PublicKey(result.mintAddress),
[], Math.floor(amount / 3));
const transaction = new web3_js_1.Transaction();
transaction.add(createAssociatedTokenAccountIx);
transaction.add(instruction);
void (yield sendTxnAdmin(transaction));
return { mintAddress: new web3_js_1.PublicKey(result.mintAddress), tokenAccAddress: new web3_js_1.PublicKey(result.tokenAccAddress),
someAccAddress: receiverTokenAccAddress };
}
return { mintAddress: new web3_js_1.PublicKey(result.mintAddress), tokenAccAddress: new web3_js_1.PublicKey(result.tokenAccAddress) };
});
}
exports.createAndMintTokens = createAndMintTokens;
function initializePoolConfig(adminKeypair, mintPubkey, connection, fraktProgramPubKey) {
return __awaiter(this, void 0, void 0, function* () {
const harvest_unlock_at = Math.floor(Date.now() / 1000);
void (yield index_stake.initializePoolConfig(10, harvest_unlock_at, mintPubkey, adminKeypair.publicKey, fraktProgramPubKey, (txn) => __awaiter(this, void 0, void 0, function* () { return void connection.sendTransaction(txn, [adminKeypair]); }), { connection }));
});
}
exports.initializePoolConfig = initializePoolConfig;
function topupStakingPool(amount, adminKeypair, farmingMintPubKey, connection, programPubkey) {
return __awaiter(this, void 0, void 0, function* () {
void (yield index_stake.topupStakingPool({ topup_amount: amount, adminPublicKey: adminKeypair.publicKey,
farmingMintPubKey, programPubkey, sendTxn: (txn) => __awaiter(this, void 0, void 0, function* () { return void connection.sendTransaction(txn, [adminKeypair]); }) }));
});
}
exports.topupStakingPool = topupStakingPool;
function stakeFRKT(amount, connection, programPubkey, userKey, mintPubkey) {
return __awaiter(this, void 0, void 0, function* () {
let sendTxn = (txn) => __awaiter(this, void 0, void 0, function* () { return void connection.sendTransaction(txn, [userKey]); });
const userPublicKey = userKey.publicKey;
const stakeAccountGiven = null;
yield index_stake.stakeFRKT({ amount, programPubkey, userPublicKey, mintPubkey, connection, sendTxn, stakeAccountGiven });
});
}
exports.stakeFRKT = stakeFRKT;
function harvestFRKT(connection, programPubkey, userKey, mintPubkey) {
return __awaiter(this, void 0, void 0, function* () {
const sendTxn = (txn) => __awaiter(this, void 0, void 0, function* () { return void connection.sendTransaction(txn, [userKey]); });
const userPublicKey = userKey.publicKey;
let allAccounts = yield connection.getProgramAccounts(programPubkey, 'singleGossip');
const stakeFRKTAccounts = allAccounts
.filter((rawStakeFRKT) => rawStakeFRKT.account.data.length == index_stake.STAKE_FRKT_ACCOUNT_DATA_LAYOUT.span)
.map(rawStakeFRKT => index_stake.decodedStakeFRKTBuffersToUI(index_stake.STAKE_FRKT_ACCOUNT_DATA_LAYOUT.decode(rawStakeFRKT.account.data), rawStakeFRKT.pubkey));
const currentTimeStamp = Math.floor(Date.now() / 1000);
const customerStakeFRKTAccounts = stakeFRKTAccounts.filter((rawStakeFRKT) => (rawStakeFRKT.stake_owner == userPublicKey.toBase58()) &&
(rawStakeFRKT.is_staked == true) && (rawStakeFRKT.mint_pubkey == mintPubkey.toBase58()));
const customerFRKTAccounts = [];
customerStakeFRKTAccounts.forEach(rawStakeFRKT => customerFRKTAccounts.push(new web3_js_1.PublicKey(rawStakeFRKT.stakeAccountPubkey)));
yield index_stake.harvestFRKT({ programPubkey,
userPublicKey, mintPubkey, sendTxn, connection, customerFRKTAccounts });
});
}
exports.harvestFRKT = harvestFRKT;
exports.getTokenBalance = (pubkey, connection) => __awaiter(void 0, void 0, void 0, function* () {
return parseInt((yield connection.getTokenAccountBalance(pubkey)).value.amount);
});
function unstakeFRKT(connection, programPubkey, userKey, mintPubkey) {
return __awaiter(this, void 0, void 0, function* () {
const sendTxn = (txn) => __awaiter(this, void 0, void 0, function* () { return void connection.sendTransaction(txn, [userKey]); });
const userPublicKey = userKey.publicKey;
let allAccounts = yield connection.getProgramAccounts(programPubkey, 'singleGossip');
const stakeFRKTAccounts = allAccounts
.filter((rawStakeFRKT) => rawStakeFRKT.account.data.length == index_stake.STAKE_FRKT_ACCOUNT_DATA_LAYOUT.span)
.map(rawStakeFRKT => index_stake.decodedStakeFRKTBuffersToUI(index_stake.STAKE_FRKT_ACCOUNT_DATA_LAYOUT.decode(rawStakeFRKT.account.data), rawStakeFRKT.pubkey));
const currentTimeStamp = Math.floor(Date.now() / 1000);
const customerStakeFRKTAccounts = stakeFRKTAccounts.filter((rawStakeFRKT) => (rawStakeFRKT.stake_owner == userPublicKey.toBase58()) &&
(rawStakeFRKT.is_staked == true) && (rawStakeFRKT.mint_pubkey == mintPubkey.toBase58()) &&
(rawStakeFRKT.stake_end_at < currentTimeStamp));
let customerFRKTAccounts = [];
customerStakeFRKTAccounts.forEach(rawStakeFRKT => customerFRKTAccounts.push(new web3_js_1.PublicKey(rawStakeFRKT.stakeAccountPubkey)));
yield index_stake.unstakeFRKT({ programPubkey,
userPublicKey, mintPubkey, sendTxn, connection, customerFRKTAccounts });
});
}
exports.unstakeFRKT = unstakeFRKT;
function initializeCumulativeAccount(connection, programPubkey, userKey) {
return __awaiter(this, void 0, void 0, function* () {
const sendTxn = (txn) => __awaiter(this, void 0, void 0, function* () { return void connection.sendTransaction(txn, [userKey]); });
const userPublicKey = userKey.publicKey;
yield index_stake.initializeCumulativeAccount({ programPubkey, userPublicKey, sendTxn });
});
}
exports.initializeCumulativeAccount = initializeCumulativeAccount;
function getAllProgramAccounts(launchpdProgramPubKey, { connection }) {
return __awaiter(this, void 0, void 0, function* () {
const accounts = yield index_stake.getAllProgramAccounts(launchpdProgramPubKey, { connection });
console.log(accounts);
return accounts.cumulativeAccount.apr;
});
}
exports.getAllProgramAccounts = getAllProgramAccounts;
function approxed(v1, v2, epsilon) {
if (epsilon == null) {
epsilon = 0.001;
}
return Math.abs(v1 - v2) < epsilon;
}
exports.approxed = approxed;
;
function initializeConfigs(adminKeypair, farmingMintPubkey, connection, fraktProgramPubKey) {
return __awaiter(this, void 0, void 0, function* () {
void (yield index_stake.initializeConfigs({ farmingMintPubkey, adminPubkey: adminKeypair.publicKey,
programPubkey: fraktProgramPubKey, sendTxn: (txn) => __awaiter(this, void 0, void 0, function* () { return void connection.sendTransaction(txn, [adminKeypair]); }), connection }));
});
}
exports.initializeConfigs = initializeConfigs;
function decreaseStakingPool(amount, adminKeypair, farmingMintPubKey, connection, programPubkey) {
return __awaiter(this, void 0, void 0, function* () {
void (yield index_stake.decreaseStakingPool({ decrease_amount: amount, adminPublicKey: adminKeypair.publicKey,
farmingMintPubKey, programPubkey, sendTxn: (txn) => __awaiter(this, void 0, void 0, function* () { return void connection.sendTransaction(txn, [adminKeypair]); }) }));
});
}
exports.decreaseStakingPool = decreaseStakingPool;