Skip to content

Commit

Permalink
Add threshold for confidence that sets status to unknown (#8)
Browse files Browse the repository at this point in the history
* add threshold for confidence that sets status to unknown

* fix build

Co-authored-by: Jayant Krishnamurthy <[email protected]>
  • Loading branch information
jayantk and Jayant Krishnamurthy authored Jan 28, 2022
1 parent 7cfadd9 commit 1bbb321
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 7 deletions.
25 changes: 18 additions & 7 deletions program/src/serum-pyth/serum-pyth.c
Original file line number Diff line number Diff line change
Expand Up @@ -243,10 +243,9 @@ static inline sp_errcode_t sp_get_pyth_instruction(
}

// Convert Serum prices into Pyth formatted prices
sp_size_t pyth_bid = 0;
sp_size_t pyth_ask = 0;
int64_t pyth_price = 0;
uint64_t pyth_conf = 0;
if ( SP_LIKELY( trading ) ) {

const sp_size_t serum_to_pyth = sp_serum_to_pyth(
pyth_exponent,
quote_exponent,
Expand All @@ -259,8 +258,20 @@ static inline sp_errcode_t sp_get_pyth_instruction(
return ERROR_INVALID_ACCOUNT_DATA;
}

pyth_bid = serum_bid * serum_to_pyth;
pyth_ask = serum_ask * serum_to_pyth;
sp_size_t pyth_bid = serum_bid * serum_to_pyth;
sp_size_t pyth_ask = serum_ask * serum_to_pyth;
pyth_price = ( int64_t ) sp_midpt( pyth_bid, pyth_ask );
pyth_conf = sp_confidence( pyth_bid, pyth_ask );

// status will be unknown unless the spread is sufficiently tight.
int64_t threshold_conf = (pyth_price / PRICE_CONF_THRESHOLD);
if (threshold_conf < 0) {
// Safe as long as threshold_conf isn't the min int64, which it isn't as long as PRICE_CONF_THRESHOLD > 1.
threshold_conf = -threshold_conf;
}
if ( pyth_conf > (uint64_t) threshold_conf ) {
trading = false;
}
}

// Prepare pyth-client instruction for cross-program invocation.
Expand All @@ -269,8 +280,8 @@ static inline sp_errcode_t sp_get_pyth_instruction(
cmd->cmd_ = e_cmd_upd_price;
cmd->status_ = ( trading ? PC_STATUS_TRADING : PC_STATUS_UNKNOWN );
cmd->unused_ = 0;
cmd->price_ = ( int64_t ) sp_midpt( pyth_bid, pyth_ask );
cmd->conf_ = sp_confidence( pyth_bid, pyth_ask );
cmd->price_ = pyth_price;
cmd->conf_ = pyth_conf;
cmd->pub_slot_ = ( // TODO: Use direct syscall.
( const sysvar_clock_t* ) account_sysvar_clock->data
)->slot_;
Expand Down
4 changes: 4 additions & 0 deletions program/src/serum-pyth/sp-util.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ static const sp_expo_t SP_EXP_MAX = SOL_ARRAY_SIZE( SP_POW10 ) - 1;
static const sp_size_t SP_SIZE_OVERFLOW = UINT64_MAX;
static const sp_size_t SP_SIZE_MAX = SP_SIZE_OVERFLOW - 1;

// The feed's status will be unknown if conf > (price / this value).
// e.g. 20 means the confidence interval is at most 5% of the price.
static const int64_t PRICE_CONF_THRESHOLD = 20;

// Calculate 10^exp * numer / denom
static inline sp_size_t sp_pow10_divide(
sp_size_t numer,
Expand Down

0 comments on commit 1bbb321

Please sign in to comment.