Skip to content

Commit

Permalink
fix: display token supply with correct decimals
Browse files Browse the repository at this point in the history
  • Loading branch information
rafaelcr committed Dec 16, 2024
1 parent 82ac443 commit 584d081
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 16 deletions.
3 changes: 1 addition & 2 deletions src/api/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ import FastifyCors from '@fastify/cors';
import { StatusRoutes } from './routes/status';
import FastifyMetrics, { IFastifyMetrics } from 'fastify-metrics';
import { Server } from 'http';
import { isProdEnv } from './util/helpers';
import { PINO_LOGGER_CONFIG } from '@hirosystems/api-toolkit';
import { isProdEnv, PINO_LOGGER_CONFIG } from '@hirosystems/api-toolkit';

export const Api: FastifyPluginAsync<Record<never, never>, Server, TypeBoxTypeProvider> = async (
fastify,
Expand Down
12 changes: 10 additions & 2 deletions src/api/routes/ft.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
import { handleChainTipCache, handleTokenCache } from '../util/cache';
import { generateTokenErrorResponse, TokenErrorResponseSchema } from '../util/errors';
import { parseMetadataLocaleBundle } from '../util/helpers';
import BigNumber from 'bignumber.js';

const IndexRoutes: FastifyPluginCallback<Record<never, never>, Server, TypeBoxTypeProvider> = (
fastify,
Expand Down Expand Up @@ -121,11 +122,18 @@ const ShowRoutes: FastifyPluginCallback<Record<never, never>, Server, TypeBoxTyp
locale: request.query.locale,
});
const contract = metadataBundle?.smartContract;
const decimals = metadataBundle?.token?.decimals ?? undefined;
const total_supply = metadataBundle?.token?.total_supply ?? undefined;
await reply.send({
name: metadataBundle?.token?.name ?? undefined,
symbol: metadataBundle?.token?.symbol ?? undefined,
decimals: metadataBundle?.token?.decimals ?? undefined,
total_supply: metadataBundle?.token?.total_supply ?? undefined,
decimals,
total_supply:
decimals != undefined && total_supply != undefined
? BigNumber(total_supply)
.dividedBy(10 ** decimals)
.toFixed(decimals)
: undefined,
token_uri: metadataBundle?.token?.uri ?? undefined,
description: metadataBundle?.metadataLocale?.metadata?.description ?? undefined,
tx_id: contract.tx_id,
Expand Down
8 changes: 0 additions & 8 deletions src/api/util/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,6 @@
import { DbMetadataLocaleBundle } from '../../pg/types';
import { MetadataPropertiesType, MetadataType, MetadataValueType } from '../schemas';

export const isDevEnv = process.env.NODE_ENV === 'development';
export const isTestEnv = process.env.NODE_ENV === 'test';
export const isProdEnv =
process.env.NODE_ENV === 'production' ||
process.env.NODE_ENV === 'prod' ||
!process.env.NODE_ENV ||
(!isTestEnv && !isDevEnv);

export function parseMetadataLocaleBundle(
locale?: DbMetadataLocaleBundle
): MetadataType | undefined {
Expand Down
8 changes: 6 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,12 @@ import { buildApiServer, buildPromServer } from './api/init';
import { TokenProcessorMetrics } from './token-processor/token-processor-metrics';
import { ENV } from './env';
import { buildAdminRpcServer } from './admin-rpc/init';
import { isProdEnv } from './api/util/helpers';
import { buildProfilerServer, logger, registerShutdownConfig } from '@hirosystems/api-toolkit';
import {
buildProfilerServer,
isProdEnv,
logger,
registerShutdownConfig,
} from '@hirosystems/api-toolkit';

Check warning on line 12 in src/index.ts

View check run for this annotation

Codecov / codecov/patch

src/index.ts#L7-L12

Added lines #L7 - L12 were not covered by tests
import { closeChainhookServer, startChainhookServer } from './chainhook/server';

/**
Expand Down
4 changes: 2 additions & 2 deletions tests/api/ft.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ describe('FT routes', () => {
name: 'hello-world',
symbol: 'HELLO',
token_uri: 'http://test.com/uri.json',
total_supply: '1',
total_supply: '0.000001',
sender_address: 'SP2SYHR84SDJJDK8M09HFS4KBFXPPCX9H7RZ9YVTS',
asset_identifier: 'SP2SYHR84SDJJDK8M09HFS4KBFXPPCX9H7RZ9YVTS.hello-world::ft-token',
tx_id: '0x123456',
Expand Down Expand Up @@ -240,7 +240,7 @@ describe('FT routes', () => {
name: 'hello-world',
symbol: 'HELLO',
token_uri: 'http://test.com/uri.json',
total_supply: '1',
total_supply: '0.000001',
decimals: 6,
sender_address: 'SP2SYHR84SDJJDK8M09HFS4KBFXPPCX9H7RZ9YVTS',
asset_identifier: 'SP2SYHR84SDJJDK8M09HFS4KBFXPPCX9H7RZ9YVTS.hello-world::ft-token',
Expand Down

0 comments on commit 584d081

Please sign in to comment.