This repository provides open source access to SolToolkit (Typescript) SDK.
npm i @solworks/soltoolkit-sdk
ConnectionManager is a singleton class that manages web3.js Connection(s). It takes the following parameters on initialization using the async getInstance() method:
{
network: Cluster;
endpoint?: string;
endpoints?: string[];
config?: ConnectionConfig;
commitment?: Commitment;
mode?: Mode;}networkis the cluster to connect to, possible values are 'mainnet-beta', 'testnet', 'devnet', 'localnet'. This is required. If you do not pass in any values forendpointorendpoints, the default endpoints for the network will be used.endpointis a single endpoint to connect to. This is optional.endpointsis an array of endpoints to connect to. This is optional.configis a web3.js ConnectionConfig object. This is optional.commitmentis the commitment level to use for transactions. This is optional, will default to 'max'.modeis the Mode for the ConnectionManager. This is optional, will default to 'single'. Possible values are:- 'single' - Uses the
endpointparam, that falls back to the first endpoint provided inendpoints, that falls back to the default endpoints for the network. - 'first' - Uses the first endpoint provided in
endpoints. Throws an error if no endpoints are provided. - 'last' - Uses the last endpoint provided in
endpoints. Throws an error if no endpoints are provided. - 'round-robin' - Uses the endpoints provided in
endpointsin a round-robin fashion (cycles through each endpoint in sequence starting from the first). Throws an error if no endpoints are provided. - 'random' - Uses a random endpoint provided in
endpoints. Throws an error if no endpoints are provided. - 'fastest' - Uses the fastest endpoint provided in
endpoints. Throws an error if no endpoints are provided. - 'highest-slot' - Uses the endpoint with the highest slot provided in
endpoints. Throws an error if no endpoints are provided.
- 'single' - Uses the
getInstance()- Returns the singleton instance of the ConnectionManager. This method is async and must be awaited.getInstanceSync()- Returns the singleton instance of the ConnectionManager. This method is synchronous. This method should only be used after initializing the ConnectionManager withgetInstance().conn()- Returns a web3.js connection. This method will update the summary for each RPC to determine the 'fastest' or 'highest slot' endpoint. This method is async and must be awaited.connSync()- Returns a web3.js connection. This method will use fastest' or 'highest slot' endpoint determined during initialization. This method is synchronous.
import{ConnectionManager}from"@solworks/soltoolkit-sdk";(async()=>{// create connection managerconstcm=awaitConnectionManager.getInstance({commitment: "max",endpoints: ["https://api.devnet.solana.com","https://solana-devnet-rpc.allthatnode.com","https://mango.devnet.rpcpool.com","https://rpc.ankr.com/solana_devnet",],mode: "fastest",network: "devnet"});// get fastest endpointconstfastestEndpoint=cm._fastestEndpoint;console.log(`Fastest endpoint: ${fastestEndpoint}`);})();import{ConnectionManager,Logger}from"@solworks/soltoolkit-sdk";(async()=>{// create connection managerconstcm=awaitConnectionManager.getInstance({commitment: "max",endpoints: ["https://api.devnet.solana.com","https://solana-devnet-rpc.allthatnode.com","https://mango.devnet.rpcpool.com","https://rpc.ankr.com/solana_devnet",],mode: "highest-slot",network: "devnet"});// get highest slot endpointconsthighestSlotEndpoint=cm._highestSlotEndpoint;console.log(`Highest slot endpoint: ${_highestSlotEndpoint}`);})();import{ConnectionManager,Logger}from"@solworks/soltoolkit-sdk";(async()=>{constlogger=newLogger("example");// create connection managerconstcm=awaitConnectionManager.getInstance({commitment: "max",endpoints: ["https://api.devnet.solana.com","https://solana-devnet-rpc.allthatnode.com","https://mango.devnet.rpcpool.com","https://rpc.ankr.com/solana_devnet",],mode: "fastest",network: "devnet"});// get summary of endpoint speedsconstsummary=awaitcm.getEndpointsSummary();logger.debug(JSON.stringify(summary,null,2));})();import{Keypair,LAMPORTS_PER_SOL,Signer}from"@solana/web3.js";import{ConnectionManager,TransactionBuilder,TransactionWrapper,Logger}from"@solworks/soltoolkit-sdk";constlogger=newLogger("example");constsender=Keypair.generate();constreceiver=Keypair.generate();(async()=>{// create connection managerconstcm=awaitConnectionManager.getInstance({commitment: COMMITMENT,endpoints: ["https://api.devnet.solana.com","https://solana-devnet-rpc.allthatnode.com","https://mango.devnet.rpcpool.com","https://rpc.ankr.com/solana_devnet",],mode: "fastest",network: "devnet",});// airdrop sol to the generated addressconstairdropSig=awaitcm.connSync({airdrop: true}).requestAirdrop(sender.publicKey,LAMPORTS_PER_SOL);// confirm airdrop txawaitTransactionWrapper.confirmTx({connectionManager: cm,changeConn: false,signature: airdropSig,commitment: "max",});// create builder and add token transfer ixvarbuilder=TransactionBuilder.create().addSolTransferIx({from: sender.publicKey,to: receiver.publicKey,amountLamports: 10_000_000,}).addMemoIx({memo: "gm",signer: sender.publicKey,});// build the transaction// returns a transaction with no fee payer or blockhashlettx=builder.build();// feed transaction into TransactionWrapperconstwrapper=awaitTransactionWrapper.create({connectionManager: cm,transaction: tx,signer: sender.publicKey,}).addBlockhashAndFeePayer();// sign the transactionconstsignedTx=awaitwrapper.sign({signer: senderasSigner,});// send and confirm the transactionconsttransferSig=awaitwrapper.sendAndConfirm({serialisedTx: signedTx.serialize(),});})();import{Keypair,LAMPORTS_PER_SOL,Signer}from"@solana/web3.js";import{ConnectionManager,TransactionBuilder,TransactionWrapper,Logger}from"@solworks/soltoolkit-sdk";constlogger=newLogger("example");constsender=Keypair.generate();constreceiver=Keypair.generate();(async()=>{// create connection managerconstcm=awaitConnectionManager.getInstance({commitment: COMMITMENT,endpoints: ["https://api.devnet.solana.com","https://solana-devnet-rpc.allthatnode.com","https://mango.devnet.rpcpool.com","https://rpc.ankr.com/solana_devnet",],mode: "fastest",network: "devnet",});// airdrop sol to the generated addressconstairdropSig=awaitcm.connSync({airdrop: true}).requestAirdrop(sender.publicKey,LAMPORTS_PER_SOL);// confirm airdrop txawaitTransactionWrapper.confirmTx({connectionManager: cm,changeConn: false,signature: airdropSig,commitment: "max",});// create builder and add token transfer ixvarbuilder=TransactionBuilder.create().addMemoIx({memo: "gm",signer: sender.publicKey,});// build the transaction// returns a transaction with no fee payer or blockhashlettx=builder.build();// feed transaction into TransactionWrapperconstwrapper=awaitTransactionWrapper.create({connectionManager: cm,transaction: tx,signer: sender.publicKey,}).addBlockhashAndFeePayer();// sign the transactionconstsignedTx=awaitwrapper.sign({signer: senderasSigner,});// send and confirm the transactionconsttransferSig=awaitwrapper.sendAndConfirm({serialisedTx: signedTx.serialize(),});import{PublicKey}from"@solana/web3.js";import{SNSDomainResolver,Logger}from"@solworks/soltoolkit-sdk";constlogger=newLogger("example");constaddressString='5F6gcdzpw7wUjNEugdsD4aLJdEQ4Wt8d6E85vaQXZQSJ';constaddressPublicKey=newPublicKey(addressString);(async()=>{// use the SNSDomainResolver to get the first .sol domain associated with an address// getDomainFromAddress takes a PublicKey or an address string as an argument// getDomainFromAddress returns a Promise<string | null>constfirstDomainPk=awaitSNSDomainResolver.getDomainFromAddress(addressPublicKey);logger.info(`First domain for address: ${firstDomainPk||"no domain found"}`);constfirstDomainString=awaitSNSDomainResolver.getDomainFromAddress(addressString);logger.info(`First domain for address: ${firstDomainString||"no domain found"}`);// use the SNSDomainResolver to get all .sol domains associated with an address// getDomainsFromAddress takes a PublicKey or an address string as an argument// getDomainsFromAddress returns a Promise<string[] | null>constallDomainsPk=awaitSNSDomainResolver.getDomainsFromAddress(addressPublicKey);logger.info(`All domains for address: ${allDomainsPk||"no domains found"}`);constallDomainsString=awaitSNSDomainResolver.getDomainsFromAddress(addressString);logger.info(`All domains for address: ${allDomainsString||"no domains found"}`);})();import{Keypair,LAMPORTS_PER_SOL,Signer}from"@solana/web3.js";import{ConnectionManager,TransactionBuilder,TransactionWrapper,Logger,sendTxUsingJito}from"@solworks/soltoolkit-sdk";(async()=>{// create connection managerconstcm=awaitConnectionManager.getInstance({commitment: 'processed',endpoints: ["https://api.mainnet-beta.solana.com",],mode: "fastest",network: "mainnet-beta",});// create builder and add token transfer ixvarbuilder=TransactionBuilder.create().addMemoIx({memo: "gm",signer: sender.publicKey,});// build the transaction// returns a transaction with no fee payer or blockhashlettx=builder.build();// feed transaction into TransactionWrapperconstwrapper=awaitTransactionWrapper.create({connectionManager: cm,transaction: tx,signer: sender.publicKey,}).addBlockhashAndFeePayer();// sign the transactionconstsignedTx=awaitwrapper.sign({signer: senderasSigner,});// send and confirm the transactionconsttransferSig=awaitwrapper.sendTxUsingJito({serialisedTx: signedTx.serialize(),region: 'mainnet',sendOptions: {}});// OR use static methodconsttransferSig=awaitsendTxUsingJito({serialisedTx: signedTx.serialize(),region: 'mainnet',sendOptions: {}});})();See example.
SolToolkit is licensed under Affero GPL.