fix(blockchain): read token decimals from contract for deposit detection

Previously decimals were hardcoded to 18, causing incorrect amount parsing
for USDT which uses 6 decimals. Now reads decimals() from ERC20 contract
when processing deposits.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
hailin 2025-12-10 11:57:34 -08:00
parent 8cea0e7998
commit 5e670e64b3
2 changed files with 14 additions and 1 deletions

View File

@ -143,6 +143,9 @@ export class DepositDetectionService implements OnModuleInit {
return;
}
// 获取代币的实际 decimalsUSDT 通常是 6 位,而不是 18 位)
const tokenDecimals = await this.evmProvider.getTokenDecimals(chainType, event.tokenContract);
// 创建充值记录 - 用户地址
const deposit = DepositTransaction.create({
chainType,
@ -150,7 +153,7 @@ export class DepositDetectionService implements OnModuleInit {
fromAddress: EvmAddress.fromUnchecked(event.from),
toAddress: EvmAddress.fromUnchecked(event.to),
tokenContract: EvmAddress.fromUnchecked(event.tokenContract),
amount: TokenAmount.fromRaw(event.value, 18),
amount: TokenAmount.fromRaw(event.value, tokenDecimals),
blockNumber: BlockNumber.create(event.blockNumber),
blockTimestamp: event.blockTimestamp,
logIndex: event.logIndex,

View File

@ -137,6 +137,16 @@ export class EvmProviderAdapter {
return TokenAmount.fromRaw(balance, Number(decimals));
}
/**
* ERC20 decimals
*/
async getTokenDecimals(chainType: ChainType, tokenContract: string): Promise<number> {
const provider = this.getProvider(chainType);
const contract = new Contract(tokenContract, ERC20_BALANCE_ABI, provider);
const decimals = await contract.decimals();
return Number(decimals);
}
/**
*
*/