repositories
loading repo index
repositories
loading repo index
repository
loading code, commits, and activity
public Clawd ADK gateway launch mirror
stars
latest
clone command
git clone gitlawb://did:key:z6Mkq5mY...iFZ5/my-project-publ...git clone gitlawb://did:key:z6Mkq5mY.../my-project-publ...2fa351d6docs: add automaton and perps launch sources16d ago| #1 | # Transaction Patterns Reference |
| #2 | |
| #3 | Detailed transaction patterns for Solana with Phantom Connect SDKs. |
| #4 | |
| #5 | ## Solana Transactions |
| #6 | |
| #7 | ### Dependencies |
| #8 | |
| #9 | ```bash |
| #10 | npm install @solana/web3.js |
| #11 | # OR for newer @solana/kit |
| #12 | npm install @solana/kit |
| #13 | ``` |
| #14 | |
| #15 | ### SOL Transfer with @solana/web3.js |
| #16 | |
| #17 | ```ts |
| #18 | import { |
| #19 | Connection, |
| #20 | PublicKey, |
| #21 | SystemProgram, |
| #22 | VersionedTransaction, |
| #23 | TransactionMessage, |
| #24 | } from "@solana/web3.js"; |
| #25 | |
| #26 | async function transferSol(solana, recipient: string, lamports: number) { |
| #27 | const connection = new Connection("https://api.mainnet-beta.solana.com"); |
| #28 | const { blockhash } = await connection.getLatestBlockhash(); |
| #29 | |
| #30 | const fromAddress = await solana.getPublicKey(); |
| #31 | |
| #32 | const message = new TransactionMessage({ |
| #33 | payerKey: new PublicKey(fromAddress), |
| #34 | recentBlockhash: blockhash, |
| #35 | instructions: [ |
| #36 | SystemProgram.transfer({ |
| #37 | fromPubkey: new PublicKey(fromAddress), |
| #38 | toPubkey: new PublicKey(recipient), |
| #39 | lamports, |
| #40 | }), |
| #41 | ], |
| #42 | }).compileToV0Message(); |
| #43 | |
| #44 | const transaction = new VersionedTransaction(message); |
| #45 | const result = await solana.signAndSendTransaction(transaction); |
| #46 | |
| #47 | return result.hash; |
| #48 | } |
| #49 | |
| #50 | // Usage: 0.001 SOL = 1,000,000 lamports |
| #51 | const txHash = await transferSol(solana, "recipient-address", 1000000); |
| #52 | ``` |
| #53 | |
| #54 | ### SOL Transfer with @solana/kit |
| #55 | |
| #56 | ```ts |
| #57 | import { |
| #58 | createSolanaRpc, |
| #59 | pipe, |
| #60 | createTransactionMessage, |
| #61 | setTransactionMessageFeePayer, |
| #62 | setTransactionMessageLifetimeUsingBlockhash, |
| #63 | address, |
| #64 | compileTransaction, |
| #65 | } from "@solana/kit"; |
| #66 | |
| #67 | async function transferSolKit(solana, recipient: string) { |
| #68 | const rpc = createSolanaRpc("https://api.mainnet-beta.solana.com"); |
| #69 | const { value: latestBlockhash } = await rpc.getLatestBlockhash().send(); |
| #70 | |
| #71 | const userPublicKey = await solana.getPublicKey(); |
| #72 | |
| #73 | const transactionMessage = pipe( |
| #74 | createTransactionMessage({ version: 0 }), |
| #75 | (tx) => setTransactionMessageFeePayer(address(userPublicKey), tx), |
| #76 | (tx) => setTransactionMessageLifetimeUsingBlockhash(latestBlockhash, tx) |
| #77 | ); |
| #78 | |
| #79 | const transaction = compileTransaction(transactionMessage); |
| #80 | const result = await solana.signAndSendTransaction(transaction); |
| #81 | |
| #82 | return result.hash; |
| #83 | } |
| #84 | ``` |
| #85 | |
| #86 | ### SPL Token Transfer |
| #87 | |
| #88 | ```ts |
| #89 | import { |
| #90 | Connection, |
| #91 | PublicKey, |
| #92 | VersionedTransaction, |
| #93 | TransactionMessage, |
| #94 | } from "@solana/web3.js"; |
| #95 | import { |
| #96 | getAssociatedTokenAddress, |
| #97 | createTransferInstruction, |
| #98 | } from "@solana/spl-token"; |
| #99 | |
| #100 | async function transferToken( |
| #101 | solana, |
| #102 | mint: string, |
| #103 | recipient: string, |
| #104 | amount: number, |
| #105 | decimals: number |
| #106 | ) { |
| #107 | const connection = new Connection("https://api.mainnet-beta.solana.com"); |
| #108 | const { blockhash } = await connection.getLatestBlockhash(); |
| #109 | |
| #110 | const fromAddress = await solana.getPublicKey(); |
| #111 | const fromPubkey = new PublicKey(fromAddress); |
| #112 | const mintPubkey = new PublicKey(mint); |
| #113 | const toPubkey = new PublicKey(recipient); |
| #114 | |
| #115 | const fromAta = await getAssociatedTokenAddress(mintPubkey, fromPubkey); |
| #116 | const toAta = await getAssociatedTokenAddress(mintPubkey, toPubkey); |
| #117 | |
| #118 | const transferAmount = amount * Math.pow(10, decimals); |
| #119 | |
| #120 | const message = new TransactionMessage({ |
| #121 | payerKey: fromPubkey, |
| #122 | recentBlockhash: blockhash, |
| #123 | instructions: [ |
| #124 | createTransferInstruction(fromAta, toAta, fromPubkey, transferAmount), |
| #125 | ], |
| #126 | }).compileToV0Message(); |
| #127 | |
| #128 | const transaction = new VersionedTransaction(message); |
| #129 | return await solana.signAndSendTransaction(transaction); |
| #130 | } |
| #131 | ``` |
| #132 | |
| #133 | ### Sign Without Sending |
| #134 | |
| #135 | ```ts |
| #136 | // Sign only (for offline signing, multi-sig, etc.) |
| #137 | const signedTx = await solana.signTransaction(transaction); |
| #138 | |
| #139 | // Then send later |
| #140 | const connection = new Connection("https://api.mainnet-beta.solana.com"); |
| #141 | const signature = await connection.sendRawTransaction(signedTx.serialize()); |
| #142 | ``` |
| #143 | |
| #144 | ### Network Switching |
| #145 | |
| #146 | ```ts |
| #147 | // Switch networks before transactions |
| #148 | await solana.switchNetwork("devnet"); |
| #149 | // Options: "mainnet-beta", "devnet", "testnet" |
| #150 | ``` |
| #151 | |
| #152 | ## Message Signing |
| #153 | |
| #154 | ```ts |
| #155 | // Sign a message |
| #156 | const message = "Hello World"; |
| #157 | const { signature } = await solana.signMessage(message); |
| #158 | console.log("Signature:", signature); |
| #159 | ``` |
| #160 | |
| #161 | ## Error Handling |
| #162 | |
| #163 | ```ts |
| #164 | try { |
| #165 | const result = await solana.signAndSendTransaction(transaction); |
| #166 | console.log("Success:", result.hash); |
| #167 | } catch (error) { |
| #168 | if (error.message.includes("User rejected")) { |
| #169 | console.log("User cancelled the transaction"); |
| #170 | } else if (error.message.includes("insufficient funds")) { |
| #171 | console.log("Not enough balance"); |
| #172 | } else { |
| #173 | console.error("Transaction failed:", error); |
| #174 | } |
| #175 | } |
| #176 | ``` |
| #177 | |
| #178 | ## Transaction Status Checking |
| #179 | |
| #180 | ```ts |
| #181 | import { Connection } from "@solana/web3.js"; |
| #182 | |
| #183 | const connection = new Connection("https://api.mainnet-beta.solana.com"); |
| #184 | |
| #185 | // Wait for confirmation |
| #186 | const confirmation = await connection.confirmTransaction(signature, "confirmed"); |
| #187 | |
| #188 | // Get transaction details |
| #189 | const txDetails = await connection.getTransaction(signature); |
| #190 | ``` |
| #191 | |
| #192 | ## Fee Estimation |
| #193 | |
| #194 | ```ts |
| #195 | import { Connection } from "@solana/web3.js"; |
| #196 | |
| #197 | const connection = new Connection("https://api.mainnet-beta.solana.com"); |
| #198 | |
| #199 | // Get recent prioritization fees |
| #200 | const fees = await connection.getRecentPrioritizationFees(); |
| #201 | |
| #202 | // Get minimum balance for rent exemption |
| #203 | const minBalance = await connection.getMinimumBalanceForRentExemption(dataSize); |
| #204 | ``` |
| #205 |