From b2bb6705951b3bbb9f6e8174fa45f26c2b10ce2d Mon Sep 17 00:00:00 2001 From: Marc Velmer Date: Tue, 12 Sep 2023 14:24:20 +0200 Subject: [PATCH] Added supported chains endpoint in Census3 --- CHANGELOG.md | 6 ++++++ src/api/census3/index.ts | 1 + src/api/census3/service.ts | 35 +++++++++++++++++++++++++++++++++++ src/census3.ts | 10 ++++++++++ 4 files changed, 52 insertions(+) create mode 100644 src/api/census3/service.ts diff --git a/CHANGELOG.md b/CHANGELOG.md index a77df450..527fe576 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## [Unreleased] + +### Added + +- Census3 supported chains information. + ## [0.2.0] - 2023-09-04 ### Fixed diff --git a/src/api/census3/index.ts b/src/api/census3/index.ts index b0e94979..f9e04d2d 100644 --- a/src/api/census3/index.ts +++ b/src/api/census3/index.ts @@ -1,4 +1,5 @@ export * from './census'; export * from './strategy'; +export * from './service'; export * from './token'; export * from './errors'; diff --git a/src/api/census3/service.ts b/src/api/census3/service.ts new file mode 100644 index 00000000..bd0d1154 --- /dev/null +++ b/src/api/census3/service.ts @@ -0,0 +1,35 @@ +import axios from 'axios'; +import { Census3API } from './api'; + +enum Census3ServiceAPIMethods { + INFO = '/info', +} + +export interface ICensus3ServiceInfoResponse { + /** + * The list of supported chains + */ + chainIDs: Array; +} + +export abstract class Census3ServiceAPI extends Census3API { + /** + * Cannot be constructed. + */ + private constructor() { + super(); + } + + /** + * Fetches supported chains from the service + * + * @param {string} url API endpoint URL + * @returns {Promise} + */ + public static info(url: string): Promise { + return axios + .get(url + Census3ServiceAPIMethods.INFO) + .then((response) => response.data) + .catch(this.isApiError); + } +} diff --git a/src/census3.ts b/src/census3.ts index 07f0b347..e9e2f93d 100644 --- a/src/census3.ts +++ b/src/census3.ts @@ -2,6 +2,7 @@ import { CENSUS3_URL } from './util/constants'; import { ClientOptions } from './client'; import { Census3CensusAPI, + Census3ServiceAPI, Census3StrategyAPI, Census3TokenAPI, ICensus3CensusResponse, @@ -43,6 +44,15 @@ export class VocdoniCensus3Client { return Census3TokenAPI.list(this.url).then((list) => list.tokens ?? []); } + /** + * Returns a list of supported chain identifiers + * + * @returns {Promise} Supported chain list + */ + getSupportedChains(): Promise { + return Census3ServiceAPI.info(this.url).then((info) => info.chainIDs ?? []); + } + /** * Returns a list of supported tokens type *