@frakt-protocol/frakt-sdk
Version:
Frakt SDK for interacting with frakt.xyz protocols
159 lines (158 loc) • 6.18 kB
JavaScript
;
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.getSuggestedLoans = exports.createAssociatedTokenAccountInstruction = exports.getTokenBalance = exports.findAssociatedTokenAddress = exports.createFakeWallet = void 0;
const anchor_1 = require("@project-serum/anchor");
const nodewallet_1 = require("./classes/nodewallet");
//when we only want to view vaults, no need to connect a real wallet.
const createFakeWallet = () => {
const leakedKp = anchor_1.web3.Keypair.fromSecretKey(Uint8Array.from([
208, 175, 150, 242, 88, 34, 108, 88, 177, 16, 168, 75, 115, 181, 199, 242, 120, 4, 78, 75, 19, 227, 13, 215, 184,
108, 226, 53, 111, 149, 179, 84, 137, 121, 79, 1, 160, 223, 124, 241, 202, 203, 220, 237, 50, 242, 57, 158, 226,
207, 203, 188, 43, 28, 70, 110, 214, 234, 251, 15, 249, 157, 62, 80,
]));
return new nodewallet_1.NodeWallet(leakedKp);
};
exports.createFakeWallet = createFakeWallet;
const findAssociatedTokenAddress = (walletAddress, tokenMintAddress) => __awaiter(void 0, void 0, void 0, function* () {
return (yield anchor_1.web3.PublicKey.findProgramAddress([walletAddress.toBuffer(), anchor_1.utils.token.TOKEN_PROGRAM_ID.toBuffer(), tokenMintAddress.toBuffer()], anchor_1.utils.token.ASSOCIATED_PROGRAM_ID))[0];
});
exports.findAssociatedTokenAddress = findAssociatedTokenAddress;
const getTokenBalance = (pubkey, connection) => __awaiter(void 0, void 0, void 0, function* () {
const balance = yield connection.getTokenAccountBalance(pubkey);
return parseInt(balance.value.amount);
});
exports.getTokenBalance = getTokenBalance;
const createAssociatedTokenAccountInstruction = (associatedTokenAddress, payer, walletAddress, splTokenMintAddress) => {
const keys = [
{
pubkey: payer,
isSigner: true,
isWritable: true,
},
{
pubkey: associatedTokenAddress,
isSigner: false,
isWritable: true,
},
{
pubkey: walletAddress,
isSigner: false,
isWritable: false,
},
{
pubkey: splTokenMintAddress,
isSigner: false,
isWritable: false,
},
{
pubkey: anchor_1.web3.SystemProgram.programId,
isSigner: false,
isWritable: false,
},
{
pubkey: anchor_1.utils.token.TOKEN_PROGRAM_ID,
isSigner: false,
isWritable: false,
},
{
pubkey: anchor_1.web3.SYSVAR_RENT_PUBKEY,
isSigner: false,
isWritable: false,
},
];
return [
new anchor_1.web3.TransactionInstruction({
keys,
programId: anchor_1.utils.token.ASSOCIATED_PROGRAM_ID,
data: Buffer.from([]),
}),
];
};
exports.createAssociatedTokenAccountInstruction = createAssociatedTokenAccountInstruction;
const getSuggestedLoans = (items, minValue) => {
let sum = 0;
let i = 0;
const best = [];
const cheapest = [];
const safest = [];
const sortedElementsByValue = items.sort((a, b) => {
if (a.maxLoanValue !== b.maxLoanValue) {
return a.maxLoanValue - b.maxLoanValue;
}
return a.interest - b.interest;
});
const sortedElementsByInterest = items.sort((a, b) => {
if (a.interest !== b.interest) {
return a.interest - b.interest;
}
else if (a.maxLoanValue !== b.maxLoanValue) {
return a.maxLoanValue - b.maxLoanValue;
}
return a.amountOfDays - b.amountOfDays;
});
const priceBased = sortedElementsByInterest.filter((element) => element.maxLoanValue !== element.minLoanValue);
const timeBased = sortedElementsByInterest.filter((element) => element.maxLoanValue === element.minLoanValue);
const concated = priceBased.concat(timeBased);
while (sum < minValue && i < sortedElementsByValue.length) {
best.push({
nftMint: sortedElementsByValue[i].nftMint,
loanValue: sortedElementsByValue[i].maxLoanValue,
interest: sortedElementsByValue[i].interest,
amountOfDays: sortedElementsByValue[i].amountOfDays
});
sum += sortedElementsByValue[i].maxLoanValue;
i += 1;
}
if (sum < minValue) {
return {
best: best,
safest: best,
cheapest: best,
};
}
sum = 0;
i = 0;
while (sum < minValue && i < concated.length) {
cheapest.push({
nftMint: concated[i].nftMint,
loanValue: concated[i].maxLoanValue,
interest: concated[i].interest,
amountOfDays: concated[i].amountOfDays
});
sum += concated[i].maxLoanValue;
i += 1;
}
sum = 0;
i = 0;
while (sum < minValue && i < concated.length) {
safest.push({
nftMint: concated[i].nftMint,
loanValue: concated[i].minLoanValue,
interest: concated[i].interest,
amountOfDays: concated[i].amountOfDays
});
sum += concated[i].minLoanValue;
i += 1;
}
i = 0;
while (sum < minValue) {
sum += (concated[i].maxLoanValue - safest[i].loanValue);
safest[i].loanValue = concated[i].maxLoanValue;
i += 1;
}
return {
best,
safest,
cheapest,
};
};
exports.getSuggestedLoans = getSuggestedLoans;