Skip to content

Commit

Permalink
refactor: change raydium provider to query committed blocks instead o…
Browse files Browse the repository at this point in the history
…f finalized (#648)
  • Loading branch information
technicallyty committed Aug 6, 2024
1 parent a191f41 commit ef067ce
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 6 deletions.
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
8 changes: 4 additions & 4 deletions providers/apis/defi/raydium/price_fetcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ func TestProviderFetch(t *testing.T) {
btcVaultPk, usdcVaultPk, usdcBtcAMMIDPk, usdcBtcOpenOrdersPk,
ethVaultPk, usdtVaultPk, ethUsdtAMMIDPk, ETHUSDTOpenOrdersPk,
}, &rpc.GetMultipleAccountsOpts{
Commitment: rpc.CommitmentFinalized,
Commitment: rpc.CommitmentConfirmed,
}).Return(
&rpc.GetMultipleAccountsResult{}, nil,
).Once()
Expand Down Expand Up @@ -323,7 +323,7 @@ func TestProviderFetch(t *testing.T) {
btcVaultPk, usdcVaultPk, usdcBtcAMMIDPk, usdcBtcOpenOrdersPk,
ethVaultPk, usdtVaultPk, ethUsdtAMMIDPk, ETHUSDTOpenOrdersPk,
}, &rpc.GetMultipleAccountsOpts{
Commitment: rpc.CommitmentFinalized,
Commitment: rpc.CommitmentConfirmed,
}).Return(
&rpc.GetMultipleAccountsResult{}, err,
).Once()
Expand Down Expand Up @@ -421,7 +421,7 @@ func TestProviderFetch(t *testing.T) {
ethVaultPk, usdtVaultPk, ethUsdtAMMIDPk, ETHUSDTOpenOrdersPk,
mogVaultPk, solVaultPk, mogSolAMMIDPk, MOGSOLOpenOrdersPk,
}, &rpc.GetMultipleAccountsOpts{
Commitment: rpc.CommitmentFinalized,
Commitment: rpc.CommitmentConfirmed,
}).Return(
&rpc.GetMultipleAccountsResult{
Value: []*rpc.Account{
Expand Down Expand Up @@ -478,7 +478,7 @@ func TestProviderFetch(t *testing.T) {
client.On("GetMultipleAccountsWithOpts", mock.Anything, []solana.PublicKey{
btcVaultPk, usdcVaultPk, usdcBtcAMMIDPk, usdcBtcOpenOrdersPk,
}, &rpc.GetMultipleAccountsOpts{
Commitment: rpc.CommitmentFinalized,
Commitment: rpc.CommitmentConfirmed,
}).Return(
&rpc.GetMultipleAccountsResult{
Value: []*rpc.Account{
Expand Down

0 comments on commit ef067ce

Please sign in to comment.