frakt-client
Version:
Client library for interacting with FRAKT solana program
546 lines (545 loc) • 35.7 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.testZeroOutput = exports.testLowPool = exports.wrongSeedAssociated = exports.testWrongUnstake = exports.testMoreThanHaveInStake = exports.testLowStake = exports.testUnstakeBefore = exports.testDoubleStake = void 0;
const index = __importStar(require("./index_test"));
const index_stake = __importStar(require("./index"));
const main_index = __importStar(require("../index"));
const web3_js_1 = require("@solana/web3.js");
const spl_token_1 = require("@solana/spl-token");
const BN = require('bn.js');
const adminString = "[35,167,28,212,157,40,7,232,157,15,244,104,104,129,246,80,90,28,159,32,58,206,218,200,196,170,241,149,64,101,127,90,95,82,101,81,131,101,12,178,236,179,116,177,75,187,129,44,192,106,126,138,11,226,83,147,28,244,244,204,252,54,36,167]";
const fraktProgramKeyString = "[63,228,65,24,42,174,226,246,25,249,93,230,14,101,238,23,106,118,10,7,176,57,248,11,173,166,50,76,235,137,145,240,47,217,106,209,52,65,171,125,139,47,226,51,84,11,136,128,113,27,21,174,198,144,98,136,225,113,64,238,29,252,163,11]";
const someManString = "[66,190,236,91,27,178,55,187,92,188,204,75,211,92,202,6,103,248,49,211,226,122,252,197,168,157,146,161,205,83,91,237,64,185,53,61,160,23,27,211,177,35,127,91,77,65,38,159,211,156,189,24,66,83,255,212,199,47,243,33,39,52,152,55]";
function main() {
return __awaiter(this, void 0, void 0, function* () {
// await mathTest1(100*1e8, 100*1e8, 50*1e8)
yield mathTest2(100 * 1e8, 100 * 1e8, 50 * 1e8, 25 * 1e8);
// await testDoubleStake()
});
}
function mathTest1(pool_amount, stake1_amount, stake2_amount) {
return __awaiter(this, void 0, void 0, function* () {
console.log("Connection");
let connection = yield index.establishConnection();
const secretKey = Uint8Array.from(JSON.parse(adminString));
const adminKeypair = web3_js_1.Keypair.fromSecretKey(secretKey);
const fraktkey = Uint8Array.from(JSON.parse(fraktProgramKeyString));
const fraktKeyPair = web3_js_1.Keypair.fromSecretKey(fraktkey);
const someManKey = web3_js_1.Keypair.generate();
yield connection.requestAirdrop(adminKeypair.publicKey, 200 * 1e8);
yield connection.requestAirdrop(someManKey.publicKey, 200 * 1e8);
yield new Promise(f => setTimeout(f, 2000));
console.log(yield connection.getBalance(someManKey.publicKey));
console.log("minting");
let result = yield index.createAndMintTokens((pool_amount + stake2_amount + stake1_amount) * 2, connection, adminKeypair, someManKey);
yield new Promise(f => setTimeout(f, 2000));
console.log("configs initialization");
yield index.initializeConfigs(adminKeypair, result.mintAddress, connection, fraktKeyPair.publicKey);
// console.log("pool initialization");
// await index.initializePoolConfig(adminKeypair, result.mintAddress, connection, fraktKeyPair.publicKey);
// console.log("cumulative");
// await index.initializeCumulativeAccount(connection, fraktKeyPair.publicKey, adminKeypair);
yield new Promise(f => setTimeout(f, 2000));
console.log("fill_with_tokens");
yield index.topupStakingPool(pool_amount, adminKeypair, result.mintAddress, connection, fraktKeyPair.publicKey);
yield new Promise(f => setTimeout(f, 3000));
let balance_was = yield index.getTokenBalance(result.tokenAccAddress, connection);
console.log(balance_was);
console.log("stake_frkt");
yield index.stakeFRKT(stake1_amount, connection, fraktKeyPair.publicKey, adminKeypair, result.mintAddress);
yield new Promise(f => setTimeout(f, 9000));
let cumulative = yield index.getAllProgramAccounts(fraktKeyPair.publicKey, { connection });
yield new Promise(f => setTimeout(f, 21000));
console.log(cumulative);
let first_apr = cumulative;
console.log("second stake_frkt");
console.log(stake2_amount);
yield index.stakeFRKT(stake2_amount, connection, fraktKeyPair.publicKey, someManKey, result.mintAddress);
yield new Promise(f => setTimeout(f, 4000));
cumulative = yield index.getAllProgramAccounts(fraktKeyPair.publicKey, { connection });
yield new Promise(f => setTimeout(f, 16000));
console.log(cumulative);
let second_apr = cumulative;
yield index.unstakeFRKT(connection, fraktKeyPair.publicKey, adminKeypair, result.mintAddress);
yield new Promise(f => setTimeout(f, 1000));
let balance_now = yield index.getTokenBalance(result.tokenAccAddress, connection);
console.log('Success');
console.log("balance was");
console.log(balance_was);
console.log("balance now");
console.log(balance_now);
console.log(balance_now - balance_was);
console.log((first_apr * 30 + second_apr * 20) * stake1_amount / 31536000 / 100);
console.log(index.approxed(balance_now - balance_was, (first_apr * 30 + second_apr * 20) * stake1_amount / 31536000 / 100, 2));
});
}
function mathTest2(pool_amount, stake1_amount, stake2_amount, pool_decrease_amount) {
return __awaiter(this, void 0, void 0, function* () {
console.log("Connection");
let connection = yield index.establishConnection();
const secretKey = Uint8Array.from(JSON.parse(adminString));
const adminKeypair = web3_js_1.Keypair.fromSecretKey(secretKey);
// const fraktkey = Uint8Array.from(JSON.parse(fraktProgramKeyString));
// const fraktKeyPair = Keypair.fromSecretKey(fraktkey);
const fraktProgramPubKey = new web3_js_1.PublicKey("4DnSukrEE6Pz6eFE7Gp4XTH6LYm1UVcKqZdJC2wz4ggr");
const someManKey = web3_js_1.Keypair.generate();
yield connection.requestAirdrop(adminKeypair.publicKey, 200 * 1e8);
yield connection.requestAirdrop(someManKey.publicKey, 200 * 1e8);
yield new Promise(f => setTimeout(f, 2000));
console.log(yield connection.getBalance(someManKey.publicKey));
console.log("minting");
let result = yield index.createAndMintTokens((pool_amount + stake2_amount + stake1_amount) * 2, connection, adminKeypair, someManKey);
// let result = {tokenAccAddress: new PublicKey("BEFCSykHqZ21czSM8BS6AHKjpaMNhKevHjbkPu2oJ4dk"),
// mintAddress: new PublicKey("9J61fwAyi5uqQPyy1mKrZrKqCHNaDnxnzDNkCRnaycL8")
// }
yield new Promise(f => setTimeout(f, 2000));
console.log("configs initialization");
yield index.initializeConfigs(adminKeypair, result.mintAddress, connection, fraktProgramPubKey);
// console.log("pool initialization");
// await index.initializePoolConfig(adminKeypair, result.mintAddress, connection, fraktProgramPubKey);
// console.log("cumulative");
// await index.initializeCumulativeAccount(connection, fraktProgramPubKey, adminKeypair);
yield new Promise(f => setTimeout(f, 2000));
console.log("fill_with_tokens");
yield index.topupStakingPool(pool_amount, adminKeypair, result.mintAddress, connection, fraktProgramPubKey);
yield new Promise(f => setTimeout(f, 3000));
let balance_was = yield index.getTokenBalance(result.tokenAccAddress, connection);
console.log(balance_was);
console.log("stake_frkt");
yield index.stakeFRKT(stake1_amount, connection, fraktProgramPubKey, adminKeypair, result.mintAddress);
yield new Promise(f => setTimeout(f, 6000));
let cumulative = yield index.getAllProgramAccounts(fraktProgramPubKey, { connection });
yield new Promise(f => setTimeout(f, 24000));
console.log(cumulative);
let first_apr = cumulative;
console.log("decrease pool amount");
yield new Promise(f => setTimeout(f, 300));
yield index.decreaseStakingPool(pool_decrease_amount, adminKeypair, result.mintAddress, connection, fraktProgramPubKey);
yield new Promise(f => setTimeout(f, 3000));
cumulative = yield index.getAllProgramAccounts(fraktProgramPubKey, { connection });
yield new Promise(f => setTimeout(f, 12300));
console.log(cumulative);
let second_apr = cumulative;
console.log("second stake_frkt");
console.log(stake2_amount);
yield index.stakeFRKT(stake2_amount, connection, fraktProgramPubKey, someManKey, result.mintAddress);
yield new Promise(f => setTimeout(f, 4000));
cumulative = yield index.getAllProgramAccounts(fraktProgramPubKey, { connection });
yield new Promise(f => setTimeout(f, 16000));
console.log(cumulative);
let third_apr = cumulative;
console.log("unstake");
yield index.unstakeFRKT(connection, fraktProgramPubKey, adminKeypair, result.mintAddress);
yield new Promise(f => setTimeout(f, 3000));
let balance_now = yield index.getTokenBalance(result.tokenAccAddress, connection);
cumulative = yield index.getAllProgramAccounts(fraktProgramPubKey, { connection });
console.log('Success');
console.log("balance was");
console.log(balance_was);
console.log("balance now");
console.log(balance_now - pool_decrease_amount);
console.log(balance_now - balance_was - pool_decrease_amount);
let counted_return = (first_apr * 30 + second_apr * 16 + third_apr * 20) * stake1_amount / 31536000 / 100;
console.log(counted_return);
console.log(index.approxed(balance_now - balance_was - pool_decrease_amount, counted_return, 2));
});
}
function testDoubleStake() {
return __awaiter(this, void 0, void 0, function* () {
let connection = yield index.establishConnection();
const secretKey = Uint8Array.from(JSON.parse(adminString));
const adminKeypair = web3_js_1.Keypair.fromSecretKey(secretKey);
const fraktkey = Uint8Array.from(JSON.parse(fraktProgramKeyString));
const fraktKeyPair = web3_js_1.Keypair.fromSecretKey(fraktkey);
yield connection.requestAirdrop(adminKeypair.publicKey, 100000000000);
yield new Promise(f => setTimeout(f, 2000));
console.log("minting");
let result = yield index.createAndMintTokens(100 * 1e8, connection, adminKeypair, null);
yield new Promise(f => setTimeout(f, 2000));
console.log("pool initialization");
yield index.initializePoolConfig(adminKeypair, result.mintAddress, connection, fraktKeyPair.publicKey);
yield new Promise(f => setTimeout(f, 2000));
console.log("fill_with_tokens");
yield index.topupStakingPool(50 * 1e8, adminKeypair, result.mintAddress, connection, fraktKeyPair.publicKey);
yield new Promise(f => setTimeout(f, 2000));
console.log("cumulative");
yield index.initializeCumulativeAccount(connection, fraktKeyPair.publicKey, adminKeypair);
yield new Promise(f => setTimeout(f, 2000));
let sendTxn = (txn) => __awaiter(this, void 0, void 0, function* () { return void connection.sendTransaction(txn, [adminKeypair]); });
const userPublicKey = adminKeypair.publicKey;
let stakeAccountGiven = null;
yield index_stake.stakeFRKT({ amount: 1e7, programPubkey: fraktKeyPair.publicKey, userPublicKey, mintPubkey: result.mintAddress, connection, sendTxn, stakeAccountGiven });
yield new Promise(f => setTimeout(f, 3000));
let allAccounts = yield connection.getProgramAccounts(fraktKeyPair.publicKey, 'singleGossip');
let 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));
console.log(stakeFRKTAccounts);
stakeAccountGiven = new web3_js_1.PublicKey(stakeFRKTAccounts[0].stakeAccountPubkey);
try {
yield index_stake.stakeFRKT({ amount: 1e7, programPubkey: fraktKeyPair.publicKey, userPublicKey, mintPubkey: result.mintAddress, connection, sendTxn, stakeAccountGiven });
yield new Promise(f => setTimeout(f, 3000));
}
catch (e) {
let result = e.message; // error under useUnknownInCatchVariables
if (typeof e === "string") {
console.log(e.toUpperCase());
e.toUpperCase(); // works, `e` narrowed to string
}
else if (e instanceof Error) {
console.log(e.message);
e.message; // works, `e` narrowed to Error
}
}
allAccounts = yield connection.getProgramAccounts(fraktKeyPair.publicKey, 'singleGossip');
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));
console.log(stakeFRKTAccounts);
yield new Promise(f => setTimeout(f, 40000));
let customerFrktAccounts = [];
customerFrktAccounts.push(stakeAccountGiven);
yield index_stake.unstakeFRKT({ programPubkey: fraktKeyPair.publicKey, userPublicKey, mintPubkey: result.mintAddress, connection, sendTxn, customerFRKTAccounts: customerFrktAccounts });
});
}
exports.testDoubleStake = testDoubleStake;
function testUnstakeBefore() {
return __awaiter(this, void 0, void 0, function* () {
let connection = yield index.establishConnection();
const secretKey = Uint8Array.from(JSON.parse(adminString));
const adminKeypair = web3_js_1.Keypair.fromSecretKey(secretKey);
const fraktkey = Uint8Array.from(JSON.parse(fraktProgramKeyString));
const fraktKeyPair = web3_js_1.Keypair.fromSecretKey(fraktkey);
yield connection.requestAirdrop(adminKeypair.publicKey, 100000000000);
yield new Promise(f => setTimeout(f, 2000));
console.log("minting");
let result = yield index.createAndMintTokens(100 * 1e8, connection, adminKeypair, null);
yield new Promise(f => setTimeout(f, 2000));
console.log("pool initialization");
yield index.initializePoolConfig(adminKeypair, result.mintAddress, connection, fraktKeyPair.publicKey);
yield new Promise(f => setTimeout(f, 2000));
console.log("fill_with_tokens");
yield index.topupStakingPool(50 * 1e8, adminKeypair, result.mintAddress, connection, fraktKeyPair.publicKey);
yield new Promise(f => setTimeout(f, 2000));
console.log("cumulative");
yield index.initializeCumulativeAccount(connection, fraktKeyPair.publicKey, adminKeypair);
yield new Promise(f => setTimeout(f, 2000));
let sendTxn = (txn) => __awaiter(this, void 0, void 0, function* () { return void connection.sendTransaction(txn, [adminKeypair]); });
const userPublicKey = adminKeypair.publicKey;
let stakeAccountGiven = null;
let programPubkey = fraktKeyPair.publicKey;
let mintPubkey = result.mintAddress;
yield index_stake.stakeFRKT({ amount: 1e7, programPubkey, userPublicKey, mintPubkey, connection, sendTxn, stakeAccountGiven });
yield new Promise(f => setTimeout(f, 3000));
let allAccounts = yield connection.getProgramAccounts(programPubkey, 'singleGossip');
let 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));
console.log(stakeFRKTAccounts);
stakeAccountGiven = new web3_js_1.PublicKey(stakeFRKTAccounts[0].stakeAccountPubkey);
allAccounts = yield connection.getProgramAccounts(programPubkey, 'singleGossip');
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));
console.log(stakeFRKTAccounts);
yield new Promise(f => setTimeout(f, 20000));
let customerFrktAccounts = [];
customerFrktAccounts.push(stakeAccountGiven);
yield index_stake.unstakeFRKT({ programPubkey, userPublicKey, mintPubkey, connection, sendTxn, customerFRKTAccounts: customerFrktAccounts });
});
}
exports.testUnstakeBefore = testUnstakeBefore;
function testLowStake() {
return __awaiter(this, void 0, void 0, function* () {
let connection = yield index.establishConnection();
const secretKey = Uint8Array.from(JSON.parse(adminString));
const adminKeypair = web3_js_1.Keypair.fromSecretKey(secretKey);
const fraktkey = Uint8Array.from(JSON.parse(fraktProgramKeyString));
const fraktKeyPair = web3_js_1.Keypair.fromSecretKey(fraktkey);
yield connection.requestAirdrop(adminKeypair.publicKey, 100000000000);
yield new Promise(f => setTimeout(f, 2000));
console.log("minting");
let result = yield index.createAndMintTokens(100 * 1e8, connection, adminKeypair, null);
yield new Promise(f => setTimeout(f, 2000));
yield new Promise(f => setTimeout(f, 2000));
console.log("fill_with_tokens");
yield index.topupStakingPool(100 * 1e8, adminKeypair, result.mintAddress, connection, fraktKeyPair.publicKey);
yield new Promise(f => setTimeout(f, 2000));
yield new Promise(f => setTimeout(f, 2000));
let sendTxn = (txn) => __awaiter(this, void 0, void 0, function* () { return void connection.sendTransaction(txn, [adminKeypair]); });
const userPublicKey = adminKeypair.publicKey;
let stakeAccountGiven = null;
let programPubkey = fraktKeyPair.publicKey;
let mintPubkey = result.mintAddress;
yield index_stake.stakeFRKT({ amount: 2, programPubkey, userPublicKey, mintPubkey, connection, sendTxn, stakeAccountGiven });
yield new Promise(f => setTimeout(f, 3000));
let allAccounts = yield connection.getProgramAccounts(programPubkey, 'singleGossip');
let 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));
console.log(stakeFRKTAccounts);
stakeAccountGiven = new web3_js_1.PublicKey(stakeFRKTAccounts[0].stakeAccountPubkey);
yield new Promise(f => setTimeout(f, 40000));
let customerFrktAccounts = [];
customerFrktAccounts.push(stakeAccountGiven);
yield index_stake.unstakeFRKT({ programPubkey, userPublicKey, mintPubkey, connection, sendTxn, customerFRKTAccounts: customerFrktAccounts });
});
}
exports.testLowStake = testLowStake;
function testMoreThanHaveInStake() {
return __awaiter(this, void 0, void 0, function* () {
let connection = yield index.establishConnection();
const secretKey = Uint8Array.from(JSON.parse(adminString));
const adminKeypair = web3_js_1.Keypair.fromSecretKey(secretKey);
const fraktkey = Uint8Array.from(JSON.parse(fraktProgramKeyString));
const fraktKeyPair = web3_js_1.Keypair.fromSecretKey(fraktkey);
yield connection.requestAirdrop(adminKeypair.publicKey, 100000000000);
yield new Promise(f => setTimeout(f, 2000));
console.log("minting");
let result = yield index.createAndMintTokens(100 * 1e8, connection, adminKeypair, null);
yield new Promise(f => setTimeout(f, 2000));
yield new Promise(f => setTimeout(f, 2000));
console.log("fill_with_tokens");
yield index.topupStakingPool(100 * 1e8, adminKeypair, result.mintAddress, connection, fraktKeyPair.publicKey);
yield new Promise(f => setTimeout(f, 2000));
yield new Promise(f => setTimeout(f, 2000));
let sendTxn = (txn) => __awaiter(this, void 0, void 0, function* () { return void connection.sendTransaction(txn, [adminKeypair]); });
const userPublicKey = adminKeypair.publicKey;
let stakeAccountGiven = null;
let programPubkey = fraktKeyPair.publicKey;
let mintPubkey = result.mintAddress;
yield index_stake.stakeFRKT({ amount: 700000 * 1e8, programPubkey, userPublicKey, mintPubkey, connection, sendTxn, stakeAccountGiven });
yield new Promise(f => setTimeout(f, 3000));
});
}
exports.testMoreThanHaveInStake = testMoreThanHaveInStake;
function testWrongUnstake() {
return __awaiter(this, void 0, void 0, function* () {
let connection = yield index.establishConnection();
const secretKey = Uint8Array.from(JSON.parse(adminString));
const adminKeypair = web3_js_1.Keypair.fromSecretKey(secretKey);
const fraktkey = Uint8Array.from(JSON.parse(fraktProgramKeyString));
const fraktKeyPair = web3_js_1.Keypair.fromSecretKey(fraktkey);
const comeManKey = web3_js_1.Keypair.generate();
yield connection.requestAirdrop(adminKeypair.publicKey, 100000000000);
yield connection.requestAirdrop(comeManKey.publicKey, 100000000000);
yield new Promise(f => setTimeout(f, 2000));
console.log("minting");
let result = yield index.createAndMintTokens(100 * 1e8, connection, adminKeypair, null);
yield new Promise(f => setTimeout(f, 2000));
yield new Promise(f => setTimeout(f, 2000));
console.log("fill_with_tokens");
yield index.topupStakingPool(100 * 1e8, adminKeypair, result.mintAddress, connection, fraktKeyPair.publicKey);
yield new Promise(f => setTimeout(f, 2000));
yield new Promise(f => setTimeout(f, 2000));
let sendTxn = (txn) => __awaiter(this, void 0, void 0, function* () { return void connection.sendTransaction(txn, [adminKeypair]); });
let userPublicKey = adminKeypair.publicKey;
let stakeAccountGiven = null;
let programPubkey = fraktKeyPair.publicKey;
let mintPubkey = result.mintAddress;
yield index_stake.stakeFRKT({ amount: 100 * 1e8, programPubkey, userPublicKey, mintPubkey, connection, sendTxn, stakeAccountGiven });
yield new Promise(f => setTimeout(f, 3000));
let allAccounts = yield connection.getProgramAccounts(programPubkey, 'singleGossip');
let 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));
console.log(stakeFRKTAccounts);
stakeAccountGiven = new web3_js_1.PublicKey(stakeFRKTAccounts[0].stakeAccountPubkey);
yield new Promise(f => setTimeout(f, 40000));
let customerFrktAccounts = [];
customerFrktAccounts.push(stakeAccountGiven);
userPublicKey = comeManKey.publicKey;
yield index_stake.unstakeFRKT({ programPubkey, userPublicKey, mintPubkey, connection, sendTxn, customerFRKTAccounts: customerFrktAccounts });
});
}
exports.testWrongUnstake = testWrongUnstake;
function wrongSeedAssociated() {
return __awaiter(this, void 0, void 0, function* () {
let connection = yield index.establishConnection();
const secretKey = Uint8Array.from(JSON.parse(adminString));
const adminKeypair = web3_js_1.Keypair.fromSecretKey(secretKey);
const fraktkey = Uint8Array.from(JSON.parse(fraktProgramKeyString));
const fraktKeyPair = web3_js_1.Keypair.fromSecretKey(fraktkey);
const comeManKey = web3_js_1.Keypair.generate();
yield connection.requestAirdrop(adminKeypair.publicKey, 100000000000);
yield connection.requestAirdrop(comeManKey.publicKey, 100000000000);
yield new Promise(f => setTimeout(f, 2000));
console.log("minting");
let result = yield index.createAndMintTokens(100 * 1e8, connection, adminKeypair, null);
yield new Promise(f => setTimeout(f, 2000));
yield new Promise(f => setTimeout(f, 2000));
console.log("fill_with_tokens");
yield index.topupStakingPool(100 * 1e8, adminKeypair, result.mintAddress, connection, fraktKeyPair.publicKey);
yield new Promise(f => setTimeout(f, 2000));
yield new Promise(f => setTimeout(f, 2000));
let sendTxn = (txn) => __awaiter(this, void 0, void 0, function* () { return void connection.sendTransaction(txn, [adminKeypair]); });
let userPublicKey = adminKeypair.publicKey;
let stakeAccountGiven = null;
let programPubkey = fraktKeyPair.publicKey;
let mintPubkey = result.mintAddress;
yield stakeFRKT({ amount: 100 * 1e8, programPubkey, userPublicKey, mintPubkey, connection, sendTxn, stakeAccountGiven });
yield new Promise(f => setTimeout(f, 3000));
});
}
exports.wrongSeedAssociated = wrongSeedAssociated;
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 = main_index.makeid(8);
const stakeAccounted = yield web3_js_1.PublicKey.createWithSeed(userPublicKey, seedStake, programPubkey);
const size = index_stake.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 main_index.findAssociatedTokenAddress(userPublicKey, programPubkey);
const associatedFarmingVaultTokenAddress = yield main_index.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));
});
}
function testLowPool() {
return __awaiter(this, void 0, void 0, function* () {
let connection = yield index.establishConnection();
const secretKey = Uint8Array.from(JSON.parse(adminString));
const adminKeypair = web3_js_1.Keypair.fromSecretKey(secretKey);
const fraktkey = Uint8Array.from(JSON.parse(fraktProgramKeyString));
const fraktKeyPair = web3_js_1.Keypair.fromSecretKey(fraktkey);
const comeManKey = web3_js_1.Keypair.generate();
yield connection.requestAirdrop(adminKeypair.publicKey, 100000000000);
yield connection.requestAirdrop(comeManKey.publicKey, 100000000000);
yield new Promise(f => setTimeout(f, 2000));
console.log("minting");
let result = yield index.createAndMintTokens(100 * 1e8, connection, adminKeypair, null);
yield new Promise(f => setTimeout(f, 2000));
console.log("fill_with_tokens");
yield index.topupStakingPool(100, adminKeypair, result.mintAddress, connection, fraktKeyPair.publicKey);
yield new Promise(f => setTimeout(f, 2000));
let sendTxn = (txn) => __awaiter(this, void 0, void 0, function* () { return void connection.sendTransaction(txn, [adminKeypair]); });
let userPublicKey = adminKeypair.publicKey;
let stakeAccountGiven = null;
let programPubkey = fraktKeyPair.publicKey;
let mintPubkey = result.mintAddress;
yield index_stake.stakeFRKT({ amount: 100 * 1e8, programPubkey, userPublicKey, mintPubkey, connection, sendTxn, stakeAccountGiven });
yield new Promise(f => setTimeout(f, 3000));
let allAccounts = yield connection.getProgramAccounts(programPubkey, 'singleGossip');
let 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));
console.log(stakeFRKTAccounts);
stakeAccountGiven = new web3_js_1.PublicKey(stakeFRKTAccounts[0].stakeAccountPubkey);
yield new Promise(f => setTimeout(f, 40000));
let customerFrktAccounts = [];
customerFrktAccounts.push(stakeAccountGiven);
userPublicKey = comeManKey.publicKey;
yield index_stake.unstakeFRKT({ programPubkey, userPublicKey, mintPubkey, connection, sendTxn, customerFRKTAccounts: customerFrktAccounts });
});
}
exports.testLowPool = testLowPool;
function testZeroOutput() {
return __awaiter(this, void 0, void 0, function* () {
let connection = yield index.establishConnection();
const secretKey = Uint8Array.from(JSON.parse(adminString));
const adminKeypair = web3_js_1.Keypair.fromSecretKey(secretKey);
const fraktkey = Uint8Array.from(JSON.parse(fraktProgramKeyString));
const fraktKeyPair = web3_js_1.Keypair.fromSecretKey(fraktkey);
const comeManKey = web3_js_1.Keypair.generate();
yield connection.requestAirdrop(adminKeypair.publicKey, 100000000000);
yield connection.requestAirdrop(comeManKey.publicKey, 100000000000);
yield new Promise(f => setTimeout(f, 2000));
console.log("minting");
let result = yield index.createAndMintTokens(100 * 1e8, connection, adminKeypair, null);
yield new Promise(f => setTimeout(f, 2000));
console.log("fill_with_tokens");
yield index.topupStakingPool(1000, adminKeypair, result.mintAddress, connection, fraktKeyPair.publicKey);
yield new Promise(f => setTimeout(f, 2000));
let sendTxn = (txn) => __awaiter(this, void 0, void 0, function* () { return void connection.sendTransaction(txn, [adminKeypair]); });
let userPublicKey = adminKeypair.publicKey;
let stakeAccountGiven = null;
let programPubkey = fraktKeyPair.publicKey;
let mintPubkey = result.mintAddress;
yield index_stake.stakeFRKT({ amount: 100 * 1e8, programPubkey, userPublicKey, mintPubkey, connection, sendTxn, stakeAccountGiven });
yield new Promise(f => setTimeout(f, 3000));
let allAccounts = yield connection.getProgramAccounts(programPubkey, 'singleGossip');
let 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));
console.log(stakeFRKTAccounts);
stakeAccountGiven = new web3_js_1.PublicKey(stakeFRKTAccounts[0].stakeAccountPubkey);
yield new Promise(f => setTimeout(f, 2000));
let customerFrktAccounts = [];
customerFrktAccounts.push(stakeAccountGiven);
userPublicKey = comeManKey.publicKey;
yield index_stake.harvestFRKT({ programPubkey, userPublicKey, mintPubkey, connection, sendTxn, customerFRKTAccounts: customerFrktAccounts });
});
}
exports.testZeroOutput = testZeroOutput;
main().then(() => process.exit(), err => {
console.error(err);
process.exit(-1);
});