forked from s-a-y/simple-trader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
149 lines (132 loc) · 5.24 KB
/
index.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
import {Asset, Keypair, Networks, Operation, Server, TransactionBuilder} from 'stellar-sdk';
import fetch from 'node-fetch';
import { readFileSync } from 'fs';
const server = new Server('https://horizon.stellar.org');
const rebalanceAutomatically = true;
const markets = JSON.parse(readFileSync('./config.json').toString());
async function reset() {
for (const market of markets) {
if (!market.secret) {
console.log('Please put Stellar account secret into config')
continue;
}
const keypair = Keypair.fromSecret(market.secret);
const ratePromise = fetch('https://rates.apay.io')
.then((response) => {
return response.json();
})
.then((result) => {
if (!result.USD) {
throw new Error('rates are not available yet');
}
return parseFloat(result[market.base.rate]) / parseFloat(result[market.quote.rate]);
});
const baseAsset = new Asset(market.base.code, market.base.issuer);
const quoteAsset = new Asset(market.quote.code, market.quote.issuer)
const accountPromise = server.loadAccount(keypair.publicKey());
const offersPromise = server.offers().forAccount(keypair.publicKey()).call()
.then((result) => {
return result.records.map((v) => {
return {
offerId: v.id,
account: v.seller,
sellingAssetType: v.selling.asset_type,
sellingAssetCode: v.selling.asset_code,
sellingAssetIssuer: v.selling.asset_issuer,
buyingAssetType: v.buying.asset_type,
buyingAssetCode: v.buying.asset_code,
buyingAssetIssuer: v.buying.asset_issuer,
amount: v.amount,
price: v.price
}
});
});
Promise.all([ratePromise, accountPromise, offersPromise])
.then(([rate, account, offers]) => {
const baseAssetBalance = getAssetBalance(account, baseAsset);
const quoteAssetBalance = getAssetBalance(account, quoteAsset);
const ratio = rebalanceAutomatically ? quoteAssetBalance / (quoteAssetBalance + baseAssetBalance / rate) : 0.5;
console.log(rate, baseAsset.code, '/', quoteAsset.code, quoteAssetBalance, baseAssetBalance / rate, ratio);
const txBuilder = new TransactionBuilder(account, {
fee: '200',
networkPassphrase: Networks.PUBLIC,
});
offers.forEach((offer) => {
txBuilder.addOperation(removeOffer(offer, keypair.publicKey()));
});
if (quoteAssetBalance) {
let sum = 0;
market.levels.forEach((offset) => {
const amount = (Math.min(quoteAssetBalance / market.levels.length, quoteAssetBalance - sum)).toFixed(7);
if (parseFloat(amount) > 0) {
txBuilder
.addOperation(Operation.manageSellOffer({
selling: quoteAsset,
buying: baseAsset,
amount: amount,
price: (rate * (1 + offset + (0.5 - ratio) * market.levels[0])).toFixed(7),
}));
// console.log((rate * (1 + offset + (0.5 - ratio) * levels[0])).toFixed(7));
sum += parseFloat(amount);
}
});
}
if (baseAssetBalance) {
let sum = 0;
market.levels.forEach((offset) => {
const amount = (Math.min(baseAssetBalance / market.levels.length, baseAssetBalance - sum)).toFixed(7);
if (parseFloat(amount) > 0) {
txBuilder
.addOperation(Operation.manageSellOffer({
selling: baseAsset,
buying: quoteAsset,
amount: amount,
price: (1 / rate / (1 - offset - (0.5 - ratio) * market.levels[0])).toFixed(7),
}));
// console.log((rate * (1 - offset - (0.5 - ratio) * levels[0])).toFixed(7))
sum += parseFloat(amount);
}
});
}
const tx = txBuilder.setTimeout(30).build();
tx.sign(keypair);
// console.log(tx.toEnvelope().toXDR().toString('base64'));
return server.submitTransaction(tx);
})
.then(() => {
console.log('sent tx successfully');
})
.catch((err) => {
console.error(err.message, err.response && err.response.data || err);
});
}
}
function getXLMBalance(account) {
return Math.max(0, parseFloat(account.balances.find((balance) => {
return balance.asset_type === 'native';
}).balance) - 10);
}
function getAssetBalance(account, asset) {
if (!asset.issuer) {
return getXLMBalance(account);
}
const balance = account.balances.find((balance) => {
return balance.asset_code === asset.getCode() && balance.asset_issuer === asset.getIssuer();
});
return balance ? parseFloat(balance.balance) : 0;
}
function removeOffer(offer, source) {
return Operation.manageSellOffer({
selling: offer.sellingAssetType === 'native' ? Asset.native() : (
new Asset(offer.sellingAssetCode, offer.sellingAssetIssuer)
),
buying: offer.buyingAssetType === 'native' ? Asset.native() : (
new Asset(offer.buyingAssetCode, offer.buyingAssetIssuer)
),
amount: '0.0000000',
price: offer.price,
offerId: offer.offerId,
})
}
setInterval(reset, 30000 + Math.random() * 60000);
reset();