feat: return native Solana InstructionError code in TradeError
- Simplify error code handling by directly returning Solana's native InstructionError codes
- Custom errors return their embedded code directly
- Standard InstructionError variants map to fixed codes (1-10, 999)
- Remove nested match pattern for better code readability
- Error format: buy_failed|sig={signature}|code={error_code}|msg={message}
Benefits:
- More accurate error identification using native Solana error codes
- Simpler and cleaner code structure
- Easier to maintain and extend
- Consistent with Solana ecosystem standards
This commit is contained in:
+19
-5
@@ -133,18 +133,32 @@ pub async fn poll_transaction_confirmation(
|
||||
let ui_err = meta.err.unwrap();
|
||||
let tx_err: TransactionError =
|
||||
serde_json::from_value(serde_json::to_value(&ui_err)?)?;
|
||||
let mut code = 500;
|
||||
|
||||
// 直接使用Solana原生的InstructionError中的错误码
|
||||
let mut code = 0u32;
|
||||
let mut index = None;
|
||||
match &tx_err {
|
||||
TransactionError::InstructionError(i, i_error) => {
|
||||
match i_error {
|
||||
solana_sdk::instruction::InstructionError::Custom(c) => code = *c,
|
||||
_ => {}
|
||||
}
|
||||
// 直接匹配所有InstructionError类型,Custom也是其中之一
|
||||
code = match i_error {
|
||||
solana_sdk::instruction::InstructionError::Custom(c) => *c,
|
||||
solana_sdk::instruction::InstructionError::GenericError => 1,
|
||||
solana_sdk::instruction::InstructionError::InvalidArgument => 2,
|
||||
solana_sdk::instruction::InstructionError::InvalidInstructionData => 3,
|
||||
solana_sdk::instruction::InstructionError::InvalidAccountData => 4,
|
||||
solana_sdk::instruction::InstructionError::AccountDataTooSmall => 5,
|
||||
solana_sdk::instruction::InstructionError::InsufficientFunds => 6,
|
||||
solana_sdk::instruction::InstructionError::IncorrectProgramId => 7,
|
||||
solana_sdk::instruction::InstructionError::MissingRequiredSignature => 8,
|
||||
solana_sdk::instruction::InstructionError::AccountAlreadyInitialized => 9,
|
||||
solana_sdk::instruction::InstructionError::UninitializedAccount => 10,
|
||||
_ => 999, // 其他未知错误
|
||||
};
|
||||
index = Some(*i);
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
|
||||
return Err(anyhow::Error::new(TradeError {
|
||||
code: code,
|
||||
message: format!("{} {:?}", tx_err, error_msg),
|
||||
|
||||
Reference in New Issue
Block a user