From 03354dd58ff22a540d5fb490399014a3b420c771 Mon Sep 17 00:00:00 2001 From: Jorge Cortes Date: Wed, 13 Nov 2024 16:00:57 -0500 Subject: [PATCH] [Components] beaconchain --- .../actions/get-epoch/get-epoch.mjs | 42 ++++++++++++++++++ .../get-execution-blocks.mjs | 43 +++++++++++++++++++ .../actions/get-slots/get-slots.mjs | 43 +++++++++++++++++++ .../actions/get-validators/get-validators.mjs | 43 +++++++++++++++++++ components/beaconchain/beaconchain.app.mjs | 39 ++++++++++++++--- components/beaconchain/common/constants.mjs | 7 +++ components/beaconchain/package.json | 7 ++- pnpm-lock.yaml | 5 ++- 8 files changed, 221 insertions(+), 8 deletions(-) create mode 100644 components/beaconchain/actions/get-epoch/get-epoch.mjs create mode 100644 components/beaconchain/actions/get-execution-blocks/get-execution-blocks.mjs create mode 100644 components/beaconchain/actions/get-slots/get-slots.mjs create mode 100644 components/beaconchain/actions/get-validators/get-validators.mjs create mode 100644 components/beaconchain/common/constants.mjs diff --git a/components/beaconchain/actions/get-epoch/get-epoch.mjs b/components/beaconchain/actions/get-epoch/get-epoch.mjs new file mode 100644 index 0000000000000..fb75c4fe52e13 --- /dev/null +++ b/components/beaconchain/actions/get-epoch/get-epoch.mjs @@ -0,0 +1,42 @@ +import app from "../../beaconchain.app.mjs"; + +export default { + key: "beaconchain-get-epoch", + name: "Get Epoch", + description: "Returns information for a specified epoch by the epoch number or an epoch tag (can be latest or finalized). [See the documentation](https://beaconcha.in/api/v1/docs/index.html#/Epoch/get_api_v1_epoch__epoch_)", + version: "0.0.1", + type: "action", + props: { + app, + epoch: { + propDefinition: [ + app, + "epoch", + ], + }, + }, + methods: { + getEpoch({ + epoch, ...args + } = {}) { + return this.app._makeRequest({ + path: `/epoch/${epoch}`, + ...args, + }); + }, + }, + async run({ $ }) { + const { + getEpoch, + epoch, + } = this; + + const response = await getEpoch({ + $, + epoch, + }); + + $.export("$summary", `Successfully retrieved epoch \`${response.data.epoch}\`.`); + return response; + }, +}; diff --git a/components/beaconchain/actions/get-execution-blocks/get-execution-blocks.mjs b/components/beaconchain/actions/get-execution-blocks/get-execution-blocks.mjs new file mode 100644 index 0000000000000..849ab5104a46e --- /dev/null +++ b/components/beaconchain/actions/get-execution-blocks/get-execution-blocks.mjs @@ -0,0 +1,43 @@ +import app from "../../beaconchain.app.mjs"; + +export default { + key: "beaconchain-get-execution-blocks", + name: "Get Execution Blocks", + description: "Retrieve execution blocks by execution block number. [See the documentation](https://beaconcha.in/api/v1/docs/index.html#/Execution/get_api_v1_execution_block__blockNumber_)", + version: "0.0.1", + type: "action", + props: { + app, + blockNumbers: { + type: "string[]", + label: "Block Number", + description: "Enter one or more execution block numbers, up to a maximum of 100.", + }, + }, + methods: { + getExecutionBlocks({ + blockNumber, ...args + } = {}) { + return this.app._makeRequest({ + path: `/execution/block/${blockNumber}`, + ...args, + }); + }, + }, + async run({ $ }) { + const { + getExecutionBlocks, + blockNumbers, + } = this; + + const response = await getExecutionBlocks({ + $, + blockNumber: Array.isArray(blockNumbers) + ? blockNumbers.map((value) => value.trim()).join(",") + : blockNumbers, + }); + + $.export("$summary", "Successfully retrieved execution blocks."); + return response; + }, +}; diff --git a/components/beaconchain/actions/get-slots/get-slots.mjs b/components/beaconchain/actions/get-slots/get-slots.mjs new file mode 100644 index 0000000000000..59eb8a851554d --- /dev/null +++ b/components/beaconchain/actions/get-slots/get-slots.mjs @@ -0,0 +1,43 @@ +import app from "../../beaconchain.app.mjs"; + +export default { + key: "beaconchain-get-slots", + name: "Get Slots", + description: "Returns all slots for a specified epoch. [See the documentation](https://beaconcha.in/api/v1/docs/index.html#/Epoch/get_api_v1_epoch__epoch__slots)", + version: "0.0.1", + type: "action", + props: { + app, + epoch: { + description: "Returns all slots for a specified epoch.", + propDefinition: [ + app, + "epoch", + ], + }, + }, + methods: { + getEpochSlots({ + epoch, ...args + } = {}) { + return this.app._makeRequest({ + path: `/epoch/${epoch}/slots`, + ...args, + }); + }, + }, + async run({ $ }) { + const { + getEpochSlots, + epoch, + } = this; + + const response = await getEpochSlots({ + $, + epoch, + }); + + $.export("$summary", `Successfully retrieved \`${response.data.length}\` slot(s).`); + return response; + }, +}; diff --git a/components/beaconchain/actions/get-validators/get-validators.mjs b/components/beaconchain/actions/get-validators/get-validators.mjs new file mode 100644 index 0000000000000..a9eb627e7bcc0 --- /dev/null +++ b/components/beaconchain/actions/get-validators/get-validators.mjs @@ -0,0 +1,43 @@ +import app from "../../beaconchain.app.mjs"; + +export default { + key: "beaconchain-get-validators", + name: "Get Validators", + description: "Returns information for all validators up to 100 by index or public key. [See the documentation](https://beaconcha.in/api/v1/docs/index.html#/Validator/get_api_v1_validator__indexOrPubkey_).", + version: "0.0.1", + type: "action", + props: { + app, + indexesOrPubkeys: { + type: "string[]", + label: "Validator Indexes Or Public Keys", + description: "Enter up to 100 validator indices or public keys.", + }, + }, + methods: { + getValidators({ + indexOrPubkey, ...args + } = {}) { + return this.app._makeRequest({ + path: `/validator/${indexOrPubkey}`, + ...args, + }); + }, + }, + async run({ $ }) { + const { + getValidators, + indexesOrPubkeys, + } = this; + + const response = await getValidators({ + $, + indexOrPubkey: Array.isArray(indexesOrPubkeys) + ? indexesOrPubkeys.map((value) => value.trim()).join(",") + : indexesOrPubkeys, + }); + + $.export("$summary", "Successfully retrieved validators."); + return response; + }, +}; diff --git a/components/beaconchain/beaconchain.app.mjs b/components/beaconchain/beaconchain.app.mjs index 54c24bcb7b6f8..887f2e4ade733 100644 --- a/components/beaconchain/beaconchain.app.mjs +++ b/components/beaconchain/beaconchain.app.mjs @@ -1,11 +1,40 @@ +import { axios } from "@pipedream/platform"; +import constants from "./common/constants.mjs"; + export default { type: "app", app: "beaconchain", - propDefinitions: {}, + propDefinitions: { + epoch: { + type: "string", + label: "Epoch", + description: "Epoch number, the string `latest` or the string `finalized`.", + }, + }, methods: { - // this.$auth contains connected account data - authKeys() { - console.log(Object.keys(this.$auth)); + getUrl(path) { + return `${constants.BASE_URL}${constants.VERSION_PATH}${path}`; + }, + getHeaders(headers) { + return { + ...headers, + "apikey": `${this.$auth.api_key}`, + }; + }, + _makeRequest({ + $ = this, path, headers, ...args + } = {}) { + return axios($, { + ...args, + url: this.getUrl(path), + headers: this.getHeaders(headers), + }); + }, + post(args = {}) { + return this._makeRequest({ + method: "POST", + ...args, + }); }, }, -}; \ No newline at end of file +}; diff --git a/components/beaconchain/common/constants.mjs b/components/beaconchain/common/constants.mjs new file mode 100644 index 0000000000000..3fc051957d671 --- /dev/null +++ b/components/beaconchain/common/constants.mjs @@ -0,0 +1,7 @@ +const BASE_URL = "https://beaconcha.in"; +const VERSION_PATH = "/api/v1"; + +export default { + BASE_URL, + VERSION_PATH, +}; diff --git a/components/beaconchain/package.json b/components/beaconchain/package.json index bbb615242643b..b15f4a742e4ca 100644 --- a/components/beaconchain/package.json +++ b/components/beaconchain/package.json @@ -1,6 +1,6 @@ { "name": "@pipedream/beaconchain", - "version": "0.0.1", + "version": "0.1.0", "description": "Pipedream Beaconchain Components", "main": "beaconchain.app.mjs", "keywords": [ @@ -11,5 +11,8 @@ "author": "Pipedream (https://pipedream.com/)", "publishConfig": { "access": "public" + }, + "dependencies": { + "@pipedream/platform": "3.0.3" } -} \ No newline at end of file +} diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 1961b265a9372..1de3e9243aecc 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -1020,7 +1020,10 @@ importers: specifiers: {} components/beaconchain: - specifiers: {} + specifiers: + '@pipedream/platform': 3.0.3 + dependencies: + '@pipedream/platform': 3.0.3 components/beaconstac: specifiers: {}