From 97349fe599cd18fcd2aec3aab4f4b6a9766aaffc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sylwester=20R=C4=85pa=C5=82a?= Date: Thu, 26 Sep 2024 09:44:22 +0200 Subject: [PATCH] fix: deserialize of enhanced transaction when transactionError contains different error than instructionError --- src/types/enhanced_transaction_types.rs | 5 +- tests/tests.rs | 4 + tests/types/deserialize_enhanced_tx.rs | 24 +++ ...__transaction_error_instruction_error.json | 162 ++++++++++++++++++ ...transaction_error_insufficient_founds.json | 52 ++++++ 5 files changed, 246 insertions(+), 1 deletion(-) create mode 100644 tests/types/deserialize_enhanced_tx.rs create mode 100644 tests/types/enhanced_tx__transaction_error_instruction_error.json create mode 100644 tests/types/enhanced_tx__transaction_error_insufficient_founds.json diff --git a/src/types/enhanced_transaction_types.rs b/src/types/enhanced_transaction_types.rs index 98d4dc7..b0dd612 100644 --- a/src/types/enhanced_transaction_types.rs +++ b/src/types/enhanced_transaction_types.rs @@ -77,8 +77,11 @@ pub struct TokenTransfer { #[derive(Serialize, Deserialize, Debug)] #[serde(rename_all = "camelCase")] pub struct TransactionError { - #[serde(rename = "InstructionError")] + #[serde(default, rename = "InstructionError")] pub instruciton_error: serde_json::Value, + // TODO adding below allow to handle other errors too but adding this field is breaking change as all other fields are pub + // #[serde(flatten)] + // other: serde_json::Value, } #[derive(Serialize, Deserialize, Debug)] diff --git a/tests/tests.rs b/tests/tests.rs index fba8dbc..e539da8 100644 --- a/tests/tests.rs +++ b/tests/tests.rs @@ -4,6 +4,10 @@ mod utils { mod test_make_keypairs; } +mod types { + mod deserialize_enhanced_tx; +} + mod rpc { mod test_get_asset; mod test_get_asset_batch; diff --git a/tests/types/deserialize_enhanced_tx.rs b/tests/types/deserialize_enhanced_tx.rs new file mode 100644 index 0000000..e017ac8 --- /dev/null +++ b/tests/types/deserialize_enhanced_tx.rs @@ -0,0 +1,24 @@ +use helius::types::EnhancedTransaction; + +#[test] +fn dese_enhanced_tx_with_transaction_error_insufficient_founds() { + let raw_json = include_str!("./enhanced_tx__transaction_error_insufficient_founds.json"); + let enhanced_tx: EnhancedTransaction = serde_json::from_str(raw_json).unwrap(); + + // There was different error that can be handled because of backward compatibility + assert_eq!( + enhanced_tx.transaction_error.unwrap().instruciton_error, + serde_json::Value::Null + ); +} + +#[test] +fn dese_enhanced_tx_with_instruction_error() { + let raw_json = include_str!("./enhanced_tx__transaction_error_instruction_error.json"); + let enhanced_tx: EnhancedTransaction = serde_json::from_str(raw_json).unwrap(); + + assert_ne!( + enhanced_tx.transaction_error.unwrap().instruciton_error, + serde_json::Value::Null + ); +} diff --git a/tests/types/enhanced_tx__transaction_error_instruction_error.json b/tests/types/enhanced_tx__transaction_error_instruction_error.json new file mode 100644 index 0000000..ef80c29 --- /dev/null +++ b/tests/types/enhanced_tx__transaction_error_instruction_error.json @@ -0,0 +1,162 @@ +{ + "description": "", + "type": "SWAP", + "source": "RAYDIUM", + "fee": 31563, + "feePayer": "22WLnDCeMvcoboRgVt8a4ztkd4yA1ptSv9JpnE2gx95R", + "signature": "3VvunzA4uPRjGeZcE8GjDUbPPPs2oQKR3AEDqw87vKiSYKr5SMH9uAWjigsTt5fEcqeC5NCt5DvZV8gkTnrU4piM", + "slot": 292070637, + "timestamp": 1727334237, + "tokenTransfers": [], + "nativeTransfers": [], + "accountData": [ + { + "account": "22WLnDCeMvcoboRgVt8a4ztkd4yA1ptSv9JpnE2gx95R", + "nativeBalanceChange": -31563, + "tokenBalanceChanges": [] + }, + { + "account": "9PG9Y6X27hZWzU6b1jJFv1PsvGU1o2xUzVZjcQMgbirC", + "nativeBalanceChange": 0, + "tokenBalanceChanges": [] + }, + { + "account": "AbcBkTe7AjStTBNiHb54ayfhFk69YBW2s6hSFrCfWDpu", + "nativeBalanceChange": 0, + "tokenBalanceChanges": [] + }, + { + "account": "9DCxsMizn3H1hprZ7xWe6LDzeUeZBksYFpBWBtSf1PQX", + "nativeBalanceChange": 0, + "tokenBalanceChanges": [] + }, + { + "account": "DBnvPrA6AekVqb6SLpGBY9QVaufvgvSmQ7PbztekWLoe", + "nativeBalanceChange": 0, + "tokenBalanceChanges": [] + }, + { + "account": "CnHbayYtvaXGyX9te1yCPVpiZysFTSPJ9H78CMHmoRKu", + "nativeBalanceChange": 0, + "tokenBalanceChanges": [] + }, + { + "account": "AEdY78voJWRRqcB4cARvAtgsmakdvKwWW5i9VHe3AxyA", + "nativeBalanceChange": 0, + "tokenBalanceChanges": [] + }, + { + "account": "H6cj3ZBQ4kf81mySHu4WTZhJmDEU5Duy5BvDLhRcZ8cy", + "nativeBalanceChange": 0, + "tokenBalanceChanges": [] + }, + { + "account": "83PT4anismqyBaXsjSnpWN7YbkxK6eGFykV9koqoCZ87", + "nativeBalanceChange": 0, + "tokenBalanceChanges": [] + }, + { + "account": "526LzAE23TE8cCCQmJw4bD8BM6N7JXV8Hm1CSJeCULiC", + "nativeBalanceChange": 0, + "tokenBalanceChanges": [] + }, + { + "account": "B8QYVSiaNnYzYe3mNaAPjrWAwnJFxRcTLr68TfCEyxBj", + "nativeBalanceChange": 0, + "tokenBalanceChanges": [] + }, + { + "account": "HCZiweAwvyGgAN3AXj5y7vzreozAY68PCqCm6PNHjS7R", + "nativeBalanceChange": 0, + "tokenBalanceChanges": [] + }, + { + "account": "1o6ECsnK5vzSkNVvSWNixEYSfr9tE2ota7ZdmU9j4TZ", + "nativeBalanceChange": 0, + "tokenBalanceChanges": [] + }, + { + "account": "6aXrakfEbmHv3BDRudMHvT5orGH7N36tjGh1RRgoFQDy", + "nativeBalanceChange": 0, + "tokenBalanceChanges": [] + }, + { + "account": "ComputeBudget111111111111111111111111111111", + "nativeBalanceChange": 0, + "tokenBalanceChanges": [] + }, + { + "account": "675kPX9MHTjS2zt1qfr1NYHuzeLXfQM9H24wFSUt1Mp8", + "nativeBalanceChange": 0, + "tokenBalanceChanges": [] + }, + { + "account": "EyxQ6UceYeAqN4tie9tBFSE1XorN7fWxSfX11WUxT1TX", + "nativeBalanceChange": 0, + "tokenBalanceChanges": [] + }, + { + "account": "TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA", + "nativeBalanceChange": 0, + "tokenBalanceChanges": [] + }, + { + "account": "5Q544fKrFoe6tsEbD7S8EmxGTJYAKtTVhAW5Q5pge4j1", + "nativeBalanceChange": 0, + "tokenBalanceChanges": [] + }, + { + "account": "srmqPvymJeFKQ4zGQed1GFppgkRHL9kaELCbyksJtPX", + "nativeBalanceChange": 0, + "tokenBalanceChanges": [] + } + ], + "transactionError": { + "InstructionError": [ + 2, + { + "Custom": 30 + } + ] + }, + "instructions": [ + { + "accounts": [], + "data": "3kw4sRb1pXrP", + "programId": "ComputeBudget111111111111111111111111111111", + "innerInstructions": [] + }, + { + "accounts": [], + "data": "EP8BzP", + "programId": "ComputeBudget111111111111111111111111111111", + "innerInstructions": [] + }, + { + "accounts": [ + "TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA", + "9PG9Y6X27hZWzU6b1jJFv1PsvGU1o2xUzVZjcQMgbirC", + "5Q544fKrFoe6tsEbD7S8EmxGTJYAKtTVhAW5Q5pge4j1", + "AbcBkTe7AjStTBNiHb54ayfhFk69YBW2s6hSFrCfWDpu", + "9DCxsMizn3H1hprZ7xWe6LDzeUeZBksYFpBWBtSf1PQX", + "DBnvPrA6AekVqb6SLpGBY9QVaufvgvSmQ7PbztekWLoe", + "CnHbayYtvaXGyX9te1yCPVpiZysFTSPJ9H78CMHmoRKu", + "srmqPvymJeFKQ4zGQed1GFppgkRHL9kaELCbyksJtPX", + "AEdY78voJWRRqcB4cARvAtgsmakdvKwWW5i9VHe3AxyA", + "H6cj3ZBQ4kf81mySHu4WTZhJmDEU5Duy5BvDLhRcZ8cy", + "83PT4anismqyBaXsjSnpWN7YbkxK6eGFykV9koqoCZ87", + "526LzAE23TE8cCCQmJw4bD8BM6N7JXV8Hm1CSJeCULiC", + "B8QYVSiaNnYzYe3mNaAPjrWAwnJFxRcTLr68TfCEyxBj", + "HCZiweAwvyGgAN3AXj5y7vzreozAY68PCqCm6PNHjS7R", + "EyxQ6UceYeAqN4tie9tBFSE1XorN7fWxSfX11WUxT1TX", + "1o6ECsnK5vzSkNVvSWNixEYSfr9tE2ota7ZdmU9j4TZ", + "6aXrakfEbmHv3BDRudMHvT5orGH7N36tjGh1RRgoFQDy", + "22WLnDCeMvcoboRgVt8a4ztkd4yA1ptSv9JpnE2gx95R" + ], + "data": "676XSqJuLojyTjc1qzZmEfH", + "programId": "675kPX9MHTjS2zt1qfr1NYHuzeLXfQM9H24wFSUt1Mp8", + "innerInstructions": [] + } + ], + "events": {} +} \ No newline at end of file diff --git a/tests/types/enhanced_tx__transaction_error_insufficient_founds.json b/tests/types/enhanced_tx__transaction_error_insufficient_founds.json new file mode 100644 index 0000000..9dc1ada --- /dev/null +++ b/tests/types/enhanced_tx__transaction_error_insufficient_founds.json @@ -0,0 +1,52 @@ +{ + "description": "7mHVPmoTJQZUGDHpWHRr2dV2e9UBaGknopjh3AdJpj89 transferred 0.05008025 SOL to BZkAJnd9QaNWgAXkTjNmKvYiAGXVAzhWZHJpJgo6RsAt.", + "type": "TRANSFER", + "source": "SYSTEM_PROGRAM", + "fee": 5000, + "feePayer": "7mHVPmoTJQZUGDHpWHRr2dV2e9UBaGknopjh3AdJpj89", + "signature": "ArRXvQdphogFmmuULMjHhk1nBV7vhjHwQxUbKAEkexPJYWmeFQXt5ycoyA2wKMTN9STdtHJ1Nm7d9csjC1oDd32", + "slot": 289356858, + "timestamp": 1726146439, + "tokenTransfers": [], + "nativeTransfers": [ + { + "fromUserAccount": "7mHVPmoTJQZUGDHpWHRr2dV2e9UBaGknopjh3AdJpj89", + "toUserAccount": "BZkAJnd9QaNWgAXkTjNmKvYiAGXVAzhWZHJpJgo6RsAt", + "amount": 50080250 + } + ], + "accountData": [ + { + "account": "7mHVPmoTJQZUGDHpWHRr2dV2e9UBaGknopjh3AdJpj89", + "nativeBalanceChange": -5000, + "tokenBalanceChanges": [] + }, + { + "account": "BZkAJnd9QaNWgAXkTjNmKvYiAGXVAzhWZHJpJgo6RsAt", + "nativeBalanceChange": 0, + "tokenBalanceChanges": [] + }, + { + "account": "11111111111111111111111111111111", + "nativeBalanceChange": 0, + "tokenBalanceChanges": [] + } + ], + "transactionError": { + "InsufficientFundsForRent": { + "account_index": 0 + } + }, + "instructions": [ + { + "accounts": [ + "7mHVPmoTJQZUGDHpWHRr2dV2e9UBaGknopjh3AdJpj89", + "BZkAJnd9QaNWgAXkTjNmKvYiAGXVAzhWZHJpJgo6RsAt" + ], + "data": "3Bxs4ihZh9cqzbLb", + "programId": "11111111111111111111111111111111", + "innerInstructions": [] + } + ], + "events": {} +} \ No newline at end of file