Skip to content

Commit 4d20918

Browse files
authored
fix: sign the unsigned tx within omni-executor (#3441)
* fix: sign the unsigned tx within omni-executor * refactor: unwraps and fmt * refactor: log errors * fix: use empty string for recipient_address * refactor: cargo fmt
1 parent fafc11e commit 4d20918

File tree

1 file changed

+89
-18
lines changed
  • tee-worker/omni-executor/intent/executors/cross-chain/src

1 file changed

+89
-18
lines changed

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

Lines changed: 89 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414
// You should have received a copy of the GNU General Public License
1515
// along with Litentry. If not, see <https://www.gnu.org/licenses/>.
1616

17-
use alloy::primitives::{Address, U256};
17+
use alloy::consensus::{SignableTransaction, TxLegacy};
18+
use alloy::primitives::{Address, PrimitiveSignature, U256};
1819
use async_trait::async_trait;
1920
use base58::ToBase58;
2021
use binance_api::spot_trading_api::types::{
@@ -50,16 +51,8 @@ use pumpx::methods::create_cross_order::CrossOrderInfo;
5051
use pumpx::signer_client::ChainType;
5152
use pumpx::signer_client::SignerClient;
5253

53-
use pumpx::methods::create_cross_order::CreateCrossOrderBody;
54-
use pumpx::methods::create_limit_order::CreateLimitOrderBody;
55-
use pumpx::methods::create_market_order_tx::CreateMarketOrderTxBody;
56-
use pumpx::methods::cross_fail::CrossFailBody;
57-
use pumpx::PumpxApi;
58-
use pumpx::{pubkey_to_evm_address, pubkey_to_solana_address};
59-
use std::collections::HashMap;
60-
use std::sync::Arc;
61-
6254
use accounting_contract_client::AccountingContractApi;
55+
use alloy::primitives::private::alloy_rlp::Decodable;
6356
use binance_api::BinanceApi;
6457
use executor_primitives::AccountId;
6558
use executor_primitives::ChainAsset;
@@ -69,9 +62,18 @@ use parentchain_rpc_client::CustomConfig;
6962
use parentchain_rpc_client::SubxtClient;
7063
use parentchain_rpc_client::SubxtClientFactory;
7164
use parentchain_signer::TxSigner;
65+
use pumpx::methods::create_cross_order::CreateCrossOrderBody;
66+
use pumpx::methods::create_limit_order::CreateLimitOrderBody;
67+
use pumpx::methods::create_market_order_tx::CreateMarketOrderTxBody;
68+
use pumpx::methods::cross_fail::CrossFailBody;
69+
use pumpx::PumpxApi;
70+
use pumpx::{pubkey_to_evm_address, pubkey_to_solana_address};
71+
use std::collections::HashMap;
72+
use std::sync::Arc;
7273

7374
use log::debug;
74-
75+
use pumpx::methods::create_market_order_unsigned_tx::CreateMarketOrderUnsignedTxBody;
76+
use pumpx::methods::send_order_tx::{SendOrderTxBody, SendOrderTxResponse};
7577
// use intent_asset_lock::always_unlocked::AlwaysUnlockedAssetsLock;
7678
// use intent_asset_lock::AccountAssetLocks;
7779

@@ -1010,7 +1012,7 @@ impl<BinanceClient: BinanceApi> IntentExecutor for CrossChainIntentExecutor<Bina
10101012
};
10111013

10121014
debug!("Doing market order");
1013-
let body = CreateMarketOrderTxBody {
1015+
let body = CreateMarketOrderUnsignedTxBody {
10141016
request_id: intent_id,
10151017
chain_id: pumpx_config.to_chain_id,
10161018
token_ca: to_token_ca.clone(),
@@ -1039,14 +1041,14 @@ impl<BinanceClient: BinanceApi> IntentExecutor for CrossChainIntentExecutor<Bina
10391041
},
10401042
slippage: pumpx_config.slippage,
10411043
wallet_index: pumpx_config.wallet_index,
1044+
recipient_address: String::from(""),
10421045
};
10431046
debug!("Calling pumpx create_market_order_tx, body: {:?}", body);
1044-
let response =
1045-
self.pumpx_api.create_market_order_tx(&access_token, body).await.map_err(
1046-
|_| {
1047-
log::error!("Failed to create market order tx");
1048-
},
1049-
)?;
1047+
1048+
let response = self
1049+
.create_market_order_tx(&access_token, body, account_id)
1050+
.await
1051+
.map_err(|_| log::error!("Failed to create and submit market order tx"))?;
10501052

10511053
debug!("Response create_market_order_tx: {:?}", response);
10521054
result = (Some(response.encode()), true);
@@ -1074,6 +1076,75 @@ impl<BinanceClient: BinanceApi> IntentExecutor for CrossChainIntentExecutor<Bina
10741076
}
10751077
}
10761078

