@drift-labs/sdk 2.71.0-beta.3 → 2.71.0-beta.4
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.
- package/VERSION +1 -1
- package/lib/factory/oracleClient.js +4 -3
- package/lib/idl/switchboard.json +8354 -0
- package/lib/oracles/switchboardClient.d.ts +11 -0
- package/lib/oracles/switchboardClient.js +40 -0
- package/lib/types.d.ts +3 -0
- package/lib/types.js +1 -1
- package/package.json +1 -1
- package/src/factory/oracleClient.ts +4 -3
- package/src/idl/switchboard.json +8354 -0
- package/src/oracles/switchboardClient.ts +77 -0
- package/src/types.ts +1 -1
|
@@ -0,0 +1,77 @@
|
|
|
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
|
+
}
|
package/src/types.ts
CHANGED
|
@@ -93,7 +93,7 @@ export class OracleSource {
|
|
|
93
93
|
static readonly PYTH = { pyth: {} };
|
|
94
94
|
static readonly PYTH_1K = { pyth1K: {} };
|
|
95
95
|
static readonly PYTH_1M = { pyth1M: {} };
|
|
96
|
-
|
|
96
|
+
static readonly SWITCHBOARD = { switchboard: {} };
|
|
97
97
|
static readonly QUOTE_ASSET = { quoteAsset: {} };
|
|
98
98
|
static readonly PYTH_STABLE_COIN = { pythStableCoin: {} };
|
|
99
99
|
static readonly Prelaunch = { prelaunch: {} };
|