Ellipsis-Labs / phoenix-sdk

SDK for interacting with the Phoenix DEX.
83 stars 39 forks source link

Introduces a SDK function for simulating comprehensive Market Buy and Sell operations #135

Closed PirosB3 closed 6 months ago

PirosB3 commented 1 year ago

In this pull request, I've added a simulate_market_transaction function that allows for simulating market buys/sells off-chain (without using simulateTransaction)

Key Changes:

🔐 Security-wise: all functions are non &mut, and there are no code deletions - only additions.

Testing Considerations:

How to review

Suggested way to review

E2E tests using Phoenix CLI:

I did some E2E tests by creating a dummy function on phoenix-cli which calls simulate_market_transaction on an assortment of prices and directions. IN specifies what we are selling and OUT is the outcome of the swap

SOL/USD book
--------------------------------------------

[IN] Token: So11111111111111111111111111111111111111112
[IN] Amount size: 200 SOL
[IN] Amount atoms: 200000000000
[OUT] Base 200 (200000000000 atoms)
[OUT] Quote 4000.840321 (4000840321 atoms)
[OUT] Price: 20.004201605000002
--------------------------------------------
[IN] Token: So11111111111111111111111111111111111111112
[IN] Amount size: 500  SOL
[IN] Amount atoms: 500000000000
[OUT] Base 500 (500000000000 atoms)
[OUT] Quote 10001.170765 (10001170765 atoms)
[OUT] Price: 20.002341530000002
--------------------------------------------
[IN] Token: So11111111111111111111111111111111111111112
[IN] Amount size: 10000  SOL
[IN] Amount atoms: 10000000000000
[OUT] Base 6342.761 (6342761000000 atoms)  👈 Book exhausted
[OUT] Quote 68833.945844 (68833945844 atoms)
[OUT] Price: 10.852363165504737

--------------------------------------------
[IN] Token: EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v
[IN] Amount size: 200 USDC
[IN] Amount atoms: 200000000
[OUT] Base 9.988 (9988000000 atoms)
[OUT] Quote 199.95976 (199959760 atoms)
[OUT] Price: 20.02
--------------------------------------------
[IN] Token: EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v
[IN] Amount size: 500 USDC
[IN] Amount atoms: 500000000
[OUT] Base 24.967 (24967000000 atoms)
[OUT] Quote 499.880829 (499880829 atoms)
[OUT] Price: 20.02166175351464
--------------------------------------------
[IN] Token: EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v
[IN] Amount size: 10000 USDC
[IN] Amount atoms: 10000000000
[OUT] Base 499.258 (499258000000 atoms)
[OUT] Quote 9997.999507 (9997999507 atoms)
[OUT] Price: 20.025717178292588

Appreciate your feedback! and let me know what you think

PirosB3 commented 11 months ago

Hey @jarry-xiao by any chance did you get a chance to take a look at this? 😀