Skip to content

Commit

Permalink
fix: baseToken attributes for custom chain
Browse files Browse the repository at this point in the history
  • Loading branch information
petarTxFusion committed Jan 17, 2025
1 parent 20d2444 commit 5b44b4e
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 12 deletions.
8 changes: 8 additions & 0 deletions packages/worker/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,14 @@ ENABLE_TOKEN_OFFCHAIN_DATA_SAVER=false
UPDATE_TOKEN_OFFCHAIN_DATA_INTERVAL=86400000
SELECTED_TOKEN_OFFCHAIN_DATA_PROVIDER=coingecko

BASE_TOKEN_SYMBOL=ETH
BASE_TOKEN_DECIMALS=18
BASE_TOKEN_L1_ADDRESS=0x0000000000000000000000000000000000000000
BASE_TOKEN_ICON_URL=https://assets.coingecko.com/coins/images/279/large/ethereum.png?1698873266
BASE_TOKEN_NAME=Ether
BASE_TOKEN_LIQUIDITY=220000000000
BASE_TOKEN_USDPRICE=1800

FROM_BLOCK=0
TO_BLOCK=

Expand Down
16 changes: 16 additions & 0 deletions packages/worker/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,13 @@ export default () => {
COINGECKO_API_KEY,
DISABLE_MISSING_BLOCKS_METRIC,
CHECK_MISSING_BLOCKS_METRIC_INTERVAL,
BASE_TOKEN_SYMBOL,
BASE_TOKEN_DECIMALS,
BASE_TOKEN_L1_ADDRESS,
BASE_TOKEN_ICON_URL,
BASE_TOKEN_NAME,
BASE_TOKEN_LIQUIDITY,
BASE_TOKEN_USDPRICE,
} = process.env;

return {
Expand Down Expand Up @@ -88,6 +95,15 @@ export default () => {
isProPlan: COINGECKO_IS_PRO_PLAN === "true",
apiKey: COINGECKO_API_KEY,
},
baseToken: {
symbol: BASE_TOKEN_SYMBOL,
decimals: parseInt(BASE_TOKEN_DECIMALS, 10),
l1Address: BASE_TOKEN_L1_ADDRESS,
iconUrl: BASE_TOKEN_ICON_URL,
name: BASE_TOKEN_NAME,
liquidity: parseInt(BASE_TOKEN_LIQUIDITY, 10),
usdPrice: parseFloat(BASE_TOKEN_USDPRICE),
},
},
metrics: {
collectDbConnectionPoolMetricsInterval: parseInt(COLLECT_DB_CONNECTION_POOL_METRICS_INTERVAL, 10) || 10000,
Expand Down
34 changes: 23 additions & 11 deletions packages/worker/src/token/token.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { ContractAddress } from "../dataFetcher/types";
import parseLog from "../utils/parseLog";
import { stringTransformer } from "../transformers/string.transformer";
import { CONTRACT_INTERFACES } from "../constants";
import { ConfigService } from "@nestjs/config";

export interface Token {
l2Address: string;
Expand All @@ -31,6 +32,7 @@ export class TokenService {
private readonly blockchainService: BlockchainService,
private readonly addressRepository: AddressRepository,
private readonly tokenRepository: TokenRepository,
private readonly configService: ConfigService,
@InjectMetric(GET_TOKEN_INFO_DURATION_METRIC_NAME)
private readonly getTokenInfoDurationMetric: Histogram
) {
Expand Down Expand Up @@ -97,17 +99,27 @@ export class TokenService {
tokenAddress: contractAddress.address,
});

await this.tokenRepository.upsert({
...erc20Token,
blockNumber: contractAddress.blockNumber,
transactionHash: contractAddress.transactionHash,
l2Address: contractAddress.address,
logIndex: contractAddress.logIndex,
// add L1 address for ETH token
...(contractAddress.address.toLowerCase() === utils.L2_BASE_TOKEN_ADDRESS && {
l1Address: utils.ETH_ADDRESS,
}),
});
if (contractAddress.address.toLowerCase() === utils.L2_BASE_TOKEN_ADDRESS.toLowerCase()) {
await this.tokenRepository.upsert({
blockNumber: contractAddress.blockNumber,
transactionHash: contractAddress.transactionHash,
logIndex: contractAddress.logIndex,
l2Address: utils.L2_BASE_TOKEN_ADDRESS,
l1Address: this.configService.get<string>("tokens.baseToken.l1Address") ?? utils.ETH_ADDRESS,
symbol: this.configService.get<string>("tokens.baseToken.symbol") ?? "ETH",
name: this.configService.get<string>("tokens.baseToken.name") ?? "Ethereum",
decimals: this.configService.get<number>("tokens.baseToken.decimals") ?? 18,
iconURL: this.configService.get<string>("tokens.baseToken.iconUrl"),
});
} else {
await this.tokenRepository.upsert({
...erc20Token,
blockNumber: contractAddress.blockNumber,
transactionHash: contractAddress.transactionHash,
l2Address: contractAddress.address,
logIndex: contractAddress.logIndex,
});
}
}
}

Expand Down
19 changes: 18 additions & 1 deletion packages/worker/src/transaction/transaction.processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import {
} from "../repositories";
import { TRANSACTION_PROCESSING_DURATION_METRIC_NAME } from "../metrics";
import { TransactionData } from "../dataFetcher/types";
import { ConfigService } from "@nestjs/config";
import { utils } from "zksync-ethers";

@Injectable()
export class TransactionProcessor {
Expand All @@ -23,6 +25,7 @@ export class TransactionProcessor {
private readonly transferRepository: TransferRepository,
private readonly addressRepository: AddressRepository,
private readonly tokenRepository: TokenRepository,
private readonly configService: ConfigService,
@InjectMetric(TRANSACTION_PROCESSING_DURATION_METRIC_NAME)
private readonly transactionProcessingDurationMetric: Histogram
) {
Expand Down Expand Up @@ -103,7 +106,21 @@ export class TransactionProcessor {
});
await Promise.all(
transactionData.tokens.map((token) => {
return this.tokenRepository.upsert(token);
if (token.l2Address.toLowerCase() === utils.L2_BASE_TOKEN_ADDRESS.toLowerCase()) {
this.tokenRepository.upsert({
blockNumber: token.blockNumber,
transactionHash: token.transactionHash,
logIndex: token.logIndex,
l2Address: utils.L2_BASE_TOKEN_ADDRESS,
l1Address: this.configService.get<string>("tokens.baseToken.l1Address") ?? utils.ETH_ADDRESS,
symbol: this.configService.get<string>("tokens.baseToken.symbol") ?? "ETH",
name: this.configService.get<string>("tokens.baseToken.name") ?? "Ethereum",
decimals: this.configService.get<number>("tokens.baseToken.decimals") ?? 18,
iconURL: this.configService.get<string>("tokens.baseToken.iconUrl"),
});
} else {
this.tokenRepository.upsert(token);
}
})
);

Expand Down

0 comments on commit 5b44b4e

Please sign in to comment.