@hubbleprotocol/farms-sdk
Version:
98 lines (97 loc) • 7.33 kB
TypeScript
import * as anchor from "@coral-xyz/anchor";
import { Connection, Keypair, PublicKey, TransactionInstruction, Transaction, Signer } from "@solana/web3.js";
import { Decimal } from "decimal.js";
import * as web3 from "@solana/web3.js";
import { Env } from "./setup";
import { GlobalConfig, UserState, FarmState } from "../rpc_client/accounts";
export declare const FarmsIdl: anchor.Idl;
export declare const WAD: Decimal;
export declare function parseKeypairFile(file: string): Keypair;
export declare function collToLamportsDecimal(amount: Decimal, decimals: number): Decimal;
export declare function lamportsToCollDecimal(amount: Decimal, decimals: number): Decimal;
export interface GlobalConfigAccounts {
globalAdmin: Keypair;
globalConfig: Keypair;
treasuryVaults: Array<PublicKey>;
treasuryVaultAuthority: PublicKey;
globalAdminRewardAtas: Array<PublicKey>;
}
export interface FarmAccounts {
farmAdmin: Keypair;
farmState: Keypair;
tokenMint: PublicKey;
farmVault: PublicKey;
rewardVaults: Array<PublicKey>;
farmVaultAuthority: PublicKey;
rewardMints: Array<PublicKey>;
adminRewardAtas: Array<PublicKey>;
}
export interface UserAccounts {
owner: Keypair;
userState: PublicKey;
tokenAta: PublicKey;
rewardAtas: Array<PublicKey>;
}
export declare function createMint(provider: anchor.AnchorProvider, authority: PublicKey, decimals?: number): Promise<PublicKey>;
export declare function createMintFromKeypair(provider: anchor.AnchorProvider, authority: PublicKey, mint: Keypair, decimals?: number): Promise<PublicKey>;
export declare function solAirdrop(provider: anchor.AnchorProvider, account: PublicKey, solAirdrop: Decimal): Promise<Decimal>;
export declare function solAirdropMin(provider: anchor.AnchorProvider, account: PublicKey, minSolAirdrop: Decimal): Promise<Decimal>;
export declare function checkIfAccountExists(connection: Connection, account: PublicKey): Promise<boolean>;
export declare function getAssociatedTokenAddress(owner: PublicKey, tokenMintAddress: PublicKey, tokenProgram: PublicKey): Promise<PublicKey>;
export declare function createAssociatedTokenAccountIdempotentInstruction(owner: PublicKey, mint: PublicKey, payer: PublicKey | undefined, tokenProgram: PublicKey, ata?: PublicKey): Promise<[PublicKey, TransactionInstruction]>;
export declare function setupAta(provider: anchor.AnchorProvider, tokenMintAddress: PublicKey, user: Keypair): Promise<PublicKey>;
export declare function mintTo(provider: anchor.AnchorProvider, mintPubkey: PublicKey, tokenAccount: PublicKey, amount: number): Promise<void>;
export declare function transferToken(provider: anchor.AnchorProvider, fromAccount: Signer, fromTokenAccount: PublicKey, toTokenAccount: PublicKey, amount: number): Promise<void>;
/**
* Get the custom program error code if there's any in the error message and return parsed error code hex to number string
* @param errMessage string - error message that would contain the word "custom program error:" if it's a customer program error
* @returns [boolean, string] - probably not a custom program error if false otherwise the second element will be the code number in string
*/
export declare const getCustomProgramErrorCode: (errMessage: string) => [boolean, string];
/**
*
* Maps the private Anchor type ProgramError to a normal Error.
* Pass ProgramErr.msg as the Error message so that it can be used with chai matchers
*
* @param fn - function which may throw an anchor ProgramError
*/
export declare function mapAnchorError<T>(fn: Promise<T>): Promise<T>;
export declare function getTokenAccountBalance(provider: anchor.AnchorProvider, tokenAccount: PublicKey): Promise<Decimal>;
export declare function getSolBalanceInLamports(provider: anchor.AnchorProvider, account: PublicKey): Promise<number>;
export declare function getSolBalance(provider: anchor.AnchorProvider, account: PublicKey): Promise<Decimal>;
export type Cluster = "localnet" | "devnet" | "mainnet";
export type SolEnv = {
cluster: Cluster;
ownerKeypairPath: string;
endpoint: string;
};
export declare function getFarmsProgramId(cluster: string): anchor.web3.PublicKey;
export declare function endpointFromCluster(cluster: string | undefined): string;
export declare function pubkeyFromFile(filepath: string): PublicKey;
export declare function createAddExtraComputeUnitsTransaction(owner: PublicKey, units: number): TransactionInstruction;
export declare function u16ToBytes(num: number): Uint8Array;
export declare function accountExist(connection: anchor.web3.Connection, account: anchor.web3.PublicKey): Promise<boolean>;
export declare function fetchFarmStateWithRetry(env: Env, address: PublicKey): Promise<FarmState | null>;
export declare function fetchGlobalConfigWithRetry(env: Env, address: PublicKey): Promise<GlobalConfig>;
export declare function fetchUserStateWithRetry(env: Env, address: PublicKey): Promise<UserState>;
export declare function getTreasuryVaultPDA(programId: PublicKey, globalConfig: PublicKey, rewardMint: PublicKey): PublicKey;
export declare function getTreasuryAuthorityPDA(programId: PublicKey, globalConfig: PublicKey): PublicKey;
export declare function getFarmAuthorityPDA(programId: PublicKey, farmState: PublicKey): PublicKey;
export declare function getFarmVaultPDA(programId: PublicKey, farmState: PublicKey, tokenMint: PublicKey): PublicKey;
export declare function getRewardVaultPDA(programId: PublicKey, farmState: PublicKey, rewardMint: PublicKey): PublicKey;
export declare function getUserStatePDA(programId: PublicKey, farmState: PublicKey, owner: PublicKey): PublicKey;
export declare function sendAndConfirmInstructions(env: Env, ixns: [TransactionInstruction]): Promise<web3.TransactionSignature>;
export declare function getGlobalConfigValue(flagValueType: string, flagValue: string): number[];
export declare function createKeypairRentExempt(provider: anchor.AnchorProvider, programId: PublicKey, address: Keypair, size: number): Promise<web3.Keypair>;
export declare function createKeypairRentExemptIx(connection: Connection, payer: PublicKey, account: Keypair, size: number, programId?: PublicKey): Promise<TransactionInstruction>;
export declare function createGlobalConfigPublicKeyRentExempt(provider: anchor.AnchorProvider, programId: PublicKey): Promise<Keypair>;
export declare function createFarmPublicKeyRentExempt(provider: anchor.AnchorProvider, programId: PublicKey): Promise<Keypair>;
export declare function buildAndSendTxnWithLogs(c: Connection, tx: Transaction, owner: Keypair, signers: Signer[]): Promise<void>;
export declare function sleep(ms: number): Promise<unknown>;
export declare function scaleDownWads(value: anchor.BN): number;
export declare function convertStakeToAmount(stake: Decimal, totalStaked: Decimal, totalAmount: Decimal): Decimal;
export declare function convertAmountToStake(amount: Decimal, totalStaked: Decimal, totalAmount: Decimal): Decimal;
export declare const parseTokenSymbol: (tokenSymbol: number[]) => string;
export declare function getMintDecimals(env: Env, mint: PublicKey, tokenProgram: PublicKey): Promise<number>;
export declare function retryAsync(fn: () => Promise<any>, retriesLeft?: number, interval?: number): Promise<any>;
export declare function noopProfiledFunctionExecution(promise: Promise<any>): Promise<any>;