-
Notifications
You must be signed in to change notification settings - Fork 1
/
handler.js
73 lines (67 loc) · 2.38 KB
/
handler.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
const AWS = require('aws-sdk');
const Kraken = require('kraken-api');
const s3 = new AWS.S3();
const timestamp = () => new Date().toISOString();
// set an higher timeout
const client = new Kraken(process.env.KRAKEN_KEY, process.env.KRAKEN_SECRET, {
timeout: 60 * 60 * 48 * 1000
});
const investmentAmount = process.env.INVESTMENT_AMOUNT;
// see full list of exhange pairs here
// https://api.kraken.com/0/public/AssetPairs
const pair = (process.env.ASSETS_PAIR || 'XXBTZEUR').toUpperCase();
const cryptoCurrency = pair.split('X')[1].slice(0, 3);
const fiatCurrency = pair.split('Z')[1].slice(0, 3);
const logging = async log => {
try {
const data = await s3.getObject({
Bucket: process.env.BUCKET,
Key: process.env.LOG_FILE
}).promise();
const body = data ? data.Body.toString('ascii') + '\n' : '';
await s3.putObject({
Bucket: process.env.BUCKET,
Key: process.env.LOG_FILE,
Body: body + log
}).promise();
} catch (e) {
console.error(e);
}
};
export const start = async (event, context, callback) => {
try {
// Retrieve crypto/eur price
const tickResponse = await client.api('Ticker', {pair});
const cryptoPrice = tickResponse['result'][pair]['a'][0];
if (typeof cryptoPrice === 'undefined') {
callback(null, { error: 'cryptoCurrency undefined' })
return;
}
const volumeToBuy = (investmentAmount/cryptoPrice).toFixed(6);
const roundedInvestmentAmount = (volumeToBuy*cryptoPrice).toFixed(3);
// Kraken does not allow to buy less than 0.002XBT
if (volumeToBuy < 0.002) {
callback(null, { error: 'Increase your investmentAmount' })
}
const logMessage = `[${timestamp()}] Buying ${volumeToBuy} ${cryptoCurrency}` +
` which is equal to ${roundedInvestmentAmount} ${fiatCurrency}` +
` at price ${cryptoPrice} ${fiatCurrency}/${cryptoCurrency}\n`;
// Log prices to file
await logging(logMessage);
// buy disposed amount for today
const tradeResponse = await client.api('AddOrder', {
pair,
volume: volumeToBuy,
type: 'buy',
ordertype: 'market'
});
// Retrieve and log transaction ids
const txIds = tradeResponse['result']['txid'];
if (typeof txIds === 'undefined') {
callback(null, { error: 'Unable to read transaction ids' });
}
callback(null, { message: txIds, event });
} catch (e) {
callback(null, { error: e, event });
}
}