@drift-labs/sdk 0.1.11 → 0.1.15
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/README.md +2 -2
- package/lib/accounts/defaultClearingHouseAccountSubscriber.d.ts +1 -0
- package/lib/accounts/defaultClearingHouseAccountSubscriber.d.ts.map +1 -1
- package/lib/accounts/defaultClearingHouseAccountSubscriber.js +17 -0
- package/lib/accounts/defaultUserAccountSubscriber.d.ts +2 -0
- package/lib/accounts/defaultUserAccountSubscriber.d.ts.map +1 -1
- package/lib/accounts/defaultUserAccountSubscriber.js +16 -0
- package/lib/accounts/types.d.ts +3 -21
- package/lib/accounts/types.d.ts.map +1 -1
- package/lib/accounts/webSocketAccountSubscriber.d.ts +2 -0
- package/lib/accounts/webSocketAccountSubscriber.d.ts.map +1 -1
- package/lib/accounts/webSocketAccountSubscriber.js +13 -3
- package/lib/clearingHouse.d.ts +4 -0
- package/lib/clearingHouse.d.ts.map +1 -1
- package/lib/clearingHouse.js +8 -0
- package/lib/clearingHouseUser.d.ts +33 -5
- package/lib/clearingHouseUser.d.ts.map +1 -1
- package/lib/clearingHouseUser.js +224 -78
- package/lib/constants/markets.d.ts.map +1 -1
- package/lib/constants/markets.js +21 -0
- package/lib/constants/numericConstants.d.ts +1 -0
- package/lib/constants/numericConstants.d.ts.map +1 -1
- package/lib/constants/numericConstants.js +2 -1
- package/lib/examples/makeTradeExample.d.ts.map +1 -1
- package/lib/examples/makeTradeExample.js +14 -13
- package/lib/idl/clearing_house.json +94 -42
- package/lib/index.d.ts +4 -1
- package/lib/index.d.ts.map +1 -1
- package/lib/index.js +4 -1
- package/lib/math/amm.d.ts +26 -1
- package/lib/math/amm.d.ts.map +1 -1
- package/lib/math/amm.js +85 -10
- package/lib/math/funding.d.ts +6 -6
- package/lib/math/funding.d.ts.map +1 -1
- package/lib/math/funding.js +69 -25
- package/lib/math/insuranceFund.d.ts +15 -0
- package/lib/math/insuranceFund.d.ts.map +1 -0
- package/lib/math/insuranceFund.js +36 -0
- package/lib/math/position.d.ts +1 -1
- package/lib/math/position.d.ts.map +1 -1
- package/lib/math/position.js +15 -23
- package/lib/math/trade.d.ts +1 -1
- package/lib/math/trade.d.ts.map +1 -1
- package/lib/math/trade.js +9 -4
- package/lib/types.d.ts +0 -50
- package/lib/types.d.ts.map +1 -1
- package/lib/util/computeUnits.d.ts +3 -0
- package/lib/util/computeUnits.d.ts.map +1 -0
- package/lib/util/computeUnits.js +27 -0
- package/lib/util/tps.d.ts +3 -0
- package/lib/util/tps.d.ts.map +1 -0
- package/lib/util/tps.js +27 -0
- package/lib/wallet.d.ts +10 -0
- package/lib/wallet.d.ts.map +1 -0
- package/lib/wallet.js +35 -0
- package/package.json +3 -13
- package/src/accounts/defaultClearingHouseAccountSubscriber.ts +18 -0
- package/src/accounts/defaultUserAccountSubscriber.ts +18 -0
- package/src/accounts/types.ts +3 -28
- package/src/accounts/webSocketAccountSubscriber.ts +16 -6
- package/src/clearingHouse.ts +9 -3
- package/src/clearingHouseUser.ts +325 -107
- package/src/constants/markets.ts +21 -0
- package/src/constants/numericConstants.ts +2 -0
- package/src/examples/makeTradeExample.ts +2 -1
- package/src/idl/clearing_house.json +94 -42
- package/src/index.ts +4 -1
- package/src/math/amm.ts +120 -13
- package/src/math/funding.ts +109 -51
- package/src/math/insuranceFund.ts +29 -0
- package/src/math/position.ts +16 -28
- package/src/math/trade.ts +9 -5
- package/src/types.ts +0 -54
- package/src/util/computeUnits.ts +21 -0
- package/src/util/tps.ts +27 -0
- package/src/wallet.ts +22 -0
- package/.eslintrc.json +0 -36
- package/.prettierignore +0 -1
- package/.prettierrc.js +0 -9
- package/lib/accounts/defaultHistoryAccountSubscriber.d.ts +0 -29
- package/lib/accounts/defaultHistoryAccountSubscriber.d.ts.map +0 -1
- package/lib/accounts/defaultHistoryAccountSubscriber.js +0 -110
- package/src/accounts/defaultHistoryAccountSubscriber.ts +0 -179
|
@@ -1,110 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
-
});
|
|
10
|
-
};
|
|
11
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
-
exports.DefaultHistoryAccountSubscriber = void 0;
|
|
13
|
-
const types_1 = require("./types");
|
|
14
|
-
const events_1 = require("events");
|
|
15
|
-
const addresses_1 = require("../addresses");
|
|
16
|
-
const webSocketAccountSubscriber_1 = require("./webSocketAccountSubscriber");
|
|
17
|
-
class DefaultHistoryAccountSubscriber {
|
|
18
|
-
constructor(program) {
|
|
19
|
-
this.isSubscribed = false;
|
|
20
|
-
this.program = program;
|
|
21
|
-
this.eventEmitter = new events_1.EventEmitter();
|
|
22
|
-
}
|
|
23
|
-
subscribe() {
|
|
24
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
25
|
-
if (this.isSubscribed) {
|
|
26
|
-
return true;
|
|
27
|
-
}
|
|
28
|
-
const statePublicKey = yield addresses_1.getClearingHouseStateAccountPublicKey(this.program.programId);
|
|
29
|
-
const state = yield this.program.account.state.fetch(statePublicKey);
|
|
30
|
-
this.tradeHistoryAccountSubscriber = new webSocketAccountSubscriber_1.WebSocketAccountSubscriber('tradeHistory', this.program, state.tradeHistory);
|
|
31
|
-
yield this.tradeHistoryAccountSubscriber.subscribe((data) => {
|
|
32
|
-
this.eventEmitter.emit('tradeHistoryAccountUpdate', data);
|
|
33
|
-
this.eventEmitter.emit('update');
|
|
34
|
-
});
|
|
35
|
-
this.depositHistoryAccountSubscriber = new webSocketAccountSubscriber_1.WebSocketAccountSubscriber('depositHistory', this.program, state.depositHistory);
|
|
36
|
-
yield this.depositHistoryAccountSubscriber.subscribe((data) => {
|
|
37
|
-
this.eventEmitter.emit('depositHistoryAccountUpdate', data);
|
|
38
|
-
this.eventEmitter.emit('update');
|
|
39
|
-
});
|
|
40
|
-
this.fundingPaymentHistoryAccountSubscriber =
|
|
41
|
-
new webSocketAccountSubscriber_1.WebSocketAccountSubscriber('fundingPaymentHistory', this.program, state.fundingPaymentHistory);
|
|
42
|
-
yield this.fundingPaymentHistoryAccountSubscriber.subscribe((data) => {
|
|
43
|
-
this.eventEmitter.emit('fundingPaymentHistoryAccountUpdate', data);
|
|
44
|
-
this.eventEmitter.emit('update');
|
|
45
|
-
});
|
|
46
|
-
this.fundingRateHistoryAccountSubscriber = new webSocketAccountSubscriber_1.WebSocketAccountSubscriber('fundingRateHistory', this.program, state.fundingRateHistory);
|
|
47
|
-
yield this.fundingRateHistoryAccountSubscriber.subscribe((data) => {
|
|
48
|
-
this.eventEmitter.emit('fundingRateHistoryAccountUpdate', data);
|
|
49
|
-
this.eventEmitter.emit('update');
|
|
50
|
-
});
|
|
51
|
-
this.liquidationHistoryAccountSubscriber = new webSocketAccountSubscriber_1.WebSocketAccountSubscriber('liquidationHistory', this.program, state.liquidationHistory);
|
|
52
|
-
yield this.liquidationHistoryAccountSubscriber.subscribe((data) => {
|
|
53
|
-
this.eventEmitter.emit('liquidationHistoryAccountUpdate', data);
|
|
54
|
-
this.eventEmitter.emit('update');
|
|
55
|
-
});
|
|
56
|
-
this.curveHistoryAccountSubscriber = new webSocketAccountSubscriber_1.WebSocketAccountSubscriber('curveHistory', this.program, state.curveHistory);
|
|
57
|
-
yield this.curveHistoryAccountSubscriber.subscribe((data) => {
|
|
58
|
-
this.eventEmitter.emit('curveHistoryAccountUpdate', data);
|
|
59
|
-
this.eventEmitter.emit('update');
|
|
60
|
-
});
|
|
61
|
-
this.eventEmitter.emit('update');
|
|
62
|
-
this.isSubscribed = true;
|
|
63
|
-
return true;
|
|
64
|
-
});
|
|
65
|
-
}
|
|
66
|
-
unsubscribe() {
|
|
67
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
68
|
-
if (!this.isSubscribed) {
|
|
69
|
-
return;
|
|
70
|
-
}
|
|
71
|
-
yield this.tradeHistoryAccountSubscriber.unsubscribe();
|
|
72
|
-
yield this.fundingRateHistoryAccountSubscriber.unsubscribe();
|
|
73
|
-
yield this.fundingPaymentHistoryAccountSubscriber.unsubscribe();
|
|
74
|
-
yield this.depositHistoryAccountSubscriber.unsubscribe();
|
|
75
|
-
yield this.curveHistoryAccountSubscriber.unsubscribe();
|
|
76
|
-
yield this.liquidationHistoryAccountSubscriber.unsubscribe();
|
|
77
|
-
this.isSubscribed = false;
|
|
78
|
-
});
|
|
79
|
-
}
|
|
80
|
-
assertIsSubscribed() {
|
|
81
|
-
if (!this.isSubscribed) {
|
|
82
|
-
throw new types_1.NotSubscribedError('You must call `subscribe` before using this function');
|
|
83
|
-
}
|
|
84
|
-
}
|
|
85
|
-
getTradeHistoryAccount() {
|
|
86
|
-
this.assertIsSubscribed();
|
|
87
|
-
return this.tradeHistoryAccountSubscriber.data;
|
|
88
|
-
}
|
|
89
|
-
getDepositHistoryAccount() {
|
|
90
|
-
this.assertIsSubscribed();
|
|
91
|
-
return this.depositHistoryAccountSubscriber.data;
|
|
92
|
-
}
|
|
93
|
-
getFundingPaymentHistoryAccount() {
|
|
94
|
-
this.assertIsSubscribed();
|
|
95
|
-
return this.fundingPaymentHistoryAccountSubscriber.data;
|
|
96
|
-
}
|
|
97
|
-
getFundingRateHistoryAccount() {
|
|
98
|
-
this.assertIsSubscribed();
|
|
99
|
-
return this.fundingRateHistoryAccountSubscriber.data;
|
|
100
|
-
}
|
|
101
|
-
getCurveHistoryAccount() {
|
|
102
|
-
this.assertIsSubscribed();
|
|
103
|
-
return this.curveHistoryAccountSubscriber.data;
|
|
104
|
-
}
|
|
105
|
-
getLiquidationHistoryAccount() {
|
|
106
|
-
this.assertIsSubscribed();
|
|
107
|
-
return this.liquidationHistoryAccountSubscriber.data;
|
|
108
|
-
}
|
|
109
|
-
}
|
|
110
|
-
exports.DefaultHistoryAccountSubscriber = DefaultHistoryAccountSubscriber;
|
|
@@ -1,179 +0,0 @@
|
|
|
1
|
-
import { ClearingHouseAccountEvents, HistoryAccountSubscriber } from './types';
|
|
2
|
-
import { AccountSubscriber, NotSubscribedError } from './types';
|
|
3
|
-
import {
|
|
4
|
-
CurveHistoryAccount,
|
|
5
|
-
DepositHistoryAccount,
|
|
6
|
-
FundingPaymentHistoryAccount,
|
|
7
|
-
FundingRateHistoryAccount,
|
|
8
|
-
LiquidationHistoryAccount,
|
|
9
|
-
StateAccount,
|
|
10
|
-
TradeHistoryAccount,
|
|
11
|
-
} from '../types';
|
|
12
|
-
import { Program } from '@project-serum/anchor';
|
|
13
|
-
import StrictEventEmitter from 'strict-event-emitter-types';
|
|
14
|
-
import { EventEmitter } from 'events';
|
|
15
|
-
import { getClearingHouseStateAccountPublicKey } from '../addresses';
|
|
16
|
-
import { WebSocketAccountSubscriber } from './webSocketAccountSubscriber';
|
|
17
|
-
|
|
18
|
-
export class DefaultHistoryAccountSubscriber
|
|
19
|
-
implements HistoryAccountSubscriber
|
|
20
|
-
{
|
|
21
|
-
isSubscribed: boolean;
|
|
22
|
-
program: Program;
|
|
23
|
-
eventEmitter: StrictEventEmitter<EventEmitter, ClearingHouseAccountEvents>;
|
|
24
|
-
tradeHistoryAccountSubscriber?: AccountSubscriber<TradeHistoryAccount>;
|
|
25
|
-
depositHistoryAccountSubscriber?: AccountSubscriber<DepositHistoryAccount>;
|
|
26
|
-
fundingPaymentHistoryAccountSubscriber?: AccountSubscriber<FundingPaymentHistoryAccount>;
|
|
27
|
-
fundingRateHistoryAccountSubscriber?: AccountSubscriber<FundingRateHistoryAccount>;
|
|
28
|
-
curveHistoryAccountSubscriber?: AccountSubscriber<CurveHistoryAccount>;
|
|
29
|
-
liquidationHistoryAccountSubscriber?: AccountSubscriber<LiquidationHistoryAccount>;
|
|
30
|
-
|
|
31
|
-
public constructor(program: Program) {
|
|
32
|
-
this.isSubscribed = false;
|
|
33
|
-
this.program = program;
|
|
34
|
-
this.eventEmitter = new EventEmitter();
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
public async subscribe(): Promise<boolean> {
|
|
38
|
-
if (this.isSubscribed) {
|
|
39
|
-
return true;
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
const statePublicKey = await getClearingHouseStateAccountPublicKey(
|
|
43
|
-
this.program.programId
|
|
44
|
-
);
|
|
45
|
-
const state: StateAccount = await this.program.account.state.fetch(
|
|
46
|
-
statePublicKey
|
|
47
|
-
);
|
|
48
|
-
|
|
49
|
-
this.tradeHistoryAccountSubscriber = new WebSocketAccountSubscriber(
|
|
50
|
-
'tradeHistory',
|
|
51
|
-
this.program,
|
|
52
|
-
state.tradeHistory
|
|
53
|
-
);
|
|
54
|
-
await this.tradeHistoryAccountSubscriber.subscribe(
|
|
55
|
-
(data: TradeHistoryAccount) => {
|
|
56
|
-
this.eventEmitter.emit('tradeHistoryAccountUpdate', data);
|
|
57
|
-
this.eventEmitter.emit('update');
|
|
58
|
-
}
|
|
59
|
-
);
|
|
60
|
-
|
|
61
|
-
this.depositHistoryAccountSubscriber = new WebSocketAccountSubscriber(
|
|
62
|
-
'depositHistory',
|
|
63
|
-
this.program,
|
|
64
|
-
state.depositHistory
|
|
65
|
-
);
|
|
66
|
-
await this.depositHistoryAccountSubscriber.subscribe(
|
|
67
|
-
(data: DepositHistoryAccount) => {
|
|
68
|
-
this.eventEmitter.emit('depositHistoryAccountUpdate', data);
|
|
69
|
-
this.eventEmitter.emit('update');
|
|
70
|
-
}
|
|
71
|
-
);
|
|
72
|
-
|
|
73
|
-
this.fundingPaymentHistoryAccountSubscriber =
|
|
74
|
-
new WebSocketAccountSubscriber(
|
|
75
|
-
'fundingPaymentHistory',
|
|
76
|
-
this.program,
|
|
77
|
-
state.fundingPaymentHistory
|
|
78
|
-
);
|
|
79
|
-
await this.fundingPaymentHistoryAccountSubscriber.subscribe(
|
|
80
|
-
(data: FundingPaymentHistoryAccount) => {
|
|
81
|
-
this.eventEmitter.emit('fundingPaymentHistoryAccountUpdate', data);
|
|
82
|
-
this.eventEmitter.emit('update');
|
|
83
|
-
}
|
|
84
|
-
);
|
|
85
|
-
|
|
86
|
-
this.fundingRateHistoryAccountSubscriber = new WebSocketAccountSubscriber(
|
|
87
|
-
'fundingRateHistory',
|
|
88
|
-
this.program,
|
|
89
|
-
state.fundingRateHistory
|
|
90
|
-
);
|
|
91
|
-
await this.fundingRateHistoryAccountSubscriber.subscribe(
|
|
92
|
-
(data: FundingRateHistoryAccount) => {
|
|
93
|
-
this.eventEmitter.emit('fundingRateHistoryAccountUpdate', data);
|
|
94
|
-
this.eventEmitter.emit('update');
|
|
95
|
-
}
|
|
96
|
-
);
|
|
97
|
-
|
|
98
|
-
this.liquidationHistoryAccountSubscriber = new WebSocketAccountSubscriber(
|
|
99
|
-
'liquidationHistory',
|
|
100
|
-
this.program,
|
|
101
|
-
state.liquidationHistory
|
|
102
|
-
);
|
|
103
|
-
await this.liquidationHistoryAccountSubscriber.subscribe(
|
|
104
|
-
(data: LiquidationHistoryAccount) => {
|
|
105
|
-
this.eventEmitter.emit('liquidationHistoryAccountUpdate', data);
|
|
106
|
-
this.eventEmitter.emit('update');
|
|
107
|
-
}
|
|
108
|
-
);
|
|
109
|
-
|
|
110
|
-
this.curveHistoryAccountSubscriber = new WebSocketAccountSubscriber(
|
|
111
|
-
'curveHistory',
|
|
112
|
-
this.program,
|
|
113
|
-
state.curveHistory
|
|
114
|
-
);
|
|
115
|
-
await this.curveHistoryAccountSubscriber.subscribe(
|
|
116
|
-
(data: CurveHistoryAccount) => {
|
|
117
|
-
this.eventEmitter.emit('curveHistoryAccountUpdate', data);
|
|
118
|
-
this.eventEmitter.emit('update');
|
|
119
|
-
}
|
|
120
|
-
);
|
|
121
|
-
|
|
122
|
-
this.eventEmitter.emit('update');
|
|
123
|
-
|
|
124
|
-
this.isSubscribed = true;
|
|
125
|
-
return true;
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
public async unsubscribe(): Promise<void> {
|
|
129
|
-
if (!this.isSubscribed) {
|
|
130
|
-
return;
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
await this.tradeHistoryAccountSubscriber.unsubscribe();
|
|
134
|
-
await this.fundingRateHistoryAccountSubscriber.unsubscribe();
|
|
135
|
-
await this.fundingPaymentHistoryAccountSubscriber.unsubscribe();
|
|
136
|
-
await this.depositHistoryAccountSubscriber.unsubscribe();
|
|
137
|
-
await this.curveHistoryAccountSubscriber.unsubscribe();
|
|
138
|
-
await this.liquidationHistoryAccountSubscriber.unsubscribe();
|
|
139
|
-
this.isSubscribed = false;
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
assertIsSubscribed(): void {
|
|
143
|
-
if (!this.isSubscribed) {
|
|
144
|
-
throw new NotSubscribedError(
|
|
145
|
-
'You must call `subscribe` before using this function'
|
|
146
|
-
);
|
|
147
|
-
}
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
public getTradeHistoryAccount(): TradeHistoryAccount {
|
|
151
|
-
this.assertIsSubscribed();
|
|
152
|
-
return this.tradeHistoryAccountSubscriber.data;
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
public getDepositHistoryAccount(): DepositHistoryAccount {
|
|
156
|
-
this.assertIsSubscribed();
|
|
157
|
-
return this.depositHistoryAccountSubscriber.data;
|
|
158
|
-
}
|
|
159
|
-
|
|
160
|
-
public getFundingPaymentHistoryAccount(): FundingPaymentHistoryAccount {
|
|
161
|
-
this.assertIsSubscribed();
|
|
162
|
-
return this.fundingPaymentHistoryAccountSubscriber.data;
|
|
163
|
-
}
|
|
164
|
-
|
|
165
|
-
public getFundingRateHistoryAccount(): FundingRateHistoryAccount {
|
|
166
|
-
this.assertIsSubscribed();
|
|
167
|
-
return this.fundingRateHistoryAccountSubscriber.data;
|
|
168
|
-
}
|
|
169
|
-
|
|
170
|
-
public getCurveHistoryAccount(): CurveHistoryAccount {
|
|
171
|
-
this.assertIsSubscribed();
|
|
172
|
-
return this.curveHistoryAccountSubscriber.data;
|
|
173
|
-
}
|
|
174
|
-
|
|
175
|
-
public getLiquidationHistoryAccount(): LiquidationHistoryAccount {
|
|
176
|
-
this.assertIsSubscribed();
|
|
177
|
-
return this.liquidationHistoryAccountSubscriber.data;
|
|
178
|
-
}
|
|
179
|
-
}
|