Skip to content

Commit d6a776b

Browse files
authored
Merge pull request #11495 from ethereum/remove-lambdas
Remove old lambda functions adapters
2 parents ba7a5c3 + 546dffe commit d6a776b

15 files changed

+167
-358
lines changed

src/api/calendarEvents.ts

Lines changed: 45 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,54 @@
1+
import axios from "axios"
12
import type { GatsbyFunctionRequest, GatsbyFunctionResponse } from "gatsby"
2-
import { lambda } from "../lambda/calendarEvents"
33

44
async function handler(
55
__req: GatsbyFunctionRequest,
66
res: GatsbyFunctionResponse
77
): Promise<void> {
8-
// passing env vars as arguments due to a bug on GC functions where env vars
9-
// can not be accessed by imported functions
10-
const { statusCode, body } = await lambda(
11-
process.env.GOOGLE_API_KEY!,
12-
process.env.GOOGLE_CALENDAR_ID!
13-
)
14-
res.status(statusCode).send(body)
8+
const apiKey = process.env.GOOGLE_API_KEY
9+
const calendarId = process.env.GOOGLE_CALENDAR_ID
10+
11+
try {
12+
const futureEventsReq = await axios.get(
13+
`https://content.googleapis.com/calendar/v3/calendars/${calendarId}/events`,
14+
{
15+
params: {
16+
key: apiKey,
17+
timeMin: new Date().toISOString(),
18+
maxResults: 3,
19+
singleEvents: true,
20+
orderBy: "startTime",
21+
},
22+
}
23+
)
24+
25+
const pastEventsReq = await axios.get(
26+
`https://content.googleapis.com/calendar/v3/calendars/${calendarId}/events`,
27+
{
28+
params: {
29+
key: apiKey,
30+
timeMax: new Date().toISOString(),
31+
maxResults: 4,
32+
singleEvents: true,
33+
orderBy: "startTime",
34+
},
35+
}
36+
)
37+
38+
const response = {
39+
pastEvents: pastEventsReq.data.items,
40+
futureEvents: futureEventsReq.data.items,
41+
}
42+
43+
res.status(200).send(JSON.stringify(response))
44+
} catch (error) {
45+
console.error(error)
46+
res.status(500).send(
47+
JSON.stringify({
48+
msg: "Something went wrong with requesting the calendar events data.",
49+
})
50+
)
51+
}
1552
}
1653

1754
export default handler

src/api/defipulse.ts

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,37 @@
1+
import axios from "axios"
2+
import takeRightWhile from "lodash/takeRightWhile"
13
import type { GatsbyFunctionRequest, GatsbyFunctionResponse } from "gatsby"
24

3-
import { lambda } from "../lambda/defipulse"
4-
55
async function handler(
66
__req: GatsbyFunctionRequest,
77
res: GatsbyFunctionResponse
88
): Promise<void> {
9-
// passing env vars as arguments due to a bug on GC functions where env vars
10-
// can not be accessed by imported functions
11-
const { statusCode, body } = await lambda()
12-
res.status(statusCode).send(body)
9+
try {
10+
const response = await axios.get(`https://api.llama.fi/charts/Ethereum`)
11+
if (response.status < 200 || response.status >= 300) {
12+
return res
13+
.status(response.status)
14+
.send(JSON.stringify(response.statusText))
15+
}
16+
17+
const { data } = response
18+
19+
// get only the last 90 days
20+
const daysToFetch = 90
21+
const now = new Date()
22+
const startDate = new Date(now.setDate(now.getDate() - daysToFetch))
23+
const startTimestamp = Math.round(startDate.getTime() / 1000)
24+
25+
const trimmedData = takeRightWhile(
26+
data,
27+
({ date }) => Number(date) > startTimestamp
28+
)
29+
30+
res.status(200).send(JSON.stringify(trimmedData))
31+
} catch (error) {
32+
console.error(error)
33+
res.status(500).send(JSON.stringify({ msg: (error as Error)?.message }))
34+
}
1335
}
1436

1537
export default handler

src/api/etherscan.ts

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,33 @@
1+
import axios from "axios"
12
import { GatsbyFunctionRequest, GatsbyFunctionResponse } from "gatsby"
23

3-
import { lambda } from "../lambda/etherscan"
4-
54
async function handler(
65
__req: GatsbyFunctionRequest,
76
res: GatsbyFunctionResponse
87
) {
9-
// passing env vars as arguments due to a bug on GC functions where env vars
10-
// can not be accessed by imported functions
11-
const { statusCode, body } = await lambda(process.env.ETHERSCAN_API_KEY)
12-
res.status(statusCode).send(body)
8+
const apiKey = process.env.ETHERSCAN_API_KEY
9+
10+
const daysToFetch = 90
11+
const now = new Date()
12+
const endDate = now.toISOString().split("T")[0] // YYYY-MM-DD
13+
const startDate = new Date(now.setDate(now.getDate() - daysToFetch))
14+
.toISOString()
15+
.split("T")[0] // {daysToFetch} days ago
16+
try {
17+
const response = await axios.get(
18+
`https://api.etherscan.io/api?module=stats&action=nodecounthistory&startdate=${startDate}&enddate=${endDate}&sort=desc&apikey=${apiKey}`
19+
)
20+
if (response.status < 200 || response.status >= 300) {
21+
return res
22+
.status(response.status)
23+
.send(JSON.stringify(response.statusText))
24+
}
25+
const { data } = response
26+
res.status(200).send(JSON.stringify(data))
27+
} catch (error) {
28+
console.error(error)
29+
res.status(500).send(JSON.stringify({ msg: (error as Error).message }))
30+
}
1331
}
1432

