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

Release: TKEY stats endpoint #175

Merged
merged 1 commit into from
Sep 17, 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
5 changes: 5 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import { redisClient } from './redis';
import { evmRouter } from './routers/evm';
import { adRulesRouter } from './routers/slise-ad-rules';
import { getTkeyStats } from './tkey-stats';
import { getABData } from './utils/ab-test';
import { cancelAliceBobOrder } from './utils/alice-bob/cancel-alice-bob-order';
import { createAliceBobOrder } from './utils/alice-bob/create-alice-bob-order';
Expand Down Expand Up @@ -105,6 +106,10 @@
res.status(200).send(coinGeckoTokens);
});

app.get('/api/tkey', async (_req, res) => {
res.send(await getTkeyStats());
});

app.get('/api/notifications', async (_req, res) => {
try {
const { platform, startFromTime } = _req.query;
Expand Down Expand Up @@ -156,7 +161,7 @@
await redisClient.lpush('notifications', JSON.stringify(newNotification));

res.status(200).send({ message: 'Notification added successfully', notification: newNotification });
} catch (error: any) {

Check warning on line 164 in src/index.ts

View workflow job for this annotation

GitHub Actions / Checks if ts and lint works

Unexpected any. Specify a different type
res.status(500).send({ error: error.message });
}
});
Expand Down Expand Up @@ -343,7 +348,7 @@
await startMagicSquareQuest(req.body);

res.status(200).send({ message: 'Quest successfully started' });
} catch (error: any) {

Check warning on line 351 in src/index.ts

View workflow job for this annotation

GitHub Actions / Checks if ts and lint works

Unexpected any. Specify a different type
console.error(error);

if (error instanceof CodedError) {
Expand All @@ -357,7 +362,7 @@
app.get('/api/magic-square-quest/participants', basicAuth, async (req, res) => {
try {
res.status(200).send(await getMagicSquareQuestParticipants());
} catch (error: any) {

Check warning on line 365 in src/index.ts

View workflow job for this annotation

GitHub Actions / Checks if ts and lint works

Unexpected any. Specify a different type
console.error(error);

if (error instanceof CodedError) {
Expand All @@ -371,10 +376,10 @@
app.get('/api/signing-nonce', (req, res) => {
try {
const pkh = req.query.pkh;
if (!pkh || typeof pkh !== 'string') throw new Error('PKH is not a string');

Check warning on line 379 in src/index.ts

View workflow job for this annotation

GitHub Actions / Checks if ts and lint works

Unexpected value in conditional. A boolean expression is required

res.status(200).send(getSigningNonce(pkh));
} catch (error: any) {

Check warning on line 382 in src/index.ts

View workflow job for this annotation

GitHub Actions / Checks if ts and lint works

Unexpected any. Specify a different type
console.error(error);

if (error instanceof CodedError) {
Expand Down
52 changes: 52 additions & 0 deletions src/tkey-stats.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import axios from 'axios';
import memoizee from 'memoizee';

const BURN_ADDRESS = 'tz1burnburnburnburnburnburnburjAYjjX';
const INCENTIVES_ADDRESS = 'tz1Ntpk55Q6AVJHVrCs1uN4HyTTxBbVMFZcb';
const INVESTMENT_ADDRESS = 'tz1imUX3aTc4HX6KygaQUe8e1sQqYvGs6eCF';
const DEVELOPER_REWARDS_ADDRESS = 'tz1bxHEHAtKubVy44vBDFVEZ1iqYPYdJVS9U';
const CONTRACT = 'KT1VaEsVNiBoA56eToEK6n6BcPgh1tdx9eXi';
const DECIMALS = 18n;
const TOTAL_SUPPLY = 14_000_000_000_000_000_000_000_000n;
const TOTAL_SUPPLY_WITH_DECIMALS = TOTAL_SUPPLY / 10n ** DECIMALS;

const getTkeyBalance = memoizee(
async (holder: string) => {
const response = await axios.get(
`https://api.tzkt.io/v1/tokens/balances?account=${holder}&token.contract=${CONTRACT}`
);

return BigInt(response.data[0].balance) / 10n ** DECIMALS;
},
{
maxAge: 1000 * 60 * 60 // 1 hour
}
);

const getBurnedTokens = () => getTkeyBalance(BURN_ADDRESS);
const getInvestmentFund = () => getTkeyBalance(INVESTMENT_ADDRESS);
const getIncentivesFund = () => getTkeyBalance(INCENTIVES_ADDRESS);
const getDeveloperRewardsFund = () => getTkeyBalance(DEVELOPER_REWARDS_ADDRESS);

export const getTkeyStats = memoizee(
async () => {
const burned = await getBurnedTokens();
const incentives = await getIncentivesFund();
const investment = await getInvestmentFund();
const developerRewards = await getDeveloperRewardsFund();

const circulating = TOTAL_SUPPLY_WITH_DECIMALS - incentives - developerRewards - investment - burned;

return {
incentivesFund: incentives.toString(),
investmentFund: investment.toString(),
developerRewardsFund: developerRewards.toString(),
totalSupply: TOTAL_SUPPLY_WITH_DECIMALS.toString(),
circulating: circulating.toString(),
burned: burned.toString()
};
},
{
maxAge: 1000 * 60 * 60 // 1 hour
}
);
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"compilerOptions": {
/* Basic Options */
"target": "ES2019",
"target": "ES2020",
"module": "commonjs",
"lib": ["esnext"],
"allowJs": true,
Expand Down
Loading