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:
parent
8cea0e7998
commit
5e670e64b3
|
|
@ -143,6 +143,9 @@ export class DepositDetectionService implements OnModuleInit {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 获取代币的实际 decimals(USDT 通常是 6 位,而不是 18 位)
|
||||||
|
const tokenDecimals = await this.evmProvider.getTokenDecimals(chainType, event.tokenContract);
|
||||||
|
|
||||||
// 创建充值记录 - 用户地址
|
// 创建充值记录 - 用户地址
|
||||||
const deposit = DepositTransaction.create({
|
const deposit = DepositTransaction.create({
|
||||||
chainType,
|
chainType,
|
||||||
|
|
@ -150,7 +153,7 @@ export class DepositDetectionService implements OnModuleInit {
|
||||||
fromAddress: EvmAddress.fromUnchecked(event.from),
|
fromAddress: EvmAddress.fromUnchecked(event.from),
|
||||||
toAddress: EvmAddress.fromUnchecked(event.to),
|
toAddress: EvmAddress.fromUnchecked(event.to),
|
||||||
tokenContract: EvmAddress.fromUnchecked(event.tokenContract),
|
tokenContract: EvmAddress.fromUnchecked(event.tokenContract),
|
||||||
amount: TokenAmount.fromRaw(event.value, 18),
|
amount: TokenAmount.fromRaw(event.value, tokenDecimals),
|
||||||
blockNumber: BlockNumber.create(event.blockNumber),
|
blockNumber: BlockNumber.create(event.blockNumber),
|
||||||
blockTimestamp: event.blockTimestamp,
|
blockTimestamp: event.blockTimestamp,
|
||||||
logIndex: event.logIndex,
|
logIndex: event.logIndex,
|
||||||
|
|
|
||||||
|
|
@ -137,6 +137,16 @@ export class EvmProviderAdapter {
|
||||||
return TokenAmount.fromRaw(balance, Number(decimals));
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 查询原生代币余额
|
* 查询原生代币余额
|
||||||
*/
|
*/
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue