-
Notifications
You must be signed in to change notification settings - Fork 4
/
handler.ts
167 lines (146 loc) · 4.92 KB
/
handler.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
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/** Disable to allow printing of error messages on arbitrarily thrown objects. */
/* eslint-disable @typescript-eslint/no-unsafe-member-access */
import { APIGatewayProxyHandler } from 'aws-lambda'
import OracleService from './src/oracle-service'
import { initOracleLib } from '@tacoinfra/harbinger-lib'
import AwsSigner from './src/aws-signer'
import HttpResponseCode from './src/http-response-code'
import BinanceCandleProvider from './src/binance-candle-provider'
import CoinbaseCandleProvider from './src/coinbase-candle-provider'
import GeminiCandleProvider from './src/gemini-candle-provider'
import OkexCandleProvider from './src/okex-candle-provider'
/** Handler for the Oracle Feed endpoint. */
export const oracle: APIGatewayProxyHandler = async (_event, _context) => {
try {
const oracleService = await getOracleService()
const body = await oracleService.oracle()
return {
statusCode: HttpResponseCode.ok,
body: JSON.stringify(body),
}
} catch (exception) {
console.log(exception.message)
console.log(exception.stack)
return {
statusCode: HttpResponseCode.serverError,
body: `Error: ${JSON.stringify(exception.message)}`,
}
}
}
/** Handler for the Revoke endpoint. */
export const revoke: APIGatewayProxyHandler = async (_event, _context) => {
try {
const oracleService = await getOracleService()
const body = await oracleService.revoke()
return {
statusCode: HttpResponseCode.ok,
body: body,
}
} catch (exception) {
console.log(exception.message)
console.log(exception.stack)
return {
statusCode: HttpResponseCode.serverError,
body: `Error: ${JSON.stringify(exception.message)}`,
}
}
}
/** Handler for the Info endpoint. */
export const info: APIGatewayProxyHandler = async (_event, _context) => {
try {
const oracleService = await getOracleService()
const body = await oracleService.info()
return {
statusCode: HttpResponseCode.ok,
body: JSON.stringify(body),
}
} catch (exception) {
console.log(exception.message)
console.log(exception.stack)
return {
statusCode: HttpResponseCode.serverError,
body: `Error: ${JSON.stringify(exception.message)}`,
}
}
}
/**
* Helper function to retrieve an {@link OracleService}.
*/
const getOracleService = async () => {
// Validate asset lists.
const assetList = process.env.ASSETS
if (assetList == undefined) {
throw new Error('No asset list defined. Please check your configuration')
}
const assets = assetList.split(',').sort()
// Initialize OracleLib.
initOracleLib()
const candleProvider = getCandleProvider()
const signer = await getSigner()
return new OracleService(assets, candleProvider, signer)
}
/**
* Helper function to validate inputs and create a {@link Signer}.
*/
const getSigner = () => {
const awsKmsKeyId = process.env.AWS_KMS_KEY_ID
const awsKmsKeyRegion = process.env.AWS_KMS_KEY_REGION
if (awsKmsKeyId === undefined || awsKmsKeyRegion === undefined) {
throw new Error(
'Fatal: Missing an input to create Signer. Please check your configuration.',
)
}
return AwsSigner.from(awsKmsKeyId, awsKmsKeyRegion)
}
/** Helper function to validate inputs and create a {@link CandleProvider}. */
const getCandleProvider = () => {
// Provide a candle provider based on the value of the CANDLE_PROVIDER env var.
const candleProvider = process.env.CANDLE_PROVIDER
if (candleProvider === 'COINBASE') {
return getCoinbaseCandleProvider()
} else if (candleProvider === 'BINANCE') {
return getBinanceCandleProvider()
} else if (candleProvider === 'GEMINI') {
return getGeminiCandleProvider()
} else if (candleProvider === 'OKEX') {
return getOkexCandleProvider()
} else {
throw new Error(
`Unknown CANDLE_PROVIDER passed in env var: ${JSON.stringify(
candleProvider,
)}`,
)
}
}
/** Helper function to return a `CoinbaseCandleProvider` */
const getCoinbaseCandleProvider = () => {
const coinbaseApiKeyId = process.env.COINBASE_API_KEY_ID
const coinbaseApiKeySecret = process.env.COINBASE_API_KEY_SECRET
const coinbaseApiKeyPassphrase = process.env.COINBASE_API_KEY_PASSPHRASE
if (
coinbaseApiKeyId === undefined ||
coinbaseApiKeySecret === undefined ||
coinbaseApiKeyPassphrase === undefined
) {
throw new Error(
'Fatal: Missing an input to create CandleProvider. Please check your configuration.',
)
}
return new CoinbaseCandleProvider(
coinbaseApiKeyId,
coinbaseApiKeySecret,
coinbaseApiKeyPassphrase,
)
}
/** Helper function to return a `BinanceCandleProvider` */
const getBinanceCandleProvider = () => {
return new BinanceCandleProvider()
}
/** Helper function to return a `GeminiCandleProvider` */
const getGeminiCandleProvider = () => {
return new GeminiCandleProvider()
}
/** Helper function to return an `OkexCandleProvider` */
const getOkexCandleProvider = () => {
return new OkexCandleProvider()
}