Skip to content

Commit 228b10a

Browse files
authored
Fix ccsp (#3567)
* feat: add bsc token FC for ccsp * fix: fix wrong logic of get_nonce in solana cotract client * fix: fix wrong logic of get_nonce in solana cotract client and add trade_symbol for token FC --------- Signed-off-by: wli <120463031+wli-pro@users.noreply.github.com> Co-authored-by: wli-pro <wli-pro>
1 parent 63e5741 commit 228b10a

File tree

5 files changed

+37
-47
lines changed

5 files changed

+37
-47
lines changed

tee-worker/omni-executor/Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ accounting-contract = { workspace = true }
88
alloy = { workspace = true, features = ["contract", "signer-local", "rpc", "rpc-types", "eips"] }
99
anchor-client = { workspace = true }
1010
async-trait = { workspace = true }
11+
bincode = { workspace = true }
1112
ethereum-rpc = { workspace = true }
1213
mockall = { workspace = true }
1314
sp-core = { workspace = true }

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

Lines changed: 26 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ use std::sync::Arc;
33

44
use alloy::primitives::U256;
55
use anchor_client::{
6-
anchor_lang::AccountDeserialize,
76
solana_sdk::{
87
commitment_config::CommitmentConfig,
98
pubkey::Pubkey,
@@ -15,34 +14,7 @@ use anchor_client::{
1514
};
1615
use async_trait::async_trait;
1716
use sp_core::ed25519;
18-
use tracing::{error, info, warn};
19-
20-
#[derive(Debug)]
21-
pub struct NonceAccount {
22-
pub nonce: u64,
23-
}
24-
25-
impl AccountDeserialize for NonceAccount {
26-
fn try_deserialize_unchecked(buf: &mut &[u8]) -> anchor_client::anchor_lang::Result<Self> {
27-
if buf.len() < 8 {
28-
return Ok(NonceAccount { nonce: 0 });
29-
}
30-
31-
let data = &buf[8..];
32-
33-
if data.len() < 8 {
34-
return Ok(NonceAccount { nonce: 0 });
35-
}
36-
37-
let nonce = u64::from_le_bytes(data[..8].try_into().map_err(|_| {
38-
anchor_client::anchor_lang::error::Error::from(
39-
anchor_client::anchor_lang::error::ErrorCode::AccountDidNotDeserialize,
40-
)
41-
})?);
42-
43-
Ok(NonceAccount { nonce })
44-
}
45-
}
17+
use tracing::{error, warn};
4618

4719
#[async_trait]
4820
pub trait AccountingContractApi: Send + Sync {
@@ -150,28 +122,36 @@ impl AccountingContractApi for AccountingContractClient {
150122
async fn get_nonce(&self, user: Pubkey) -> Result<u64, ()> {
151123
let client = self.create_client()?;
152124

153-
let (account_nonce, _bump) =
154-
Pubkey::find_program_address(&[user.to_bytes().as_ref(), b"nonce"], &self.program_id);
155-
156125
let program = client.program(self.program_id).map_err(|e| {
157126
error!("Failed to create program client: {:?}", e);
158127
})?;
159128

160-
let nonce_account: NonceAccount =
161-
tokio::task::spawn_blocking(move || program.account(account_nonce))
162-
.await
163-
.map_err(|e| {
164-
error!("Failed to spawn blocking task: {:?}", e);
165-
})?
166-
.map_err(|e| {
167-
if e.to_string().contains("AccountNotFound") {
168-
info!("Nonce account {} not found, returning 0", account_nonce);
169-
} else {
170-
error!("Failed to get nonce account {}: {:?}", account_nonce, e);
171-
}
172-
})?;
129+
let (account_pubkey, _bump) =
130+
Pubkey::find_program_address(&[user.to_bytes().as_ref(), b"nonce"], &self.program_id);
173131

174-
Ok(nonce_account.nonce)
132+
match tokio::task::spawn_blocking(move || program.rpc().get_account(&account_pubkey)).await
133+
{
134+
// 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+
},
149+
},
150+
Err(e) => {
151+
error!("Failed to spawn blocking task: {:?}", e);
152+
Err(())
153+
},
154+
}
175155
}
176156

177157
async fn get_balance(&self) -> Result<U256, ()> {

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -878,6 +878,9 @@ pub(crate) fn determine_trade_symbol_and_order_side(
878878
to_network: BinanceNetwork,
879879
) -> Result<(String, BinanceOrderSide), ()> {
880880
match (binance_network.clone(), binance_coin.clone(), to_network.clone()) {
881+
(BinanceNetwork::Sol, BinanceCoin::Fc, BinanceNetwork::Bsc) => {
882+
Ok(("BNBFC".to_string(), BinanceOrderSide::BUY))
883+
},
881884
(BinanceNetwork::Sol, BinanceCoin::Usdc, BinanceNetwork::Bsc) => {
882885
Ok(("BNBUSDC".to_string(), BinanceOrderSide::BUY))
883886
},

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ impl BinanceNetwork {
4242
pub enum BinanceCoin {
4343
Bnb,
4444
Sol,
45+
Fc,
4546
Usdc,
4647
Usdt,
4748
}
@@ -50,21 +51,23 @@ impl BinanceCoin {
5051
// Biannce coin names
5152
const BNB: &'static str = "BNB";
5253
const SOL: &'static str = "SOL";
54+
const FC: &'static str = "FC";
5355
const USDC: &'static str = "USDC";
5456
const USDT: &'static str = "USDT";
5557

5658
pub fn name(&self) -> &'static str {
5759
match self {
5860
Self::Bnb => Self::BNB,
5961
Self::Sol => Self::SOL,
62+
Self::Fc => Self::FC,
6063
Self::Usdc => Self::USDC,
6164
Self::Usdt => Self::USDT,
6265
}
6366
}
6467

6568
pub fn decimals(&self) -> u32 {
6669
match self {
67-
BinanceCoin::Bnb => 18,
70+
BinanceCoin::Fc | BinanceCoin::Bnb => 18,
6871
BinanceCoin::Sol => 9,
6972
BinanceCoin::Usdc | BinanceCoin::Usdt => 6,
7073
}
@@ -77,6 +80,7 @@ impl BinanceCoin {
7780

7881
pub trait FromBscTokenAddress {
7982
// Bsc token addresss
83+
const BSC_TOKEN_ADDRESS_FC: &'static str = "4C3617A56a5E0Ca1D8CDd629695b0a8BE669971A";
8084
const BSC_TOKEN_ADDRESS_USDC: &'static str = "8AC76a51cc950d9822D68b83fE1Ad97B32Cd580d";
8185
const BSC_TOKEN_ADDRESS_USDT: &'static str = "55d398326f99059fF775485246999027B3197955";
8286

@@ -86,6 +90,7 @@ pub trait FromBscTokenAddress {
8690
impl FromBscTokenAddress for BinanceCoin {
8791
fn from_bsc_token_address(address: &str) -> Result<BinanceCoin, ()> {
8892
match address {
93+
Self::BSC_TOKEN_ADDRESS_FC => Ok(Self::Fc),
8994
Self::BSC_TOKEN_ADDRESS_USDC => Ok(Self::Usdc),
9095
Self::BSC_TOKEN_ADDRESS_USDT => Ok(Self::Usdt),
9196
_ => {

0 commit comments

Comments
 (0)