@drift-labs/sdk 2.10.0-beta.5 → 2.11.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.
- package/lib/factory/oracleClient.js +4 -4
- package/lib/idl/drift.json +6 -1
- package/lib/index.d.ts +0 -1
- package/lib/index.js +1 -1
- package/lib/types.d.ts +0 -3
- package/lib/types.js +1 -1
- package/lib/user.js +3 -0
- package/package.json +1 -2
- package/src/factory/oracleClient.ts +4 -4
- package/src/idl/drift.json +6 -1
- package/src/index.ts +1 -1
- package/src/types.ts +1 -1
- package/src/user.ts +4 -0
- package/lib/idl/switchboard_v2.json +0 -4663
- package/lib/oracles/switchboardClient.d.ts +0 -11
- package/lib/oracles/switchboardClient.js +0 -58
- package/src/idl/switchboard_v2.json +0 -4663
- package/src/oracles/switchboardClient.ts +0 -79
|
@@ -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
|
-
}
|