Skip to content

Commit 600168e

Browse files
authored
chore: combine do_binance_swap_sol_to_bsc and do_binance_swap_bsc_to_sol into do_binance_swap, combine do_payout_sol_to_bsc and do_payout_bsc_to_sol infodo_payout (#3510)
Co-authored-by: wli-pro <wli-pro>
1 parent 80a78a0 commit 600168e

File tree

11 files changed

+386
-624
lines changed

11 files changed

+386
-624
lines changed

tee-worker/omni-executor/accounting-contract-client/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ edition.workspace = true
66
[dependencies]
77
accounting-contract = { workspace = true }
88
alloy = { workspace = true, features = ["contract", "signer-local", "rpc", "rpc-types", "eips"] }
9-
anchor-client = { workspace = true }
9+
anchor-client = { workspace = true, features = ["async"] }
1010
async-trait = { workspace = true }
1111
bincode = { workspace = true }
1212
ethereum-rpc = { workspace = true }

tee-worker/omni-executor/accounting-contract-client/src/lib.rs

Lines changed: 39 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,22 +17,52 @@ sol!(
1717
"./abi/AccountingContract.json"
1818
);
1919

20+
pub trait Plus<RHS = Self> {
21+
type Output;
22+
23+
fn plus(self, rhs: RHS) -> Self::Output;
24+
}
25+
26+
impl Plus<u64> for U256 {
27+
type Output = U256;
28+
29+
fn plus(self, rhs: u64) -> Self::Output {
30+
self + U256::from(rhs)
31+
}
32+
}
33+
34+
impl Plus<u64> for u64 {
35+
type Output = u64;
36+
37+
fn plus(self, rhs: u64) -> Self::Output {
38+
self + rhs
39+
}
40+
}
41+
2042
#[async_trait]
21-
pub trait AccountingContractApi: Send + Sync {
43+
pub trait AccountingContractApi<A, N>: Send + Sync
44+
where
45+
A: Send + Sync,
46+
N: Plus<u64, Output = N> + std::fmt::Debug,
47+
{
2248
async fn execute_pay_out_request(
2349
&self,
24-
beneficiary: Address,
25-
nonce: U256,
50+
beneficiary: A,
51+
nonce: N,
2652
amount: U256,
2753
) -> Result<(), ()>;
2854

29-
async fn get_nonce(&self, user: Address) -> Result<U256, ()>;
55+
async fn get_nonce(&self, user: A) -> Result<N, ()>;
3056

3157
async fn get_balance(&self) -> Result<U256, ()>;
3258

33-
async fn get_address(&self) -> Address;
59+
async fn get_address(&self) -> A {
60+
unimplemented!("Please provide contract address in implementation")
61+
}
3462

35-
async fn get_signer_address(&self) -> Address;
63+
async fn get_signer_address(&self) -> A {
64+
unimplemented!("Please provide signer address in implementation")
65+
}
3666
}
3767

3868
pub struct AccountingContractClient<P: RpcProvider<Transaction = TransactionRequest> + Send + Sync>
@@ -48,8 +78,8 @@ impl<P: RpcProvider<Transaction = TransactionRequest> + Send + Sync> AccountingC
4878
}
4979

5080
#[async_trait]
51-
impl<P: RpcProvider<Transaction = TransactionRequest> + Send + Sync> AccountingContractApi
52-
for AccountingContractClient<P>
81+
impl<P: RpcProvider<Transaction = TransactionRequest> + Send + Sync>
82+
AccountingContractApi<Address, U256> for AccountingContractClient<P>
5383
{
5484
async fn execute_pay_out_request(
5585
&self,
@@ -120,7 +150,7 @@ pub mod mocks {
120150
pub AccountingContractClient {}
121151

122152
#[async_trait]
123-
impl AccountingContractApi for AccountingContractClient {
153+
impl AccountingContractApi<Address, U256> for AccountingContractClient {
124154

125155
async fn execute_pay_out_request(
126156
&self,

tee-worker/omni-executor/accounting-contract-client/src/solana.rs

Lines changed: 52 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::str::FromStr;
2-
use std::sync::Arc;
32

3+
use crate::AccountingContractApi;
44
use alloy::primitives::U256;
55
use anchor_client::{
66
solana_sdk::{
@@ -14,22 +14,9 @@ use anchor_client::{
1414
};
1515
use async_trait::async_trait;
1616
use sp_core::ed25519;
17+
use std::sync::Arc;
1718
use tracing::{error, warn};
1819

19-
#[async_trait]
20-
pub trait AccountingContractApi: Send + Sync {
21-
async fn execute_pay_out_request(
22-
&self,
23-
beneficiary: Pubkey,
24-
nonce: u64,
25-
amount: U256,
26-
) -> Result<(), ()>;
27-
28-
async fn get_nonce(&self, user: Pubkey) -> Result<u64, ()>;
29-
30-
async fn get_balance(&self) -> Result<U256, ()>;
31-
}
32-
3320
pub struct AccountingContractClient {
3421
pub payer: Keypair,
3522
pub solana_url: String,
@@ -58,65 +45,48 @@ impl AccountingContractClient {
5845
}
5946

6047
#[async_trait]
61-
impl AccountingContractApi for AccountingContractClient {
48+
impl AccountingContractApi<Pubkey, u64> for AccountingContractClient {
6249
async fn execute_pay_out_request(
6350
&self,
6451
beneficiary: Pubkey,
6552
nonce: u64,
6653
amount: U256,
6754
) -> Result<(), ()> {
6855
let client = self.create_client()?;
69-
let program = client.program(self.program_id).map_err(|e| {
70-
error!("Failed to create program client: {:?}", e);
71-
})?;
7256

73-
let result = tokio::task::spawn_blocking(move || {
74-
program
75-
.request()
76-
.accounts(accounting_contract::accounts::CreatePayRequest {
77-
pay_out_request: Pubkey::find_program_address(
78-
&[beneficiary.to_bytes().as_ref(), &nonce.to_le_bytes(), b"payout_request"],
79-
&program.id(),
80-
)
81-
.0,
82-
account_nonce: Pubkey::find_program_address(
83-
&[beneficiary.to_bytes().as_ref(), b"nonce"],
84-
&program.id(),
85-
)
86-
.0,
87-
signer: program.payer(),
88-
worker: Pubkey::find_program_address(&[b"worker"], &program.id()).0,
89-
treasury: Pubkey::find_program_address(&[b"treasury"], &program.id()).0,
90-
beneficiary,
91-
system_program: system_program::ID,
92-
})
93-
.args(accounting_contract::instruction::CreatePayRequest {
94-
amount: amount.try_into().map_err(|_| {
95-
error!("Failed to convert U256 to u64");
96-
anchor_client::ClientError::from(std::io::Error::new(
97-
std::io::ErrorKind::InvalidData,
98-
"Failed to convert U256 to u64",
99-
))
100-
})?,
101-
nonce,
102-
})
103-
.send()
104-
})
105-
.await;
106-
107-
match result {
108-
Ok(anchor_result) => match anchor_result {
109-
Ok(_) => Ok(()),
110-
Err(e) => {
111-
error!("Anchor client error: {:?}", e);
112-
Err(())
113-
},
114-
},
115-
Err(e) => {
57+
let program = client.program(self.program_id).expect("Failed to create program client");
58+
59+
program
60+
.request()
61+
.accounts(accounting_contract::accounts::CreatePayRequest {
62+
pay_out_request: Pubkey::find_program_address(
63+
&[beneficiary.to_bytes().as_ref(), &nonce.to_le_bytes(), b"payout_request"],
64+
&program.id(),
65+
)
66+
.0,
67+
account_nonce: Pubkey::find_program_address(
68+
&[beneficiary.to_bytes().as_ref(), b"nonce"],
69+
&program.id(),
70+
)
71+
.0,
72+
signer: self.payer.pubkey(),
73+
worker: Pubkey::find_program_address(&[b"worker"], &program.id()).0,
74+
treasury: Pubkey::find_program_address(&[b"treasury"], &program.id()).0,
75+
beneficiary,
76+
system_program: system_program::ID,
77+
})
78+
.args(accounting_contract::instruction::CreatePayRequest {
79+
amount: amount.try_into().map_err(|_| {
80+
error!("Failed to convert U256 amount to u64");
81+
})?,
82+
nonce,
83+
})
84+
.send()
85+
.await
86+
.map_err(|e| {
11687
error!("Failed to execute pay out request: {:?}", e);
117-
Err(())
118-
},
119-
}
88+
})?;
89+
Ok(())
12090
}
12191

12292
async fn get_nonce(&self, user: Pubkey) -> Result<u64, ()> {
@@ -129,46 +99,29 @@ impl AccountingContractApi for AccountingContractClient {
12999
let (account_pubkey, _bump) =
130100
Pubkey::find_program_address(&[user.to_bytes().as_ref(), b"nonce"], &self.program_id);
131101

132-
match tokio::task::spawn_blocking(move || program.rpc().get_account(&account_pubkey)).await
133-
{
102+
match program.rpc().get_account(&account_pubkey).await {
134103
// return default nonce 0 when fail to get nonce
135-
Ok(result) => match result {
136-
Ok(account) => {
137-
match bincode::deserialize::<accounting_contract::Nonce>(&account.data[8..]) {
138-
Ok(nonce) => Ok(nonce.nonce),
139-
Err(e) => {
140-
error!("Failed deserialize nonce from account {:?}: {:?}", account, e);
141-
Ok(0)
142-
},
143-
}
144-
},
145-
Err(e) => {
146-
error!("Failed to get_account {:?}", e);
147-
Ok(0)
148-
},
104+
Ok(account) => {
105+
match bincode::deserialize::<accounting_contract::Nonce>(&account.data[8..]) {
106+
Ok(nonce) => Ok(nonce.nonce),
107+
Err(e) => {
108+
error!("Failed deserialize nonce from account {:?}: {:?}", account, e);
109+
Ok(0)
110+
},
111+
}
149112
},
150113
Err(e) => {
151-
error!("Failed to spawn blocking task: {:?}", e);
152-
Err(())
114+
error!("Failed to get_account {:?}", e);
115+
Ok(0)
153116
},
154117
}
155118
}
156119

157120
async fn get_balance(&self) -> Result<U256, ()> {
158121
let client = self.create_client()?;
159-
let program = client.program(self.program_id).map_err(|e| {
160-
error!("Failed to create program client: {:?}", e);
161-
})?;
162122

163-
let payer_pubkey = self.payer.pubkey();
164-
let balance_result =
165-
tokio::task::spawn_blocking(move || program.rpc().get_balance(&payer_pubkey))
166-
.await
167-
.map_err(|e| {
168-
error!("Failed to spawn blocking task: {:?}", e);
169-
})?;
170-
171-
match balance_result {
123+
let program = client.program(self.program_id).expect("Failed to create program client");
124+
match program.rpc().get_balance(&self.payer.pubkey()).await {
172125
Ok(v) => {
173126
let balance = U256::try_from(v).map_err(|err| {
174127
warn!("Failed to convert balance to U256: {:?}", err);
@@ -185,17 +138,19 @@ impl AccountingContractApi for AccountingContractClient {
185138

186139
#[cfg(feature = "mocks")]
187140
pub mod mocks {
188-
use crate::solana::AccountingContractApi;
141+
use crate::AccountingContractApi;
189142
use crate::U256;
190143
use anchor_client::solana_sdk::pubkey::Pubkey;
191144
use async_trait::async_trait;
192145
use mockall::mock;
193146

194147
mock! {
148+
195149
pub AccountingContractClient {}
196150

197151
#[async_trait]
198-
impl AccountingContractApi for AccountingContractClient {
152+
impl AccountingContractApi<Pubkey, u64> for AccountingContractClient {
153+
199154
async fn execute_pay_out_request(
200155
&self,
201156
beneficiary: Pubkey,

tee-worker/omni-executor/ethereum-rpc/src/signer.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use tracing::error;
77

88
/// A remote signer implementation for EVM-compatible chains
99
/// that delegates signing operations to a remote signing service
10+
#[derive(Clone)]
1011
pub struct RemoteSigner {
1112
/// The client used to communicate with the remote signing service
1213
signer_client: Arc<Box<dyn SignerClient>>,

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

Lines changed: 7 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,7 @@ use ::pumpx::methods::cross_fail::CrossFailBody;
2626
use ::pumpx::methods::send_order_tx::SendOrderTxBody;
2727
use ::pumpx::signer_client::PumpxChainId;
2828
use aa_contracts_client::calculate_omni_account_address;
29-
use accounting_contract_client::{
30-
solana::AccountingContractApi as SolanaAccountingContractApi,
31-
AccountingContractApi as EvmAccountingContractApi,
32-
};
29+
use accounting_contract_client::AccountingContractApi;
3330
use alloy::consensus::{SignableTransaction, TxLegacy};
3431
use alloy::network::TxSigner as AlloyTxSigner;
3532
use alloy::primitives::private::alloy_rlp::Decodable;
@@ -71,7 +68,6 @@ use rust_decimal::Decimal;
7168
use signer_client::{ChainType, SignerClient};
7269
use solana::{signer::RemoteSigner as RemoteSolanaSigner, SolanaClient as SolanaClientTrait};
7370
use solana_sdk::pubkey::Pubkey;
74-
use std::str::FromStr;
7571
use std::sync::Arc;
7672
use tokio::{
7773
runtime::Handle,
@@ -113,8 +109,8 @@ pub struct CrossChainIntentExecutor<
113109
binance_api: Arc<BinanceClient>,
114110
bsc_client: Arc<EthereumClient>,
115111
solana_client: Arc<SolanaClient>,
116-
evm_accounting_contract_client: Arc<Box<dyn EvmAccountingContractApi>>,
117-
solana_accounting_contract_client: Arc<Box<dyn SolanaAccountingContractApi>>,
112+
evm_accounting_contract_client: Arc<Box<dyn AccountingContractApi<Address, U256>>>,
113+
solana_accounting_contract_client: Arc<Box<dyn AccountingContractApi<Pubkey, u64>>>,
118114
instant_payout_threshold: Decimal,
119115
// AA contracts configuration
120116
omni_account_factory_address: Address,
@@ -137,8 +133,8 @@ impl<
137133
binance_api: Arc<BinanceClient>,
138134
bsc_client: Arc<EthereumClient>,
139135
solana_client: Arc<SolanaClient>,
140-
evm_accounting_contract_client: Arc<Box<dyn EvmAccountingContractApi>>,
141-
solana_accounting_contract_client: Arc<Box<dyn SolanaAccountingContractApi>>,
136+
evm_accounting_contract_client: Arc<Box<dyn AccountingContractApi<Address, U256>>>,
137+
solana_accounting_contract_client: Arc<Box<dyn AccountingContractApi<Pubkey, u64>>>,
142138
instant_payout_threshold: Decimal,
143139
omni_account_factory_address: Address,
144140
omni_account_implementation_address: Address,
@@ -486,8 +482,8 @@ pub struct InstantFlowDetails {
486482

487483
impl<
488484
BinanceClient: BinanceApi,
489-
EthereumClient: EthereumClientTrait,
490-
SolanaClient: SolanaClientTrait,
485+
EthereumClient: EthereumClientTrait + 'static,
486+
SolanaClient: SolanaClientTrait + 'static,
491487
> CrossChainIntentExecutor<BinanceClient, EthereumClient, SolanaClient>
492488
{
493489
async fn do_omni_binance_deposit(
@@ -685,23 +681,6 @@ impl<
685681
};
686682
Ok((payout_amount, payout_amount_u256))
687683
}
688-
689-
fn calculate_amount_decimal(
690-
from_amount: Decimal,
691-
binance_coin_name: &str,
692-
) -> Result<Decimal, ()> {
693-
let asset_decimal_multiplier = match binance_coin_name {
694-
"USDC" => Decimal::from(1_000_000), // 10^6
695-
"USDT" => Decimal::from(1_000_000), // 10^6
696-
"SOL" => Decimal::from(1_000_000_000), // 10^9 TODO: double check this
697-
"BNB" => Decimal::from_str("1_000_000_000_000_000_000").unwrap(), // 10^18
698-
_ => {
699-
error!("Unsupported asset: {:?}", binance_coin_name);
700-
return Err(());
701-
},
702-
};
703-
Ok(from_amount * asset_decimal_multiplier)
704-
}
705684
}
706685

707686
fn decimal_to_u256(decimal: Decimal) -> Result<U256, ParseError> {

0 commit comments

Comments
 (0)