diff --git a/packages/wasm-solana/js/index.ts b/packages/wasm-solana/js/index.ts index 6ba44860c00..786d3361b69 100644 --- a/packages/wasm-solana/js/index.ts +++ b/packages/wasm-solana/js/index.ts @@ -27,6 +27,7 @@ export { buildFromIntent, buildFromIntent as buildTransactionFromIntent } from " export type { BaseIntent, PaymentIntent, + GoUnstakeIntent, StakeIntent, UnstakeIntent, ClaimIntent, diff --git a/packages/wasm-solana/js/intentBuilder.ts b/packages/wasm-solana/js/intentBuilder.ts index 5357855d262..28e1ae2aaa5 100644 --- a/packages/wasm-solana/js/intentBuilder.ts +++ b/packages/wasm-solana/js/intentBuilder.ts @@ -103,14 +103,28 @@ export interface PaymentIntent extends BaseIntent { recipients?: Recipient[]; } +/** goUnstake intent (payment-shaped unstaking operation) */ +export interface GoUnstakeIntent extends BaseIntent { + intentType: "goUnstake"; + recipients?: Recipient[]; +} + /** Stake intent */ -export interface StakeIntent extends BaseIntent { +type StakeIntentFields = BaseIntent & { intentType: "stake"; validatorAddress: string; amount?: { value: bigint }; - stakingType?: "NATIVE" | "JITO" | "MARINADE"; - stakePoolConfig?: StakePoolConfig; -} +}; + +export type StakeIntent = + | (StakeIntentFields & { + stakingType?: "NATIVE" | "MARINADE"; + stakePoolConfig?: StakePoolConfig; + }) + | (StakeIntentFields & { + stakingType: "JITO"; + stakePoolConfig: StakePoolConfig; + }); /** Stake pool configuration (for Jito) */ export interface StakePoolConfig { @@ -126,15 +140,24 @@ export interface StakePoolConfig { } /** Unstake intent */ -export interface UnstakeIntent extends BaseIntent { +type UnstakeIntentFields = BaseIntent & { intentType: "unstake"; stakingAddress: string; validatorAddress?: string; amount?: { value: bigint }; remainingStakingAmount?: { value: bigint }; - stakingType?: "NATIVE" | "JITO" | "MARINADE"; - stakePoolConfig?: StakePoolConfig; -} +}; + +export type UnstakeIntent = + | (UnstakeIntentFields & { + stakingType?: "NATIVE" | "MARINADE"; + stakePoolConfig?: StakePoolConfig; + }) + | (UnstakeIntentFields & { + stakingType: "JITO"; + validatorAddress: string; + stakePoolConfig: StakePoolConfig & { validatorList: string }; + }); /** Claim intent (withdraw from deactivated stake) */ export interface ClaimIntent extends BaseIntent { @@ -223,6 +246,7 @@ export interface CustomTxKey { /** Union of all supported intent types */ export type SolanaIntent = | PaymentIntent + | GoUnstakeIntent | StakeIntent | UnstakeIntent | ClaimIntent @@ -272,7 +296,7 @@ export type SolanaIntent = * ``` */ export function buildFromIntent( - intent: BaseIntent, + intent: SolanaIntent, params: BuildFromIntentParams, ): BuildFromIntentResult { const result = IntentNamespace.build_from_intent(intent, params) as WasmBuildResult; diff --git a/packages/wasm-solana/src/intent/build.rs b/packages/wasm-solana/src/intent/build.rs index d23664bd040..0635dcecd54 100644 --- a/packages/wasm-solana/src/intent/build.rs +++ b/packages/wasm-solana/src/intent/build.rs @@ -312,6 +312,15 @@ fn build_stake( let intent: StakeIntent = serde_json::from_value(intent_json.clone()) .map_err(|e| WasmSolanaError::new(&format!("Failed to parse stake intent: {}", e)))?; + let jito_config = + if intent.staking_type == Some(StakingType::Jito) { + Some(intent.stake_pool_config.as_ref().ok_or_else(|| { + WasmSolanaError::new("stakePoolConfig is required for JITO staking") + })?) + } else { + None + }; + let fee_payer: Pubkey = params .fee_payer .parse() @@ -319,11 +328,8 @@ fn build_stake( let amount: u64 = intent.amount.as_ref().map(|a| a.value).unwrap_or(0); - // Check if Jito staking - if intent.staking_type == Some(StakingType::Jito) { - if let Some(config) = &intent.stake_pool_config { - return build_jito_stake(config, &fee_payer, &intent.validator_address, amount); - } + if let Some(config) = jito_config { + return build_jito_stake(config, &fee_payer, &intent.validator_address, amount); } // Generate stake account keypair (used by both native and Marinade) @@ -524,6 +530,14 @@ fn build_unstake( let intent: UnstakeIntent = serde_json::from_value(intent_json.clone()) .map_err(|e| WasmSolanaError::new(&format!("Failed to parse unstake intent: {}", e)))?; + let jito_config = if intent.staking_type == Some(StakingType::Jito) { + Some(intent.stake_pool_config.as_ref().ok_or_else(|| { + WasmSolanaError::new("stakePoolConfig is required for JITO unstaking") + })?) + } else { + None + }; + let fee_payer: Pubkey = params .fee_payer .parse() @@ -543,12 +557,9 @@ fn build_unstake( .parse() .map_err(|_| WasmSolanaError::new("Invalid stakingAddress"))?; - // Check if Jito unstaking - if intent.staking_type == Some(StakingType::Jito) { - if let Some(config) = &intent.stake_pool_config { - let amount: u64 = intent.amount.as_ref().map(|a| a.value).unwrap_or(0); - return build_jito_unstake(config, &fee_payer, &intent.validator_address, amount); - } + if let Some(config) = jito_config { + let amount: u64 = intent.amount.as_ref().map(|a| a.value).unwrap_or(0); + return build_jito_unstake(config, &fee_payer, &intent.validator_address, amount); } // Check if partial unstake @@ -663,16 +674,6 @@ fn build_jito_unstake( let ata_program: Pubkey = SPL_ATA_PROGRAM_ID.parse().unwrap(); let clock_sysvar: Pubkey = solana_sdk::sysvar::clock::ID; - // Generate destination stake account - let unstake_keypair = Keypair::new(); - let unstake_address = unstake_keypair.address(); - let unstake_pubkey: Pubkey = unstake_address.parse().unwrap(); - - // Generate transfer authority - let transfer_authority_keypair = Keypair::new(); - let transfer_authority_address = transfer_authority_keypair.address(); - let transfer_authority_pubkey: Pubkey = transfer_authority_address.parse().unwrap(); - // Parse config addresses (with derivation for missing fields) let stake_pool: Pubkey = config .stake_pool_address @@ -731,6 +732,15 @@ fn build_jito_unstake( .parse() .map_err(|_| WasmSolanaError::new("Invalid managerFeeAccount"))?; + // Generate keys only after all Jito configuration fields are validated. + let unstake_keypair = Keypair::new(); + let unstake_address = unstake_keypair.address(); + let unstake_pubkey: Pubkey = unstake_address.parse().unwrap(); + + let transfer_authority_keypair = Keypair::new(); + let transfer_authority_address = transfer_authority_keypair.address(); + let transfer_authority_pubkey: Pubkey = transfer_authority_address.parse().unwrap(); + // 1. Approve: allow transfer_authority to spend pool tokens from user's ATA // SPL Token Approve instruction (index 4): [4u8] + amount as u64 LE let mut approve_data = vec![4u8]; @@ -1313,6 +1323,21 @@ mod tests { ); } + #[test] + fn test_jito_stake_requires_stake_pool_config() { + let intent = serde_json::json!({ + "intentType": "stake", + "validatorAddress": "invalid-validator", + "stakingType": "JITO", + "amount": { "value": "1000000000" } + }); + + let error = build_from_intent(&intent, &test_params()).unwrap_err(); + assert!(error + .to_string() + .contains("stakePoolConfig is required for JITO staking")); + } + #[test] fn test_stake_with_durable_nonce_structure() { use crate::transaction::TransactionExt; @@ -1356,6 +1381,21 @@ mod tests { ); } + #[test] + fn test_jito_unstake_requires_stake_pool_config() { + let intent = serde_json::json!({ + "intentType": "unstake", + "stakingType": "JITO", + "stakingAddress": "invalid-staking-address", + "amount": { "value": "1000000000" } + }); + + let error = build_from_intent(&intent, &test_params()).unwrap_err(); + assert!(error + .to_string() + .contains("stakePoolConfig is required for JITO unstaking")); + } + #[test] fn test_build_deactivate_intent() { let intent = serde_json::json!({ diff --git a/packages/wasm-solana/test/intentBuilder.ts b/packages/wasm-solana/test/intentBuilder.ts index c0e298cc6f2..1ba82c69183 100644 --- a/packages/wasm-solana/test/intentBuilder.ts +++ b/packages/wasm-solana/test/intentBuilder.ts @@ -9,17 +9,34 @@ import assert from "assert"; import { - buildFromIntent, + buildFromIntent as buildFromIntentApi, Transaction, parseTransaction, getAssociatedTokenAddress, } from "../dist/cjs/js/index.js"; +import type { BaseIntent, BuildFromIntentParams, SolanaIntent } from "../js/intentBuilder.js"; + +// Keep runtime-invalid inputs available to exercise Rust/WASM validation. +function buildFromIntent(intent: T, params: BuildFromIntentParams) { + return buildFromIntentApi(intent as SolanaIntent, params); +} describe("buildFromIntent", function () { // Common test params const feePayer = "DgT9qyYwYKBRDyDw3EfR12LHQCQjtNrKu2qMsXHuosmB"; const blockhash = "GWaQEymC3Z9SHM2gkh8u12xL1zJPMHPCSVR3pSDpEXE4"; const splTokenProgramId = "TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA"; + const stakePoolProgramId = "SPoo1Ku8WFXoNDMHPsrGSTSG1Y47rzgn41SLUNakuHy"; + const jitoStakePoolConfig = { + stakePoolAddress: feePayer, + withdrawAuthority: "5ZWgXcyqrrNpQHCme5SdC5hCeYb2o3fEJhF7Gok3bTVN", + reserveStake: feePayer, + destinationPoolAccount: "5ZWgXcyqrrNpQHCme5SdC5hCeYb2o3fEJhF7Gok3bTVN", + managerFeeAccount: feePayer, + poolMint: "5ZWgXcyqrrNpQHCme5SdC5hCeYb2o3fEJhF7Gok3bTVN", + validatorList: feePayer, + sourcePoolAccount: "5ZWgXcyqrrNpQHCme5SdC5hCeYb2o3fEJhF7Gok3bTVN", + }; describe("payment intent", function () { it("should build a simple payment transaction", function () { @@ -145,6 +162,173 @@ describe("buildFromIntent", function () { const stakeDelegate = parsed.instructionsData.find((i: any) => i.type === "StakingDelegate"); assert(stakeDelegate, "Should have StakingDelegate instruction"); }); + + it("should preserve explicitly selected native staking", function () { + const result = buildFromIntent( + { + intentType: "stake", + validatorAddress: "5ZWgXcyqrrNpQHCme5SdC5hCeYb2o3fEJhF7Gok3bTVN", + amount: { value: 1000000000n }, + stakingType: "NATIVE", + }, + { feePayer, nonce: { type: "blockhash", value: blockhash } }, + ); + const parsed = parseTransaction(result.transaction); + + assert.equal(result.generatedKeypairs.length, 1); + assert(parsed.instructionsData.some((i: any) => i.type === "CreateAccount")); + assert(parsed.instructionsData.some((i: any) => i.type === "StakeInitialize")); + assert(parsed.instructionsData.some((i: any) => i.type === "StakingDelegate")); + }); + }); + + describe("Jito stake intent", function () { + it("should reject missing pool configuration before native stake construction", function () { + assert.throws( + () => + buildFromIntent( + { + intentType: "stake", + validatorAddress: "invalid-validator", + amount: { value: 1000000000n }, + stakingType: "JITO", + }, + { feePayer, nonce: { type: "blockhash", value: blockhash } }, + ), + /stakePoolConfig is required for JITO staking/, + ); + }); + + it("should retain required-field errors for incomplete pool configuration", function () { + assert.throws( + () => + buildFromIntent( + { + intentType: "stake", + validatorAddress: feePayer, + amount: { value: 1000000000n }, + stakingType: "JITO", + stakePoolConfig: {}, + }, + { feePayer, nonce: { type: "blockhash", value: blockhash } }, + ), + /Missing reserveStake/, + ); + }); + + it("should build a stake-pool deposit for complete Jito configuration", function () { + const result = buildFromIntent( + { + intentType: "stake", + validatorAddress: feePayer, + amount: { value: 1000000000n }, + stakingType: "JITO", + stakePoolConfig: jitoStakePoolConfig, + }, + { feePayer, nonce: { type: "blockhash", value: blockhash } }, + ); + const parsed = parseTransaction(result.transaction); + const deposit = parsed.instructionsData.find((i: any) => i.type === "StakePoolDepositSol"); + + assert.equal( + result.generatedKeypairs.length, + 0, + "Jito deposit should not generate a stake keypair", + ); + assert.equal(result.transaction.instructions.length, 1, "Should emit only the pool deposit"); + assert.equal(result.transaction.instructions[0].programId, stakePoolProgramId); + assert(deposit, "Should emit StakePoolDepositSol"); + assert.equal((deposit as any).lamports, 1000000000n); + assert( + !parsed.instructionsData.some((i: any) => i.type === "StakingDelegate"), + "Jito deposit must not contain native delegation", + ); + }); + }); + + describe("Jito unstake intent", function () { + it("should reject missing pool configuration before native unstake construction", function () { + assert.throws( + () => + buildFromIntent( + { + intentType: "unstake", + stakingAddress: "invalid-staking-address", + amount: { value: 1000000n }, + stakingType: "JITO", + }, + { feePayer, nonce: { type: "blockhash", value: blockhash } }, + ), + /stakePoolConfig is required for JITO unstaking/, + ); + }); + + it("should retain required-field errors for incomplete pool configuration", function () { + assert.throws( + () => + buildFromIntent( + { + intentType: "unstake", + stakingAddress: feePayer, + validatorAddress: feePayer, + amount: { value: 1000000n }, + stakingType: "JITO", + stakePoolConfig: { stakePoolAddress: feePayer }, + }, + { feePayer, nonce: { type: "blockhash", value: blockhash } }, + ), + /Missing validatorList/, + ); + }); + + it("should build a stake-pool withdrawal for complete Jito configuration", function () { + const result = buildFromIntent( + { + intentType: "unstake", + stakingAddress: feePayer, + validatorAddress: "5ZWgXcyqrrNpQHCme5SdC5hCeYb2o3fEJhF7Gok3bTVN", + amount: { value: 1000000n }, + stakingType: "JITO", + stakePoolConfig: jitoStakePoolConfig, + }, + { feePayer, nonce: { type: "blockhash", value: blockhash } }, + ); + const parsed = parseTransaction(result.transaction); + const withdrawal = parsed.instructionsData.find( + (i: any) => i.type === "StakePoolWithdrawStake", + ); + + assert.equal( + result.transaction.instructions.some((i) => i.programId === stakePoolProgramId), + true, + ); + assert(withdrawal, "Should emit StakePoolWithdrawStake"); + assert.equal((withdrawal as any).poolTokens, 1000000n); + }); + }); + + describe("native unstake intent", function () { + it("should preserve explicit native deactivation", function () { + const result = buildFromIntent( + { + intentType: "unstake", + stakingAddress: "FKjSjCqByQRwSzZoMXA7bKnDbJe41YgJTHFFzBeC42bH", + stakingType: "NATIVE", + }, + { feePayer, nonce: { type: "blockhash", value: blockhash } }, + ); + const parsed = parseTransaction(result.transaction); + + assert.equal(result.generatedKeypairs.length, 0); + assert( + parsed.instructionsData.some((i: any) => i.type === "StakingDeactivate"), + "Native unstake should still deactivate the existing stake account", + ); + assert( + !result.transaction.instructions.some((i) => i.programId === stakePoolProgramId), + "Native unstake must not use the Jito stake-pool program", + ); + }); }); describe("deactivate intent", function () {