Skip to content

Commit

Permalink
feat: add radix tvl via api (#31)
Browse files Browse the repository at this point in the history
* feat: add radix tvl via api

* feat: cache to endpoint
  • Loading branch information
icfor authored Dec 5, 2023
1 parent 935de0b commit eb27fd7
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 9 deletions.
18 changes: 10 additions & 8 deletions src/graphql/resolvers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -449,16 +449,18 @@ export const resolvers = {
},
radixTVL: async (...params: unknown[]) => {
const { dataSources } = params[2] as ContextValue;
const result = commonHandler(
(await dataSources.radixPromAPI.getRadixTVL()) as Response,
);
const { status, data } = await dataSources.radixAPI.getRadixTVL();

if (!result) return;
if (status === "error") return;

return result.map((res) => ({
metric: { instance: "radix", validator_address: res.metric.address },
TVL: res.value[1],
}));
const { address, TVL } = data as NonNullable<typeof data>;

return [
{
metric: { instance: "radix", validator_address: address },
TVL,
},
];
},
radixUsers: async (...params: unknown[]) => {
const { dataSources } = params[2] as ContextValue;
Expand Down
58 changes: 58 additions & 0 deletions src/graphql/routes/radix-api.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import type { DataSourceConfig } from "@apollo/datasource-rest";
import { RESTDataSource } from "@apollo/datasource-rest";

import { CoinGeckoDataSource } from "../utils/coingecko-data";

const { RADIX_URL } = process.env;

if (!RADIX_URL) {
Expand All @@ -14,8 +17,22 @@ type RadixResponse = {
};
};

const validatorAddress =
"validator_rdx1swkmn6yvrqjzpaytvug5fp0gzfy9zdzq7j7nlxe8wgjpg76vdcma8p";

// https://dashboard.radixdlt.com/network-staking
// https://radix-babylon-gateway-api.redoc.ly/#tag/Status

export class RadixAPI extends RESTDataSource {
override baseURL = `${(RADIX_URL as string).replace(/\/$/, "")}/`;
private baseGatewayURL = "https://mainnet-gateway.radixdlt.com/";
private gecko: CoinGeckoDataSource;

constructor(options: DataSourceConfig) {
super(options);

this.gecko = new CoinGeckoDataSource(options);
}

async getTotalRadixSupply(): Promise<RadixResponse> {
return this.post("token/native", {
Expand All @@ -30,6 +47,47 @@ export class RadixAPI extends RESTDataSource {
});
}

async getRadixTVL() {
const [radixResponse, coinPrice] = await Promise.all([
this.post(`${this.baseGatewayURL}state/validators/list`, {
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
network_identifier: {
network: "mainnet",
},
validator_identifier: {
address: validatorAddress,
},
}),
}),
this.gecko.getCoinPrice("radix"),
]);

// eslint-disable-next-line @typescript-eslint/no-explicit-any
const validator = radixResponse.validators.items.find((i: any) =>
(i.address as string).includes(validatorAddress),
);

if (!validator) {
return {
status: "error",
};
}

const lockedUnit = validator.locked_owner_stake_unit_vault.balance;
const TVL = lockedUnit * Number(coinPrice);

return {
status: "ok",
data: {
TVL,
address: validatorAddress,
},
};
}

async getRadixUnbondingTime() {
const unbondingTime = "1-3 weeks (500 epochs)";

Expand Down
2 changes: 1 addition & 1 deletion src/graphql/typedefs/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ export const typeDefs = `#graphql
TVL: String
}
type RadixTVLResult {
type RadixTVLResult @cacheControl(maxAge: ${defaultMaxAge}) {
metric: AddressAndInstanceMetric
TVL: String
}
Expand Down

0 comments on commit eb27fd7

Please sign in to comment.