@drift-labs/sdk 2.68.0-beta.0 → 2.70.0-beta.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,77 +0,0 @@
1
- import { Connection, PublicKey } from '@solana/web3.js';
2
- import { PRICE_PRECISION, TEN } from '../constants/numericConstants';
3
- import { OracleClient, OraclePriceData } from './types';
4
- import switchboardV2Idl from '../idl/switchboard.json';
5
- import { BorshAccountsCoder, BN, Idl } from '@coral-xyz/anchor';
6
-
7
- type SwitchboardDecimal = {
8
- scale: number;
9
- mantissa: BN;
10
- };
11
-
12
- type AggregatorAccountData = {
13
- latestConfirmedRound: {
14
- result: SwitchboardDecimal;
15
- stdDeviation: SwitchboardDecimal;
16
- numSuccess: number;
17
- roundOpenSlot: BN;
18
- };
19
- minOracleResults: number;
20
- };
21
-
22
- export class SwitchboardClient implements OracleClient {
23
- connection: Connection;
24
- coder: BorshAccountsCoder;
25
-
26
- public constructor(connection: Connection) {
27
- this.connection = connection;
28
- this.coder = new BorshAccountsCoder(switchboardV2Idl as Idl);
29
- }
30
-
31
- public async getOraclePriceData(
32
- pricePublicKey: PublicKey
33
- ): Promise<OraclePriceData> {
34
- const accountInfo = await this.connection.getAccountInfo(pricePublicKey);
35
- return this.getOraclePriceDataFromBuffer(accountInfo.data);
36
- }
37
-
38
- public getOraclePriceDataFromBuffer(buffer: Buffer): OraclePriceData {
39
- const aggregatorAccountData = this.coder.decodeUnchecked(
40
- 'AggregatorAccountData',
41
- buffer
42
- ) as AggregatorAccountData;
43
-
44
- const price = convertSwitchboardDecimal(
45
- aggregatorAccountData.latestConfirmedRound.result
46
- );
47
-
48
- const confidence = BN.max(
49
- convertSwitchboardDecimal(
50
- aggregatorAccountData.latestConfirmedRound.stdDeviation
51
- ),
52
- price.divn(1000)
53
- );
54
-
55
- const hasSufficientNumberOfDataPoints =
56
- aggregatorAccountData.latestConfirmedRound.numSuccess >=
57
- aggregatorAccountData.minOracleResults;
58
-
59
- const slot: BN = aggregatorAccountData.latestConfirmedRound.roundOpenSlot;
60
- return {
61
- price,
62
- slot,
63
- confidence,
64
- hasSufficientNumberOfDataPoints,
65
- };
66
- }
67
- }
68
-
69
- function convertSwitchboardDecimal(switchboardDecimal: {
70
- scale: number;
71
- mantissa: BN;
72
- }): BN {
73
- const switchboardPrecision = TEN.pow(new BN(switchboardDecimal.scale));
74
- return switchboardDecimal.mantissa
75
- .mul(PRICE_PRECISION)
76
- .div(switchboardPrecision);
77
- }