Skip to content

Commit b7b5a5a

Browse files
wli-proBillyWooo
andauthored
fix: P-1485 update all fields to optional in ...ResponseData of pumpx… (#3427)
* fix: P-1485 update all fields to optional in ...ResponseData of pumpx api to avoid decode error * fix: P-1485 update format * fix: P-1485 update optional fields in submit_swap_order * fix: P-1485 remove Option for ApiResponse.data --------- Co-authored-by: wli-pro <wli-pro> Co-authored-by: BillyWooo <yang@trustcomputing.de>
1 parent afde768 commit b7b5a5a

File tree

9 files changed

+143
-125
lines changed

9 files changed

+143
-125
lines changed

tee-worker/omni-executor/intent/executors/cross-chain/src/lib.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -973,12 +973,11 @@ impl<Provider: EthereumRpcProvider<Transaction = TransactionRequest> + Send + Sy
973973
})?;
974974
debug!("Response get_gas_info: {:?}", res);
975975

976-
let Some(gas_info_data) = res.data else {
977-
log::error!("Response data of call get gas info is none");
976+
let Some(gas_info_vec) = res.data.gas_info else {
977+
log::error!("Response data.gas_info of call get gas info is none");
978978
return Err(());
979979
};
980-
let Some(gas_info) = gas_info_data
981-
.gas_info
980+
let Some(gas_info) = gas_info_vec
982981
.iter()
983982
.find(|g| g.chain_id == pumpx_config.to_chain_id.to_string())
984983
else {

tee-worker/omni-executor/native-task-handler/src/lib.rs

Lines changed: 45 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -598,16 +598,15 @@ async fn handle_native_task<
598598
};
599599
log::debug!("Response pumpx get_account_user_id: {:?}", res);
600600

601-
let Some(res_data) = res.data else {
601+
let Some(user_id) = res.data.user_id else {
602602
send_error(
603-
"Response data of call get_account_user_id is none".to_string(),
603+
"Response data.user_id of call get_account_user_id is none".to_string(),
604604
response_sender,
605605
NativeTaskError::PumpxApiError(PumpxApiError::GetAccountUserIdFailed),
606606
);
607607
return;
608608
};
609609

610-
let user_id = res_data.user_id;
611610
log::debug!("get_account_user_id ok, email: {}, user_id: {}", email, user_id);
612611
let omni_account =
613612
Identity::from_web2_account(&user_id, Web2IdentityType::Pumpx).to_omni_account();
@@ -650,20 +649,11 @@ async fn handle_native_task<
650649
log::debug!("Response pumpx user_connect: {:?}", backend_response);
651650

652651
// check google auth value
653-
if let Some(ref user_connect_res) = backend_response.data {
654-
if !user_connect_res.google_auth_check {
655-
send_error(
656-
"Google code verification failed from user_connect".to_string(),
657-
response_sender,
658-
NativeTaskError::PumpxApiError(PumpxApiError::GoogleCodeVerificationFailed),
659-
);
660-
return;
661-
}
662-
} else {
652+
if !backend_response.data.google_auth_check.unwrap_or(false) {
663653
send_error(
664-
"Invalid response data field of user_connect".to_string(),
654+
"Google code verification failed from user_connect".to_string(),
665655
response_sender,
666-
NativeTaskError::PumpxApiError(PumpxApiError::UserConnectionFailed),
656+
NativeTaskError::PumpxApiError(PumpxApiError::GoogleCodeVerificationFailed),
667657
);
668658
return;
669659
}
@@ -722,25 +712,16 @@ async fn handle_native_task<
722712
return;
723713
};
724714

725-
log::debug!("Calling pumpx verify_google_code, code: {}", google_code);
726-
let verify_result =
727-
ctx.pumpx_api.verify_google_code(&access_token, google_code, None).await;
728-
let verify_success = match verify_result {
729-
Ok(res) => match res.data {
730-
Some(data) => data.result,
731-
None => {
732-
log::error!("Google code verification response data is none");
733-
false
734-
},
735-
},
736-
Err(e) => {
737-
log::error!("Google code verification request failed: {:?}", e);
738-
false
739-
},
740-
};
715+
let verify_success = verify_google_code(
716+
ctx.pumpx_api.as_ref().as_ref(),
717+
&access_token,
718+
google_code,
719+
None,
720+
)
721+
.await;
741722
if !verify_success {
742723
send_error(
743-
"Google code verification failed".to_string(),
724+
"Failed to verify google code within NativeTask::PumpxExportWallet".to_string(),
744725
response_sender,
745726
NativeTaskError::PumpxApiError(PumpxApiError::GoogleCodeVerificationFailed),
746727
);
@@ -879,26 +860,13 @@ async fn handle_native_task<
879860
};
880861

881862
// 2. Verify google code in every case
882-
log::debug!("Calling pumpx verify_google_code, code: {}", google_code);
883-
let verify_result = ctx
884-
.pumpx_api
885-
.verify_google_code(&access_token, google_code, language.clone())
886-
.await;
887-
888-
let verify_success = match verify_result {
889-
Ok(res) => match res.data {
890-
Some(data) => data.result,
891-
None => {
892-
log::error!("Google code verification response data is none");
893-
false
894-
},
895-
},
896-
Err(e) => {
897-
log::error!("Google code verification request failed: {:?}", e);
898-
false
899-
},
900-
};
901-
863+
let verify_success = verify_google_code(
864+
ctx.pumpx_api.as_ref().as_ref(),
865+
&access_token,
866+
google_code,
867+
language.clone(),
868+
)
869+
.await;
902870
if !verify_success {
903871
send_error(
904872
"Failed to verify google code within NativeTask::PumpxTransferWidthdraw"
@@ -1111,3 +1079,28 @@ async fn notify_intent_completed<
11111079
},
11121080
};
11131081
}
1082+
1083+
async fn verify_google_code(
1084+
pumpx_api: &dyn PumpxApi,
1085+
access_token: &str,
1086+
google_code: String,
1087+
language: Option<String>,
1088+
) -> bool {
1089+
log::debug!("Calling pumpx verify_google_code, code: {}", google_code);
1090+
let verify_result = pumpx_api.verify_google_code(access_token, google_code, language).await;
1091+
verify_result.map_or_else(
1092+
|e| {
1093+
log::error!("Google code verification request failed: {:?}", e);
1094+
false
1095+
},
1096+
|res| {
1097+
res.data.result.map_or_else(
1098+
|| {
1099+
log::error!("Google code verification response result is none");
1100+
false
1101+
},
1102+
|success| success,
1103+
)
1104+
},
1105+
)
1106+
}

tee-worker/omni-executor/pumpx/src/pumpx_api/types.rs

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -67,11 +67,11 @@ impl GasType {
6767
pub struct ApiResponse<T: Codec> {
6868
pub code: u32,
6969
pub message: String,
70-
pub data: Option<T>,
70+
pub data: T,
7171
}
7272

7373
impl<T: Codec> ApiResponse<T> {
74-
pub fn data(&self) -> &Option<T> {
74+
pub fn data(&self) -> &T {
7575
&self.data
7676
}
7777
}
@@ -95,8 +95,8 @@ pub struct CreateTransferUnsignedTxBody {
9595
#[serde(rename_all = "camelCase")]
9696
pub struct CreateTransferUnsignedTxResponseData {
9797
pub tx_data: Option<Vec<String>>,
98-
pub transfer_id: u32,
99-
pub chain_id: u32,
98+
pub transfer_id: Option<u32>,
99+
pub chain_id: Option<u32>,
100100
}
101101

102102
// /v3/trade/send_transfer_tx
@@ -127,8 +127,8 @@ pub struct UserConnectBody {
127127
#[derive(Serialize, Deserialize, Encode, Decode, Debug, PartialEq, Eq, Clone)]
128128
#[serde(rename_all = "camelCase")]
129129
pub struct UserConnectResponseData {
130-
pub user_id: String,
131-
pub google_auth_check: bool,
130+
pub user_id: Option<String>,
131+
pub google_auth_check: Option<bool>,
132132
}
133133

134134
// /v3/trade/create_market_order_unsigned_tx
@@ -153,9 +153,9 @@ pub struct CreateMarketOrderUnsignedTxBody {
153153
#[derive(Serialize, Deserialize, Encode, Decode, Debug, PartialEq, Eq, Clone)]
154154
#[serde(rename_all = "camelCase")]
155155
pub struct CreateMarketOrderUnsignedTxResponseData {
156-
pub chain_id: u32,
157-
pub order_id: u32,
158-
pub tx_data: Vec<String>,
156+
pub chain_id: Option<u32>,
157+
pub order_id: Option<u32>,
158+
pub tx_data: Option<Vec<String>>,
159159
}
160160

161161
// /v3/trade/create_market_order_tx
@@ -200,9 +200,9 @@ pub struct CreateTransferTxBody {
200200
#[derive(Serialize, Deserialize, Encode, Decode, Debug, PartialEq, Eq, Clone)]
201201
#[serde(rename_all = "camelCase")]
202202
pub struct CreateTransferTxResponseData {
203-
pub tx_hash: String,
204-
pub transfer_id: u32,
205-
pub chain_id: u32,
203+
pub tx_hash: Option<String>,
204+
pub transfer_id: Option<u32>,
205+
pub chain_id: Option<u32>,
206206
}
207207

208208
// /v3/trade/send_order_tx
@@ -224,15 +224,15 @@ pub struct SendOrderTxResponseData {
224224
#[derive(Serialize, Deserialize, Encode, Decode, Debug, PartialEq, Eq, Clone)]
225225
#[serde(rename_all = "camelCase")]
226226
pub struct UserTradeInfoResponseData {
227-
pub gas_type_base: GasType,
228-
pub gas_type_bsc: GasType,
229-
pub gas_type_eth: GasType,
230-
pub gas_type_sol: GasType,
231-
pub gas_type_omni: GasType,
232-
pub is_anti_mev: bool,
233-
pub is_auto_slippage: bool,
234-
pub slippage: u32,
235-
pub slippage_display: String,
227+
pub gas_type_base: Option<GasType>,
228+
pub gas_type_bsc: Option<GasType>,
229+
pub gas_type_eth: Option<GasType>,
230+
pub gas_type_sol: Option<GasType>,
231+
pub gas_type_omni: Option<GasType>,
232+
pub is_anti_mev: Option<bool>,
233+
pub is_auto_slippage: Option<bool>,
234+
pub slippage: Option<u32>,
235+
pub slippage_display: Option<String>,
236236
}
237237

238238
// /v3/trade/create_limit_order/
@@ -274,7 +274,7 @@ pub struct GoogleCode {
274274

275275
#[derive(Serialize, Deserialize, Encode, Decode, Debug, PartialEq, Eq, Clone)]
276276
pub struct VerifyGoogleCodeResponseData {
277-
pub result: bool,
277+
pub result: Option<bool>,
278278
}
279279

280280
// /v3/trade/create_cross_order
@@ -318,7 +318,7 @@ pub struct GetGasInfoParams {
318318
#[derive(Serialize, Deserialize, Encode, Decode, Debug, PartialEq, Eq, Clone)]
319319
#[serde(rename_all = "camelCase")]
320320
pub struct GetGasInfoResponseData {
321-
pub gas_info: Vec<GasInfo>,
321+
pub gas_info: Option<Vec<GasInfo>>,
322322
}
323323

324324
#[derive(Serialize, Deserialize, Encode, Decode, Debug, PartialEq, Eq, Clone)]
@@ -346,5 +346,5 @@ pub struct GetAccountUserIdParams {
346346
#[derive(Serialize, Deserialize, Encode, Decode, Debug, PartialEq, Eq, Clone)]
347347
#[serde(rename_all = "camelCase")]
348348
pub struct GetAccountUserIdResponseData {
349-
pub user_id: String,
349+
pub user_id: Option<String>,
350350
}

tee-worker/omni-executor/rpc-server/src/error_code.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ const PUMPX_API_SEND_TRANSFER_TX_FAILED_CODE: i32 = -32036;
2626
const PUMPX_API_INVALID_INPUT_FAILED_CODE: i32 = -32037;
2727
const PUMPX_API_CREATE_TRANSFER_TX_FAILED_CODE: i32 = -32038;
2828
pub const PUMPX_API_GET_ACCOUNT_USER_ID_FAILED_CODE: i32 = -32039;
29+
pub const PUMPX_API_GET_USER_TRADE_INFO_FAILED_CODE: i32 = -32040;
2930

3031
const PUMPX_SIGNER_REQUEST_SIGNATURE_FAILED_CODE: i32 = -32050;
3132

tee-worker/omni-executor/rpc-server/src/methods/omni/submit_swap_order.rs

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -171,17 +171,18 @@ pub fn register_submit_swap_order(module: &mut RpcModule<RpcContext>) {
171171

172172
log::debug!("Response pumpx get_user_trade_info: {:?}", user_trade_info);
173173

174-
let Some(user_trade_info_data) = user_trade_info.data else {
175-
log::error!("Response data of call get_user_trade_info is none");
176-
return Err(PumpxRpcError::from_error_code(ErrorCode::ServerError(
177-
PUMPX_API_GET_ACCOUNT_USER_ID_FAILED_CODE,
178-
)));
179-
};
174+
let gas_type_base = check_and_get_option_user_trade_info_field(user_trade_info.data.gas_type_base, "gas_type_base")?;
175+
let gas_type_bsc = check_and_get_option_user_trade_info_field(user_trade_info.data.gas_type_bsc, "gas_type_bsc")?;
176+
let gas_type_eth = check_and_get_option_user_trade_info_field(user_trade_info.data.gas_type_eth, "gas_type_eth")?;
177+
let is_anti_mev = check_and_get_option_user_trade_info_field(user_trade_info.data.is_anti_mev, "is_anti_mev")?;
178+
let is_auto_slippage = check_and_get_option_user_trade_info_field(user_trade_info.data.is_auto_slippage, "is_auto_slippage")?;
179+
let slippage = check_and_get_option_user_trade_info_field(user_trade_info.data.slippage, "slippage")?;
180+
180181
let gas_type = match params.to_chain_id {
181-
BASE_CHAIN_ID => user_trade_info_data.gas_type_base.to_number() as u32,
182-
ETHEREUM_CHAIN_ID => user_trade_info_data.gas_type_eth.to_number() as u32,
183-
BSC_CHAIN_ID => user_trade_info_data.gas_type_bsc.to_number() as u32,
184-
SOLANA_CHAIN_ID => user_trade_info_data.gas_type_base.to_number() as u32,
182+
BASE_CHAIN_ID => gas_type_base.to_number() as u32,
183+
ETHEREUM_CHAIN_ID => gas_type_eth.to_number() as u32,
184+
BSC_CHAIN_ID => gas_type_bsc.to_number() as u32,
185+
SOLANA_CHAIN_ID => gas_type_base.to_number() as u32,
185186
_ => {
186187
log::error!("Unsupported chain id: {}", params.to_chain_id);
187188
return Err(PumpxRpcError::from_error_code(ErrorCode::InvalidParams));
@@ -238,10 +239,10 @@ pub fn register_submit_swap_order(module: &mut RpcModule<RpcContext>) {
238239
)?,
239240
double_out: params.double_out,
240241
is_one_click: params.is_one_click,
241-
is_anti_mev: user_trade_info_data.is_anti_mev,
242-
is_auto_slippage: user_trade_info_data.is_auto_slippage,
242+
is_anti_mev,
243+
is_auto_slippage,
243244
gas_type,
244-
slippage: user_trade_info_data.slippage,
245+
slippage,
245246
wallet_index: params.wallet_index,
246247
token_cap,
247248
price_usd,
@@ -310,3 +311,15 @@ pub fn register_submit_swap_order(module: &mut RpcModule<RpcContext>) {
310311
})
311312
.expect("Failed to register omni_submitSwapOrder method");
312313
}
314+
315+
fn check_and_get_option_user_trade_info_field<T>(
316+
field_value: Option<T>,
317+
field_name: &str,
318+
) -> Result<T, PumpxRpcError> {
319+
field_value.ok_or_else(|| {
320+
log::error!("Response data.{} of call get_user_trade_info is none", field_name);
321+
PumpxRpcError::from_error_code(ErrorCode::ServerError(
322+
PUMPX_API_GET_ACCOUNT_USER_ID_FAILED_CODE,
323+
))
324+
})
325+
}

tee-worker/omni-executor/rpc-server/src/methods/pumpx/common.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ pub struct PumpxRpcError {
1616
}
1717

1818
#[derive(Serialize, Debug)]
19+
#[serde(rename_all = "camelCase")]
1920
pub struct PumpxRpcErrorData {
2021
#[serde(skip_serializing_if = "Option::is_none")]
2122
pub backend_response: Option<PumpxRpcErrorBackendResponse>,
@@ -119,3 +120,14 @@ where
119120
}
120121
Ok(())
121122
}
123+
124+
pub fn check_and_get_option_response_data<T>(
125+
data: Option<T>,
126+
code: i32,
127+
message: &str,
128+
) -> Result<T, PumpxRpcError> {
129+
data.ok_or_else(|| {
130+
log::error!("{}", message);
131+
PumpxRpcError::from_error_code(ErrorCode::ServerError(code))
132+
})
133+
}

tee-worker/omni-executor/rpc-server/src/methods/pumpx/export_wallet.rs

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
use crate::{
2-
error_code::*, methods::pumpx::PumpxRpcError, server::RpcContext, verify_auth::verify_auth,
2+
error_code::*,
3+
methods::pumpx::{common::check_and_get_option_response_data, PumpxRpcError},
4+
server::RpcContext,
5+
verify_auth::verify_auth,
36
Deserialize, ErrorCode,
47
};
58
use ethers::types::Bytes;
@@ -81,19 +84,14 @@ pub fn register_export_wallet(module: &mut RpcModule<RpcContext>) {
8184
};
8285
log::debug!("Response pumpx get_account_user_id: {:?}", res);
8386

84-
let Some(res_data) = res.data else {
85-
log::error!("Response data of call get_account_user_id is none");
86-
return Err(PumpxRpcError::from_error_code(ErrorCode::ServerError(
87-
PUMPX_API_GET_ACCOUNT_USER_ID_FAILED_CODE,
88-
)));
89-
};
87+
let user_id = check_and_get_option_response_data(res.data.user_id, PUMPX_API_GET_ACCOUNT_USER_ID_FAILED_CODE, "Response data.user_id of call get_account_user_id is none")?;
9088

91-
if res_data.user_id != params.user_id {
89+
if user_id != params.user_id {
9290
log::error!(
9391
"Parameter mismatch: user_id {} and user_email {}, expected user_id {}",
9492
params.user_id,
9593
params.user_email,
96-
res_data.user_id
94+
user_id
9795
);
9896
return Err(PumpxRpcError::from_error_code(ErrorCode::ServerError(
9997
USER_EMAIL_ID_MISMATCH_CODE,

0 commit comments

Comments
 (0)