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

refactor: change raydium provider to query committed blocks instead of finalized #648

Merged
Show file tree
Hide file tree
Changes from 4 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
17 changes: 17 additions & 0 deletions providers/apis/defi/raydium/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Raydium Provider

The Raydium provider fetches prices from the Raydium dex via JSON-RPC requests to Solana nodes.

## How It Works

For each ticker (i.e. RAY/SOL), we query 4 accounts:

* BaseTokenVault
* QuoteTokenVault
* AMMInfo
* OpenOrders

To calculate the price, we need to get the base and quote token balances, subtract PNL feels, and add the value of open orders.

With the above values, we calculate the price by dividing quote / base and multiplying by the scaling factor.

8 changes: 6 additions & 2 deletions providers/apis/defi/raydium/price_fetcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,11 @@ func (pf *APIPriceFetcher) Fetch(
tickers []oracletypes.ProviderTicker,
) oracletypes.PriceResponse {
// get the accounts to query in order of the tickers given
expectedNumAccounts := len(tickers) * 4
expectedNumAccounts := len(tickers) * 4 // for each ticker, we query 4 accounts (base, quote, amm, open orders)

// accounts is a contiguous slice of solana.PublicKey.
// each ticker takes up 4 slots. this is functionally equivalent to [][]solana.PubKey,
// however, storing in one slice allows us query without rearranging the request data (i.e. converting [][] to []).
accounts := make([]solana.PublicKey, expectedNumAccounts)

for i, ticker := range tickers {
Expand Down Expand Up @@ -188,7 +192,7 @@ func (pf *APIPriceFetcher) Fetch(
defer cancel()

accountsResp, err := pf.client.GetMultipleAccountsWithOpts(ctx, accounts, &rpc.GetMultipleAccountsOpts{
Commitment: rpc.CommitmentFinalized,
Commitment: rpc.CommitmentConfirmed,
// TODO(nikhil): Keep track of latest height queried as well?
})
if err != nil {
Expand Down
Loading