From 046e56675f1fc1f4604cf121c5c44fc7d6fbd1fc Mon Sep 17 00:00:00 2001 From: Ignacio Date: Thu, 7 Dec 2023 20:48:25 +0800 Subject: [PATCH] refactor: validator addresses --- src/graphql/resolvers/index.ts | 4 ++-- src/graphql/routes/archway-api.ts | 19 ++++++++++--------- src/graphql/routes/radix-api.ts | 10 ++++------ src/graphql/routes/sui-api.ts | 9 +++------ src/graphql/utils/addresses.ts | 9 +++++++++ 5 files changed, 28 insertions(+), 23 deletions(-) create mode 100644 src/graphql/utils/addresses.ts diff --git a/src/graphql/resolvers/index.ts b/src/graphql/resolvers/index.ts index b238e9f..cc31498 100644 --- a/src/graphql/resolvers/index.ts +++ b/src/graphql/resolvers/index.ts @@ -451,9 +451,9 @@ export const resolvers = { const { dataSources } = params[2] as ContextValue; const { status, data } = await dataSources.radixAPI.getRadixTVL(); - if (status === "error") return; + if (status === "error" || !data) return; - const { address, TVL } = data as NonNullable; + const { address, TVL } = data; return [ { diff --git a/src/graphql/routes/archway-api.ts b/src/graphql/routes/archway-api.ts index 7f625ab..7924a25 100644 --- a/src/graphql/routes/archway-api.ts +++ b/src/graphql/routes/archway-api.ts @@ -1,11 +1,9 @@ import type { DataSourceConfig } from "@apollo/datasource-rest"; import { RESTDataSource } from "@apollo/datasource-rest"; +import { archwayValidatorAddress } from "../utils/addresses"; import { CoinGeckoDataSource } from "../utils/coingecko-data"; -const validatorAddress = - "archwayvaloper1esg4kluvdkfcxl0atcf2us2p9m9y9sjjsu04ex"; - // GET https://api.mainnet.archway.io/cosmos/bank/v1beta1/denoms_metadata const decimalsOfAArchwayBondenToken = 18; @@ -22,7 +20,7 @@ export class ArchwayAPI extends RESTDataSource { async getTVL() { const [validatorResponse, coinPrice] = await Promise.all([ - this.get(`/staking/validators/${validatorAddress}`, { + this.get(`/staking/validators/${archwayValidatorAddress}`, { headers: { "Content-Type": "application/json", }, @@ -54,7 +52,7 @@ export class ArchwayAPI extends RESTDataSource { "Content-Type": "application/json", }, }), - this.get(`/staking/validators/${validatorAddress}`, { + this.get(`/staking/validators/${archwayValidatorAddress}`, { headers: { "Content-Type": "application/json", }, @@ -99,11 +97,14 @@ export class ArchwayAPI extends RESTDataSource { } async getBondedToken() { - const result = await this.get(`/staking/validators/${validatorAddress}`, { - headers: { - "Content-Type": "application/json", + const result = await this.get( + `/staking/validators/${archwayValidatorAddress}`, + { + headers: { + "Content-Type": "application/json", + }, }, - }); + ); const bondedToken = result?.result?.tokens / 10 ** decimalsOfAArchwayBondenToken; diff --git a/src/graphql/routes/radix-api.ts b/src/graphql/routes/radix-api.ts index dcd815f..42326c8 100644 --- a/src/graphql/routes/radix-api.ts +++ b/src/graphql/routes/radix-api.ts @@ -1,6 +1,7 @@ import type { DataSourceConfig } from "@apollo/datasource-rest"; import { RESTDataSource } from "@apollo/datasource-rest"; +import { radixValidatorAddress } from "../utils/addresses"; import { CoinGeckoDataSource } from "../utils/coingecko-data"; const { RADIX_URL } = process.env; @@ -17,9 +18,6 @@ type RadixResponse = { }; }; -const validatorAddress = - "validator_rdx1swkmn6yvrqjzpaytvug5fp0gzfy9zdzq7j7nlxe8wgjpg76vdcma8p"; - // https://dashboard.radixdlt.com/network-staking // https://radix-babylon-gateway-api.redoc.ly/#tag/Status @@ -58,7 +56,7 @@ export class RadixAPI extends RESTDataSource { network: "mainnet", }, validator_identifier: { - address: validatorAddress, + address: radixValidatorAddress, }, }), }), @@ -67,7 +65,7 @@ export class RadixAPI extends RESTDataSource { // eslint-disable-next-line @typescript-eslint/no-explicit-any const validator = radixResponse.validators.items.find((i: any) => - (i.address as string).includes(validatorAddress), + (i.address as string).includes(radixValidatorAddress), ); if (!validator) { @@ -89,7 +87,7 @@ export class RadixAPI extends RESTDataSource { status: "ok", data: { TVL, - address: validatorAddress, + address: radixValidatorAddress, }, }; } diff --git a/src/graphql/routes/sui-api.ts b/src/graphql/routes/sui-api.ts index 4a49d1c..7382875 100644 --- a/src/graphql/routes/sui-api.ts +++ b/src/graphql/routes/sui-api.ts @@ -1,16 +1,13 @@ import type { DataSourceConfig } from "@apollo/datasource-rest"; import { RESTDataSource } from "@apollo/datasource-rest"; +import { suiValidatorAddress } from "../utils/addresses"; import { CoinGeckoDataSource } from "../utils/coingecko-data"; if (!process.env.DEVTOOLS_API_KEY) { throw new Error("DEVTOOLS_API_KEY is not set"); } -// https://suiexplorer.com/validator/0x1e1985024aafe50a8e4eafc5a89eb7ecd58ba08c39f37688bee00bd55c8b2059 -const validatorAddress = - "0x1e1985024aafe50a8e4eafc5a89eb7ecd58ba08c39f37688bee00bd55c8b2059"; - const coinDecimals = 9; // https://docs.sui.io/ @@ -48,7 +45,7 @@ export class SuiAPI extends RESTDataSource { return response.result.activeValidators.find( // eslint-disable-next-line @typescript-eslint/no-explicit-any - (v: any) => v.suiAddress === validatorAddress, + (v: any) => v.suiAddress === suiValidatorAddress, ); } @@ -65,7 +62,7 @@ export class SuiAPI extends RESTDataSource { const validatorItem = response.result.apys.find( // eslint-disable-next-line @typescript-eslint/no-explicit-any - (v: any) => v.address === validatorAddress, + (v: any) => v.address === suiValidatorAddress, ); if (!validatorItem) { diff --git a/src/graphql/utils/addresses.ts b/src/graphql/utils/addresses.ts new file mode 100644 index 0000000..dcb826e --- /dev/null +++ b/src/graphql/utils/addresses.ts @@ -0,0 +1,9 @@ +export const archwayValidatorAddress = + "archwayvaloper1esg4kluvdkfcxl0atcf2us2p9m9y9sjjsu04ex"; + +export const radixValidatorAddress = + "validator_rdx1swkmn6yvrqjzpaytvug5fp0gzfy9zdzq7j7nlxe8wgjpg76vdcma8p"; + +// https://suiexplorer.com/validator/0x1e1985024aafe50a8e4eafc5a89eb7ecd58ba08c39f37688bee00bd55c8b2059 +export const suiValidatorAddress = + "0x1e1985024aafe50a8e4eafc5a89eb7ecd58ba08c39f37688bee00bd55c8b2059";