Stake SOL
Effortlessly stake SOL on the Jup validator using the StakeManager class. This guide explains how to execute staking operations with simplicity and precision.
Code Breakdown
1. Endpoint Interaction
The staking function interacts with the Jup API to prepare a staking transaction:
Copy
url = f"https://worker.jup.ag/blinks/swap/So11111111111111111111111111111111111111112/jupSoLaHXQiZZTSfEWMTRRgpnyFm8f6sZdosWBjx93v/{amount}"
payload = {"account": str(agent.wallet_address)}
async with aiohttp.ClientSession() as session:
async with session.post(url, json=payload) as res:
if res.status != 200:
raise Exception(f"Failed to fetch transaction: {res.status}")
data = await res.json()
2. Transaction Deserialization and Signing
The API returns a serialized transaction that needs to be deserialized, updated, and signed by the user's wallet:
Copy
txn = VersionedTransaction.deserialize(base64.b64decode(data["transaction"]))
latest_blockhash = await agent.connection.get_latest_blockhash()
txn.message.recent_blockhash = latest_blockhash.value.blockhash
txn.sign([agent.wallet])
3. Submitting and Confirming the Transaction
The transaction is submitted to the Solana network, and its status is confirmed:
Copy
signature = await agent.connection.send_raw_transaction(
txn.serialize(),
opts={"skip_preflight": False, "max_retries": 3},
)
await agent.connection.confirm_transaction(
signature,
commitment=Confirmed,
last_valid_block_height=latest_blockhash.value.last_valid_block_height,
)
4. Error Handling
If any part of the staking process fails, meaningful exceptions are raised for debugging:
Copy
except Exception as e:
raise Exception(f"jupSOL staking failed: {str(e)}")
Key Features
Effortless Staking
Automatically interacts with Jup API to stake SOL.
Transaction Safety
Ensures the transaction uses the latest blockhash and is properly signed.
Error Resilience
Catches and reports API or network issues to ensure reliable staking operations.
Minimal Setup
Only requires the agent instance and the desired staking amount.
Last updated