Files
Heyue_test/frontend/scripts/check-withdraw-transactions.js

107 lines
3.4 KiB
JavaScript
Raw Permalink Normal View History

import { createPublicClient, http, getAddress, keccak256, toBytes } from 'viem'
import { arbitrumSepolia } from 'viem/chains'
const client = createPublicClient({
chain: arbitrumSepolia,
transport: http('https://api.zan.top/node/v1/arb/sepolia/baf84c429d284bb5b676cb8c9ca21c07')
})
const LENDING_PROXY = getAddress('0xCb4E7B1069F6C26A1c27523ce4c8dfD884552d1D')
const USER = getAddress('0xa013422A5918CD099C63c8CC35283EACa99a705d')
// 计算函数选择器
const withdrawSelector = keccak256(toBytes('withdraw(uint256)')).slice(0, 10)
const supplyCollateralSelector = keccak256(toBytes('supplyCollateral(address,uint256)')).slice(0, 10)
console.log('\n=== 查找用户的交易记录 ===\n')
console.log('用户地址:', USER)
console.log('Lending 合约:', LENDING_PROXY)
console.log('\n函数选择器:')
console.log(' withdraw:', withdrawSelector)
console.log(' supplyCollateral:', supplyCollateralSelector)
console.log()
async function main() {
const latestBlock = await client.getBlockNumber()
console.log('最新区块:', latestBlock)
console.log('查询范围: 最近 10000 个区块\n')
// 获取用户发送到 Lending 合约的所有交易
const fromBlock = latestBlock - 10000n
const toBlock = latestBlock
console.log('=== 查找所有用户 → Lending 的交易 ===\n')
let blockNum = fromBlock
const transactions = []
while (blockNum <= toBlock) {
const endBlock = blockNum + 1000n > toBlock ? toBlock : blockNum + 1000n
try {
const block = await client.getBlock({
blockNumber: blockNum,
includeTransactions: true
})
for (const tx of block.transactions) {
if (typeof tx === 'object' &&
tx.from.toLowerCase() === USER.toLowerCase() &&
tx.to?.toLowerCase() === LENDING_PROXY.toLowerCase()) {
transactions.push({
hash: tx.hash,
blockNumber: block.number,
input: tx.input
})
}
}
} catch (error) {
// Skip blocks without transactions
}
blockNum += 1000n
}
console.log(`找到 ${transactions.length} 笔交易\n`)
if (transactions.length === 0) {
console.log('没有找到任何交易')
return
}
// 分析每笔交易
for (const tx of transactions) {
console.log('---')
console.log('交易哈希:', tx.hash)
console.log('区块:', tx.blockNumber.toString())
const selector = tx.input.slice(0, 10)
console.log('函数选择器:', selector)
let functionName = '未知'
if (selector === withdrawSelector) {
functionName = 'withdraw (借款/提取)'
// 解析参数 (uint256)
const amountHex = '0x' + tx.input.slice(10)
const amount = BigInt(amountHex)
console.log('调用函数:', functionName)
console.log('金额:', Number(amount) / 1e6, 'USDC')
} else if (selector === supplyCollateralSelector) {
functionName = 'supplyCollateral (存入抵押品)'
// 解析参数 (address, uint256)
const assetAddress = '0x' + tx.input.slice(34, 74)
const amountHex = '0x' + tx.input.slice(74)
const amount = BigInt(amountHex)
console.log('调用函数:', functionName)
console.log('资产:', getAddress(assetAddress))
console.log('金额:', Number(amount) / 1e18)
} else {
console.log('调用函数:', functionName, '-', selector)
}
console.log()
}
}
main().catch(console.error)