-
Notifications
You must be signed in to change notification settings - Fork 0
/
crank-exchange.ts
102 lines (92 loc) · 2.54 KB
/
crank-exchange.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
import {
Exchange,
utils,
constants,
types,
assets,
Network,
} from "@zetamarkets/sdk";
import { Connection, Keypair, Transaction } from "@solana/web3.js";
import { Wallet } from "@project-serum/anchor";
import { _OPEN_ORDERS_LAYOUT_V2 } from "@zetamarkets/sdk/dist/serum/market";
// Adds timestamp to the start of each log, including modules
require("log-timestamp")(function () {
return `[${new Date().toUTCString()}]`;
});
process.on("uncaughtException", (error) => {
console.error("Uncaught Exception:", error);
});
process.on("unhandledRejection", (reason, promise) => {
console.error("Unhandled Rejection at:", promise, "reason:", reason);
});
async function main() {
// Enter RPC.
let endpoint = "";
let connection = new Connection(endpoint, {
commitment: "processed",
disableRetryOnRateLimit: true,
confirmTransactionInitialTimeout: 1000,
});
// Enter secret key buffer.
let privateKey = [];
let wallet: Wallet = new Wallet(
Keypair.fromSecretKey(Buffer.from(privateKey))
);
let openOrdersToMarginAccount = new Map();
const LOAD_CONFIG: types.LoadExchangeConfig = {
network: Network.MAINNET,
connection,
opts: utils.commitmentConfig("processed"),
throttleMs: 0,
loadFromStore: true,
TIFBufferSeconds: 0,
orderbookAssetSubscriptionOverride: [],
};
await Exchange.load(LOAD_CONFIG, wallet);
let assets = [constants.Asset.SOL, constants.Asset.BTC];
Exchange.updatePriorityFee(1000);
// Log event queue length.
setInterval(
async function () {
await Promise.all(
assets.map(async (asset) => {
let eventQueue = await Exchange.getPerpMarket(
asset
).serumMarket.loadEventQueue(Exchange.connection);
console.log(`${asset} EventQueue Length = ${eventQueue.length}`);
})
);
}.bind(this),
10_000
);
while (true) {
// Pass in map for caching.
await Promise.all(
assets.map(async (asset) => {
let ix = await utils.createCrankMarketIx(
asset,
openOrdersToMarginAccount
);
if (ix == null) return;
let tx = new Transaction().add(ix);
console.log(`Cranking exchange for asset ${asset}.`);
try {
await utils.processTransaction(
Exchange.provider,
tx,
undefined,
undefined,
false,
undefined,
undefined,
undefined,
true
);
} catch (e) {
console.log(e);
}
})
);
}
}
main();