-
Notifications
You must be signed in to change notification settings - Fork 3
/
example1.js
89 lines (70 loc) · 2.96 KB
/
example1.js
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
// basic example of a looping bot
// EXAMPLE ONLY, DO NOT USE
asdasdasd
import bos from './bos.js' // my wrappers for bos in bos.js file
// max and min adjusting fee to use (ppm)
const MAX_FEE_RATE = 1337
const MIN_FEE_RATE = 1
// max fee rate (ppm) and minutes to use for rebalancing
const MAX_REBALANCE_FEE_RATE = 200
const MAX_REBALANCE_MINUTES = 5
// how often to repeat
const MINUTES_BETWEEN_RUNS = 60
// waste time for set number of minutes
const sleep = async minutes => await new Promise(r => setTimeout(r, Math.trunc(minutes * 60 * 1000)))
// this function will loop itself forever
const bot = async () => {
// get peers
const allPeers = await bos.peers({active: true, public: true})
// let's add some more properties for each peer
for (const peer of allPeers) {
peer.localSats = peer.outbound_liquidity || 0
peer.remoteSats = peer.inbound_liquidity || 0
peer.totalSats = peer.localSats + peer.remoteSats
peer.balance = peer.localSats / peer.totalSats
peer.satsOffBalance = Math.abs(peer.localSats - peer.remoteSats) * 0.5
}
// for each peer set fee
for (const peer of allPeers) {
const public_key = peer.public_key
// grab properties calculated earlier
const balance = peer.balance
// calculate fee, smallest at balance 1 and largest at balance 0
const proportionalFee = Math.trunc(MIN_FEE_RATE * balance + MAX_FEE_RATE * (1 - balance))
// set the fee
const finalFee = await bos.setFees(public_key, proportionalFee)
console.log(`${peer.alias} fee rate was set to ${finalFee} ppm`)
}
// split peers by remote heavy and local heavy
const localHeavyPeers = allPeers.filter(peer => peer.balance > 0.75)
const remoteHeavyPeers = allPeers.filter(peer => peer.balance < 0.25)
// if there's at least one of each
if (localHeavyPeers.length > 0 && remoteHeavyPeers.length > 0) {
// pick random peer from both
const randomLocalIndex = Math.trunc(Math.random() * localHeavyPeers.length)
const randomRemoteIndex = Math.trunc(Math.random() * remoteHeavyPeers.length)
const localHeavy = localHeavyPeers[randomLocalIndex]
const remoteHeavy = remoteHeavyPeers[randomRemoteIndex]
// max amount to rebalance is the smaller sats off-balance between the two
const maxSatsToRebalance = Math.min(localHeavy.satsOffBalance, remoteHeavy.satsOffBalance)
// try to rebalance them
console.log(`Trying to rebalance sats from ${localHeavy.alias} to ${remoteHeavy.alias}`)
const result = await bos.rebalance({
fromChannel: localHeavy.public_key,
toChannel: remoteHeavy.public_key,
maxSats: maxSatsToRebalance,
maxMinutes: MAX_REBALANCE_MINUTES,
maxFeeRate: MAX_REBALANCE_FEE_RATE
})
console.log(result)
}
// try reconnecting to disconnected peers
await bos.reconnect(true)
// wait 60 minutes
console.log('sleep time (minutes):', MINUTES_BETWEEN_RUNS)
await sleep(MINUTES_BETWEEN_RUNS)
// start this function from start again
bot()
}
// starts the loop
bot()