-
Notifications
You must be signed in to change notification settings - Fork 295
feat(sdk-coin-sol): implement staking activate for jito #6502
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+585
−54
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
/** | ||
* Stakes JitoSOL tokens on Solana devnet. | ||
* | ||
* Copyright 2025, BitGo, Inc. All Rights Reserved. | ||
*/ | ||
import { BitGoAPI } from '@bitgo/sdk-api' | ||
import { TransactionBuilderFactory, Tsol } from '@bitgo/sdk-coin-sol' | ||
import { coins } from '@bitgo/statics' | ||
import { Connection, PublicKey, clusterApiUrl, Transaction, Keypair, LAMPORTS_PER_SOL } from "@solana/web3.js" | ||
import { getStakePoolAccount, updateStakePool } from '@solana/spl-stake-pool' | ||
import * as bs58 from 'bs58'; | ||
|
||
require('dotenv').config({ path: '../../.env' }) | ||
|
||
const AMOUNT_LAMPORTS = 1000 | ||
const JITO_STAKE_POOL_ADDRESS = 'Jito4APyf642JPZPx3hGc6WWJ8zPKtRbRs4P815Awbb' | ||
const NETWORK = 'devnet' | ||
|
||
const bitgo = new BitGoAPI({ | ||
accessToken: process.env.TESTNET_ACCESS_TOKEN, | ||
env: 'test', | ||
}) | ||
const coin = coins.get("tsol") | ||
bitgo.register(coin.name, Tsol.createInstance) | ||
|
||
async function main() { | ||
const account = getAccount() | ||
const connection = new Connection(clusterApiUrl(NETWORK), 'confirmed') | ||
const recentBlockhash = await connection.getLatestBlockhash() | ||
const stakePoolAccount = await getStakePoolAccount(connection, new PublicKey(JITO_STAKE_POOL_ADDRESS)) | ||
|
||
|
||
// Account should have sufficient balance | ||
const accountBalance = await connection.getBalance(account.publicKey) | ||
if (accountBalance < 0.1 * LAMPORTS_PER_SOL) { | ||
console.info(`Your account balance is ${accountBalance / LAMPORTS_PER_SOL} SOL, requesting airdrop`) | ||
const sig = await connection.requestAirdrop(account.publicKey, 2 * LAMPORTS_PER_SOL) | ||
await connection.confirmTransaction(sig) | ||
console.info(`Airdrop successful: ${sig}`) | ||
} | ||
|
||
// Stake pool should be up to date | ||
const epochInfo = await connection.getEpochInfo() | ||
if (stakePoolAccount.account.data.lastUpdateEpoch.ltn(epochInfo.epoch)) { | ||
console.info('Stake pool is out of date.') | ||
const usp = await updateStakePool(connection, stakePoolAccount) | ||
const tx = new Transaction() | ||
tx.add(...usp.updateListInstructions, ...usp.finalInstructions) | ||
const signer = Keypair.fromSecretKey(account.secretKeyArray) | ||
const sig = await connection.sendTransaction(tx, [signer]) | ||
await connection.confirmTransaction(sig) | ||
console.info(`Stake pool updated: ${sig}`) | ||
} | ||
|
||
// Use BitGoAPI to build depositSol instruction | ||
const txBuilder = new TransactionBuilderFactory(coin).getStakingActivateBuilder() | ||
txBuilder | ||
.amount(`${AMOUNT_LAMPORTS}`) | ||
.sender(account.publicKey.toBase58()) | ||
.stakingAddress(JITO_STAKE_POOL_ADDRESS) | ||
.validator(JITO_STAKE_POOL_ADDRESS) | ||
.isJito(true) | ||
.nonce(recentBlockhash.blockhash) | ||
txBuilder.sign({ key: account.secretKey }) | ||
const tx = await txBuilder.build() | ||
const serializedTx = tx.toBroadcastFormat() | ||
console.info(`Transaction JSON:\n${JSON.stringify(tx.toJson(), undefined, 2)}`) | ||
|
||
// Send transaction | ||
try { | ||
const sig = await connection.sendRawTransaction(Buffer.from(serializedTx, 'base64')) | ||
await connection.confirmTransaction(sig) | ||
console.log(`${AMOUNT_LAMPORTS / LAMPORTS_PER_SOL} SOL deposited`, sig) | ||
} catch (e) { | ||
console.log('Error sending transaction') | ||
console.error(e) | ||
if (e.transactionMessage === 'Transaction simulation failed: Error processing Instruction 0: Provided owner is not allowed') { | ||
console.error('If you successfully staked JitoSOL once, you cannot stake again.') | ||
} | ||
} | ||
} | ||
|
||
const getAccount = () => { | ||
const publicKey = process.env.ACCOUNT_PUBLIC_KEY | ||
const secretKey = process.env.ACCOUNT_SECRET_KEY | ||
if (publicKey === undefined || secretKey === undefined) { | ||
const { publicKey, secretKey } = Keypair.generate() | ||
console.log('Here is a new account to save into your .env file.') | ||
console.log(`ACCOUNT_PUBLIC_KEY=${publicKey.toBase58()}`) | ||
console.log(`ACCOUNT_SECRET_KEY=${bs58.encode(secretKey)}`) | ||
throw new Error("Missing account information") | ||
} | ||
|
||
return { | ||
publicKey: new PublicKey(publicKey), | ||
secretKey, | ||
secretKeyArray: new Uint8Array(bs58.decode(secretKey)), | ||
} | ||
} | ||
|
||
main().catch((e) => console.error(e)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Create an enum for marinade, jito. There might be new staking types later. You can refactor in a separate PR with WP changes
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can I address this in a follow-up? If I create an enum now, then other Jito PRs will require a Marinade refactor as well.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is already being changed at the api layer, we might as well address it here as well.
https://github.com/BitGo/public-types/pull/213
I'm fine with doing this in a follow up, but the api will use an enum.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Assuming a refactor is done in a follow up:
Can we keep the
isJito
field in https://github.com/BitGo/public-types/pull/213 as well for now?I agree having individual flags is messy, but I think having flags (
isMarinade
) and enums (stakingType === "JITO"
) in the same codebase is worse because it's inconsistent.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Refactor ticket: https://bitgoinc.atlassian.net/browse/SC-2620.