Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: return a quote when not enough funds #418

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
176 changes: 176 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions auction-server/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ strum.workspace = true
spl-associated-token-account = { workspace = true }
spl-token = { workspace = true }
mockall_double = "0.3.1"
spl-token-2022 = "5.0.0"

[dev-dependencies]
mockall = "0.13.1"
26 changes: 23 additions & 3 deletions auction-server/src/auction/service/verification.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,19 +70,26 @@ use {
U256,
},
},
express_relay::error::ErrorCode,
litesvm::types::FailedTransactionMetadata,
solana_sdk::{
address_lookup_table::state::AddressLookupTable,
clock::Slot,
commitment_config::CommitmentConfig,
compute_budget,
instruction::CompiledInstruction,
instruction::{
CompiledInstruction,
InstructionError,
},
pubkey::Pubkey,
signature::Signature,
signer::Signer as _,
system_instruction::SystemInstruction,
system_program,
transaction::VersionedTransaction,
transaction::{
TransactionError,
VersionedTransaction,
},
},
spl_associated_token_account::{
get_associated_token_address,
Expand Down Expand Up @@ -1245,7 +1252,19 @@ impl Service<Svm> {
.await;
match simulation {
Ok(simulation) => {
if simulation.value.err.is_some() {
if let Some(transaction_error) = simulation.value.err {
if let TransactionError::InstructionError(_, instruction_error) =
transaction_error
{
if instruction_error
== InstructionError::Custom(ErrorCode::InsufficientUserFunds.into())
{
// This path only works as long as none of the other accepted programs use this error number, which is currently true since express relay is the only program in the whitelist that uses anchor error codes
// TODO: Also check the instruction index here so we are sure it's coming from the swap instruction
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wouldn't this TODO resolve the above comment? since ER doesn't make any CPIs to anything except system & token program?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

YES! let me clarify it

return Ok(());
}
}

let msgs = simulation.value.logs.unwrap_or_default();
Err(RestError::SimulationError {
result: Default::default(),
Expand All @@ -1255,6 +1274,7 @@ impl Service<Svm> {
Ok(())
}
}

Err(e) => {
tracing::error!("Error while simulating swap bid: {:?}", e);
Err(RestError::TemporarilyUnavailable)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
use {
super::{
get_token_program::GetTokenProgramInput,
ChainTypeSvm,
Service,
},
crate::{
api::RestError,
kernel::entities::ChainId,
},
solana_sdk::{
commitment_config::CommitmentConfig,
pubkey::Pubkey,
},
spl_associated_token_account::get_associated_token_address_with_program_id,
spl_token_2022::{
extension::StateWithExtensions as TokenAccountWithExtensions,
state::Account as TokenAccount,
},
};

pub struct CheckUserTokenBalanceInput {
pub chain_id: ChainId,
pub user: Pubkey,
pub mint_user: Pubkey,
pub amount_user: u64,
}

impl Service<ChainTypeSvm> {
pub async fn check_user_token_balance(
&self,
input: CheckUserTokenBalanceInput,
) -> Result<bool, RestError> {
let config = self.get_config(&input.chain_id)?;

let native_amount_user = if input.mint_user == spl_token::native_mint::id() {
config
.rpc_client
.get_account_with_commitment(&input.user, CommitmentConfig::processed())
.await
.map_err(|err| {
tracing::error!(error = ?err, "Failed to get user wallet");
RestError::TemporarilyUnavailable
})?
.value
.map(|account| account.lamports)
.unwrap_or_default()
} else {
0
};

let user_ata_mint_user = get_associated_token_address_with_program_id(
&input.user,
&input.mint_user,
&self
.get_token_program(GetTokenProgramInput {
chain_id: input.chain_id.clone(),
mint: input.mint_user,
})
.await?,
);
let amount_user: u64 = config
.rpc_client
.get_account_with_commitment(&user_ata_mint_user, CommitmentConfig::processed())
.await
.map_err(|err| {
tracing::error!(error = ?err, "Failed to get user token account");
RestError::TemporarilyUnavailable
})?
.value
.map(|account| {
TokenAccountWithExtensions::<TokenAccount>::unpack(account.data.as_slice())
.map_err(|err| {
tracing::error!(error = ?err, "Failed to deserialize user token account");
RestError::TemporarilyUnavailable
})
.map(|token_account_with_extensions| token_account_with_extensions.base.amount)
})
.transpose()?
.unwrap_or_default();

Ok(input.amount_user <= amount_user.saturating_add(native_amount_user))
}
}
Loading
Loading