This repository has been archived by the owner on Oct 8, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
004-bot.ts
51 lines (50 loc) · 1.51 KB
/
004-bot.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
import {
getBalance,
getPrice,
swapInToken,
openPosition,
getPositions,
closePosition,
Position,
} from "./999-functions";
import { targetToken, usdc } from "./000-config";
import { predictPriceRange } from "./003-predict-from-api";
export async function bot() {
let positions: Position[] = await getPositions();
if (positions.length > 0) {
for (let position of positions) {
if (
position.tokenA === targetToken.mint.toBase58() &&
position.tokenB === usdc.mint.toBase58()
) {
console.log("close position", position.positionAddress);
await closePosition(position.positionAddress);
}
}
}
let price = await getPrice();
let balance = await getBalance();
const targetValue = balance.target.mul(price);
const usdcValue = balance.usdc;
const diff = targetValue.sub(usdcValue);
if (diff.lt(balance.usdc.mul(0.01))) {
console.log("skip swap");
} else {
console.log("swap");
if (diff.gt(0)) {
await swapInToken(targetToken, diff.div(price).div(2));
} else {
await swapInToken(usdc, diff.div(2));
}
price = await getPrice();
balance = await getBalance();
}
console.log("predict price range");
let predictedPriceRange = await predictPriceRange();
const targetDepositValue = balance.target.mul(0.9); // 全額だと足りない場合があるので9割で流動性提供する
await openPosition(
price.minus(predictedPriceRange * 0.5),
price.add(predictedPriceRange * 0.5),
targetDepositValue
);
}