Skip to content

Commit

Permalink
feat: add support for using Marinade Native in the SDK
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexStefan committed Sep 20, 2023
1 parent 01493fc commit ab79ee1
Show file tree
Hide file tree
Showing 5 changed files with 167 additions and 9 deletions.
11 changes: 6 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,24 +37,28 @@
"dependencies": {
"@coral-xyz/anchor": "^0.28.0",
"@marinade.finance/directed-stake-sdk": "^0.0.4",
"@marinade.finance/native-staking-sdk": "^1.0.0",
"@solana/spl-stake-pool": "^0.6.5",
"@solana/spl-token-3.x": "npm:@solana/spl-token@^0.3.8",
"borsh": "^0.7.0",
"bs58": "^5.0.0"
},
"devDependencies": {
"@jest/globals": "^29.5.0",
"@solana/web3.js": "^1.74.0",
"@types/bn.js": "^5.1.1",
"@types/bs58": "^4.0.1",
"@types/jest": "^29.5.1",
"@jest/globals": "^29.5.0",
"@types/node": "^18.16.3",
"@typescript-eslint/eslint-plugin": "^5.59.1",
"@typescript-eslint/parser": "^5.59.1",
"bn.js": "^5.2.1",
"eslint": "^8.39.0",
"gts": "^3.1.1",
"husky": "^8.0.3",
"jest": "^29.5.0",
"jest-each": "^29.5.0",
"jsbi": "^4.3.0",
"lint-staged": "^13.2.2",
"node-polyfill-webpack-plugin": "^2.0.1",
"terser-webpack-plugin": "^5.3.7",
Expand All @@ -63,10 +67,7 @@
"ts-node": "^10.9.1",
"typescript": "^5.0.4",
"webpack": "^5.81.0",
"webpack-cli": "^5.0.2",
"@solana/web3.js": "^1.74.0",
"bn.js": "^5.2.1",
"jsbi": "^4.3.0"
"webpack-cli": "^5.0.2"
},
"engines": {
"node": ">=16.0.0",
Expand Down
31 changes: 28 additions & 3 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

121 changes: 121 additions & 0 deletions src/marinade-native-stake.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
import {
PublicKey,
TransactionInstruction,
VersionedTransaction,
} from '@solana/web3.js'
import BN from 'bn.js'
import { NativeStakingSDK } from '@marinade.finance/native-staking-sdk'
import {
AuthStakeSOLIxResponse,
UnstakeSOLIxResponse,
} from './marinade-native-stake.types'

/**
* Fetches the instruction to stake SOL in Marinade Native
*
* @param {PublicKey} userPublicKey - The PublicKey of the user
* @param {BN} amountToStake - The amount to deposit in lamports
* @returns {AuthStakeSOLIxResponse} - The instructions to add in transaction to stake in Marinade Native and new stake keypair
*/
export function getAuthNativeStakeSOLIx({
userPublicKey,
amountToStake,
}: {
userPublicKey: PublicKey
amountToStake: BN
}): AuthStakeSOLIxResponse {
const nativeSdk = new NativeStakingSDK()
return nativeSdk.buildCreateAuthorizedStakeInstructions(
userPublicKey,
amountToStake
)
}

/**
* Fetches the transaction to stake SOL in Marinade Native with refCode
*
* @param {PublicKey} userPublicKey - The PublicKey of the user
* @param {BN} amountToStake - The amount to deposit in lamports
* @param {string} mNativeRefCode - Marinade Native referral code
* @returns {Promise<VersionedTransaction>} - The transaction to stake SOL into Marinade native using Referral Code
*/
export async function getRefNativeStakeSOLTx({
userPublicKey,
amountToStake,
mNativeRefCode,
}: {
userPublicKey: PublicKey
amountToStake: BN
mNativeRefCode: string
}): Promise<VersionedTransaction> {
const response = await fetch(
`https://native-staking-referral.marinade.finance/v1/tx/deposit-sol?amount=${amountToStake.toString()}&code=${mNativeRefCode}&user=${userPublicKey.toString()}`
)
const serializedTx = await response.json()

const txBuffer = Buffer.from(serializedTx, 'base64')
return VersionedTransaction.deserialize(txBuffer)
}

/**
* Fetches the instruction to stake Stake Account in Marinade Native
*
* @param {PublicKey} userPublicKey - The PublicKey of the user
* @param {PublicKey[]} stakeAccounts - The stake accounts that are about to be deposited in Marinade Native
* @returns {TransactionInstruction} - The instruction to add in transaction to stake in Marinade Native
*/
export function getAuthNativeStakeAccountIx({
userPublicKey,
stakeAccounts,
}: {
userPublicKey: PublicKey
stakeAccounts: PublicKey[]
}): TransactionInstruction[] {
const nativeSdk = new NativeStakingSDK()
return nativeSdk.buildAuthorizeInstructions(userPublicKey, stakeAccounts)
}

/**
* Fetches the transaction to deposit Stake Account in Marinade Native with refCode
*
* @param {PublicKey} userPublicKey - The PublicKey of the user
* @param {PublicKey} stakeAccountAddress - The stake account to be deposited into Marinade Native
* @param {string} mNativeRefCode - Marinade Native referral code
* @returns {Promise<VersionedTransaction>} - The transaction to deposit Stake Account into Marinade native using Referral Code
*/
export async function getRefNativeStakeAccountTx({
userPublicKey,
stakeAccountAddress,
mNativeRefCode,
}: {
userPublicKey: PublicKey
stakeAccountAddress: PublicKey
mNativeRefCode: string
}): Promise<VersionedTransaction> {
const response = await fetch(
`https://native-staking-referral.marinade.finance/v1/tx/deposit-stake-account?stake=${stakeAccountAddress.toString()}&code=${mNativeRefCode}&user=${userPublicKey.toString()}`
)

const serializedTx = await response.json()

const txBuffer = Buffer.from(serializedTx, 'base64')
return VersionedTransaction.deserialize(txBuffer)
}

/**
* Fetches the instruction to unstake from Marinade Native
*
* @param {PublicKey} userPublicKey - The PublicKey of the user
* @param {BN} amountToUnstake - The amount to unstake in lamports
* @returns {UnstakeSOLIxResponse} - The instructions to pay the fee for unstake and the event that should be called after fee is paid in order to trigger unstake event faster (calling this is optional, but it ensures better experience for user).
*/
export async function getNativeUnstakeSOLIx({
userPublicKey,
amountToUnstake,
}: {
userPublicKey: PublicKey
amountToUnstake: BN
}): Promise<UnstakeSOLIxResponse> {
const nativeSdk = new NativeStakingSDK()
return await nativeSdk.initPrepareForRevoke(userPublicKey, amountToUnstake)
}
11 changes: 11 additions & 0 deletions src/marinade-native-stake.types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { Keypair, TransactionInstruction } from '@solana/web3.js'

export type AuthStakeSOLIxResponse = {
createAuthorizedStake: TransactionInstruction[]
stakeKeypair: Keypair
}

export type UnstakeSOLIxResponse = {
payFees: TransactionInstruction[]
onPaid: (signature: string) => Promise<unknown>
}
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"$schema": "https://json.schemastore.org/tsconfig",
"compilerOptions": {
"lib": ["es2021"],
"lib": ["es2021", "dom"],
"target": "ES6",
"module": "CommonJS",
"rootDir": ".",
Expand Down

0 comments on commit ab79ee1

Please sign in to comment.