Files
Heyue_test/frontend/scripts/find-missing-tokens.js

131 lines
4.2 KiB
JavaScript
Raw Permalink Normal View History

import { createPublicClient, http, getAddress } from 'viem'
import { arbitrumSepolia } from 'viem/chains'
const client = createPublicClient({
chain: arbitrumSepolia,
transport: http('https://api.zan.top/node/v1/arb/sepolia/baf84c429d284bb5b676cb8c9ca21c07')
})
const YT_A = getAddress('0x97204190B35D9895a7a47aa7BaC61ac08De3cF05')
const USER = getAddress('0xa013422A5918CD099C63c8CC35283EACa99a705d')
const LENDING_PROXY = getAddress('0xCb4E7B1069F6C26A1c27523ce4c8dfD884552d1D')
const TRANSFER_EVENT = {
type: 'event',
name: 'Transfer',
inputs: [
{ indexed: true, name: 'from', type: 'address' },
{ indexed: true, name: 'to', type: 'address' },
{ indexed: false, name: 'value', type: 'uint256' }
]
}
async function main() {
console.log('\n追踪丢失的 YT-A 代币...\n')
console.log('你的地址:', USER)
console.log('当前余额: 10 YT-A')
console.log('之前余额: 400 YT-A')
console.log('丢失数量: 390 YT-A\n')
const latestBlock = await client.getBlockNumber()
console.log('最新区块:', latestBlock)
console.log('查询范围: 最近 10000 个区块\n')
try {
// 查找所有从用户地址发出的转账
console.log('=== 查找所有转出记录 ===\n')
const logsOut = await client.getLogs({
address: YT_A,
event: TRANSFER_EVENT,
args: { from: USER },
fromBlock: latestBlock - 10000n,
toBlock: latestBlock
})
if (logsOut.length === 0) {
console.log('✗ 未找到任何转出记录(最近 10000 个区块)\n')
} else {
console.log(`✓ 找到 ${logsOut.length} 笔转出记录:\n`)
let totalOut = 0n
logsOut.forEach((log, i) => {
const { from, to, value } = log.args
const isToLending = to.toLowerCase() === LENDING_PROXY.toLowerCase()
totalOut += value
console.log(`${i + 1}. 区块 ${log.blockNumber}`)
console.log(` 交易: ${log.transactionHash}`)
console.log(` To: ${to} ${isToLending ? '<- Lending 合约' : ''}`)
console.log(` 数量: ${Number(value) / 1e18} YT-A`)
console.log()
})
console.log(`总转出: ${Number(totalOut) / 1e18} YT-A\n`)
}
// 查找所有转入用户地址的转账
console.log('=== 查找所有转入记录 ===\n')
const logsIn = await client.getLogs({
address: YT_A,
event: TRANSFER_EVENT,
args: { to: USER },
fromBlock: latestBlock - 10000n,
toBlock: latestBlock
})
if (logsIn.length === 0) {
console.log('✗ 未找到任何转入记录\n')
} else {
console.log(`✓ 找到 ${logsIn.length} 笔转入记录:\n`)
let totalIn = 0n
logsIn.forEach((log, i) => {
const { from, to, value } = log.args
totalIn += value
console.log(`${i + 1}. 区块 ${log.blockNumber}`)
console.log(` 交易: ${log.transactionHash}`)
console.log(` From: ${from}`)
console.log(` 数量: ${Number(value) / 1e18} YT-A`)
console.log()
})
console.log(`总转入: ${Number(totalIn) / 1e18} YT-A\n`)
}
// 计算净流出
if (logsOut.length > 0 || logsIn.length > 0) {
const totalOut = logsOut.reduce((sum, log) => sum + log.args.value, 0n)
const totalIn = logsIn.reduce((sum, log) => sum + log.args.value, 0n)
const netFlow = Number(totalIn - totalOut) / 1e18
console.log('=== 汇总 ===')
console.log(`总转入: ${Number(totalIn) / 1e18} YT-A`)
console.log(`总转出: ${Number(totalOut) / 1e18} YT-A`)
console.log(`净变化: ${netFlow > 0 ? '+' : ''}${netFlow} YT-A`)
console.log(`当前余额: 10 YT-A\n`)
// 检查代币是否在 Lending 合约中
console.log('=== 检查 Lending 合约余额 ===')
const lendingBalance = await client.readContract({
address: YT_A,
abi: [{
inputs: [{ name: 'account', type: 'address' }],
name: 'balanceOf',
outputs: [{ name: '', type: 'uint256' }],
stateMutability: 'view',
type: 'function'
}],
functionName: 'balanceOf',
args: [LENDING_PROXY]
})
console.log(`Lending 合约持有的 YT-A: ${Number(lendingBalance) / 1e18}\n`)
}
} catch (error) {
console.error('查询失败:', error.message)
}
}
main()