@debridge-finance/solana-utils
Version:
Common utils package to power communication with Solana contracts at deBridge
153 lines • 6.17 kB
TypeScript
import { AccountInfo, Connection, PublicKey, SimulatedTransactionAccountInfo, Transaction, TransactionInstruction, VersionedTransaction } from "@solana/web3.js";
import BN from "bn.js";
import { Buffer } from "buffer";
import { SolanaPubkey } from "./interfaces";
type AmountType = string | number | BN | number[];
declare enum AccountState {
Uninitialized = 0,
Initialized = 1,
Frozen = 2
}
type AccountBalance = {
balance: number;
spl?: {
mint: PublicKey;
owner: PublicKey;
balance: bigint;
};
};
type SplDiff = {
mint: PublicKey;
owner: PublicKey;
preBalance: number;
postBalance: number;
diff: bigint;
};
type AccountBalanceDiff = {
pubKey: PublicKey;
preBalance: number;
postBalance: number;
diff: number;
spl?: SplDiff;
};
type RawAccount = {
mint: PublicKey;
owner: PublicKey;
amount: bigint;
delegateOption: 1 | 0;
delegate: PublicKey;
state: AccountState;
isNativeOption: 1 | 0;
isNative: bigint;
delegatedAmount: bigint;
closeAuthorityOption: 1 | 0;
closeAuthority: PublicKey;
};
export declare enum AccountType {
System = 0,
Token = 1,
CorrectATA = 2,
Unknown = 3,
NotExists = 4
}
export interface WalletInfo {
/** Address of the account */
address: PublicKey;
/** Mint associated with the account */
mint: PublicKey;
/** Owner of the account */
owner: PublicKey;
/** Number of tokens the account holds */
amount: BN;
decimals: number;
/** True if the account is initialized */
isInitialized: boolean;
/** True if the account is a native token account */
isNative: boolean;
}
/**
* Get name, symbol and decimals for provided token
* @param connection solana web3 connection
* @param tokenAddress address to get metaplex info for
* @returns
*/
export declare function getTokenInfo(connection: Connection, tokenAddress: PublicKey): Promise<{
address: PublicKey;
decimals: number;
name: string;
symbol: string;
json: string;
} | null>;
/**
* Builds instruction for creation of associated wallet for specified token
* @param tokenMint mint account of SPL-token
* @param associatedAccount associated account address
* @param owner owner of the associated account
* @param payer who pays for account creation
*/
export declare function createAssociatedWalletInstruction(tokenMint: SolanaPubkey, associatedAccount: PublicKey, owner: SolanaPubkey, payer: SolanaPubkey, associatedTokenProramId?: PublicKey): TransactionInstruction;
export declare function parseSplAccount(info: AccountInfo<Buffer> | null): null | RawAccount;
export declare function parseSplAccount(data: Buffer): null | RawAccount;
export declare function isWalletCorrectATA(walletAddress: SolanaPubkey, data: Buffer): boolean;
/**
* Requests account from blockchain and returns result with account type
* @param account address of account
* @returns account info and account type
*/
export declare function getAccountWithType(connection: Connection, account: SolanaPubkey): Promise<[AccountInfo<Buffer> | null, AccountType]>;
/**
* Returns balance of specified wallet in native tokens
* @param wallet wallet to inspect
*/
export declare function getNativeWalletBalance(connection: Connection, wallet: SolanaPubkey): Promise<BN>;
/**
* Returns balance of associated wallet in SPL-tokens
* @param originalWallet native tokens wallet address
* @param tokenMint mint of SPL-Token
*/
export declare function getAssocSPLWalletBalance(connection: Connection, originalWallet: SolanaPubkey, tokenMint: SolanaPubkey): Promise<BN>;
/**
* Returns balance of specified wallet in SPL-tokens
* @param wallet spl-tokens wallet address
*/
export declare function getSPLWalletBalance(connection: Connection, wallet: SolanaPubkey): Promise<BN>;
/**
* Gets list of user's SPL-token accounts
* @param owner owner of the SPL wallets
* @returns list of accounts with amount on them
*/
export declare function getAllTokenAccountsWithBalances(connection: Connection, owner: SolanaPubkey): Promise<WalletInfo[]>;
/**
* Gets list of user's tokenMint token accounts
* @param tokenMint
* @param owner owner of the wallets
* @returns list of accounts with amount on them
*/
export declare function getTokenAccountsWithBalance(connection: Connection, tokenMint: SolanaPubkey, owner: SolanaPubkey): Promise<WalletInfo[]>;
/**
* Builds transaction to transfer&wrap native sol from src wallet to dst wallet
* @param amount number of lamports to transfer and wrap
* @param transferFrom source native account
* @param transferTo destination wallet
* @returns transaction to transfer&wrap sol
*/
export declare function buildReplenishWsolBalanceTransaction(amount: AmountType, transferFrom: SolanaPubkey, transferTo: SolanaPubkey): Transaction;
/**
* Builds transaction that wraps specified amount of lamports into spl, if account is missing will create it
* @param amount lamports to wrap, ATA will contain exact amount of lamports
* @param owner owner of created wallet
*/
export declare function updateWsolBalance(connection: Connection, amount: AmountType, owner: SolanaPubkey): Promise<Transaction | null>;
/**
* Checks existance of associated wallet for specified token
* @param mintAccount mint account of SPL-token
* @param originalWallet account of solana wallet
* @returns true if account exists
*/
export declare function checkIfAssociatedWalletExists(connection: Connection, mintAccount: PublicKey, originalWallet: PublicKey): Promise<boolean>;
export declare function getTransactionAccountsForSimulationDiff(connection: Connection, tx: VersionedTransaction | Transaction): Promise<string[]>;
export declare function parseMultipleAccounts(accounts: (AccountInfo<Buffer> | null)[] | (SimulatedTransactionAccountInfo | null)[]): AccountBalance[];
export declare function getSplDiff(pre: AccountBalance["spl"], post: AccountBalance["spl"]): SplDiff;
export declare function getTransactionDiff(connection: Connection, tx: VersionedTransaction, txAccounts?: PublicKey[]): Promise<Map<string, AccountBalanceDiff>>;
export {};
//# sourceMappingURL=spl.d.ts.map