1533
export default handler

src/api/etherscanBlock.ts

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,26 @@
1+
import axios from "axios"
12
import { GatsbyFunctionRequest, GatsbyFunctionResponse } from "gatsby"
23

3-
import { lambda } from "../lambda/etherscanBlock"
4-
54
async function handler(
65
__req: GatsbyFunctionRequest,
76
res: GatsbyFunctionResponse
87
) {
9-
// passing env vars as arguments due to a bug on GC functions where env vars
10-
// can not be accessed by imported functions
11-
const { statusCode, body } = await lambda(process.env.ETHERSCAN_API_KEY)
12-
res.status(statusCode).send(body)
8+
const apiKey = process.env.ETHERSCAN_API_KEY
9+
10+
try {
11+
const response = await axios.get(
12+
`https://api.etherscan.io/api?module=block&action=getblockcountdown&blockno=12965000&apikey=${apiKey}`
13+
)
14+
if (response.status < 200 || response.status >= 300) {
15+
return res.status(response.status).send(response.statusText)
16+
}
17+
18+
const { data } = response
19+
res.status(200).send(JSON.stringify(data.result.EstimateTimeInSec || 0))
20+
} catch (error) {
21+
console.error(error)
22+
res.status(500).send(JSON.stringify({ msg: (error as Error).message }))
23+
}
1324
}
1425

1526
export default handler

src/api/l2beat.ts

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,22 @@
1+
import axios from "axios"
12
import { GatsbyFunctionRequest, GatsbyFunctionResponse } from "gatsby"
23

3-
import { lambda } from "../lambda/l2beat"
4-
54
async function handler(
65
__req: GatsbyFunctionRequest,
76
res: GatsbyFunctionResponse
87
) {
9-
// passing env vars as arguments due to a bug on GC functions where env vars
10-
// can not be accessed by imported functions
11-
const { statusCode, body } = await lambda()
12-
res.status(statusCode).send(body)
8+
try {
9+
const response = await axios.get(`https://api.l2beat.com/api/tvl`)
10+
if (response.status < 200 || response.status >= 300) {
11+
return res.status(response.status).send(response.statusText)
12+
}
13+
14+
const { data } = response
15+
res.status(200).send(JSON.stringify(data))
16+
} catch (error) {
17+
console.error(error)
18+
res.status(500).send(JSON.stringify({ msg: (error as Error).message }))
19+
}
1320
}
1421

1522
export default handler

src/api/translations.ts

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,27 @@
1+
import axios from "axios"
12
import { GatsbyFunctionRequest, GatsbyFunctionResponse } from "gatsby"
23

3-
import { lambda } from "../lambda/translations"
4-
54
async function handler(
65
__req: GatsbyFunctionRequest,
76
res: GatsbyFunctionResponse
87
) {
9-
// passing env vars as arguments due to a bug on GC functions where env vars
10-
// can not be accessed by imported functions
11-
const { statusCode, body } = await lambda(process.env.CROWDIN_API_KEY)
12-
res.status(statusCode).send(body)
8+
const apiKey = process.env.CROWDIN_API_KEY
9+
10+
try {
11+
const baseURL = "https://api.crowdin.com/api/project/ethereum-org/status"
12+
13+
const resp = await axios.get(`${baseURL}?key=${apiKey}&json`)
14+
15+
if (resp.status < 200 || resp.status >= 300) {
16+
return res.status(resp.status).send(resp.statusText)
17+
}
18+
19+
const data = await resp.data
20+
res.status(200).send(JSON.stringify({ data }))
21+
} catch (error) {
22+
console.log(error) // output to netlify function log
23+
res.status(500).send(JSON.stringify({ msg: (error as Error).message }))
24+
}
1325
}
1426

1527
export default handler

src/api/txs.ts

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,31 @@
1+
import axios from "axios"
12
import { GatsbyFunctionRequest, GatsbyFunctionResponse } from "gatsby"
23

3-
import { lambda } from "../lambda/txs"
4-
54
async function handler(
65
__req: GatsbyFunctionRequest,
76
res: GatsbyFunctionResponse
87
) {
9-
// passing env vars as arguments due to a bug on GC functions where env vars
10-
// can not be accessed by imported functions
11-
const { statusCode, body } = await lambda(process.env.ETHERSCAN_API_KEY)
12-
res.status(statusCode).send(body)
8+
const apiKey = process.env.ETHERSCAN_API_KEY
9+
10+
try {
11+
const daysToFetch = 90
12+
const now = new Date()
13+
const endDate = now.toISOString().split("T")[0] // YYYY-MM-DD
14+
const startDate = new Date(now.setDate(now.getDate() - daysToFetch))
15+
.toISOString()
16+
.split("T")[0] // {daysToFetch} days ago
17+
const response = await axios.get(
18+
`https://api.etherscan.io/api?module=stats&action=dailytx&startdate=${startDate}&enddate=${endDate}&sort=asc&apikey=${apiKey}`
19+
)
20+
if (response.status < 200 || response.status >= 300) {
21+
return res.status(response.status).send(response.statusText)
22+
}
23+
const { data } = response
24+
res.status(200).send(JSON.stringify(data.result))
25+
} catch (error) {
26+
console.error(error)
27+
res.status(500).send(JSON.stringify({ msg: (error as Error).message }))
28+
}
1329
}
1430

1531
export default handler

src/lambda/calendarEvents.ts

Lines changed: 0 additions & 66 deletions
This file was deleted.

src/lambda/defipulse.ts

Lines changed: 0 additions & 39 deletions
This file was deleted.

0 commit comments

Comments
 (0)