Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/wasm-solana/js/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export { buildFromIntent, buildFromIntent as buildTransactionFromIntent } from "
export type {
BaseIntent,
PaymentIntent,
GoUnstakeIntent,
StakeIntent,
UnstakeIntent,
ClaimIntent,
Expand Down
42 changes: 33 additions & 9 deletions packages/wasm-solana/js/intentBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down Expand Up @@ -223,6 +246,7 @@ export interface CustomTxKey {
/** Union of all supported intent types */
export type SolanaIntent =
| PaymentIntent
| GoUnstakeIntent
| StakeIntent
| UnstakeIntent
| ClaimIntent
Expand Down Expand Up @@ -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;
Expand Down
82 changes: 61 additions & 21 deletions packages/wasm-solana/src/intent/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -312,18 +312,24 @@ 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()
.map_err(|_| WasmSolanaError::new("Invalid feePayer"))?;

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)
Expand Down Expand Up @@ -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()
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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];
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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!({
Expand Down
Loading
Loading