Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add radix data #37

Merged
merged 5 commits into from
Jan 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .commitlintrc.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module.exports = {
extends: ["@commitlint/config-conventional"],
rules: {
"header-max-length": [2, "always", 72],
},
};
1 change: 1 addition & 0 deletions .eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ module.exports = {
"object-shorthand": "error",
"operator-assignment": "error",

"perfectionist/sort-classes": "error",
"perfectionist/sort-enums": "error",
"perfectionist/sort-exports": "error",
"perfectionist/sort-interfaces": "error",
Expand Down
4 changes: 4 additions & 0 deletions .husky/commit-msg
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/usr/bin/env sh
. "$(dirname -- "$0")/_/husky.sh"

npx --no -- commitlint --edit ${1}
4 changes: 2 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM node:20-alpine as builer
FROM node:20-alpine as builder
WORKDIR /app
COPY package.json yarn.lock .yarnrc.yml ./
COPY .yarn ./.yarn
Expand All @@ -12,6 +12,6 @@ COPY package.json yarn.lock .yarnrc.yml ./
COPY .yarn ./.yarn
ENV NODE_ENV=production
RUN yarn --immutable
COPY --from=builer /app/dist ./dist
COPY --from=builder /app/dist ./dist
RUN npm i --global pm2
CMD ["pm2-runtime", "dist/index.js"]
4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"build": "rm -rf dist && tsc",
"dev": "NODE_ENV=development nodemon",
"lint": "eslint . --ext .ts && prettier './**/*.{ts,js}' --check",
"postinstall": "husky install",
"start": "NODE_ENV=production node dist/index.js",
"test": "echo No tests"
},
Expand All @@ -24,6 +25,8 @@
"express": "^4.18.2"
},
"devDependencies": {
"@commitlint/cli": "^18.4.3",
"@commitlint/config-conventional": "^18.4.3",
"@stylistic/eslint-plugin": "^1.5.1",
"@trivago/prettier-plugin-sort-imports": "^4.3.0",
"@types/cors": "^2.8.17",
Expand All @@ -38,6 +41,7 @@
"eslint-plugin-perfectionist": "^2.5.0",
"eslint-plugin-prettier": "^5.1.2",
"graphql": "^16.8.1",
"husky": "^8.0.3",
"nodemon": "^3.0.2",
"prettier": "^3.1.1",
"ts-node": "^10.9.2",
Expand Down
44 changes: 30 additions & 14 deletions src/graphql/resolvers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,6 @@ export const resolvers = {

return result.map((res) => ({ cosmosTVL: res.value[1] }));
},
allRadixStakedTokens: async (...params: unknown[]) => {
const { dataSources } = params[2] as ContextValue;

const result = commonHandler(
(await dataSources.radixPromAPI.getStakedRadix()) as Response,
);

if (!result) return;

return result.map((res) => ({
bondedToken: res.value[1],
metric: { instance: "radix", validator_address: res.metric.address },
}));
},
allRadixTotalSupply: async (...params: unknown[]) => {
const { dataSources } = params[2] as ContextValue;
const result = await dataSources.radixAPI.getTotalRadixSupply();
Expand Down Expand Up @@ -367,6 +353,36 @@ export const resolvers = {
usersCount: res.value[1],
}));
},
radixAPY: async (...params: unknown[]) => {
const { dataSources } = params[2] as ContextValue;
const { data, status } = await dataSources.radixAPI.getRadixAPY();

if (status === "error" || !data) return;

const { address, APY } = data;

return [
{
APY,
metric: { instance: "radix", validator_address: address },
},
];
},
radixBondedToken: async (...params: unknown[]) => {
const { dataSources } = params[2] as ContextValue;
const { data, status } = await dataSources.radixAPI.getRadixBondedToken();

if (status === "error" || !data) return;

const { address, bondedToken } = data;

return [
{
bondedToken,
metric: { instance: "radix", validator_address: address },
},
];
},
radixTVL: async (...params: unknown[]) => {
const { dataSources } = params[2] as ContextValue;
const { data, status } = await dataSources.radixAPI.getRadixTVL();
Expand Down
52 changes: 26 additions & 26 deletions src/graphql/routes/archway-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,32 +18,6 @@ export class ArchwayAPI extends RESTDataSource {
this.gecko = new CoinGeckoDataSource(options);
}

async getTVL() {
const [validatorResponse, coinPrice] = await Promise.all([
this.get(`/staking/validators/${archwayValidatorAddress}`, {
headers: {
"Content-Type": "application/json",
},
}),
this.gecko.getCoinPrice("archway"),
]);

// https://github.com/forbole/cosmos-exporter/blob/8e78b8ea5a37d113f3448c423540df4edb1f4ebb/collector/validator_status.go#L40C72-L40C72
// Validator.DelegatorShares
const tokens =
Number(validatorResponse.result.delegator_shares) /
10 ** decimalsOfAArchwayBondenToken;

const TVL = tokens * Number(coinPrice);

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

async getAPY() {
const [inflationResponse, validatorResponse, supplyResponse] =
await Promise.all([
Expand Down Expand Up @@ -124,4 +98,30 @@ export class ArchwayAPI extends RESTDataSource {
status: "ok",
};
}

async getTVL() {
const [validatorResponse, coinPrice] = await Promise.all([
this.get(`/staking/validators/${archwayValidatorAddress}`, {
headers: {
"Content-Type": "application/json",
},
}),
this.gecko.getCoinPrice("archway"),
]);

// https://github.com/forbole/cosmos-exporter/blob/8e78b8ea5a37d113f3448c423540df4edb1f4ebb/collector/validator_status.go#L40C72-L40C72
// Validator.DelegatorShares
const tokens =
Number(validatorResponse.result.delegator_shares) /
10 ** decimalsOfAArchwayBondenToken;

const TVL = tokens * Number(coinPrice);

return {
data: {
TVL,
},
status: "ok",
};
}
}
28 changes: 14 additions & 14 deletions src/graphql/routes/cosmos-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,21 @@ import { RESTDataSource } from "@apollo/datasource-rest";
export class CosmosAPI extends RESTDataSource {
override baseURL = `${process.env.PROM_QUERY_URL}/prometheus/api/v1/`;

async getAllCosmosUsers() {
async getAllCosmosTVL() {
return this.get(
`query?query=sum(max_over_time(tendermint_validator_delegators_total[${process.env.MAX_OVER_TIME_DURATION}]))`,
`query?query=sum(max_over_time(tendermint_validator_voting_power_total[${process.env.MAX_OVER_TIME_DURATION}]) * on (denom) group_left token_price)`,
);
}

async getAllCosmosTVL() {
async getAllCosmosUsers() {
return this.get(
`query?query=sum(max_over_time(tendermint_validator_voting_power_total[${process.env.MAX_OVER_TIME_DURATION}]) * on (denom) group_left token_price)`,
`query?query=sum(max_over_time(tendermint_validator_delegators_total[${process.env.MAX_OVER_TIME_DURATION}]))`,
);
}

async getEachCosmosChainTVL() {
async getEachCosmosAPY() {
return this.get(
`query?query=max_over_time(tendermint_validator_voting_power_total[${process.env.MAX_OVER_TIME_DURATION}]) * on (denom) group_left token_price`,
`query?query=(max_over_time(tendermint_inflation_rate[${process.env.MAX_OVER_TIME_DURATION}]) * (1 - tendermint_community_tax_rate)) / (tendermint_bonded_token / tendermint_circulating_supply)`,
);
}

Expand All @@ -27,21 +27,21 @@ export class CosmosAPI extends RESTDataSource {
);
}

async getEachCosmosCommission() {
async getEachCosmosChainTVL() {
return this.get(
`query?query=max_over_time(tendermint_validator_commission_rate[${process.env.MAX_OVER_TIME_DURATION}])`,
`query?query=max_over_time(tendermint_validator_voting_power_total[${process.env.MAX_OVER_TIME_DURATION}]) * on (denom) group_left token_price`,
);
}

async getEachCosmosUnbondingTime() {
async getEachCosmosCommission() {
return this.get(
`query?query=max_over_time(tendermint_unbonding_time[${process.env.MAX_OVER_TIME_DURATION}])`,
`query?query=max_over_time(tendermint_validator_commission_rate[${process.env.MAX_OVER_TIME_DURATION}])`,
);
}

async getEachCosmosAPY() {
async getEachCosmosInflationRate() {
return this.get(
`query?query=(max_over_time(tendermint_inflation_rate[${process.env.MAX_OVER_TIME_DURATION}]) * (1 - tendermint_community_tax_rate)) / (tendermint_bonded_token / tendermint_circulating_supply)`,
`query?query=max_over_time(tendermint_inflation_rate[${process.env.MAX_OVER_TIME_DURATION}])`,
);
}

Expand All @@ -51,9 +51,9 @@ export class CosmosAPI extends RESTDataSource {
);
}

async getEachCosmosInflationRate() {
async getEachCosmosUnbondingTime() {
return this.get(
`query?query=max_over_time(tendermint_inflation_rate[${process.env.MAX_OVER_TIME_DURATION}])`,
`query?query=max_over_time(tendermint_unbonding_time[${process.env.MAX_OVER_TIME_DURATION}])`,
);
}
}
28 changes: 14 additions & 14 deletions src/graphql/routes/elrond-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,21 @@ export class ElrondAPI extends RESTDataSource {
);
}

async getElrondTVL() {
async getElrondBondedToken() {
return this.get(
`query?query=max_over_time(elrond_provider_locked[${process.env.MAX_OVER_TIME_DURATION}]) * on (denom) token_price`,
`query?query=max_over_time(elrond_provider_locked[${process.env.MAX_OVER_TIME_DURATION}])`,
);
}

async getElrondCommission() {
async getElrondCirculatingSupply() {
return this.get(
`query?query=max_over_time(elrond_provider_service_fee[${process.env.MAX_OVER_TIME_DURATION}])`,
`query?query=max_over_time(elrond_circulating_supply[${process.env.MAX_OVER_TIME_DURATION}])`,
);
}

async getElrondBondedToken() {
async getElrondCommission() {
return this.get(
`query?query=max_over_time(elrond_provider_locked[${process.env.MAX_OVER_TIME_DURATION}])`,
`query?query=max_over_time(elrond_provider_service_fee[${process.env.MAX_OVER_TIME_DURATION}])`,
);
}

Expand All @@ -33,15 +33,9 @@ export class ElrondAPI extends RESTDataSource {
);
}

async getElrondCirculatingSupply() {
return this.get(
`query?query=max_over_time(elrond_circulating_supply[${process.env.MAX_OVER_TIME_DURATION}])`,
);
}

async getElrondUsers() {
async getElrondTVL() {
return this.get(
`query?query=max_over_time(elrond_provider_num_users[${process.env.MAX_OVER_TIME_DURATION}])`,
`query?query=max_over_time(elrond_provider_locked[${process.env.MAX_OVER_TIME_DURATION}]) * on (denom) token_price`,
);
}

Expand All @@ -50,4 +44,10 @@ export class ElrondAPI extends RESTDataSource {

return unbondingTime;
}

async getElrondUsers() {
return this.get(
`query?query=max_over_time(elrond_provider_num_users[${process.env.MAX_OVER_TIME_DURATION}])`,
);
}
}
12 changes: 6 additions & 6 deletions src/graphql/routes/oasis-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,6 @@ import { RESTDataSource } from "@apollo/datasource-rest";
export class OasisAPI extends RESTDataSource {
override baseURL = `${process.env.PROM_QUERY_URL}/prometheus/api/v1/`;

async getOasisUsers() {
return this.get(
`query?query=max_over_time(oasis_validator_delegators_total[${process.env.MAX_OVER_TIME_DURATION}])`,
);
}

async getOasisBondedToken() {
return this.get(
`query?query=max_over_time(oasis_validator_staked[${process.env.MAX_OVER_TIME_DURATION}])`,
Expand All @@ -26,4 +20,10 @@ export class OasisAPI extends RESTDataSource {
`query?query=max_over_time(oasis_validator_staked{}[${process.env.MAX_OVER_TIME_DURATION}]) * on (denom) group_left token_price`,
);
}

async getOasisUsers() {
return this.get(
`query?query=max_over_time(oasis_validator_delegators_total[${process.env.MAX_OVER_TIME_DURATION}])`,
);
}
}
Loading