Skip to content

Commit

Permalink
Example cross market maker + byproduct utility functions (#107)
Browse files Browse the repository at this point in the history
* 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 <mail@maximilianschneider.net>
  • Loading branch information
waterquarks and mschneider authored Jul 25, 2022
1 parent 3b5834f commit eadbedc
Show file tree
Hide file tree
Showing 4 changed files with 920 additions and 5 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
"cross-env": "^7.0.2",
"eslint": "^7.28.0",
"eslint-config-prettier": "^7.2.0",
"mango_risk_check": "^1.2.1",
"mocha": "9",
"prettier": "^2.0.5",
"ts-node": "^9.1.1",
Expand Down
58 changes: 58 additions & 0 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5491,4 +5491,62 @@ export class MangoClient {

return await this.sendTransaction(transaction, owner, []);
}

async ensureOpenOrdersAccount(
mangoAccount: MangoAccount,
mangoGroup: MangoGroup,
payer: Keypair,
spotMarket: Market,
spotMarketConfig,
) {
if (mangoAccount.spotOpenOrdersAccounts[spotMarketConfig.marketIndex]) {
return;
}

const [openOrdersPk] = await PublicKey.findProgramAddress(
[
mangoAccount.publicKey.toBytes(),
new BN(spotMarketConfig.marketIndex).toArrayLike(Buffer, 'le', 8),
new Buffer('OpenOrders', 'utf-8'),
],
this.programId,
);

const createSpotOpenOrdersInstruction = makeCreateSpotOpenOrdersInstruction(
this.programId,
mangoGroup.publicKey,
mangoAccount.publicKey,
payer.publicKey,
mangoGroup.dexProgramId,
openOrdersPk,
spotMarket.publicKey,
mangoGroup.signerKey,
);

const recentBlockHash = await this.connection.getLatestBlockhash(
'finalized',
);

const tx = new Transaction({
recentBlockhash: recentBlockHash.blockhash,
feePayer: payer.publicKey,
});

tx.add(createSpotOpenOrdersInstruction);

tx.sign(payer);

try {
await this.sendSignedTransaction({
signedTransaction: tx,
signedAtBlock: recentBlockHash,
});
} catch (e) {
console.error(e);
}

await mangoAccount.reload(this.connection, mangoGroup.dexProgramId);
// ^ The newly created open orders account isn't immediately visible in
// the already fetched Mango account, hence why it needs to be reloaded
}
}
Loading

0 comments on commit eadbedc

Please sign in to comment.