Skip to content

Commit

Permalink
Merge develop to mainnet-backend (20241015) (#246)
Browse files Browse the repository at this point in the history
* Revert "Merge develop into main branch (20240923)"

* Revert "Revert "Merge develop into main branch (20240923)""

* feat(frontend): ga (#230)

* feat(frontend): add file `robots.txt` (#233)

* feat(frontend): add file `robots.txt`

* feat(frontend): add file `robots.txt`

* feat(frontend): add file `robots.txt` (#233) (#234)

* feat(frontend): add file `robots.txt`

* feat(frontend): add file `robots.txt`

* feat(frontend): fix file `robots.txt` on middleware (#235)

* Merge develop to main (20241008) (#236)

* feat(frontend): add file `robots.txt` (#233)

* feat(frontend): add file `robots.txt`

* feat(frontend): add file `robots.txt`

* feat(frontend): fix file `robots.txt` on middleware (#235)

* feat(frontend): add rules to file `robots.txt`

* feat(frontend): add rules to file `robots.txt`

* refactor(backend): improve Redis cache and bootstrap process (#243)

* feat(frontend): github env (#244)

* feat(frontend): github env

* fix(frontend): fix lingui text

* fix(frontend): fix lingui text

---------

Co-authored-by: INS <[email protected]>
Co-authored-by: ahonn <[email protected]>
  • Loading branch information
3 people authored Oct 15, 2024
1 parent 57d989e commit 3d9491b
Show file tree
Hide file tree
Showing 16 changed files with 131 additions and 54 deletions.
18 changes: 15 additions & 3 deletions backend/src/app.module.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { CacheModule, CacheStore } from '@nestjs/cache-manager';
import { Module } from '@nestjs/common';
import { Logger, Module } from '@nestjs/common';
import { ConfigModule, ConfigService } from '@nestjs/config';
import { SentryModule } from '@sentry/nestjs/setup';
import type { RedisClientOptions } from 'redis';
Expand All @@ -14,17 +14,29 @@ import { AppController } from './app.controller';
import { BootstrapService } from './bootstrap.service';
import { EventEmitterModule } from '@nestjs/event-emitter';

const logger = new Logger('CacheStore');

async function createCacheStore(redisUrl: string) {
const store = await redisStore({
url: redisUrl,
isCacheable: (value: unknown) => value !== undefined,
});
return {
async set<T>(key: string, value: T, ttl?: number): Promise<void> {
return store.set(key, value, ttl);
try {
return store.set(key, value, ttl);
} catch (e) {
logger.error(`Failed to set cache key ${key}: ${e}`);
return undefined;
}
},
async get<T>(key: string): Promise<T | undefined> {
return store.get(key);
try {
return store.get(key);
} catch (e) {
logger.error(`Failed to get cache key ${key}: ${e}`);
return undefined;
}
},
async del(key: string): Promise<void> {
return store.del(key);
Expand Down
1 change: 0 additions & 1 deletion backend/src/common/date.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
export const ONE_MONTH_MS = 30 * 24 * 60 * 60 * 1000;
export const ONE_HOUR_MS = 60 * 60 * 1000;
export const TEN_MINUTES_MS = 10 * 60 * 1000;
export const ONE_DAY_MS = 24 * 60 * 60 * 1000;
Expand Down
12 changes: 6 additions & 6 deletions backend/src/core/bitcoin-api/bitcoin-api.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { IBitcoinDataProvider } from './bitcoin-api.interface';
import { ElectrsService } from './provider/electrs.service';
import { MempoolService } from './provider/mempool.service';
import { ChainInfo, Transaction } from './bitcoin-api.schema';
import { ONE_HOUR_MS, ONE_MONTH_MS, TEN_MINUTES_MS } from 'src/common/date';
import { ONE_HOUR_MS, ONE_DAY_MS, TEN_MINUTES_MS } from 'src/common/date';
import { Cacheable } from 'src/decorators/cacheable.decorator';
import * as Sentry from '@sentry/nestjs';
import { PLimit } from 'src/decorators/plimit.decorator';
Expand Down Expand Up @@ -206,7 +206,7 @@ export class BitcoinApiService {
@Cacheable({
namespace: 'bitcoinApiService',
key: ({ txid }) => `getTx:${txid}`,
ttl: ONE_MONTH_MS,
ttl: ONE_DAY_MS,
shouldCache: (tx: Transaction) =>
tx.status.confirmed &&
!!tx.status.block_time &&
Expand All @@ -223,7 +223,7 @@ export class BitcoinApiService {
@Cacheable({
namespace: 'bitcoinApiService',
key: ({ txid }) => `getTxOutSpends:${txid}`,
ttl: ONE_MONTH_MS,
ttl: ONE_DAY_MS,
})
public async getTxOutSpends({ txid }: { txid: string }) {
return this.call('getTxOutSpends', { txid });
Expand All @@ -241,7 +241,7 @@ export class BitcoinApiService {
@Cacheable({
namespace: 'bitcoinApiService',
key: ({ hash }) => `getBlock:${hash}`,
ttl: ONE_MONTH_MS,
ttl: ONE_DAY_MS,
})
public async getBlock({ hash }: { hash: string }) {
return this.call('getBlock', { hash });
Expand All @@ -250,7 +250,7 @@ export class BitcoinApiService {
@Cacheable({
namespace: 'bitcoinApiService',
key: ({ hash, startIndex }) => `getBlockTxs:${hash}:${startIndex}`,
ttl: ONE_MONTH_MS,
ttl: ONE_DAY_MS,
})
public async getBlockTxs({ hash, startIndex }: { hash: string; startIndex?: number }) {
return this.call('getBlockTxs', { hash, startIndex });
Expand All @@ -268,7 +268,7 @@ export class BitcoinApiService {
@Cacheable({
namespace: 'bitcoinApiService',
key: ({ hash }) => `getBlockTxids:${hash}`,
ttl: ONE_MONTH_MS,
ttl: ONE_DAY_MS,
})
public async getBlockTxids({ hash }: { hash: string }) {
return this.call('getBlockTxids', { hash });
Expand Down
10 changes: 5 additions & 5 deletions backend/src/core/blockchain/blockchain.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
TransactionWithStatusResponse,
} from './blockchain.interface';
import { Cacheable } from 'src/decorators/cacheable.decorator';
import { ONE_MONTH_MS } from 'src/common/date';
import { ONE_DAY_MS } from 'src/common/date';
import { CKB_MIN_SAFE_CONFIRMATIONS } from 'src/constants';
import * as Sentry from '@sentry/nestjs';
import { Chain } from '@prisma/client';
Expand Down Expand Up @@ -118,7 +118,7 @@ export class BlockchainService {
}
return key;
},
ttl: ONE_MONTH_MS,
ttl: ONE_DAY_MS,
shouldCache: async (tx: TransactionWithStatusResponse, that: BlockchainService) => {
if (tx.tx_status.status !== 'committed' || !tx.tx_status.block_number) {
return false;
Expand Down Expand Up @@ -157,7 +157,7 @@ export class BlockchainService {
}
return key;
},
ttl: ONE_MONTH_MS,
ttl: ONE_DAY_MS,
shouldCache: async (block: Block, that: BlockchainService) => {
if (!block?.header) {
return false;
Expand Down Expand Up @@ -202,7 +202,7 @@ export class BlockchainService {
}
return key;
},
ttl: ONE_MONTH_MS,
ttl: ONE_DAY_MS,
shouldCache: async (block: Block, that: BlockchainService) => {
const { number } = block.header;
return that.isSafeConfirmations(number);
Expand Down Expand Up @@ -235,7 +235,7 @@ export class BlockchainService {
@Cacheable({
namespace: 'BlockchainService',
key: (blockHash: string) => `getBlockEconomicState:${blockHash}`,
ttl: ONE_MONTH_MS,
ttl: ONE_DAY_MS,
})
public async getBlockEconomicState(blockHash: string): Promise<BlockEconomicState> {
await this.websocketReady;
Expand Down
6 changes: 3 additions & 3 deletions backend/src/core/ckb-explorer/ckb-explorer.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import {
TransactionFeesStatistic,
TransactionListSortType,
} from './ckb-explorer.interface';
import { ONE_MONTH_MS } from 'src/common/date';
import { ONE_DAY_MS } from 'src/common/date';
import { CACHE_MANAGER, Cache } from '@nestjs/cache-manager';
import { Cacheable } from 'src/decorators/cacheable.decorator';
import { CkbRpcWebsocketService } from '../ckb-rpc/ckb-rpc-websocket.service';
Expand Down Expand Up @@ -132,7 +132,7 @@ export class CkbExplorerService {
@Cacheable({
namespace: 'CkbExplorerService',
key: (heightOrHash: string) => `getBlock:${heightOrHash}`,
ttl: ONE_MONTH_MS,
ttl: ONE_DAY_MS,
shouldCache: async (block: NonPaginatedResponse<Block>, that: CkbExplorerService) => {
const { number } = block.data.attributes;
return that.isSafeConfirmations(number);
Expand Down Expand Up @@ -192,7 +192,7 @@ export class CkbExplorerService {
@Cacheable({
namespace: 'CkbExplorerService',
key: (txHash: string) => `getTransaction:${txHash}`,
ttl: ONE_MONTH_MS,
ttl: ONE_DAY_MS,
shouldCache: async (tx: NonPaginatedResponse<DetailTransaction>, that: CkbExplorerService) => {
const { tx_status, block_number } = tx.data.attributes;
const isSafeConfirmations = await that.isSafeConfirmations(block_number);
Expand Down
4 changes: 2 additions & 2 deletions backend/src/core/core.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { Env } from 'src/env';
import { Transaction } from './blockchain/blockchain.interface';
import { BlockchainServiceFactory } from './blockchain/blockchain.factory';
import { LeapDirection } from '@prisma/client';
import { ONE_MONTH_MS } from 'src/common/date';
import { ONE_DAY_MS } from 'src/common/date';
import { Cacheable } from 'src/decorators/cacheable.decorator';

export const CELLBASE_TX_HASH =
Expand Down Expand Up @@ -79,7 +79,7 @@ export class CoreService {
key: (chainId: number, ckbTx: Transaction) => {
return `getLeapDirectionByCkbTx:${chainId}:${ckbTx.hash}`;
},
ttl: ONE_MONTH_MS,
ttl: ONE_DAY_MS,
})
public async getLeapDirectionByCkbTx(chainId: number, ckbTx: Transaction) {
const blockchainService = this.blockchainServiceFactory.getService(chainId);
Expand Down
17 changes: 16 additions & 1 deletion backend/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ import { AppModule } from './app.module';
import { FastifyAdapter, NestFastifyApplication } from '@nestjs/platform-fastify';
import { envSchema } from './env';
import { BootstrapService } from './bootstrap.service';
import { LogLevel } from '@nestjs/common';
import { Logger, LogLevel } from '@nestjs/common';
import { ClusterService } from './cluster.service';
import Redis from 'ioredis';

const env = envSchema.parse(process.env);
const LOGGER_LEVELS: LogLevel[] = ['verbose', 'debug', 'log', 'warn', 'error'];
Expand All @@ -18,6 +19,8 @@ function getLoggerOptions() {
return LOGGER_LEVELS.slice(index);
}

const logger = new Logger('Main');

async function bootstrap() {
const app = await NestFactory.create<NestFastifyApplication>(
AppModule,
Expand All @@ -29,6 +32,18 @@ async function bootstrap() {
},
);

const redis = new Redis(env.REDIS_CACHE_URL);
await new Promise((resolve, reject) => {
redis.on('ready', () => {
logger.log('Redis cache ready');
resolve(undefined);
});
redis.on('error', (error) => {
logger.error(`Redis cache error: ${error}`);
reject(undefined);
});
});

const bootstrapService = app.get(BootstrapService);
await bootstrapService.bootstrap();

Expand Down
8 changes: 4 additions & 4 deletions backend/src/modules/rgbpp/transaction/transaction.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import * as CkbRpcInterface from 'src/core/ckb-rpc/ckb-rpc.interface';
import { RgbppService } from '../rgbpp.service';
import { BI, HashType } from '@ckb-lumos/lumos';
import { Cacheable } from 'src/decorators/cacheable.decorator';
import { ONE_MONTH_MS } from 'src/common/date';
import { ONE_DAY_MS } from 'src/common/date';
import { LeapDirection } from '@prisma/client';
import { PrismaService } from 'src/core/database/prisma/prisma.service';
import { CKB_CHAIN_ID } from 'src/constants';
Expand Down Expand Up @@ -105,7 +105,7 @@ export class RgbppTransactionService {
@Cacheable({
namespace: 'RgbppTransactionService',
key: (tx: CkbRpcInterface.Transaction) => `getLeapDirectionByCkbTx:${tx.hash}`,
ttl: ONE_MONTH_MS,
ttl: ONE_DAY_MS,
})
public async getLeapDirectionByCkbTx(ckbTx: CkbRpcInterface.Transaction) {
const inputCells = await Promise.all(
Expand Down Expand Up @@ -157,7 +157,7 @@ export class RgbppTransactionService {
@Cacheable({
namespace: 'RgbppTransactionService',
key: (btcTx: BitcoinApiInterface.Transaction) => `queryRgbppLockTx:${btcTx.txid}`,
ttl: ONE_MONTH_MS,
ttl: ONE_DAY_MS,
})
public async queryRgbppLockTx(btcTx: BitcoinApiInterface.Transaction) {
const ckbTxs = await Promise.all(
Expand Down Expand Up @@ -199,7 +199,7 @@ export class RgbppTransactionService {
@Cacheable({
namespace: 'RgbppTransactionService',
key: (btcTx: BitcoinApiInterface.Transaction) => `queryRgbppBtcTimeLockTx:${btcTx.txid}`,
ttl: ONE_MONTH_MS,
ttl: ONE_DAY_MS,
})
public async queryRgbppBtcTimeLockTx(btcTx: BitcoinApiInterface.Transaction) {
const ckbTxs = (
Expand Down
1 change: 1 addition & 0 deletions frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"@lingui/macro": "^4.11.2",
"@lingui/react": "^4.11.2",
"@nervosnetwork/ckb-sdk-utils": "^0.109.2",
"@next/third-parties": "^14.2.13",
"@rgbpp-sdk/ckb": "^0.5.0",
"@tanstack/react-query": "^5.51.11",
"axios": "^1.7.2",
Expand Down
19 changes: 19 additions & 0 deletions frontend/public/robots.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
User-agent: meta-externalagent
Disallow: /

User-agent: DataForSeoBot
Disallow: /

User-agent: *
Disallow: /en/explorer/btc
Disallow: /en/explorer/ckb
Disallow: /en/assets/coins
Disallow: /en/block
Disallow: /en/transaction
Disallow: /en/address
Disallow: /explorer/btc
Disallow: /explorer/ckb
Disallow: /assets/coins
Disallow: /block
Disallow: /transaction
Disallow: /address
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use client'

import { t } from '@lingui/macro'
import { Trans } from '@lingui/macro'
import { VStack } from 'styled-system/jsx'

import ComingSoonSVG from '@/assets/coming-soon.svg'
Expand All @@ -10,7 +10,9 @@ export default function Page() {
return (
<VStack gap="10px" w="100%" bg="bg.card" maxW="content" p="30px" rounded="8px" pt="74px" pb="260px">
<ComingSoonSVG w="200px" h="200px" />
<Text color="text.third" fontSize="14px">{t`Coming soon, please stay tuned`}</Text>
<Text color="text.third" fontSize="14px">
<Trans>Coming soon, please stay tuned</Trans>
</Text>
</VStack>
)
}
3 changes: 3 additions & 0 deletions frontend/src/app/layout.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import '@/styles/globals.css'

import { GoogleAnalytics } from '@next/third-parties/google'
import type { Metadata } from 'next'
import { Montserrat } from 'next/font/google'
import type { PropsWithChildren } from 'react'

import { Footer } from '@/components/footer'
import { Navbar } from '@/components/navbar'
import { Providers } from '@/components/providers'
import { env } from '@/constants/env'
import { getLocaleFromHeaders } from '@/lib/get-locale-from-headers'

const montserrat = Montserrat({
Expand All @@ -27,6 +29,7 @@ export default function RootLayout({ children }: PropsWithChildren) {
return (
<html lang={locale}>
<body className={`${montserrat.variable}`}>
{env.share.GA_ID ? <GoogleAnalytics gaId={env.share.GA_ID} /> : null}
<Providers lang={locale}>
<Navbar />
{children}
Expand Down
5 changes: 4 additions & 1 deletion frontend/src/constants/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const publicEnvSchema = z.object({
.default('https://github.com/ckb-cell/RGBPlusPlus-design/blob/main/docs/lockscript-design-prd-en.md'),
RGBPP_SDK_URL: z.string().default('https://github.com/ckb-cell/rgbpp-sdk'),
UTXO_STACK_TWITTER_URL: z.string().default('https://x.com/utxostack'),
CKB_CELL_GITHUB_URL: z.string().default('https://github.com/ckb-cell'),
CKB_CELL_GITHUB_URL: z.string().default('https://github.com/utxostack'),
UTXO_STACK_MEDIUM_URL: z.string().default('https://medium.com/@utxostack'),
RGBPP_EXPLORER_TESTNET_URL: z.string().default('https://testnet.explorer.utxostack.network'),
RGBPP_EXPLORER_MAINNET_URL: z.string().default('https://explorer.utxostack.network'),
Expand All @@ -33,11 +33,13 @@ const publicEnvSchema = z.object({

const sharedEnvSchema = z.object({
RGBPP_EXPLORER_API_URL: z.string().default('https://testnet-api.explorer.rgbpp.io/graphql'),
GA_ID: z.string().optional(),
})

export const env = {
share: sharedEnvSchema.parse({
RGBPP_EXPLORER_API_URL: process.env.NEXT_PUBLIC_RGBPP_EXPLORER_API_URL,
GA_ID: process.env.NEXT_PUBLIC_GA_ID,
}),
internal: (typeof window === 'undefined' ? internalEnvSchema.parse(process.env) : {}) as z.infer<
typeof internalEnvSchema
Expand All @@ -52,5 +54,6 @@ export const env = {
RGBPP_SCRIPT_URL: process.env.NEXT_PUBLIC_RGBPP_SCRIPT_URL,
RGBPP_SDK_URL: process.env.NEXT_PUBLIC_RGBPP_SDK_URL,
RGBPP_DOMAINS: process.env.NEXT_PUBLIC_RGBPP_DOMAINS,
CKB_CELL_GITHUB_URL: process.env.NEXT_PUBLIC_CKB_CELL_GITHUB_URL,
}),
}
Loading

0 comments on commit 3d9491b

Please sign in to comment.