Skip to content

Add swap rate tolerance threshold when swapping SOL or USDC into USDCpo#31

Open
gigileungyingchi wants to merge 5 commits intomainfrom
swap-tolerance-threshold
Open

Add swap rate tolerance threshold when swapping SOL or USDC into USDCpo#31
gigileungyingchi wants to merge 5 commits intomainfrom
swap-tolerance-threshold

Conversation

@gigileungyingchi
Copy link
Copy Markdown
Contributor

No description provided.

@vercel
Copy link
Copy Markdown

vercel bot commented Dec 19, 2023

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
offset-bridge ✅ Ready (Inspect) Visit Preview 💬 Add feedback Mar 23, 2024 2:53pm

}

let input_initial_balance = ctx.remaining_accounts[0].lamports() as f64;
let output_intitial_balance = ctx.remaining_accounts[&ctx.remaining_accounts.len()-1].lamports() as f64;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

typo - too many 't's in 'initial'

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

this typo is still there (line 66 now)

pub state: Account<'info, State>,
pub jupiter_program: Program<'info, Jupiter>,
/// CHECK: the price feed account needs to correspond to the exchange pair
pub pyth_price_feed_account: AccountInfo<'info>,
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Better to use UncheckedAccount<'info> here - Anchor has special build-time checks for it.

In reality, this is fixed based on the input pair, right? Does this check happen inside the pyth program?

if ctx.accounts.pyth_price_feed_account.key() != ctx.accounts.state.pyth_price_feed_key {
return Err(ErrorCode::IncorrectPriceFeedOracleAccount.into());
}
let price_feed: PriceFeed = load_price_feed_from_account_info( &ctx.accounts.pyth_price_feed_account ).unwrap();
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

See my comment below on the pyth_price_feed_account - this is where I would assume to be passing in the currency pair.

My thinking is:
Let's say an attacker, for whatever reason, passes in SOL, that will be converted to USDC at a rate of 1:100, but passes in the pyth account for BTC, which gives a rate of 1:40,000. Obviously the jupiter accounts would have to be falsified too, but I think it is possible to pass in an unfavourable juniper route that would result in the bad rate.

Maybe I'm missing where this is checked? I.e. specifically check that the juniper in and out ccy are equal to the pyth pair.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

In Pyth you need to pass in the price feed account which are based on the pair of prices you want to check, and unfortunately one cannot just pass in the two tokens. So the way I try to make sure this is the correct pyth price feed account here is to first have it specified in the State struct when the program will be initialised. So when the swap function is called, the price feed account that is passed in will be checked against the one specified in the state. Do you think that is sufficient?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Ah I just found that "Product accounts store metadata about a product, such as its symbol (e.g., "BTC/USD")." on Pyth's page. So I can also check the account product symbol against the in and out pair. I will add this check :)


#[error_code]
pub enum ErrorCode {
#[msg("Swap rate below accepted tolerance")]
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

nitpick: do we allow incorrect rates in the other direction?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants