Skip to content

Commit eadbedc

Browse files
Example cross market maker + byproduct utility functions (#107)
* First commit * Formatting, move pkg to dev. deps * Accurately calculate base perp position on fill * Act on Max's suggestions * Risk checker adjustments * Misc * Delete _.ts * Rename xmm.ts to serumExampleHedge.ts * Update serumExampleHedge.ts Co-authored-by: Maximilian Schneider <[email protected]>
1 parent 3b5834f commit eadbedc

File tree

4 files changed

+920
-5
lines changed

4 files changed

+920
-5
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@
6363
"cross-env": "^7.0.2",
6464
"eslint": "^7.28.0",
6565
"eslint-config-prettier": "^7.2.0",
66+
"mango_risk_check": "^1.2.1",
6667
"mocha": "9",
6768
"prettier": "^2.0.5",
6869
"ts-node": "^9.1.1",

src/client.ts

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5491,4 +5491,62 @@ export class MangoClient {
54915491

54925492
return await this.sendTransaction(transaction, owner, []);
54935493
}
5494+
5495+
async ensureOpenOrdersAccount(
5496+
mangoAccount: MangoAccount,
5497+
mangoGroup: MangoGroup,
5498+
payer: Keypair,
5499+
spotMarket: Market,
5500+
spotMarketConfig,
5501+
) {
5502+
if (mangoAccount.spotOpenOrdersAccounts[spotMarketConfig.marketIndex]) {
5503+
return;
5504+
}
5505+
5506+
const [openOrdersPk] = await PublicKey.findProgramAddress(
5507+
[
5508+
mangoAccount.publicKey.toBytes(),
5509+
new BN(spotMarketConfig.marketIndex).toArrayLike(Buffer, 'le', 8),
5510+
new Buffer('OpenOrders', 'utf-8'),
5511+
],
5512+
this.programId,
5513+
);
5514+
5515+
const createSpotOpenOrdersInstruction = makeCreateSpotOpenOrdersInstruction(
5516+
this.programId,
5517+
mangoGroup.publicKey,
5518+
mangoAccount.publicKey,
5519+
payer.publicKey,
5520+
mangoGroup.dexProgramId,
5521+
openOrdersPk,
5522+
spotMarket.publicKey,
5523+
mangoGroup.signerKey,
5524+
);
5525+
5526+
const recentBlockHash = await this.connection.getLatestBlockhash(
5527+
'finalized',
5528+
);
5529+
5530+
const tx = new Transaction({
5531+
recentBlockhash: recentBlockHash.blockhash,
5532+
feePayer: payer.publicKey,
5533+
});
5534+
5535+
tx.add(createSpotOpenOrdersInstruction);
5536+
5537+
tx.sign(payer);
5538+
5539+
try {
5540+
await this.sendSignedTransaction({
5541+
signedTransaction: tx,
5542+
signedAtBlock: recentBlockHash,
5543+
});
5544+
} catch (e) {
5545+
console.error(e);
5546+
}
5547+
5548+
await mangoAccount.reload(this.connection, mangoGroup.dexProgramId);
5549+
// ^ The newly created open orders account isn't immediately visible in
5550+
// the already fetched Mango account, hence why it needs to be reloaded
5551+
}
54945552
}

0 commit comments

Comments
 (0)