@drift-labs/sdk 0.1.36-master.5 → 0.2.0-master.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/accounts/bulkAccountLoader.d.ts +7 -6
- package/lib/accounts/bulkAccountLoader.js +83 -93
- package/lib/accounts/bulkUserSubscription.js +13 -57
- package/lib/accounts/fetch.d.ts +4 -0
- package/lib/accounts/fetch.js +18 -0
- package/lib/accounts/pollingClearingHouseAccountSubscriber.d.ts +34 -38
- package/lib/accounts/pollingClearingHouseAccountSubscriber.js +224 -224
- package/lib/accounts/pollingOracleSubscriber.d.ts +3 -3
- package/lib/accounts/pollingOracleSubscriber.js +37 -49
- package/lib/accounts/pollingTokenAccountSubscriber.d.ts +3 -3
- package/lib/accounts/pollingTokenAccountSubscriber.js +35 -50
- package/lib/accounts/pollingUserAccountSubscriber.d.ts +7 -13
- package/lib/accounts/pollingUserAccountSubscriber.js +71 -134
- package/lib/accounts/types.d.ts +34 -41
- package/lib/accounts/webSocketAccountSubscriber.d.ts +6 -4
- package/lib/accounts/webSocketAccountSubscriber.js +39 -35
- package/lib/accounts/webSocketClearingHouseAccountSubscriber.d.ts +33 -28
- package/lib/accounts/webSocketClearingHouseAccountSubscriber.js +153 -185
- package/lib/accounts/webSocketUserAccountSubscriber.d.ts +5 -11
- package/lib/accounts/webSocketUserAccountSubscriber.js +22 -67
- package/lib/addresses/marketAddresses.d.ts +4 -0
- package/lib/addresses/marketAddresses.js +15 -0
- package/lib/addresses/pda.d.ts +12 -0
- package/lib/addresses/pda.js +83 -0
- package/lib/admin.d.ts +8 -12
- package/lib/admin.js +366 -490
- package/lib/clearingHouse.d.ts +84 -103
- package/lib/clearingHouse.js +779 -810
- package/lib/clearingHouseConfig.d.ts +34 -0
- package/lib/clearingHouseConfig.js +2 -0
- package/lib/clearingHouseUser.d.ts +19 -18
- package/lib/clearingHouseUser.js +157 -115
- package/lib/clearingHouseUserConfig.d.ts +14 -0
- package/lib/clearingHouseUserConfig.js +2 -0
- package/lib/config.d.ts +12 -0
- package/lib/config.js +35 -4
- package/lib/constants/banks.d.ts +16 -0
- package/lib/constants/banks.js +34 -0
- package/lib/constants/markets.d.ts +8 -3
- package/lib/constants/markets.js +13 -206
- package/lib/constants/numericConstants.d.ts +16 -0
- package/lib/constants/numericConstants.js +22 -6
- package/lib/events/eventList.d.ts +22 -0
- package/lib/events/eventList.js +77 -0
- package/lib/events/eventSubscriber.d.ts +34 -0
- package/lib/events/eventSubscriber.js +126 -0
- package/lib/events/fetchLogs.d.ts +13 -0
- package/lib/events/fetchLogs.js +39 -0
- package/lib/events/pollingLogProvider.d.ts +15 -0
- package/lib/events/pollingLogProvider.js +53 -0
- package/lib/events/sort.d.ts +2 -0
- package/lib/events/sort.js +44 -0
- package/lib/events/txEventCache.d.ts +24 -0
- package/lib/events/txEventCache.js +71 -0
- package/lib/events/types.d.ts +49 -0
- package/lib/events/types.js +20 -0
- package/lib/events/webSocketLogProvider.d.ts +12 -0
- package/lib/events/webSocketLogProvider.js +30 -0
- package/lib/examples/makeTradeExample.js +26 -27
- package/lib/factory/bigNum.d.ts +112 -0
- package/lib/factory/bigNum.js +356 -0
- package/lib/factory/oracleClient.d.ts +1 -2
- package/lib/factory/oracleClient.js +6 -2
- package/lib/idl/clearing_house.json +1739 -2287
- package/lib/index.d.ts +13 -4
- package/lib/index.js +13 -4
- package/lib/math/amm.d.ts +19 -29
- package/lib/math/amm.js +129 -179
- package/lib/math/auction.d.ts +5 -0
- package/lib/math/auction.js +39 -0
- package/lib/math/bankBalance.d.ts +9 -0
- package/lib/math/bankBalance.js +75 -0
- package/lib/math/conversion.d.ts +0 -1
- package/lib/math/conversion.js +1 -5
- package/lib/math/funding.d.ts +6 -6
- package/lib/math/funding.js +158 -175
- package/lib/math/market.d.ts +6 -6
- package/lib/math/market.js +10 -9
- package/lib/math/oracles.d.ts +3 -0
- package/lib/math/oracles.js +26 -0
- package/lib/math/orders.d.ts +5 -0
- package/lib/math/orders.js +31 -1
- package/lib/math/position.d.ts +7 -4
- package/lib/math/position.js +27 -9
- package/lib/math/repeg.d.ts +22 -0
- package/lib/math/repeg.js +128 -0
- package/lib/math/trade.d.ts +13 -8
- package/lib/math/trade.js +85 -22
- package/lib/mockUSDCFaucet.js +87 -116
- package/lib/oracles/oracleClientCache.d.ts +8 -0
- package/lib/oracles/oracleClientCache.js +19 -0
- package/lib/oracles/pythClient.d.ts +3 -5
- package/lib/oracles/pythClient.js +12 -30
- package/lib/oracles/quoteAssetOracleClient.d.ts +9 -0
- package/lib/oracles/quoteAssetOracleClient.js +21 -0
- package/lib/oracles/switchboardClient.d.ts +3 -5
- package/lib/oracles/switchboardClient.js +29 -47
- package/lib/oracles/types.d.ts +7 -1
- package/lib/orders.d.ts +6 -6
- package/lib/orders.js +10 -9
- package/lib/slot/SlotSubscriber.d.ts +12 -0
- package/lib/slot/SlotSubscriber.js +23 -0
- package/lib/tx/retryTxSender.d.ts +2 -2
- package/lib/tx/retryTxSender.js +108 -123
- package/lib/tx/types.d.ts +5 -1
- package/lib/tx/utils.d.ts +1 -1
- package/lib/tx/utils.js +11 -2
- package/lib/types.d.ts +105 -98
- package/lib/types.js +13 -1
- package/lib/userName.d.ts +4 -0
- package/lib/userName.js +20 -0
- package/lib/util/computeUnits.js +10 -21
- package/lib/util/tps.js +11 -22
- package/lib/wallet.js +7 -20
- package/package.json +10 -3
- package/src/accounts/bulkAccountLoader.ts +26 -15
- package/src/accounts/bulkUserSubscription.ts +1 -45
- package/src/accounts/fetch.ts +33 -0
- package/src/accounts/pollingClearingHouseAccountSubscriber.ts +249 -214
- package/src/accounts/pollingOracleSubscriber.ts +16 -8
- package/src/accounts/pollingTokenAccountSubscriber.ts +11 -8
- package/src/accounts/pollingUserAccountSubscriber.ts +21 -86
- package/src/accounts/types.ts +41 -70
- package/src/accounts/webSocketAccountSubscriber.ts +33 -16
- package/src/accounts/webSocketClearingHouseAccountSubscriber.ts +190 -240
- package/src/accounts/webSocketUserAccountSubscriber.ts +11 -71
- package/src/addresses/marketAddresses.ts +18 -0
- package/src/addresses/pda.ts +118 -0
- package/src/admin.ts +209 -267
- package/src/clearingHouse.ts +921 -829
- package/src/clearingHouseConfig.ts +37 -0
- package/src/clearingHouseUser.ts +280 -127
- package/src/clearingHouseUserConfig.ts +18 -0
- package/src/config.ts +54 -1
- package/src/constants/banks.ts +43 -0
- package/src/constants/markets.ts +16 -207
- package/src/constants/numericConstants.ts +33 -5
- package/src/events/eventList.ts +94 -0
- package/src/events/eventSubscriber.ts +194 -0
- package/src/events/fetchLogs.ts +80 -0
- package/src/events/pollingLogProvider.ts +79 -0
- package/src/events/sort.ts +65 -0
- package/src/events/txEventCache.ts +74 -0
- package/src/events/types.ts +98 -0
- package/src/events/webSocketLogProvider.ts +38 -0
- package/src/examples/makeTradeExample.ts +20 -11
- package/src/factory/bigNum.ts +507 -0
- package/src/factory/oracleClient.ts +7 -4
- package/src/idl/clearing_house.json +1739 -2287
- package/src/index.ts +13 -4
- package/src/math/amm.ts +229 -245
- package/src/math/auction.ts +39 -0
- package/src/math/bankBalance.ts +112 -0
- package/src/math/conversion.ts +1 -11
- package/src/math/funding.ts +12 -9
- package/src/math/market.ts +37 -30
- package/src/math/oracles.ts +36 -0
- package/src/math/orders.ts +38 -0
- package/src/math/position.ts +48 -13
- package/src/math/repeg.ts +175 -0
- package/src/math/trade.ts +114 -36
- package/src/math/utils.js +27 -0
- package/src/math/utils.js.map +1 -0
- package/src/oracles/oracleClientCache.ts +20 -0
- package/src/oracles/pythClient.ts +5 -11
- package/src/oracles/quoteAssetOracleClient.ts +25 -0
- package/src/oracles/switchboardClient.ts +16 -24
- package/src/oracles/types.ts +8 -1
- package/src/orders.ts +35 -20
- package/src/slot/SlotSubscriber.ts +32 -0
- package/src/tx/retryTxSender.ts +6 -4
- package/src/tx/types.ts +6 -1
- package/src/tx/utils.ts +22 -3
- package/src/types.ts +108 -110
- package/src/userName.ts +20 -0
- package/src/util/computeUnits.js +17 -0
- package/src/util/computeUnits.js.map +1 -0
- package/tests/bn/test.ts +255 -0
- package/tsconfig.json +12 -12
- package/lib/addresses.d.ts +0 -9
- package/lib/addresses.js +0 -87
- package/lib/constants/accounts.d.ts +0 -15
- package/lib/constants/accounts.js +0 -18
- package/lib/factory/clearingHouse.d.ts +0 -35
- package/lib/factory/clearingHouse.js +0 -81
- package/lib/factory/clearingHouseUser.d.ts +0 -19
- package/lib/factory/clearingHouseUser.js +0 -34
- package/lib/math/insuranceFund.d.ts +0 -15
- package/lib/math/insuranceFund.js +0 -33
- package/lib/tx/defaultTxSender.d.ts +0 -8
- package/lib/tx/defaultTxSender.js +0 -12
- package/src/addresses.ts +0 -71
- package/src/constants/accounts.ts +0 -26
- package/src/factory/clearingHouse.ts +0 -173
- package/src/factory/clearingHouseUser.ts +0 -73
- package/src/math/insuranceFund.ts +0 -29
- package/src/tx/defaultTxSender.ts +0 -24
package/lib/mockUSDCFaucet.js
CHANGED
|
@@ -22,15 +22,6 @@ var __importStar = (this && this.__importStar) || function (mod) {
|
|
|
22
22
|
__setModuleDefault(result, mod);
|
|
23
23
|
return result;
|
|
24
24
|
};
|
|
25
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
26
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
27
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
28
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
29
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
30
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
31
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
32
|
-
});
|
|
33
|
-
};
|
|
34
25
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
35
26
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
36
27
|
};
|
|
@@ -50,126 +41,106 @@ class MockUSDCFaucet {
|
|
|
50
41
|
this.provider = provider;
|
|
51
42
|
this.program = new anchor_1.Program(mock_usdc_faucet_json_1.default, programId, provider);
|
|
52
43
|
}
|
|
53
|
-
getMockUSDCFaucetStatePublicKeyAndNonce() {
|
|
54
|
-
return
|
|
55
|
-
return anchor.web3.PublicKey.findProgramAddress([Buffer.from(anchor.utils.bytes.utf8.encode('mock_usdc_faucet'))], this.program.programId);
|
|
56
|
-
});
|
|
44
|
+
async getMockUSDCFaucetStatePublicKeyAndNonce() {
|
|
45
|
+
return anchor.web3.PublicKey.findProgramAddress([Buffer.from(anchor.utils.bytes.utf8.encode('mock_usdc_faucet'))], this.program.programId);
|
|
57
46
|
}
|
|
58
|
-
getMockUSDCFaucetStatePublicKey() {
|
|
59
|
-
|
|
60
|
-
if (this.mockUSDCFaucetStatePublicKey) {
|
|
61
|
-
return this.mockUSDCFaucetStatePublicKey;
|
|
62
|
-
}
|
|
63
|
-
this.mockUSDCFaucetStatePublicKey = (yield this.getMockUSDCFaucetStatePublicKeyAndNonce())[0];
|
|
47
|
+
async getMockUSDCFaucetStatePublicKey() {
|
|
48
|
+
if (this.mockUSDCFaucetStatePublicKey) {
|
|
64
49
|
return this.mockUSDCFaucetStatePublicKey;
|
|
65
|
-
}
|
|
50
|
+
}
|
|
51
|
+
this.mockUSDCFaucetStatePublicKey = (await this.getMockUSDCFaucetStatePublicKeyAndNonce())[0];
|
|
52
|
+
return this.mockUSDCFaucetStatePublicKey;
|
|
66
53
|
}
|
|
67
|
-
initialize() {
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
programId: spl_token_1.TOKEN_PROGRAM_ID,
|
|
80
|
-
});
|
|
81
|
-
const [mintAuthority, _mintAuthorityNonce] = yield web3_js_1.PublicKey.findProgramAddress([fakeUSDCMint.publicKey.toBuffer()], this.program.programId);
|
|
82
|
-
const initUSDCMintIx = spl_token_1.Token.createInitMintInstruction(spl_token_1.TOKEN_PROGRAM_ID, fakeUSDCMint.publicKey, 6, mintAuthority, null);
|
|
83
|
-
const [mockUSDCFaucetStatePublicKey, mockUSDCFaucetStateNonce] = yield this.getMockUSDCFaucetStatePublicKeyAndNonce();
|
|
84
|
-
return yield this.program.rpc.initialize(mockUSDCFaucetStateNonce, {
|
|
85
|
-
accounts: {
|
|
86
|
-
mockUsdcFaucetState: mockUSDCFaucetStatePublicKey,
|
|
87
|
-
admin: this.wallet.publicKey,
|
|
88
|
-
mintAccount: fakeUSDCMint.publicKey,
|
|
89
|
-
rent: web3_js_1.SYSVAR_RENT_PUBKEY,
|
|
90
|
-
systemProgram: anchor.web3.SystemProgram.programId,
|
|
91
|
-
},
|
|
92
|
-
instructions: [createUSDCMintAccountIx, initUSDCMintIx],
|
|
93
|
-
signers: [fakeUSDCMint],
|
|
94
|
-
});
|
|
54
|
+
async initialize() {
|
|
55
|
+
const stateAccountRPCResponse = await this.connection.getParsedAccountInfo(await this.getMockUSDCFaucetStatePublicKey());
|
|
56
|
+
if (stateAccountRPCResponse.value !== null) {
|
|
57
|
+
throw new Error('Faucet already initialized');
|
|
58
|
+
}
|
|
59
|
+
const fakeUSDCMint = anchor.web3.Keypair.generate();
|
|
60
|
+
const createUSDCMintAccountIx = web3_js_1.SystemProgram.createAccount({
|
|
61
|
+
fromPubkey: this.wallet.publicKey,
|
|
62
|
+
newAccountPubkey: fakeUSDCMint.publicKey,
|
|
63
|
+
lamports: await spl_token_1.Token.getMinBalanceRentForExemptMint(this.connection),
|
|
64
|
+
space: spl_token_1.MintLayout.span,
|
|
65
|
+
programId: spl_token_1.TOKEN_PROGRAM_ID,
|
|
95
66
|
});
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
67
|
+
const [mintAuthority, _mintAuthorityNonce] = await web3_js_1.PublicKey.findProgramAddress([fakeUSDCMint.publicKey.toBuffer()], this.program.programId);
|
|
68
|
+
const initUSDCMintIx = spl_token_1.Token.createInitMintInstruction(spl_token_1.TOKEN_PROGRAM_ID, fakeUSDCMint.publicKey, 6, mintAuthority, null);
|
|
69
|
+
const [mockUSDCFaucetStatePublicKey, mockUSDCFaucetStateNonce] = await this.getMockUSDCFaucetStatePublicKeyAndNonce();
|
|
70
|
+
return await this.program.rpc.initialize(mockUSDCFaucetStateNonce, {
|
|
71
|
+
accounts: {
|
|
72
|
+
mockUsdcFaucetState: mockUSDCFaucetStatePublicKey,
|
|
73
|
+
admin: this.wallet.publicKey,
|
|
74
|
+
mintAccount: fakeUSDCMint.publicKey,
|
|
75
|
+
rent: web3_js_1.SYSVAR_RENT_PUBKEY,
|
|
76
|
+
systemProgram: anchor.web3.SystemProgram.programId,
|
|
77
|
+
},
|
|
78
|
+
instructions: [createUSDCMintAccountIx, initUSDCMintIx],
|
|
79
|
+
signers: [fakeUSDCMint],
|
|
100
80
|
});
|
|
101
81
|
}
|
|
102
|
-
|
|
103
|
-
return
|
|
104
|
-
const state = yield this.fetchState();
|
|
105
|
-
return yield this.program.rpc.mintToUser(amount, {
|
|
106
|
-
accounts: {
|
|
107
|
-
mockUsdcFaucetState: yield this.getMockUSDCFaucetStatePublicKey(),
|
|
108
|
-
mintAccount: state.mint,
|
|
109
|
-
userTokenAccount,
|
|
110
|
-
mintAuthority: state.mintAuthority,
|
|
111
|
-
tokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
|
|
112
|
-
},
|
|
113
|
-
});
|
|
114
|
-
});
|
|
82
|
+
async fetchState() {
|
|
83
|
+
return await this.program.account.mockUsdcFaucetState.fetch(await this.getMockUSDCFaucetStatePublicKey());
|
|
115
84
|
}
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
85
|
+
async mintToUser(userTokenAccount, amount) {
|
|
86
|
+
const state = await this.fetchState();
|
|
87
|
+
return await this.program.rpc.mintToUser(amount, {
|
|
88
|
+
accounts: {
|
|
89
|
+
mockUsdcFaucetState: await this.getMockUSDCFaucetStatePublicKey(),
|
|
90
|
+
mintAccount: state.mint,
|
|
91
|
+
userTokenAccount,
|
|
92
|
+
mintAuthority: state.mintAuthority,
|
|
93
|
+
tokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
|
|
94
|
+
},
|
|
122
95
|
});
|
|
123
96
|
}
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
const mintToIx = yield this.program.instruction.mintToUser(amount, {
|
|
130
|
-
accounts: {
|
|
131
|
-
mockUsdcFaucetState: yield this.getMockUSDCFaucetStatePublicKey(),
|
|
132
|
-
mintAccount: state.mint,
|
|
133
|
-
userTokenAccount: associateTokenPublicKey,
|
|
134
|
-
mintAuthority: state.mintAuthority,
|
|
135
|
-
tokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
|
|
136
|
-
},
|
|
137
|
-
});
|
|
138
|
-
return [associateTokenPublicKey, createAssociatedAccountIx, mintToIx];
|
|
139
|
-
});
|
|
97
|
+
async createAssociatedTokenAccountAndMintTo(userPublicKey, amount) {
|
|
98
|
+
const [associatedTokenPublicKey, createAssociatedAccountIx, mintToTx] = await this.createAssociatedTokenAccountAndMintToInstructions(userPublicKey, amount);
|
|
99
|
+
const tx = new web3_js_1.Transaction().add(createAssociatedAccountIx).add(mintToTx);
|
|
100
|
+
const txSig = await this.program.provider.sendAndConfirm(tx, [], this.opts);
|
|
101
|
+
return [associatedTokenPublicKey, txSig];
|
|
140
102
|
}
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
103
|
+
async createAssociatedTokenAccountAndMintToInstructions(userPublicKey, amount) {
|
|
104
|
+
const state = await this.fetchState();
|
|
105
|
+
const associateTokenPublicKey = await this.getAssosciatedMockUSDMintAddress({ userPubKey: userPublicKey });
|
|
106
|
+
const createAssociatedAccountIx = spl_token_1.Token.createAssociatedTokenAccountInstruction(spl_token_1.ASSOCIATED_TOKEN_PROGRAM_ID, spl_token_1.TOKEN_PROGRAM_ID, state.mint, associateTokenPublicKey, userPublicKey, this.wallet.publicKey);
|
|
107
|
+
const mintToIx = await this.program.instruction.mintToUser(amount, {
|
|
108
|
+
accounts: {
|
|
109
|
+
mockUsdcFaucetState: await this.getMockUSDCFaucetStatePublicKey(),
|
|
110
|
+
mintAccount: state.mint,
|
|
111
|
+
userTokenAccount: associateTokenPublicKey,
|
|
112
|
+
mintAuthority: state.mintAuthority,
|
|
113
|
+
tokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
|
|
114
|
+
},
|
|
145
115
|
});
|
|
116
|
+
return [associateTokenPublicKey, createAssociatedAccountIx, mintToIx];
|
|
146
117
|
}
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
const state = yield this.fetchState();
|
|
151
|
-
const token = new spl_token_1.Token(this.connection, state.mint, spl_token_1.TOKEN_PROGRAM_ID,
|
|
152
|
-
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
153
|
-
// @ts-ignore
|
|
154
|
-
this.provider.payer);
|
|
155
|
-
return yield token.getAccountInfo(assosciatedKey);
|
|
156
|
-
});
|
|
118
|
+
async getAssosciatedMockUSDMintAddress(props) {
|
|
119
|
+
const state = await this.fetchState();
|
|
120
|
+
return spl_token_1.Token.getAssociatedTokenAddress(spl_token_1.ASSOCIATED_TOKEN_PROGRAM_ID, spl_token_1.TOKEN_PROGRAM_ID, state.mint, props.userPubKey);
|
|
157
121
|
}
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
122
|
+
async getTokenAccountInfo(props) {
|
|
123
|
+
const assosciatedKey = await this.getAssosciatedMockUSDMintAddress(props);
|
|
124
|
+
const state = await this.fetchState();
|
|
125
|
+
const token = new spl_token_1.Token(this.connection, state.mint, spl_token_1.TOKEN_PROGRAM_ID,
|
|
126
|
+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
127
|
+
// @ts-ignore
|
|
128
|
+
this.provider.payer);
|
|
129
|
+
return await token.getAccountInfo(assosciatedKey);
|
|
130
|
+
}
|
|
131
|
+
async subscribeToTokenAccount(props) {
|
|
132
|
+
try {
|
|
133
|
+
const tokenAccountKey = await this.getAssosciatedMockUSDMintAddress(props);
|
|
134
|
+
props.callback(await this.getTokenAccountInfo(props));
|
|
135
|
+
// Couldn't find a way to do it using anchor framework subscription, someone on serum discord recommended this way
|
|
136
|
+
this.connection.onAccountChange(tokenAccountKey, async (_accountInfo /* accountInfo is a buffer which we don't know how to deserialize */) => {
|
|
137
|
+
props.callback(await this.getTokenAccountInfo(props));
|
|
138
|
+
});
|
|
139
|
+
return true;
|
|
140
|
+
}
|
|
141
|
+
catch (e) {
|
|
142
|
+
return false;
|
|
143
|
+
}
|
|
173
144
|
}
|
|
174
145
|
}
|
|
175
146
|
exports.MockUSDCFaucet = MockUSDCFaucet;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { OracleClient } from './types';
|
|
2
|
+
import { OracleSource } from '../types';
|
|
3
|
+
import { Connection } from '@solana/web3.js';
|
|
4
|
+
export declare class OracleClientCache {
|
|
5
|
+
cache: Map<string, OracleClient>;
|
|
6
|
+
constructor();
|
|
7
|
+
get(oracleSource: OracleSource, connection: Connection): OracleClient;
|
|
8
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.OracleClientCache = void 0;
|
|
4
|
+
const oracleClient_1 = require("../factory/oracleClient");
|
|
5
|
+
class OracleClientCache {
|
|
6
|
+
constructor() {
|
|
7
|
+
this.cache = new Map();
|
|
8
|
+
}
|
|
9
|
+
get(oracleSource, connection) {
|
|
10
|
+
const key = Object.keys(oracleSource)[0];
|
|
11
|
+
if (this.cache.has(key)) {
|
|
12
|
+
return this.cache.get(key);
|
|
13
|
+
}
|
|
14
|
+
const client = (0, oracleClient_1.getOracleClient)(oracleSource, connection);
|
|
15
|
+
this.cache.set(key, client);
|
|
16
|
+
return client;
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
exports.OracleClientCache = OracleClientCache;
|
|
@@ -1,14 +1,12 @@
|
|
|
1
1
|
/// <reference types="node" />
|
|
2
2
|
/// <reference types="bn.js" />
|
|
3
|
-
import { PriceData } from '@pythnetwork/client';
|
|
4
3
|
import { Connection, PublicKey } from '@solana/web3.js';
|
|
5
|
-
import { OraclePriceData } from './types';
|
|
4
|
+
import { OracleClient, OraclePriceData } from './types';
|
|
6
5
|
import { BN } from '@project-serum/anchor';
|
|
7
|
-
export declare class PythClient {
|
|
6
|
+
export declare class PythClient implements OracleClient {
|
|
8
7
|
private connection;
|
|
9
8
|
constructor(connection: Connection);
|
|
10
|
-
getPriceData(pricePublicKey: PublicKey): Promise<PriceData>;
|
|
11
9
|
getOraclePriceData(pricePublicKey: PublicKey): Promise<OraclePriceData>;
|
|
12
|
-
getOraclePriceDataFromBuffer(buffer: Buffer):
|
|
10
|
+
getOraclePriceDataFromBuffer(buffer: Buffer): OraclePriceData;
|
|
13
11
|
}
|
|
14
12
|
export declare function convertPythPrice(price: number, exponent: number): BN;
|
|
@@ -1,13 +1,4 @@
|
|
|
1
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
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
3
|
exports.convertPythPrice = exports.PythClient = void 0;
|
|
13
4
|
const client_1 = require("@pythnetwork/client");
|
|
@@ -17,29 +8,20 @@ class PythClient {
|
|
|
17
8
|
constructor(connection) {
|
|
18
9
|
this.connection = connection;
|
|
19
10
|
}
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
return (0, client_1.parsePriceData)(account.data);
|
|
24
|
-
});
|
|
25
|
-
}
|
|
26
|
-
getOraclePriceData(pricePublicKey) {
|
|
27
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
28
|
-
const accountInfo = yield this.connection.getAccountInfo(pricePublicKey);
|
|
29
|
-
return this.getOraclePriceDataFromBuffer(accountInfo.data);
|
|
30
|
-
});
|
|
11
|
+
async getOraclePriceData(pricePublicKey) {
|
|
12
|
+
const accountInfo = await this.connection.getAccountInfo(pricePublicKey);
|
|
13
|
+
return this.getOraclePriceDataFromBuffer(accountInfo.data);
|
|
31
14
|
}
|
|
32
15
|
getOraclePriceDataFromBuffer(buffer) {
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
});
|
|
16
|
+
const priceData = (0, client_1.parsePriceData)(buffer);
|
|
17
|
+
return {
|
|
18
|
+
price: convertPythPrice(priceData.aggregate.price, priceData.exponent),
|
|
19
|
+
slot: new anchor_1.BN(priceData.lastSlot.toString()),
|
|
20
|
+
confidence: convertPythPrice(priceData.confidence, priceData.exponent),
|
|
21
|
+
twap: convertPythPrice(priceData.twap.value, priceData.exponent),
|
|
22
|
+
twapConfidence: convertPythPrice(priceData.twac.value, priceData.exponent),
|
|
23
|
+
hasSufficientNumberOfDataPoints: true,
|
|
24
|
+
};
|
|
43
25
|
}
|
|
44
26
|
}
|
|
45
27
|
exports.PythClient = PythClient;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
import { PublicKey } from '@solana/web3.js';
|
|
3
|
+
import { OracleClient, OraclePriceData } from './types';
|
|
4
|
+
export declare const QUOTE_ORACLE_PRICE_DATA: OraclePriceData;
|
|
5
|
+
export declare class QuoteAssetOracleClient implements OracleClient {
|
|
6
|
+
constructor();
|
|
7
|
+
getOraclePriceData(_pricePublicKey: PublicKey): Promise<OraclePriceData>;
|
|
8
|
+
getOraclePriceDataFromBuffer(_buffer: Buffer): OraclePriceData;
|
|
9
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.QuoteAssetOracleClient = exports.QUOTE_ORACLE_PRICE_DATA = void 0;
|
|
4
|
+
const anchor_1 = require("@project-serum/anchor");
|
|
5
|
+
const numericConstants_1 = require("../constants/numericConstants");
|
|
6
|
+
exports.QUOTE_ORACLE_PRICE_DATA = {
|
|
7
|
+
price: numericConstants_1.MARK_PRICE_PRECISION,
|
|
8
|
+
slot: new anchor_1.BN(0),
|
|
9
|
+
confidence: new anchor_1.BN(1),
|
|
10
|
+
hasSufficientNumberOfDataPoints: true,
|
|
11
|
+
};
|
|
12
|
+
class QuoteAssetOracleClient {
|
|
13
|
+
constructor() { }
|
|
14
|
+
async getOraclePriceData(_pricePublicKey) {
|
|
15
|
+
return Promise.resolve(exports.QUOTE_ORACLE_PRICE_DATA);
|
|
16
|
+
}
|
|
17
|
+
getOraclePriceDataFromBuffer(_buffer) {
|
|
18
|
+
return exports.QUOTE_ORACLE_PRICE_DATA;
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
exports.QuoteAssetOracleClient = QuoteAssetOracleClient;
|
|
@@ -1,13 +1,11 @@
|
|
|
1
1
|
/// <reference types="node" />
|
|
2
2
|
import { Connection, PublicKey } from '@solana/web3.js';
|
|
3
|
-
import { DriftEnv } from '../config';
|
|
4
3
|
import { Program } from '@project-serum/anchor';
|
|
5
4
|
import { OracleClient, OraclePriceData } from './types';
|
|
6
5
|
export declare class SwitchboardClient implements OracleClient {
|
|
7
6
|
connection: Connection;
|
|
8
|
-
|
|
9
|
-
constructor(connection: Connection, env: DriftEnv);
|
|
7
|
+
constructor(connection: Connection);
|
|
10
8
|
getOraclePriceData(pricePublicKey: PublicKey): Promise<OraclePriceData>;
|
|
11
|
-
getOraclePriceDataFromBuffer(buffer: Buffer):
|
|
12
|
-
getProgram():
|
|
9
|
+
getOraclePriceDataFromBuffer(buffer: Buffer): OraclePriceData;
|
|
10
|
+
getProgram(): Program;
|
|
13
11
|
}
|
|
@@ -1,72 +1,54 @@
|
|
|
1
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
2
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
12
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
13
4
|
};
|
|
14
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
6
|
exports.SwitchboardClient = void 0;
|
|
16
|
-
const switchboard_v2_1 = require("@switchboard-xyz/switchboard-v2");
|
|
17
7
|
const web3_js_1 = require("@solana/web3.js");
|
|
18
8
|
const anchor_1 = require("@project-serum/anchor");
|
|
19
9
|
const numericConstants_1 = require("../constants/numericConstants");
|
|
20
10
|
const wallet_1 = require("../wallet");
|
|
21
11
|
const switchboard_v2_json_1 = __importDefault(require("../idl/switchboard_v2.json"));
|
|
22
|
-
|
|
23
|
-
const programMap = new Map();
|
|
12
|
+
let program;
|
|
24
13
|
class SwitchboardClient {
|
|
25
|
-
constructor(connection
|
|
14
|
+
constructor(connection) {
|
|
26
15
|
this.connection = connection;
|
|
27
|
-
this.env = env;
|
|
28
16
|
}
|
|
29
|
-
getOraclePriceData(pricePublicKey) {
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
return this.getOraclePriceDataFromBuffer(accountInfo.data);
|
|
33
|
-
});
|
|
17
|
+
async getOraclePriceData(pricePublicKey) {
|
|
18
|
+
const accountInfo = await this.connection.getAccountInfo(pricePublicKey);
|
|
19
|
+
return this.getOraclePriceDataFromBuffer(accountInfo.data);
|
|
34
20
|
}
|
|
35
21
|
getOraclePriceDataFromBuffer(buffer) {
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
22
|
+
const program = this.getProgram();
|
|
23
|
+
const aggregatorAccountData = program.account.aggregatorAccountData.coder.accounts.decode('AggregatorAccountData', buffer);
|
|
24
|
+
const price = convertSwitchboardDecimal(aggregatorAccountData.latestConfirmedRound.result);
|
|
25
|
+
const confidence = convertSwitchboardDecimal(aggregatorAccountData.latestConfirmedRound
|
|
26
|
+
.stdDeviation);
|
|
27
|
+
const hasSufficientNumberOfDataPoints = aggregatorAccountData.latestConfirmedRound.numSuccess >=
|
|
28
|
+
aggregatorAccountData.minOracleResults;
|
|
29
|
+
const slot = aggregatorAccountData.latestConfirmedRound.roundOpenSlot;
|
|
30
|
+
return {
|
|
31
|
+
price,
|
|
32
|
+
slot,
|
|
33
|
+
confidence,
|
|
34
|
+
hasSufficientNumberOfDataPoints,
|
|
35
|
+
};
|
|
49
36
|
}
|
|
50
37
|
getProgram() {
|
|
51
|
-
|
|
52
|
-
if (programMap.has(this.env)) {
|
|
53
|
-
return programMap.get(this.env);
|
|
54
|
-
}
|
|
55
|
-
const program = yield getSwitchboardProgram(this.env, this.connection);
|
|
56
|
-
programMap.set(this.env, program);
|
|
38
|
+
if (program) {
|
|
57
39
|
return program;
|
|
58
|
-
}
|
|
40
|
+
}
|
|
41
|
+
program = getSwitchboardProgram(this.connection);
|
|
42
|
+
return program;
|
|
59
43
|
}
|
|
60
44
|
}
|
|
61
45
|
exports.SwitchboardClient = SwitchboardClient;
|
|
62
|
-
function getSwitchboardProgram(
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
return new anchor_1.Program(switchboard_v2_json_1.default, programId, provider);
|
|
69
|
-
});
|
|
46
|
+
function getSwitchboardProgram(connection) {
|
|
47
|
+
const DEFAULT_KEYPAIR = web3_js_1.Keypair.fromSeed(new Uint8Array(32).fill(1));
|
|
48
|
+
const programId = web3_js_1.PublicKey.default;
|
|
49
|
+
const wallet = new wallet_1.Wallet(DEFAULT_KEYPAIR);
|
|
50
|
+
const provider = new anchor_1.AnchorProvider(connection, wallet, {});
|
|
51
|
+
return new anchor_1.Program(switchboard_v2_json_1.default, programId, provider);
|
|
70
52
|
}
|
|
71
53
|
function convertSwitchboardDecimal(switchboardDecimal) {
|
|
72
54
|
const switchboardPrecision = numericConstants_1.TEN.pow(new anchor_1.BN(switchboardDecimal.scale));
|
package/lib/oracles/types.d.ts
CHANGED
|
@@ -2,14 +2,20 @@
|
|
|
2
2
|
/// <reference types="node" />
|
|
3
3
|
import { BN } from '@project-serum/anchor';
|
|
4
4
|
import { PublicKey } from '@solana/web3.js';
|
|
5
|
+
import { OracleSource } from '../types';
|
|
5
6
|
export declare type OraclePriceData = {
|
|
6
7
|
price: BN;
|
|
7
8
|
slot: BN;
|
|
8
9
|
confidence: BN;
|
|
10
|
+
hasSufficientNumberOfDataPoints: boolean;
|
|
9
11
|
twap?: BN;
|
|
10
12
|
twapConfidence?: BN;
|
|
11
13
|
};
|
|
14
|
+
export declare type OracleInfo = {
|
|
15
|
+
publicKey: PublicKey;
|
|
16
|
+
source: OracleSource;
|
|
17
|
+
};
|
|
12
18
|
export interface OracleClient {
|
|
13
|
-
getOraclePriceDataFromBuffer(buffer: Buffer):
|
|
19
|
+
getOraclePriceDataFromBuffer(buffer: Buffer): OraclePriceData;
|
|
14
20
|
getOraclePriceData(publicKey: PublicKey): Promise<OraclePriceData>;
|
|
15
21
|
}
|
package/lib/orders.d.ts
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
/// <reference types="bn.js" />
|
|
2
|
-
import {
|
|
2
|
+
import { MarketAccount, Order, UserAccount, UserPosition } from './types';
|
|
3
3
|
import { BN, ClearingHouseUser } from '.';
|
|
4
4
|
import { OraclePriceData } from '.';
|
|
5
|
-
export declare function calculateNewStateAfterOrder(userAccount: UserAccount, userPosition: UserPosition, market:
|
|
6
|
-
export declare function calculateBaseAssetAmountMarketCanExecute(market:
|
|
7
|
-
export declare function calculateAmountToTradeForLimit(market:
|
|
8
|
-
export declare function calculateAmountToTradeForTriggerLimit(market:
|
|
9
|
-
export declare function calculateBaseAssetAmountUserCanExecute(market:
|
|
5
|
+
export declare function calculateNewStateAfterOrder(userAccount: UserAccount, userPosition: UserPosition, market: MarketAccount, order: Order): [UserAccount, UserPosition, MarketAccount] | null;
|
|
6
|
+
export declare function calculateBaseAssetAmountMarketCanExecute(market: MarketAccount, order: Order, oraclePriceData?: OraclePriceData): BN;
|
|
7
|
+
export declare function calculateAmountToTradeForLimit(market: MarketAccount, order: Order, oraclePriceData?: OraclePriceData): BN;
|
|
8
|
+
export declare function calculateAmountToTradeForTriggerLimit(market: MarketAccount, order: Order): BN;
|
|
9
|
+
export declare function calculateBaseAssetAmountUserCanExecute(market: MarketAccount, order: Order, user: ClearingHouseUser, oraclePriceData?: OraclePriceData): BN;
|
package/lib/orders.js
CHANGED
|
@@ -12,7 +12,7 @@ function calculateNewStateAfterOrder(userAccount, userPosition, market, order) {
|
|
|
12
12
|
return null;
|
|
13
13
|
}
|
|
14
14
|
const baseAssetAmountToTrade = calculateBaseAssetAmountMarketCanExecute(market, order);
|
|
15
|
-
if (baseAssetAmountToTrade.lt(market.amm.
|
|
15
|
+
if (baseAssetAmountToTrade.lt(market.amm.baseAssetAmountStepSize)) {
|
|
16
16
|
return null;
|
|
17
17
|
}
|
|
18
18
|
const userAccountAfter = Object.assign({}, userAccount);
|
|
@@ -112,15 +112,16 @@ function calculateAmountToTradeForLimit(market, order, oraclePriceData) {
|
|
|
112
112
|
limitPrice = floatingPrice;
|
|
113
113
|
}
|
|
114
114
|
}
|
|
115
|
-
const [maxAmountToTrade, direction] = (0, amm_1.calculateMaxBaseAssetAmountToTrade)(market.amm, limitPrice, order.direction
|
|
115
|
+
const [maxAmountToTrade, direction] = (0, amm_1.calculateMaxBaseAssetAmountToTrade)(market.amm, limitPrice, order.direction);
|
|
116
|
+
const baseAssetAmount = (0, _1.standardizeBaseAssetAmount)(maxAmountToTrade, market.amm.baseAssetAmountStepSize);
|
|
116
117
|
// Check that directions are the same
|
|
117
118
|
const sameDirection = isSameDirection(direction, order.direction);
|
|
118
119
|
if (!sameDirection) {
|
|
119
120
|
return numericConstants_1.ZERO;
|
|
120
121
|
}
|
|
121
|
-
return
|
|
122
|
+
return baseAssetAmount.gt(order.baseAssetAmount)
|
|
122
123
|
? order.baseAssetAmount
|
|
123
|
-
:
|
|
124
|
+
: baseAssetAmount;
|
|
124
125
|
}
|
|
125
126
|
exports.calculateAmountToTradeForLimit = calculateAmountToTradeForLimit;
|
|
126
127
|
function calculateAmountToTradeForTriggerLimit(market, order) {
|
|
@@ -142,8 +143,8 @@ function calculateAmountToTradeForTriggerMarket(market, order) {
|
|
|
142
143
|
? order.baseAssetAmount
|
|
143
144
|
: numericConstants_1.ZERO;
|
|
144
145
|
}
|
|
145
|
-
function isTriggerConditionSatisfied(market, order) {
|
|
146
|
-
const markPrice = (0, market_1.calculateMarkPrice)(market);
|
|
146
|
+
function isTriggerConditionSatisfied(market, order, oraclePriceData) {
|
|
147
|
+
const markPrice = (0, market_1.calculateMarkPrice)(market, oraclePriceData);
|
|
147
148
|
if ((0, types_1.isVariant)(order.triggerCondition, 'above')) {
|
|
148
149
|
return markPrice.gt(order.triggerPrice);
|
|
149
150
|
}
|
|
@@ -151,7 +152,7 @@ function isTriggerConditionSatisfied(market, order) {
|
|
|
151
152
|
return markPrice.lt(order.triggerPrice);
|
|
152
153
|
}
|
|
153
154
|
}
|
|
154
|
-
function calculateBaseAssetAmountUserCanExecute(market, order, user) {
|
|
155
|
+
function calculateBaseAssetAmountUserCanExecute(market, order, user, oraclePriceData) {
|
|
155
156
|
const maxLeverage = user.getMaxLeverage(order.marketIndex, 'Initial');
|
|
156
157
|
const freeCollateral = user.getFreeCollateral();
|
|
157
158
|
let quoteAssetAmount;
|
|
@@ -161,7 +162,7 @@ function calculateBaseAssetAmountUserCanExecute(market, order, user) {
|
|
|
161
162
|
else {
|
|
162
163
|
const position = user.getUserPosition(order.marketIndex) ||
|
|
163
164
|
user.getEmptyPosition(order.marketIndex);
|
|
164
|
-
const positionValue = (0, _1.calculateBaseAssetValue)(market, position);
|
|
165
|
+
const positionValue = (0, _1.calculateBaseAssetValue)(market, position, oraclePriceData);
|
|
165
166
|
quoteAssetAmount = freeCollateral
|
|
166
167
|
.mul(maxLeverage)
|
|
167
168
|
.div(_1.TEN_THOUSAND)
|
|
@@ -176,7 +177,7 @@ function calculateBaseAssetAmountUserCanExecute(market, order, user) {
|
|
|
176
177
|
const useSpread = !order.postOnly;
|
|
177
178
|
let amm;
|
|
178
179
|
if (useSpread) {
|
|
179
|
-
const { baseAssetReserve, quoteAssetReserve } = (0, _1.calculateSpreadReserves)(market.amm, order.direction);
|
|
180
|
+
const { baseAssetReserve, quoteAssetReserve } = (0, _1.calculateSpreadReserves)(market.amm, order.direction, oraclePriceData);
|
|
180
181
|
amm = {
|
|
181
182
|
baseAssetReserve,
|
|
182
183
|
quoteAssetReserve,
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { Connection } from '@solana/web3.js';
|
|
2
|
+
declare type SlotSubscriberConfig = {};
|
|
3
|
+
export declare class SlotSubscriber {
|
|
4
|
+
private connection;
|
|
5
|
+
currentSlot: number;
|
|
6
|
+
subscriptionId: number;
|
|
7
|
+
constructor(connection: Connection, _config?: SlotSubscriberConfig);
|
|
8
|
+
subscribe(): Promise<void>;
|
|
9
|
+
getSlot(): number;
|
|
10
|
+
unsubscribe(): Promise<void>;
|
|
11
|
+
}
|
|
12
|
+
export {};
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.SlotSubscriber = void 0;
|
|
4
|
+
class SlotSubscriber {
|
|
5
|
+
constructor(connection, _config) {
|
|
6
|
+
this.connection = connection;
|
|
7
|
+
}
|
|
8
|
+
async subscribe() {
|
|
9
|
+
this.currentSlot = await this.connection.getSlot('confirmed');
|
|
10
|
+
this.subscriptionId = this.connection.onSlotChange((slotInfo) => {
|
|
11
|
+
this.currentSlot = slotInfo.slot;
|
|
12
|
+
});
|
|
13
|
+
}
|
|
14
|
+
getSlot() {
|
|
15
|
+
return this.currentSlot;
|
|
16
|
+
}
|
|
17
|
+
async unsubscribe() {
|
|
18
|
+
if (this.subscriptionId) {
|
|
19
|
+
await this.connection.removeSlotChangeListener(this.subscriptionId);
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
exports.SlotSubscriber = SlotSubscriber;
|