@drift-labs/sdk 0.1.24 → 0.1.27
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 +1 -0
- package/lib/accounts/bulkAccountLoader.js +16 -2
- package/lib/accounts/pollingClearingHouseAccountSubscriber.js +3 -3
- package/lib/accounts/pollingOracleSubscriber.d.ts +26 -0
- package/lib/accounts/pollingOracleSubscriber.js +79 -0
- package/lib/accounts/pollingTokenAccountSubscriber.js +2 -2
- package/lib/accounts/pollingUserAccountSubscriber.js +4 -4
- package/lib/accounts/types.d.ts +14 -1
- package/lib/accounts/webSocketAccountSubscriber.js +2 -2
- package/lib/accounts/webSocketClearingHouseAccountSubscriber.js +1 -1
- package/lib/accounts/webSocketUserAccountSubscriber.js +2 -2
- package/lib/addresses.js +5 -1
- package/lib/admin.d.ts +1 -1
- package/lib/admin.js +15 -10
- package/lib/clearingHouse.d.ts +4 -4
- package/lib/clearingHouse.js +56 -25
- package/lib/clearingHouseUser.js +18 -18
- package/lib/constants/markets.d.ts +8 -6
- package/lib/constants/markets.js +85 -26
- package/lib/constants/numericConstants.d.ts +1 -0
- package/lib/constants/numericConstants.js +2 -1
- package/lib/examples/makeTradeExample.js +6 -6
- package/lib/factory/oracleClient.d.ts +5 -0
- package/lib/factory/oracleClient.js +16 -0
- package/lib/idl/clearing_house.json +22 -2
- package/lib/idl/switchboard_v2.json +4663 -0
- package/lib/index.d.ts +5 -1
- package/lib/index.js +10 -2
- package/lib/math/amm.js +12 -12
- package/lib/math/conversion.js +1 -1
- package/lib/math/funding.d.ts +6 -6
- package/lib/math/funding.js +5 -17
- package/lib/math/market.d.ts +2 -0
- package/lib/math/market.js +8 -3
- package/lib/math/orders.js +9 -9
- package/lib/math/position.js +1 -1
- package/lib/math/trade.js +18 -18
- package/lib/mockUSDCFaucet.js +5 -1
- package/lib/oracles/pythClient.d.ts +14 -0
- package/lib/oracles/pythClient.js +53 -0
- package/lib/oracles/switchboardClient.d.ts +13 -0
- package/lib/oracles/switchboardClient.js +76 -0
- package/lib/oracles/types.d.ts +15 -0
- package/lib/oracles/types.js +2 -0
- package/lib/orderParams.d.ts +1 -1
- package/lib/orderParams.js +3 -3
- package/lib/orders.d.ts +3 -2
- package/lib/orders.js +29 -22
- package/lib/tx/retryTxSender.js +1 -1
- package/lib/types.d.ts +1 -0
- package/package.json +2 -1
- package/src/accounts/bulkAccountLoader.js +180 -0
- package/src/accounts/bulkAccountLoader.js.map +1 -0
- package/src/accounts/bulkAccountLoader.ts +19 -0
- package/src/accounts/bulkUserSubscription.js +56 -0
- package/src/accounts/bulkUserSubscription.js.map +1 -0
- package/src/accounts/pollingClearingHouseAccountSubscriber.js +210 -0
- package/src/accounts/pollingClearingHouseAccountSubscriber.js.map +1 -0
- package/src/accounts/pollingOracleSubscriber.js +65 -0
- package/src/accounts/pollingOracleSubscriber.js.map +1 -0
- package/src/accounts/pollingOracleSubscriber.ts +103 -0
- package/src/accounts/pollingTokenAccountSubscriber.js +65 -0
- package/src/accounts/pollingTokenAccountSubscriber.js.map +1 -0
- package/src/accounts/pollingUserAccountSubscriber.js +139 -0
- package/src/accounts/pollingUserAccountSubscriber.js.map +1 -0
- package/src/accounts/types.js +1 -0
- package/src/accounts/types.js.map +1 -0
- package/src/accounts/types.ts +22 -1
- package/src/accounts/utils.js +1 -0
- package/src/accounts/utils.js.map +1 -0
- package/src/accounts/webSocketAccountSubscriber.js +15 -27
- package/src/accounts/webSocketAccountSubscriber.js.map +1 -0
- package/src/accounts/webSocketClearingHouseAccountSubscriber.js +212 -0
- package/src/accounts/webSocketClearingHouseAccountSubscriber.js.map +1 -0
- package/src/accounts/webSocketUserAccountSubscriber.js +78 -0
- package/src/accounts/webSocketUserAccountSubscriber.js.map +1 -0
- package/src/addresses.js +20 -44
- package/src/addresses.js.map +1 -0
- package/src/admin.js +443 -0
- package/src/admin.js.map +1 -0
- package/src/admin.ts +2 -0
- package/src/assert/assert.js +10 -0
- package/src/assert/assert.js.map +1 -0
- package/src/clearingHouse.js +769 -0
- package/src/clearingHouse.js.map +1 -0
- package/src/clearingHouse.ts +47 -7
- package/src/clearingHouseUser.js +581 -0
- package/src/clearingHouseUser.js.map +1 -0
- package/src/config.js +37 -0
- package/src/config.js.map +1 -0
- package/src/constants/markets.js +167 -0
- package/src/constants/markets.js.map +1 -0
- package/src/constants/markets.ts +93 -31
- package/src/constants/numericConstants.js +22 -0
- package/src/constants/numericConstants.js.map +1 -0
- package/src/constants/numericConstants.ts +1 -0
- package/src/factory/clearingHouse.js +65 -0
- package/src/factory/clearingHouse.js.map +1 -0
- package/src/factory/clearingHouseUser.js +35 -0
- package/src/factory/clearingHouseUser.js.map +1 -0
- package/src/factory/oracleClient.js +17 -0
- package/src/factory/oracleClient.js.map +1 -0
- package/src/factory/oracleClient.ts +22 -0
- package/src/idl/clearing_house.json +22 -2
- package/src/idl/switchboard_v2.json +4663 -0
- package/src/index.js +56 -0
- package/src/index.js.map +1 -0
- package/src/index.ts +5 -1
- package/src/math/amm.js +285 -0
- package/src/math/amm.js.map +1 -0
- package/src/math/conversion.js +16 -0
- package/src/math/conversion.js.map +1 -0
- package/src/math/funding.js +223 -0
- package/src/math/funding.js.map +1 -0
- package/src/math/funding.ts +9 -25
- package/src/math/insuranceFund.js +23 -0
- package/src/math/insuranceFund.js.map +1 -0
- package/src/math/market.js +30 -0
- package/src/math/market.js.map +1 -0
- package/src/math/market.ts +9 -0
- package/src/math/orders.js +73 -0
- package/src/math/orders.js.map +1 -0
- package/src/math/position.js +121 -0
- package/src/math/position.js.map +1 -0
- package/src/math/trade.js +182 -0
- package/src/math/trade.js.map +1 -0
- package/src/math/utils.js +27 -0
- package/src/math/utils.js.map +1 -0
- package/src/mockUSDCFaucet.js +88 -116
- package/src/mockUSDCFaucet.js.map +1 -0
- package/src/oracles/pythClient.js +39 -0
- package/src/oracles/pythClient.js.map +1 -0
- package/src/oracles/pythClient.ts +49 -0
- package/src/oracles/switchboardClient.js +60 -0
- package/src/oracles/switchboardClient.js.map +1 -0
- package/src/oracles/switchboardClient.ts +87 -0
- package/src/oracles/types.js +3 -0
- package/src/oracles/types.js.map +1 -0
- package/src/oracles/types.ts +15 -0
- package/src/orderParams.js +109 -0
- package/src/orderParams.js.map +1 -0
- package/src/orderParams.ts +5 -3
- package/src/orders.js +172 -0
- package/src/orders.js.map +1 -0
- package/src/orders.ts +17 -4
- package/src/token/index.js +39 -0
- package/src/token/index.js.map +1 -0
- package/src/tx/defaultTxSender.js +13 -0
- package/src/tx/defaultTxSender.js.map +1 -0
- package/src/tx/retryTxSender.js +137 -0
- package/src/tx/retryTxSender.js.map +1 -0
- package/src/tx/types.js +3 -0
- package/src/tx/types.js.map +1 -0
- package/src/tx/utils.js +9 -0
- package/src/tx/utils.js.map +1 -0
- package/src/types.js +1 -0
- package/src/types.js.map +1 -0
- package/src/types.ts +1 -0
- package/src/util/computeUnits.js +17 -0
- package/src/util/computeUnits.js.map +1 -0
- package/src/util/tps.js +17 -0
- package/src/util/tps.js.map +1 -0
- package/src/wallet.js +23 -0
- package/src/wallet.js.map +1 -0
- package/lib/pythClient.d.ts +0 -7
- package/lib/pythClient.js +0 -25
- package/src/pythClient.ts +0 -15
package/src/mockUSDCFaucet.js
CHANGED
|
@@ -18,15 +18,6 @@ var __importStar = (this && this.__importStar) || function (mod) {
|
|
|
18
18
|
__setModuleDefault(result, mod);
|
|
19
19
|
return result;
|
|
20
20
|
};
|
|
21
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
22
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
23
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
24
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
25
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
26
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
27
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
28
|
-
});
|
|
29
|
-
};
|
|
30
21
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
31
22
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
32
23
|
};
|
|
@@ -46,126 +37,107 @@ class MockUSDCFaucet {
|
|
|
46
37
|
this.provider = provider;
|
|
47
38
|
this.program = new anchor_1.Program(mock_usdc_faucet_json_1.default, programId, provider);
|
|
48
39
|
}
|
|
49
|
-
getMockUSDCFaucetStatePublicKeyAndNonce() {
|
|
50
|
-
return
|
|
51
|
-
return anchor.web3.PublicKey.findProgramAddress([Buffer.from(anchor.utils.bytes.utf8.encode('mock_usdc_faucet'))], this.program.programId);
|
|
52
|
-
});
|
|
40
|
+
async getMockUSDCFaucetStatePublicKeyAndNonce() {
|
|
41
|
+
return anchor.web3.PublicKey.findProgramAddress([Buffer.from(anchor.utils.bytes.utf8.encode('mock_usdc_faucet'))], this.program.programId);
|
|
53
42
|
}
|
|
54
|
-
getMockUSDCFaucetStatePublicKey() {
|
|
55
|
-
|
|
56
|
-
if (this.mockUSDCFaucetStatePublicKey) {
|
|
57
|
-
return this.mockUSDCFaucetStatePublicKey;
|
|
58
|
-
}
|
|
59
|
-
this.mockUSDCFaucetStatePublicKey = (yield this.getMockUSDCFaucetStatePublicKeyAndNonce())[0];
|
|
43
|
+
async getMockUSDCFaucetStatePublicKey() {
|
|
44
|
+
if (this.mockUSDCFaucetStatePublicKey) {
|
|
60
45
|
return this.mockUSDCFaucetStatePublicKey;
|
|
61
|
-
}
|
|
46
|
+
}
|
|
47
|
+
this.mockUSDCFaucetStatePublicKey = (await this.getMockUSDCFaucetStatePublicKeyAndNonce())[0];
|
|
48
|
+
return this.mockUSDCFaucetStatePublicKey;
|
|
62
49
|
}
|
|
63
|
-
initialize() {
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
programId: spl_token_1.TOKEN_PROGRAM_ID,
|
|
76
|
-
});
|
|
77
|
-
const [mintAuthority, _mintAuthorityNonce] = yield web3_js_1.PublicKey.findProgramAddress([fakeUSDCMint.publicKey.toBuffer()], this.program.programId);
|
|
78
|
-
const initUSDCMintIx = spl_token_1.Token.createInitMintInstruction(spl_token_1.TOKEN_PROGRAM_ID, fakeUSDCMint.publicKey, 6, mintAuthority, null);
|
|
79
|
-
const [mockUSDCFaucetStatePublicKey, mockUSDCFaucetStateNonce] = yield this.getMockUSDCFaucetStatePublicKeyAndNonce();
|
|
80
|
-
return yield this.program.rpc.initialize(mockUSDCFaucetStateNonce, {
|
|
81
|
-
accounts: {
|
|
82
|
-
mockUsdcFaucetState: mockUSDCFaucetStatePublicKey,
|
|
83
|
-
admin: this.wallet.publicKey,
|
|
84
|
-
mintAccount: fakeUSDCMint.publicKey,
|
|
85
|
-
rent: web3_js_1.SYSVAR_RENT_PUBKEY,
|
|
86
|
-
systemProgram: anchor.web3.SystemProgram.programId,
|
|
87
|
-
},
|
|
88
|
-
instructions: [createUSDCMintAccountIx, initUSDCMintIx],
|
|
89
|
-
signers: [fakeUSDCMint],
|
|
90
|
-
});
|
|
50
|
+
async initialize() {
|
|
51
|
+
const stateAccountRPCResponse = await this.connection.getParsedAccountInfo(await this.getMockUSDCFaucetStatePublicKey());
|
|
52
|
+
if (stateAccountRPCResponse.value !== null) {
|
|
53
|
+
throw new Error('Faucet already initialized');
|
|
54
|
+
}
|
|
55
|
+
const fakeUSDCMint = anchor.web3.Keypair.generate();
|
|
56
|
+
const createUSDCMintAccountIx = web3_js_1.SystemProgram.createAccount({
|
|
57
|
+
fromPubkey: this.wallet.publicKey,
|
|
58
|
+
newAccountPubkey: fakeUSDCMint.publicKey,
|
|
59
|
+
lamports: await spl_token_1.Token.getMinBalanceRentForExemptMint(this.connection),
|
|
60
|
+
space: spl_token_1.MintLayout.span,
|
|
61
|
+
programId: spl_token_1.TOKEN_PROGRAM_ID,
|
|
91
62
|
});
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
63
|
+
const [mintAuthority, _mintAuthorityNonce] = await web3_js_1.PublicKey.findProgramAddress([fakeUSDCMint.publicKey.toBuffer()], this.program.programId);
|
|
64
|
+
const initUSDCMintIx = spl_token_1.Token.createInitMintInstruction(spl_token_1.TOKEN_PROGRAM_ID, fakeUSDCMint.publicKey, 6, mintAuthority, null);
|
|
65
|
+
const [mockUSDCFaucetStatePublicKey, mockUSDCFaucetStateNonce] = await this.getMockUSDCFaucetStatePublicKeyAndNonce();
|
|
66
|
+
return await this.program.rpc.initialize(mockUSDCFaucetStateNonce, {
|
|
67
|
+
accounts: {
|
|
68
|
+
mockUsdcFaucetState: mockUSDCFaucetStatePublicKey,
|
|
69
|
+
admin: this.wallet.publicKey,
|
|
70
|
+
mintAccount: fakeUSDCMint.publicKey,
|
|
71
|
+
rent: web3_js_1.SYSVAR_RENT_PUBKEY,
|
|
72
|
+
systemProgram: anchor.web3.SystemProgram.programId,
|
|
73
|
+
},
|
|
74
|
+
instructions: [createUSDCMintAccountIx, initUSDCMintIx],
|
|
75
|
+
signers: [fakeUSDCMint],
|
|
96
76
|
});
|
|
97
77
|
}
|
|
98
|
-
|
|
99
|
-
return
|
|
100
|
-
const state = yield this.fetchState();
|
|
101
|
-
return yield this.program.rpc.mintToUser(amount, {
|
|
102
|
-
accounts: {
|
|
103
|
-
mockUsdcFaucetState: yield this.getMockUSDCFaucetStatePublicKey(),
|
|
104
|
-
mintAccount: state.mint,
|
|
105
|
-
userTokenAccount,
|
|
106
|
-
mintAuthority: state.mintAuthority,
|
|
107
|
-
tokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
|
|
108
|
-
},
|
|
109
|
-
});
|
|
110
|
-
});
|
|
78
|
+
async fetchState() {
|
|
79
|
+
return await this.program.account.mockUsdcFaucetState.fetch(await this.getMockUSDCFaucetStatePublicKey());
|
|
111
80
|
}
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
81
|
+
async mintToUser(userTokenAccount, amount) {
|
|
82
|
+
const state = await this.fetchState();
|
|
83
|
+
return await this.program.rpc.mintToUser(amount, {
|
|
84
|
+
accounts: {
|
|
85
|
+
mockUsdcFaucetState: await this.getMockUSDCFaucetStatePublicKey(),
|
|
86
|
+
mintAccount: state.mint,
|
|
87
|
+
userTokenAccount,
|
|
88
|
+
mintAuthority: state.mintAuthority,
|
|
89
|
+
tokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
|
|
90
|
+
},
|
|
118
91
|
});
|
|
119
92
|
}
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
const mintToIx = yield this.program.instruction.mintToUser(amount, {
|
|
126
|
-
accounts: {
|
|
127
|
-
mockUsdcFaucetState: yield this.getMockUSDCFaucetStatePublicKey(),
|
|
128
|
-
mintAccount: state.mint,
|
|
129
|
-
userTokenAccount: associateTokenPublicKey,
|
|
130
|
-
mintAuthority: state.mintAuthority,
|
|
131
|
-
tokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
|
|
132
|
-
},
|
|
133
|
-
});
|
|
134
|
-
return [associateTokenPublicKey, createAssociatedAccountIx, mintToIx];
|
|
135
|
-
});
|
|
93
|
+
async createAssociatedTokenAccountAndMintTo(userPublicKey, amount) {
|
|
94
|
+
const [associatedTokenPublicKey, createAssociatedAccountIx, mintToTx] = await this.createAssociatedTokenAccountAndMintToInstructions(userPublicKey, amount);
|
|
95
|
+
const tx = new web3_js_1.Transaction().add(createAssociatedAccountIx).add(mintToTx);
|
|
96
|
+
const txSig = await this.program.provider.send(tx, [], this.opts);
|
|
97
|
+
return [associatedTokenPublicKey, txSig];
|
|
136
98
|
}
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
99
|
+
async createAssociatedTokenAccountAndMintToInstructions(userPublicKey, amount) {
|
|
100
|
+
const state = await this.fetchState();
|
|
101
|
+
const associateTokenPublicKey = await this.getAssosciatedMockUSDMintAddress({ userPubKey: userPublicKey });
|
|
102
|
+
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);
|
|
103
|
+
const mintToIx = await this.program.instruction.mintToUser(amount, {
|
|
104
|
+
accounts: {
|
|
105
|
+
mockUsdcFaucetState: await this.getMockUSDCFaucetStatePublicKey(),
|
|
106
|
+
mintAccount: state.mint,
|
|
107
|
+
userTokenAccount: associateTokenPublicKey,
|
|
108
|
+
mintAuthority: state.mintAuthority,
|
|
109
|
+
tokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
|
|
110
|
+
},
|
|
141
111
|
});
|
|
112
|
+
return [associateTokenPublicKey, createAssociatedAccountIx, mintToIx];
|
|
142
113
|
}
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
const state = yield this.fetchState();
|
|
147
|
-
const token = new spl_token_1.Token(this.connection, state.mint, spl_token_1.TOKEN_PROGRAM_ID,
|
|
148
|
-
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
149
|
-
// @ts-ignore
|
|
150
|
-
this.provider.payer);
|
|
151
|
-
return yield token.getAccountInfo(assosciatedKey);
|
|
152
|
-
});
|
|
114
|
+
async getAssosciatedMockUSDMintAddress(props) {
|
|
115
|
+
const state = await this.fetchState();
|
|
116
|
+
return spl_token_1.Token.getAssociatedTokenAddress(spl_token_1.ASSOCIATED_TOKEN_PROGRAM_ID, spl_token_1.TOKEN_PROGRAM_ID, state.mint, props.userPubKey);
|
|
153
117
|
}
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
118
|
+
async getTokenAccountInfo(props) {
|
|
119
|
+
const assosciatedKey = await this.getAssosciatedMockUSDMintAddress(props);
|
|
120
|
+
const state = await this.fetchState();
|
|
121
|
+
const token = new spl_token_1.Token(this.connection, state.mint, spl_token_1.TOKEN_PROGRAM_ID,
|
|
122
|
+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
123
|
+
// @ts-ignore
|
|
124
|
+
this.provider.payer);
|
|
125
|
+
return await token.getAccountInfo(assosciatedKey);
|
|
126
|
+
}
|
|
127
|
+
async subscribeToTokenAccount(props) {
|
|
128
|
+
try {
|
|
129
|
+
const tokenAccountKey = await this.getAssosciatedMockUSDMintAddress(props);
|
|
130
|
+
props.callback(await this.getTokenAccountInfo(props));
|
|
131
|
+
// Couldn't find a way to do it using anchor framework subscription, someone on serum discord recommended this way
|
|
132
|
+
this.connection.onAccountChange(tokenAccountKey, async (_accountInfo /* accountInfo is a buffer which we don't know how to deserialize */) => {
|
|
133
|
+
props.callback(await this.getTokenAccountInfo(props));
|
|
134
|
+
});
|
|
135
|
+
return true;
|
|
136
|
+
}
|
|
137
|
+
catch (e) {
|
|
138
|
+
return false;
|
|
139
|
+
}
|
|
169
140
|
}
|
|
170
141
|
}
|
|
171
142
|
exports.MockUSDCFaucet = MockUSDCFaucet;
|
|
143
|
+
//# sourceMappingURL=mockUSDCFaucet.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"mockUSDCFaucet.js","sourceRoot":"","sources":["mockUSDCFaucet.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;AAAA,8DAAgD;AAChD,kDAA+D;AAC/D,iDAM2B;AAC3B,6CASyB;AAEzB,wFAA4D;AAG5D,MAAa,cAAc;IAO1B,YACC,UAAsB,EACtB,MAAe,EACf,SAAoB,EACpB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AC;IAEM,KAAK,CAAC,uBAAuB,CAAC,KAGpC;QACA,IAAI;YACH,MAAM,eAAe,GAAG,MAAM,IAAI,CAAC,gCAAgC,CAClE,KAAK,CACL,CAAC;YAEF,KAAK,CAAC,QAAQ,CAAC,MAAM,IAAI,CAAC,mBAAmB,CAAC,KAAK,CAAC,CAAC,CAAC;YAEtD,kHAAkH;YAClH,IAAI,CAAC,UAAU,CAAC,eAAe,CAC9B,eAAe,EACf,KAAK,EACJ,YAAY,CAAC,oEAAoE,EAChF,EAAE;gBACH,KAAK,CAAC,QAAQ,CAAC,MAAM,IAAI,CAAC,mBAAmB,CAAC,KAAK,CAAC,CAAC,CAAC;YACvD,CAAC,CACD,CAAC;YAEF,OAAO,IAAI,CAAC;SACZ;QAAC,OAAO,CAAC,EAAE;YACX,OAAO,KAAK,CAAC;SACb;IACF,CAAC;CACD;AAtND,wCAsNC"}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.convertPythPrice = exports.PythClient = void 0;
|
|
4
|
+
const client_1 = require("@pythnetwork/client");
|
|
5
|
+
const anchor_1 = require("@project-serum/anchor");
|
|
6
|
+
const numericConstants_1 = require("../constants/numericConstants");
|
|
7
|
+
class PythClient {
|
|
8
|
+
constructor(connection) {
|
|
9
|
+
this.connection = connection;
|
|
10
|
+
}
|
|
11
|
+
async getPriceData(pricePublicKey) {
|
|
12
|
+
const account = await this.connection.getAccountInfo(pricePublicKey);
|
|
13
|
+
return (0, client_1.parsePriceData)(account.data);
|
|
14
|
+
}
|
|
15
|
+
async getOraclePriceData(pricePublicKey) {
|
|
16
|
+
const accountInfo = await this.connection.getAccountInfo(pricePublicKey);
|
|
17
|
+
return this.getOraclePriceDataFromBuffer(accountInfo.data);
|
|
18
|
+
}
|
|
19
|
+
async getOraclePriceDataFromBuffer(buffer) {
|
|
20
|
+
const priceData = (0, client_1.parsePriceData)(buffer);
|
|
21
|
+
return {
|
|
22
|
+
price: convertPythPrice(priceData.price, priceData.exponent),
|
|
23
|
+
slot: new anchor_1.BN(priceData.lastSlot.toString()),
|
|
24
|
+
confidence: convertPythPrice(priceData.confidence, priceData.exponent),
|
|
25
|
+
twap: convertPythPrice(priceData.twap.value, priceData.exponent),
|
|
26
|
+
twapConfidence: convertPythPrice(priceData.twac.value, priceData.exponent),
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
exports.PythClient = PythClient;
|
|
31
|
+
function convertPythPrice(price, exponent) {
|
|
32
|
+
exponent = Math.abs(exponent);
|
|
33
|
+
const pythPrecision = numericConstants_1.TEN.pow(new anchor_1.BN(exponent).abs());
|
|
34
|
+
return new anchor_1.BN(price * Math.pow(10, exponent))
|
|
35
|
+
.mul(numericConstants_1.MARK_PRICE_PRECISION)
|
|
36
|
+
.div(pythPrecision);
|
|
37
|
+
}
|
|
38
|
+
exports.convertPythPrice = convertPythPrice;
|
|
39
|
+
//# sourceMappingURL=pythClient.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"pythClient.js","sourceRoot":"","sources":["pythClient.ts"],"names":[],"mappings":";;;AAAA,gDAAgE;AAGhE,kDAA2C;AAC3C,oEAA0E;AAE1E,MAAa,UAAU;IAGtB,YAAmB,UAAsB;QACxC,IAAI,CAAC,UAAU,GAAG,UAAU,CAAC;IAC9B,CAAC;IAEM,KAAK,CAAC,YAAY,CAAC,cAAyB;QAClD,MAAM,OAAO,GAAG,MAAM,IAAI,CAAC,UAAU,CAAC,cAAc,CAAC,cAAc,CAAC,CAAC;QACrE,OAAO,IAAA,uBAAc,EAAC,OAAO,CAAC,IAAI,CAAC,CAAC;IACrC,CAAC;IAEM,KAAK,CAAC,kBAAkB,CAC9B,cAAyB;QAEzB,MAAM,WAAW,GAAG,MAAM,IAAI,CAAC,UAAU,CAAC,cAAc,CAAC,cAAc,CAAC,CAAC;QACzE,OAAO,IAAI,CAAC,4BAA4B,CAAC,WAAW,CAAC,IAAI,CAAC,CAAC;IAC5D,CAAC;IAEM,KAAK,CAAC,4BAA4B,CACxC,MAAc;QAEd,MAAM,SAAS,GAAG,IAAA,uBAAc,EAAC,MAAM,CAAC,CAAC;QACzC,OAAO;YACN,KAAK,EAAE,gBAAgB,CAAC,SAAS,CAAC,KAAK,EAAE,SAAS,CAAC,QAAQ,CAAC;YAC5D,IAAI,EAAE,IAAI,WAAE,CAAC,SAAS,CAAC,QAAQ,CAAC,QAAQ,EAAE,CAAC;YAC3C,UAAU,EAAE,gBAAgB,CAAC,SAAS,CAAC,UAAU,EAAE,SAAS,CAAC,QAAQ,CAAC;YACtE,IAAI,EAAE,gBAAgB,CAAC,SAAS,CAAC,IAAI,CAAC,KAAK,EAAE,SAAS,CAAC,QAAQ,CAAC;YAChE,cAAc,EAAE,gBAAgB,CAC/B,SAAS,CAAC,IAAI,CAAC,KAAK,EACpB,SAAS,CAAC,QAAQ,CAClB;SACD,CAAC;IACH,CAAC;CACD;AAlCD,gCAkCC;AAED,SAAgB,gBAAgB,CAAC,KAAa,EAAE,QAAgB;IAC/D,QAAQ,GAAG,IAAI,CAAC,GAAG,CAAC,QAAQ,CAAC,CAAC;IAC9B,MAAM,aAAa,GAAG,sBAAG,CAAC,GAAG,CAAC,IAAI,WAAE,CAAC,QAAQ,CAAC,CAAC,GAAG,EAAE,CAAC,CAAC;IACtD,OAAO,IAAI,WAAE,CAAC,KAAK,GAAG,IAAI,CAAC,GAAG,CAAC,EAAE,EAAE,QAAQ,CAAC,CAAC;SAC3C,GAAG,CAAC,uCAAoB,CAAC;SACzB,GAAG,CAAC,aAAa,CAAC,CAAC;AACtB,CAAC;AAND,4CAMC"}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { parsePriceData, PriceData } from '@pythnetwork/client';
|
|
2
|
+
import { Connection, PublicKey } from '@solana/web3.js';
|
|
3
|
+
import { OraclePriceData } from './types';
|
|
4
|
+
import { BN } from '@project-serum/anchor';
|
|
5
|
+
import { MARK_PRICE_PRECISION, TEN } from '../constants/numericConstants';
|
|
6
|
+
|
|
7
|
+
export class PythClient {
|
|
8
|
+
private connection: Connection;
|
|
9
|
+
|
|
10
|
+
public constructor(connection: Connection) {
|
|
11
|
+
this.connection = connection;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
public async getPriceData(pricePublicKey: PublicKey): Promise<PriceData> {
|
|
15
|
+
const account = await this.connection.getAccountInfo(pricePublicKey);
|
|
16
|
+
return parsePriceData(account.data);
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
public async getOraclePriceData(
|
|
20
|
+
pricePublicKey: PublicKey
|
|
21
|
+
): Promise<OraclePriceData> {
|
|
22
|
+
const accountInfo = await this.connection.getAccountInfo(pricePublicKey);
|
|
23
|
+
return this.getOraclePriceDataFromBuffer(accountInfo.data);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
public async getOraclePriceDataFromBuffer(
|
|
27
|
+
buffer: Buffer
|
|
28
|
+
): Promise<OraclePriceData> {
|
|
29
|
+
const priceData = parsePriceData(buffer);
|
|
30
|
+
return {
|
|
31
|
+
price: convertPythPrice(priceData.price, priceData.exponent),
|
|
32
|
+
slot: new BN(priceData.lastSlot.toString()),
|
|
33
|
+
confidence: convertPythPrice(priceData.confidence, priceData.exponent),
|
|
34
|
+
twap: convertPythPrice(priceData.twap.value, priceData.exponent),
|
|
35
|
+
twapConfidence: convertPythPrice(
|
|
36
|
+
priceData.twac.value,
|
|
37
|
+
priceData.exponent
|
|
38
|
+
),
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export function convertPythPrice(price: number, exponent: number): BN {
|
|
44
|
+
exponent = Math.abs(exponent);
|
|
45
|
+
const pythPrecision = TEN.pow(new BN(exponent).abs());
|
|
46
|
+
return new BN(price * Math.pow(10, exponent))
|
|
47
|
+
.mul(MARK_PRICE_PRECISION)
|
|
48
|
+
.div(pythPrecision);
|
|
49
|
+
}
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.SwitchboardClient = void 0;
|
|
7
|
+
const switchboard_v2_1 = require("@switchboard-xyz/switchboard-v2");
|
|
8
|
+
const web3_js_1 = require("@solana/web3.js");
|
|
9
|
+
const anchor_1 = require("@project-serum/anchor");
|
|
10
|
+
const numericConstants_1 = require("../constants/numericConstants");
|
|
11
|
+
const wallet_1 = require("../wallet");
|
|
12
|
+
const switchboard_v2_json_1 = __importDefault(require("../idl/switchboard_v2.json"));
|
|
13
|
+
// cache switchboard program for every client object since itll always be the same
|
|
14
|
+
const programMap = new Map();
|
|
15
|
+
class SwitchboardClient {
|
|
16
|
+
constructor(connection, env) {
|
|
17
|
+
this.connection = connection;
|
|
18
|
+
this.env = env;
|
|
19
|
+
}
|
|
20
|
+
async getOraclePriceData(pricePublicKey) {
|
|
21
|
+
const accountInfo = await this.connection.getAccountInfo(pricePublicKey);
|
|
22
|
+
return this.getOraclePriceDataFromBuffer(accountInfo.data);
|
|
23
|
+
}
|
|
24
|
+
async getOraclePriceDataFromBuffer(buffer) {
|
|
25
|
+
const program = await this.getProgram();
|
|
26
|
+
const aggregatorAccountData = program.account.aggregatorAccountData.coder.accounts.decode('AggregatorAccountData', buffer);
|
|
27
|
+
const price = convertSwitchboardDecimal(aggregatorAccountData.latestConfirmedRound.result);
|
|
28
|
+
const confidence = convertSwitchboardDecimal(aggregatorAccountData.latestConfirmedRound
|
|
29
|
+
.stdDeviation);
|
|
30
|
+
const slot = aggregatorAccountData.latestConfirmedRound.roundOpenSlot;
|
|
31
|
+
return {
|
|
32
|
+
price,
|
|
33
|
+
slot,
|
|
34
|
+
confidence,
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
async getProgram() {
|
|
38
|
+
if (programMap.has(this.env)) {
|
|
39
|
+
return programMap.get(this.env);
|
|
40
|
+
}
|
|
41
|
+
const program = await getSwitchboardProgram(this.env, this.connection);
|
|
42
|
+
programMap.set(this.env, program);
|
|
43
|
+
return program;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
exports.SwitchboardClient = SwitchboardClient;
|
|
47
|
+
async function getSwitchboardProgram(env, connection) {
|
|
48
|
+
const DEFAULT_KEYPAIR = web3_js_1.Keypair.fromSeed(new Uint8Array(32).fill(1));
|
|
49
|
+
const programId = (0, switchboard_v2_1.getSwitchboardPid)(env);
|
|
50
|
+
const wallet = new wallet_1.Wallet(DEFAULT_KEYPAIR);
|
|
51
|
+
const provider = new anchor_1.Provider(connection, wallet, {});
|
|
52
|
+
return new anchor_1.Program(switchboard_v2_json_1.default, programId, provider);
|
|
53
|
+
}
|
|
54
|
+
function convertSwitchboardDecimal(switchboardDecimal) {
|
|
55
|
+
const switchboardPrecision = numericConstants_1.TEN.pow(new anchor_1.BN(switchboardDecimal.scale));
|
|
56
|
+
return switchboardDecimal.mantissa
|
|
57
|
+
.mul(numericConstants_1.MARK_PRICE_PRECISION)
|
|
58
|
+
.div(switchboardPrecision);
|
|
59
|
+
}
|
|
60
|
+
//# sourceMappingURL=switchboardClient.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"switchboardClient.js","sourceRoot":"","sources":["switchboardClient.ts"],"names":[],"mappings":";;;;;;AAAA,oEAGyC;AACzC,6CAAiE;AAEjE,kDAAmE;AACnE,oEAA0E;AAE1E,sCAAmC;AACnC,qFAA0D;AAE1D,kFAAkF;AAClF,MAAM,UAAU,GAAG,IAAI,GAAG,EAAmB,CAAC;AAE9C,MAAa,iBAAiB;IAI7B,YAAmB,UAAsB,EAAE,GAAa;QACvD,IAAI,CAAC,UAAU,GAAG,UAAU,CAAC;QAC7B,IAAI,CAAC,GAAG,GAAG,GAAG,CAAC;IAChB,CAA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Q,CAAC,IAAI,UAAU,CAAC,EAAE,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,CAAC;IACrE,MAAM,SAAS,GAAG,IAAA,kCAAiB,EAAC,GAAG,CAAC,CAAC;IACzC,MAAM,MAAM,GAAG,IAAI,eAAM,CAAC,eAAe,CAAC,CAAC;IAC3C,MAAM,QAAQ,GAAG,IAAI,iBAAQ,CAAC,UAAU,EAAE,MAAM,EAAE,EAAE,CAAC,CAAC;IAEtD,OAAO,IAAI,gBAAO,CAAC,6BAAuB,EAAE,SAAS,EAAE,QAAQ,CAAC,CAAC;AAClE,CAAC;AAED,SAAS,yBAAyB,CAAC,kBAAsC;IACxE,MAAM,oBAAoB,GAAG,sBAAG,CAAC,GAAG,CAAC,IAAI,WAAE,CAAC,kBAAkB,CAAC,KAAK,CAAC,CAAC,CAAC;IACvE,OAAO,kBAAkB,CAAC,QAAQ;SAChC,GAAG,CAAC,uCAAoB,CAAC;SACzB,GAAG,CAAC,oBAAoB,CAAC,CAAC;AAC7B,CAAC"}
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import {
|
|
2
|
+
getSwitchboardPid,
|
|
3
|
+
SwitchboardDecimal,
|
|
4
|
+
} from '@switchboard-xyz/switchboard-v2';
|
|
5
|
+
import { Connection, Keypair, PublicKey } from '@solana/web3.js';
|
|
6
|
+
import { DriftEnv } from '../config';
|
|
7
|
+
import { BN, Provider, Program, Idl } from '@project-serum/anchor';
|
|
8
|
+
import { MARK_PRICE_PRECISION, TEN } from '../constants/numericConstants';
|
|
9
|
+
import { OracleClient, OraclePriceData } from './types';
|
|
10
|
+
import { Wallet } from '../wallet';
|
|
11
|
+
import switchboardV2Idl from '../idl/switchboard_v2.json';
|
|
12
|
+
|
|
13
|
+
// cache switchboard program for every client object since itll always be the same
|
|
14
|
+
const programMap = new Map<string, Program>();
|
|
15
|
+
|
|
16
|
+
export class SwitchboardClient implements OracleClient {
|
|
17
|
+
connection: Connection;
|
|
18
|
+
env: DriftEnv;
|
|
19
|
+
|
|
20
|
+
public constructor(connection: Connection, env: DriftEnv) {
|
|
21
|
+
this.connection = connection;
|
|
22
|
+
this.env = env;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
public async getOraclePriceData(
|
|
26
|
+
pricePublicKey: PublicKey
|
|
27
|
+
): Promise<OraclePriceData> {
|
|
28
|
+
const accountInfo = await this.connection.getAccountInfo(pricePublicKey);
|
|
29
|
+
return this.getOraclePriceDataFromBuffer(accountInfo.data);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
public async getOraclePriceDataFromBuffer(
|
|
33
|
+
buffer: Buffer
|
|
34
|
+
): Promise<OraclePriceData> {
|
|
35
|
+
const program = await this.getProgram();
|
|
36
|
+
|
|
37
|
+
const aggregatorAccountData =
|
|
38
|
+
program.account.aggregatorAccountData.coder.accounts.decode(
|
|
39
|
+
'AggregatorAccountData',
|
|
40
|
+
buffer
|
|
41
|
+
);
|
|
42
|
+
const price = convertSwitchboardDecimal(
|
|
43
|
+
aggregatorAccountData.latestConfirmedRound.result as SwitchboardDecimal
|
|
44
|
+
);
|
|
45
|
+
|
|
46
|
+
const confidence = convertSwitchboardDecimal(
|
|
47
|
+
aggregatorAccountData.latestConfirmedRound
|
|
48
|
+
.stdDeviation as SwitchboardDecimal
|
|
49
|
+
);
|
|
50
|
+
|
|
51
|
+
const slot: BN = aggregatorAccountData.latestConfirmedRound.roundOpenSlot;
|
|
52
|
+
return {
|
|
53
|
+
price,
|
|
54
|
+
slot,
|
|
55
|
+
confidence,
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
public async getProgram(): Promise<Program> {
|
|
60
|
+
if (programMap.has(this.env)) {
|
|
61
|
+
return programMap.get(this.env);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
const program = await getSwitchboardProgram(this.env, this.connection);
|
|
65
|
+
programMap.set(this.env, program);
|
|
66
|
+
return program;
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
async function getSwitchboardProgram(
|
|
71
|
+
env: DriftEnv,
|
|
72
|
+
connection: Connection
|
|
73
|
+
): Promise<Program> {
|
|
74
|
+
const DEFAULT_KEYPAIR = Keypair.fromSeed(new Uint8Array(32).fill(1));
|
|
75
|
+
const programId = getSwitchboardPid(env);
|
|
76
|
+
const wallet = new Wallet(DEFAULT_KEYPAIR);
|
|
77
|
+
const provider = new Provider(connection, wallet, {});
|
|
78
|
+
|
|
79
|
+
return new Program(switchboardV2Idl as Idl, programId, provider);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
function convertSwitchboardDecimal(switchboardDecimal: SwitchboardDecimal): BN {
|
|
83
|
+
const switchboardPrecision = TEN.pow(new BN(switchboardDecimal.scale));
|
|
84
|
+
return switchboardDecimal.mantissa
|
|
85
|
+
.mul(MARK_PRICE_PRECISION)
|
|
86
|
+
.div(switchboardPrecision);
|
|
87
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["types.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { BN } from '@project-serum/anchor';
|
|
2
|
+
import { PublicKey } from '@solana/web3.js';
|
|
3
|
+
|
|
4
|
+
export type OraclePriceData = {
|
|
5
|
+
price: BN;
|
|
6
|
+
slot: BN;
|
|
7
|
+
confidence: BN;
|
|
8
|
+
twap?: BN;
|
|
9
|
+
twapConfidence?: BN;
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
export interface OracleClient {
|
|
13
|
+
getOraclePriceDataFromBuffer(buffer: Buffer): Promise<OraclePriceData>;
|
|
14
|
+
getOraclePriceData(publicKey: PublicKey): Promise<OraclePriceData>;
|
|
15
|
+
}
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getMarketOrderParams = exports.getTriggerLimitOrderParams = exports.getTriggerMarketOrderParams = exports.getLimitOrderParams = void 0;
|
|
4
|
+
const types_1 = require("./types");
|
|
5
|
+
const numericConstants_1 = require("./constants/numericConstants");
|
|
6
|
+
function getLimitOrderParams(marketIndex, direction, baseAssetAmount, price, reduceOnly, discountToken = false, referrer = false, userOrderId = 0, postOnly = false, oraclePriceOffset = numericConstants_1.ZERO) {
|
|
7
|
+
return {
|
|
8
|
+
orderType: types_1.OrderType.LIMIT,
|
|
9
|
+
userOrderId,
|
|
10
|
+
marketIndex,
|
|
11
|
+
direction,
|
|
12
|
+
quoteAssetAmount: numericConstants_1.ZERO,
|
|
13
|
+
baseAssetAmount,
|
|
14
|
+
price,
|
|
15
|
+
reduceOnly,
|
|
16
|
+
postOnly,
|
|
17
|
+
immediateOrCancel: false,
|
|
18
|
+
positionLimit: numericConstants_1.ZERO,
|
|
19
|
+
padding0: true,
|
|
20
|
+
padding1: numericConstants_1.ZERO,
|
|
21
|
+
optionalAccounts: {
|
|
22
|
+
discountToken,
|
|
23
|
+
referrer,
|
|
24
|
+
},
|
|
25
|
+
triggerCondition: types_1.OrderTriggerCondition.ABOVE,
|
|
26
|
+
triggerPrice: numericConstants_1.ZERO,
|
|
27
|
+
oraclePriceOffset,
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
exports.getLimitOrderParams = getLimitOrderParams;
|
|
31
|
+
function getTriggerMarketOrderParams(marketIndex, direction, baseAssetAmount, triggerPrice, triggerCondition, reduceOnly, discountToken = false, referrer = false, userOrderId = 0) {
|
|
32
|
+
return {
|
|
33
|
+
orderType: types_1.OrderType.TRIGGER_MARKET,
|
|
34
|
+
userOrderId,
|
|
35
|
+
marketIndex,
|
|
36
|
+
direction,
|
|
37
|
+
quoteAssetAmount: numericConstants_1.ZERO,
|
|
38
|
+
baseAssetAmount,
|
|
39
|
+
price: numericConstants_1.ZERO,
|
|
40
|
+
reduceOnly,
|
|
41
|
+
postOnly: false,
|
|
42
|
+
immediateOrCancel: false,
|
|
43
|
+
positionLimit: numericConstants_1.ZERO,
|
|
44
|
+
padding0: true,
|
|
45
|
+
padding1: numericConstants_1.ZERO,
|
|
46
|
+
optionalAccounts: {
|
|
47
|
+
discountToken,
|
|
48
|
+
referrer,
|
|
49
|
+
},
|
|
50
|
+
triggerCondition,
|
|
51
|
+
triggerPrice,
|
|
52
|
+
oraclePriceOffset: numericConstants_1.ZERO,
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
exports.getTriggerMarketOrderParams = getTriggerMarketOrderParams;
|
|
56
|
+
function getTriggerLimitOrderParams(marketIndex, direction, baseAssetAmount, price, triggerPrice, triggerCondition, reduceOnly, discountToken = false, referrer = false, userOrderId = 0) {
|
|
57
|
+
return {
|
|
58
|
+
orderType: types_1.OrderType.TRIGGER_LIMIT,
|
|
59
|
+
userOrderId,
|
|
60
|
+
marketIndex,
|
|
61
|
+
direction,
|
|
62
|
+
quoteAssetAmount: numericConstants_1.ZERO,
|
|
63
|
+
baseAssetAmount,
|
|
64
|
+
price,
|
|
65
|
+
reduceOnly,
|
|
66
|
+
postOnly: false,
|
|
67
|
+
immediateOrCancel: false,
|
|
68
|
+
positionLimit: numericConstants_1.ZERO,
|
|
69
|
+
padding0: true,
|
|
70
|
+
padding1: numericConstants_1.ZERO,
|
|
71
|
+
optionalAccounts: {
|
|
72
|
+
discountToken,
|
|
73
|
+
referrer,
|
|
74
|
+
},
|
|
75
|
+
triggerCondition,
|
|
76
|
+
triggerPrice,
|
|
77
|
+
oraclePriceOffset: numericConstants_1.ZERO,
|
|
78
|
+
};
|
|
79
|
+
}
|
|
80
|
+
exports.getTriggerLimitOrderParams = getTriggerLimitOrderParams;
|
|
81
|
+
function getMarketOrderParams(marketIndex, direction, quoteAssetAmount, baseAssetAmount, reduceOnly, price = numericConstants_1.ZERO, discountToken = false, referrer = false) {
|
|
82
|
+
if (baseAssetAmount.eq(numericConstants_1.ZERO) && quoteAssetAmount.eq(numericConstants_1.ZERO)) {
|
|
83
|
+
throw Error('baseAssetAmount or quoteAssetAmount must be zero');
|
|
84
|
+
}
|
|
85
|
+
return {
|
|
86
|
+
orderType: types_1.OrderType.MARKET,
|
|
87
|
+
userOrderId: 0,
|
|
88
|
+
marketIndex,
|
|
89
|
+
direction,
|
|
90
|
+
quoteAssetAmount,
|
|
91
|
+
baseAssetAmount,
|
|
92
|
+
price,
|
|
93
|
+
reduceOnly,
|
|
94
|
+
postOnly: false,
|
|
95
|
+
immediateOrCancel: false,
|
|
96
|
+
positionLimit: numericConstants_1.ZERO,
|
|
97
|
+
padding0: true,
|
|
98
|
+
padding1: numericConstants_1.ZERO,
|
|
99
|
+
optionalAccounts: {
|
|
100
|
+
discountToken,
|
|
101
|
+
referrer,
|
|
102
|
+
},
|
|
103
|
+
triggerCondition: types_1.OrderTriggerCondition.ABOVE,
|
|
104
|
+
triggerPrice: numericConstants_1.ZERO,
|
|
105
|
+
oraclePriceOffset: numericConstants_1.ZERO,
|
|
106
|
+
};
|
|
107
|
+
}
|
|
108
|
+
exports.getMarketOrderParams = getMarketOrderParams;
|
|
109
|
+
//# sourceMappingURL=orderParams.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"orderParams.js","sourceRoot":"","sources":["orderParams.ts"],"names":[],"mappings":";;;AAAA,mCAKiB;AAEjB,mEAAoD;AAEpD,SAAgB,mBAAmB,CAClC,WAAe,EACf,SAA4B,EAC5B,eAAmB,EACnB,KAAS,EACT,UAAmB,EACnB,aAAa,GAAG,KAAK,EACrB,QAAQ,GAAG,KAAK,EAChB,WAAW,GAAG,CAAC,EACf,QAAQ,GAAG,KAAK,EAChB,iBAAiB,GAAG,uBAAI;IAExB,OAAO;QACN,SAAS,EAAE,iBAAS,CAAC,KAAK;QAC1B,WAAW;QACX,WAAW;QACX,SAAS;QACT,gBAAgB,EAAE,uBAAI;QACtB,eAAe;QACf,KAAK;QACL,UAAU;QACV,QAAQ;QACR,iBAAiB,EAAE,KAAK;QACxB,aAAa,EAAE,uBAAI;QACnB,QAA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