@unloc-xyz/unloc-sdk
Version:
75 lines • 3.29 kB
JavaScript
import * as anchor from '@project-serum/anchor';
import { ASSOCIATED_TOKEN_PROGRAM_ID, Token, TOKEN_PROGRAM_ID, } from "@solana/spl-token";
import { Connection, PublicKey, } from "@solana/web3.js";
import { RPC_ENDPOINT } from "./../global-config";
export const SOLANA_CONNECTION = new Connection(RPC_ENDPOINT, {
disableRetryOnRateLimit: true
});
export async function pda(seeds, pid) {
const [pdaKey] = await anchor.web3.PublicKey.findProgramAddress(seeds, pid);
return pdaKey;
}
export const discriminatorLen = 8;
export const systemProgram = anchor.web3.SystemProgram.programId;
export const tokenProgram = TOKEN_PROGRAM_ID;
export const rent = anchor.web3.SYSVAR_RENT_PUBKEY;
export const clock = anchor.web3.SYSVAR_CLOCK_PUBKEY;
export const defaults = {
systemProgram,
tokenProgram,
rent,
clock
};
export const commitment = "confirmed";
export async function createAssociatedTokenAccountIfNotExist2(account, owner, payer, mintAddress, transaction, atas = []) {
let publicKey;
if (account) {
publicKey = new PublicKey(account);
}
const mint = new PublicKey(mintAddress);
// @ts-ignore without ts ignore, yarn build will failed
const ata = await Token.getAssociatedTokenAddress(ASSOCIATED_TOKEN_PROGRAM_ID, TOKEN_PROGRAM_ID, mint, owner, true);
if ((!publicKey || !ata.equals(publicKey)) &&
!atas.includes(ata.toBase58())) {
transaction.add(Token.createAssociatedTokenAccountInstruction(ASSOCIATED_TOKEN_PROGRAM_ID, TOKEN_PROGRAM_ID, mint, ata, owner, payer));
atas.push(ata.toBase58());
}
return ata;
}
export async function sendTransaction(connection, wallet, transaction, signers = []) {
if (wallet.isProgramWallet) {
const programWalletTransaction = await covertToProgramWalletTransaction(connection, wallet, transaction, signers);
return await wallet.signAndSendTransaction(programWalletTransaction);
}
else {
const signedTransaction = await signTransaction(connection, wallet, transaction, signers);
return await sendSignedTransaction(connection, signedTransaction);
}
}
// transaction
export async function signTransaction(connection, wallet, transaction, signers = []) {
transaction.recentBlockhash = (await connection.getRecentBlockhash()).blockhash;
transaction.setSigners(wallet.publicKey, ...signers.map((s) => s.publicKey));
if (signers.length > 0) {
transaction.partialSign(...signers);
}
return await wallet.signTransaction(transaction);
}
async function covertToProgramWalletTransaction(connection, wallet, transaction, signers = []) {
transaction.recentBlockhash = (await connection.getRecentBlockhash(commitment)).blockhash;
transaction.feePayer = wallet.publicKey;
if (signers.length > 0) {
transaction = await wallet.convertToProgramWalletTransaction(transaction);
transaction.partialSign(...signers);
}
return transaction;
}
export async function sendSignedTransaction(connection, signedTransaction) {
const rawTransaction = signedTransaction.serialize();
const txid = await connection.sendRawTransaction(rawTransaction, {
skipPreflight: true,
preflightCommitment: commitment,
});
return txid;
}
//# sourceMappingURL=index.js.map