1079+
impl<BinanceClient: BinanceApi> CrossChainIntentExecutor<BinanceClient> {
1080+
pub async fn create_market_order_tx(
1081+
&self,
1082+
access_token: &str,
1083+
order: CreateMarketOrderUnsignedTxBody,
1084+
account_id: &AccountId,
1085+
) -> Result<SendOrderTxResponse, ()> {
1086+
let wallet_index = order.wallet_index;
1087+
let response = self
1088+
.pumpx_api
1089+
.create_market_order_unsigned_tx(access_token, order)
1090+
.await
1091+
.map_err(|_| log::error!("Failed to get unsigned market order tx"))?;
1092+
1093+
let unsigned_tx_string = response.data.tx_data.ok_or_else(|| {
1094+
log::error!("Failed to unwrap tx_data");
1095+
})?;
1096+
let order_id = response.data.order_id.ok_or_else(|| {
1097+
log::error!("Failed to unwrap order_id");
1098+
})?;
1099+
let chain_id = response.data.chain_id.ok_or_else(|| {
1100+
log::error!("Failed to unwrap chain_id");
1101+
})?;
1102+
1103+
let unsigned_tx_bytes = unsigned_tx_string
1104+
.iter()
1105+
.map(|s| {
1106+
let hex_str = s.trim_start_matches("0x");
1107+
hex::decode(hex_str).expect("Invalid hex string")
1108+
})
1109+
.collect();
1110+
1111+
let signatures = self
1112+
.pumpx_signer_client
1113+
.request_signatures(
1114+
ChainType::Evm,
1115+
wallet_index,
1116+
*account_id.as_ref(),
1117+
unsigned_tx_bytes,
1118+
)
1119+
.await?;
1120+
1121+
let mut tx_data: Vec<String> = vec![];
1122+
for (x, y) in unsigned_tx_string.into_iter().zip(signatures.into_iter()) {
1123+
let bytes = hex::decode(x.trim_start_matches("0x"))
1124+
.map_err(|_| log::error!("invalid hex string"))?;
1125+
// We should be able to decode it to Legacy Transaction
1126+
// As it is RLP Encoded Bytes which adheres to string encoding rules
1127+
let unsigned_tx = TxLegacy::decode(&mut &bytes[..])
1128+
.map_err(|_| log::error!("Failed to decode legacy tx"))?;
1129+
let signature = PrimitiveSignature::try_from(y.as_ref())
1130+
.map_err(|_| log::error!("Failed to create Typed signature"))?;
1131+
1132+
let signed_tx = unsigned_tx.into_signed(signature);
1133+
let mut encoded_signed_tx = vec![];
1134+
signed_tx.rlp_encode(&mut encoded_signed_tx);
1135+
tx_data.push(hex::encode(encoded_signed_tx));
1136+
}
1137+
1138+
let response = self
1139+
.pumpx_api
1140+
.send_order_tx(access_token, SendOrderTxBody { order_id, chain_id, tx_data })
1141+
.await
1142+
.map_err(|_| log::error!("Failed to send order tx"))?;
1143+
1144+
Ok(response)
1145+
}
1146+
}
1147+
10771148
fn str_to_u256(amount: &str, decimals: u32) -> Option<U256> {
10781149
let amount = Decimal::from_str(amount).ok()?;
10791150
let factor = Decimal::from(10u64.pow(decimals));

0 commit comments

Comments
 (0)