/
serumUtils.ts
260 lines (246 loc) · 7.75 KB
/
serumUtils.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
import { Program } from "@project-serum/anchor";
import { OpenOrders } from "@project-serum/serum";
import { AccountInfo, PublicKey } from "@solana/web3.js";
import { PsyAmerican } from "./psyAmericanTypes";
import { chunkArray } from "./utils";
const textEncoder = new TextEncoder();
// b"open-orders-init"
const OPEN_ORDERS_INIT_SEED = textEncoder.encode("open-orders-init");
/**
* Load the open orders for a user based on the Serum DEX and Serum Market
* address.
*
* @param program - Anchor Psy American program
* @param dexProgramId - Serum DEX program id
* @param serumMarketAddress - Serum market address
* @returns
*/
export const findOpenOrdersAccountsForOwner = async (
program: Program<PsyAmerican>,
dexProgramId: PublicKey,
serumMarketAddress: PublicKey
) => {
const [openOrdersAddressKey, openOrdersBump] =
await PublicKey.findProgramAddress(
[
textEncoder.encode("open-orders"),
dexProgramId.toBuffer(),
serumMarketAddress.toBuffer(),
program.provider.wallet.publicKey.toBuffer(),
],
program.programId
);
const filters = [
{
memcmp: {
offset: OpenOrders.getLayout(dexProgramId).offsetOf("market"),
bytes: serumMarketAddress.toBase58(),
},
},
{
memcmp: {
offset: OpenOrders.getLayout(dexProgramId).offsetOf("owner"),
bytes: openOrdersAddressKey.toBase58(),
},
},
{
dataSize: OpenOrders.getLayout(dexProgramId).span,
},
];
const accounts = await program.provider.connection.getProgramAccounts(
dexProgramId,
{
filters,
}
);
return accounts.map(({ pubkey, account }) =>
OpenOrders.fromAccountInfo(pubkey, account, dexProgramId)
);
};
/**
* Load all the open orders for a user based on the Serum DEX and the option market keys.
*
* @param program - Anchor Psy American program
* @param serumProgramId - Serum DEX program id
* @param optionMarketKeys - Keys for the Psy American OptionMarket's to load the open orders from
* @param priceCurrencyKey - Key of the pc (aka quote currency) from the serum markets
* @param optionMetaList - Optional list of option meta data to pull serum market data from instead of deriving
* the address. This is for backwards compatibility
* @returns
*/
export const findOpenOrdersForOptionMarkets = async (
program: Program<PsyAmerican>,
serumProgramId: PublicKey,
optionMarketKeys: PublicKey[],
priceCurrencyKey: PublicKey,
optionMetaList?: {
expiration: number;
optionMarketAddress: string;
optionContractMintAddress: string;
optionWriterTokenMintAddress: string;
quoteAssetMint: string;
quoteAssetPoolAddress: string;
underlyingAssetMint: string;
underlyingAssetPoolAddress: string;
underlyingAssetPerContract: string;
quoteAssetPerContract: string;
serumMarketAddress: string;
serumProgramId: string;
psyOptionsProgramId: string;
}[]
) => {
const openOrdersKeys = await Promise.all(
optionMarketKeys.map(async (optionMarketKey) => {
// TODO check if option market key exists on market meta
let serumMarketKey: PublicKey;
if (optionMetaList) {
// default to the serum address in the provided list of options.
// This is necessary to keep backwards compatibility for option
// markets in 2021.
const serumMarketAddress = optionMetaList.find(
(option) => option.optionMarketAddress === optionMarketKey.toString()
)?.serumMarketAddress;
if (serumMarketAddress) {
serumMarketKey = new PublicKey(serumMarketAddress);
}
}
if (!serumMarketKey) {
// Derive the serum market address from the OptionMarket key
const [_serumMarketKey, _serumMarketBump] =
await deriveSerumMarketAddress(
program,
optionMarketKey,
priceCurrencyKey
);
serumMarketKey = _serumMarketKey;
}
// Derive the user's OpenOrders account address from the Serum market data
const [openOrdersAddressKey, openOrdersBump] =
await PublicKey.findProgramAddress(
[
textEncoder.encode("open-orders"),
serumProgramId.toBuffer(),
serumMarketKey.toBuffer(),
program.provider.wallet.publicKey.toBuffer(),
],
program.programId
);
return openOrdersAddressKey;
})
);
// Batch load the raw OpenOrders data
const groupOfOpenOrdersKeys: PublicKey[][] = chunkArray(openOrdersKeys, 100);
const getMultipleAccountsForOpenOrdersKeys: Promise<AccountInfo<Buffer>[]>[] =
groupOfOpenOrdersKeys.map((openOrdersKeys) => {
return program.provider.connection.getMultipleAccountsInfo(
openOrdersKeys
);
});
const results = await Promise.all(getMultipleAccountsForOpenOrdersKeys);
const openOrdersInfos: AccountInfo<Buffer>[] = results.flat();
const openOrdersByOptionMarket: Record<string, OpenOrders> = {};
// Deserialize the OpenOrders info and store mapped by Option key
openOrdersInfos.forEach((info, index) => {
if (!info) return;
openOrdersByOptionMarket[optionMarketKeys[index].toString()] =
new OpenOrders(
openOrdersKeys[index],
OpenOrders.getLayout(serumProgramId).decode(info.data),
serumProgramId
);
});
return openOrdersByOptionMarket;
};
export const deriveSerumMarketAddress = async (
program: Program<PsyAmerican>,
optionMarketKey: PublicKey,
priceCurrencyKey: PublicKey
) => {
return PublicKey.findProgramAddress(
[
optionMarketKey.toBuffer(),
priceCurrencyKey.toBuffer(),
textEncoder.encode("serumMarket"),
],
program.programId
);
};
export const deriveMarketAuthority = async (
program: Program<PsyAmerican>,
dexProgramId: PublicKey,
serumMarketKey: PublicKey
) =>
PublicKey.findProgramAddress(
[OPEN_ORDERS_INIT_SEED, dexProgramId.toBuffer(), serumMarketKey.toBuffer()],
program.programId
);
export const deriveRequestQueue = (
program: Program<PsyAmerican>,
optionMarketKey: PublicKey,
priceCurrencyKey: PublicKey
) =>
PublicKey.findProgramAddress(
[
optionMarketKey.toBuffer(),
priceCurrencyKey.toBuffer(),
textEncoder.encode("requestQueue"),
],
program.programId
);
export const deriveCoinVault = (
program: Program<PsyAmerican>,
optionMarketKey: PublicKey,
priceCurrencyKey: PublicKey
) =>
PublicKey.findProgramAddress(
[
optionMarketKey.toBuffer(),
priceCurrencyKey.toBuffer(),
textEncoder.encode("coinVault"),
],
program.programId
);
export const derivePCVault = (
program: Program<PsyAmerican>,
optionMarketKey: PublicKey,
priceCurrencyKey: PublicKey
) =>
PublicKey.findProgramAddress(
[
optionMarketKey.toBuffer(),
priceCurrencyKey.toBuffer(),
textEncoder.encode("pcVault"),
],
program.programId
);
/**
* Given an OptionMarket address and DEX program, generate the Serum market key,
* market authority, and authority bump seed.
*
* @param {Program} program - PsyOptions American V1 Anchor program
* @param {PublicKey} optionMarketKey - The key for the OptionMarket
* @param {PublicKey} dexProgramId - Serum DEX public key
* @returns
*/
export const getMarketAndAuthorityInfo = async (
program: Program<PsyAmerican>,
optionMarketKey: PublicKey,
dexProgramId: PublicKey,
priceCurrencyKey: PublicKey
): Promise<{
serumMarketKey: PublicKey;
marketAuthority: PublicKey;
marketAuthorityBump: number;
}> => {
const [serumMarketKey, _serumMarketBump] = await deriveSerumMarketAddress(
program,
optionMarketKey,
priceCurrencyKey
);
const [marketAuthority, marketAuthorityBump] = await deriveMarketAuthority(
program,
dexProgramId,
serumMarketKey
);
return { serumMarketKey, marketAuthority, marketAuthorityBump };
};