@kamino-finance/farms-sdk
Version:
108 lines • 5.28 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (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 () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.createMint = createMint;
exports.createMintFromKeypair = createMintFromKeypair;
exports.getMintDecimals = getMintDecimals;
exports.getAssociatedTokenAddress = getAssociatedTokenAddress;
exports.createAssociatedTokenAccountIdempotentInstruction = createAssociatedTokenAccountIdempotentInstruction;
exports.setupAta = setupAta;
exports.mintTo = mintTo;
const spl_token_1 = require("@solana/spl-token");
const web3_js_1 = require("@solana/web3.js");
const spl_token_2 = require("@solana/spl-token");
const anchor = __importStar(require("@coral-xyz/anchor"));
const utils_1 = require("../commands/utils");
const utils_2 = require("./utils");
async function createMint(provider, authority, decimals = 6) {
const mint = anchor.web3.Keypair.generate();
return await createMintFromKeypair(provider, authority, mint, decimals);
}
async function createMintFromKeypair(provider, authority, mint, decimals = 6) {
const instructions = await createMintInstructions(provider, authority, mint.publicKey, decimals);
const tx = new anchor.web3.Transaction();
tx.add(...instructions);
await provider.sendAndConfirm(tx, [mint]);
return mint.publicKey;
}
async function createMintInstructions(provider, authority, mint, decimals) {
return [
anchor.web3.SystemProgram.createAccount({
fromPubkey: provider.wallet.publicKey,
newAccountPubkey: mint,
space: 82,
lamports: await provider.connection.getMinimumBalanceForRentExemption(82),
programId: spl_token_1.TOKEN_PROGRAM_ID,
}),
(0, spl_token_2.createInitializeMint2Instruction)(mint, decimals, authority, null, spl_token_1.TOKEN_PROGRAM_ID),
];
}
async function getMintDecimals(connection, mintAddress) {
const acc = await connection.getAccountInfo(mintAddress);
if (!acc) {
throw new Error(`Failed to find mint account ${mintAddress.toBase58()}`);
}
return (0, spl_token_1.unpackMint)(mintAddress, acc, acc.owner).decimals;
}
function getAssociatedTokenAddress(owner, tokenMintAddress, tokenProgram) {
return (0, spl_token_1.getAssociatedTokenAddressSync)(tokenMintAddress, owner, true, tokenProgram);
}
async function createAssociatedTokenAccountIdempotentInstruction(owner, mint, payer = owner, tokenProgram, ata) {
let ataAddress = ata;
if (!ataAddress) {
ataAddress = await getAssociatedTokenAddress(owner, mint, tokenProgram);
}
const createUserTokenAccountIx = (0, spl_token_2.createAssociatedTokenAccountIdempotentInstruction)(payer, ataAddress, owner, mint, tokenProgram);
return [ataAddress, createUserTokenAccountIx];
}
async function setupAta(provider, tokenMintAddress, user) {
const ata = await getAssociatedTokenAddress(user.publicKey, tokenMintAddress, spl_token_1.TOKEN_PROGRAM_ID);
if (!(await (0, utils_2.checkIfAccountExists)(provider.connection, ata))) {
const [, ix] = await createAssociatedTokenAccountIdempotentInstruction(user.publicKey, tokenMintAddress, user.publicKey, spl_token_1.TOKEN_PROGRAM_ID, ata);
const tx = new web3_js_1.Transaction().add(ix);
await provider.connection.sendTransaction(tx, [user]);
}
return ata;
}
async function mintTo(provider, mintPubkey, tokenAccount, amount) {
const tx = new web3_js_1.Transaction().add((0, spl_token_1.createMintToInstruction)(mintPubkey, tokenAccount, provider.wallet.publicKey, amount, [], spl_token_1.TOKEN_PROGRAM_ID));
const microLamport = 10 ** 6; // 1 lamport
const computeUnits = 200_000;
const microLamportsPrioritizationFee = microLamport / computeUnits;
const priorityFeeIxn = (0, utils_1.createAddExtraComputeUnitFeeTransaction)(computeUnits, microLamportsPrioritizationFee * 5);
tx.add(...priorityFeeIxn);
await provider.sendAndConfirm(tx);
}
//# sourceMappingURL=token.js.map