@drift-labs/sdk 2.10.0 → 2.11.0-beta.1

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,79 +0,0 @@
1
- import { SwitchboardDecimal } from '@switchboard-xyz/switchboard-v2';
2
- import { Connection, Keypair, PublicKey } from '@solana/web3.js';
3
- import { BN, Program, Idl, AnchorProvider } from '@project-serum/anchor';
4
- import { PRICE_PRECISION, TEN } from '../constants/numericConstants';
5
- import { OracleClient, OraclePriceData } from './types';
6
- import { Wallet } from '../wallet';
7
- import switchboardV2Idl from '../idl/switchboard_v2.json';
8
-
9
- let program: Program | undefined;
10
-
11
- export class SwitchboardClient implements OracleClient {
12
- connection: Connection;
13
-
14
- public constructor(connection: Connection) {
15
- this.connection = connection;
16
- }
17
-
18
- public async getOraclePriceData(
19
- pricePublicKey: PublicKey
20
- ): Promise<OraclePriceData> {
21
- const accountInfo = await this.connection.getAccountInfo(pricePublicKey);
22
- return this.getOraclePriceDataFromBuffer(accountInfo.data);
23
- }
24
-
25
- public getOraclePriceDataFromBuffer(buffer: Buffer): OraclePriceData {
26
- const program = this.getProgram();
27
-
28
- const aggregatorAccountData =
29
- program.account.aggregatorAccountData.coder.accounts.decode(
30
- 'AggregatorAccountData',
31
- buffer
32
- );
33
- const price = convertSwitchboardDecimal(
34
- aggregatorAccountData.latestConfirmedRound.result as SwitchboardDecimal
35
- );
36
-
37
- const confidence = convertSwitchboardDecimal(
38
- aggregatorAccountData.latestConfirmedRound
39
- .stdDeviation as SwitchboardDecimal
40
- );
41
-
42
- const hasSufficientNumberOfDataPoints =
43
- aggregatorAccountData.latestConfirmedRound.numSuccess >=
44
- aggregatorAccountData.minOracleResults;
45
-
46
- const slot: BN = aggregatorAccountData.latestConfirmedRound.roundOpenSlot;
47
- return {
48
- price,
49
- slot,
50
- confidence,
51
- hasSufficientNumberOfDataPoints,
52
- };
53
- }
54
-
55
- public getProgram(): Program {
56
- if (program) {
57
- return program;
58
- }
59
-
60
- program = getSwitchboardProgram(this.connection);
61
- return program;
62
- }
63
- }
64
-
65
- function getSwitchboardProgram(connection: Connection): Program {
66
- const DEFAULT_KEYPAIR = Keypair.fromSeed(new Uint8Array(32).fill(1));
67
- const programId = PublicKey.default;
68
- const wallet = new Wallet(DEFAULT_KEYPAIR);
69
- const provider = new AnchorProvider(connection, wallet, {});
70
-
71
- return new Program(switchboardV2Idl as Idl, programId, provider);
72
- }
73
-
74
- function convertSwitchboardDecimal(switchboardDecimal: SwitchboardDecimal): BN {
75
- const switchboardPrecision = TEN.pow(new BN(switchboardDecimal.scale));
76
- return switchboardDecimal.mantissa
77
- .mul(PRICE_PRECISION)
78
- .div(switchboardPrecision);
79
- }