120 lines
3.5 KiB
JavaScript
120 lines
3.5 KiB
JavaScript
|
|
import { createPublicClient, http, getAddress, keccak256, toHex, pad, concat } 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 YT_A = getAddress('0x97204190B35D9895a7a47aa7BaC61ac08De3cF05')
|
||
|
|
|
||
|
|
// Compound V3 使用的公开状态变量
|
||
|
|
const COMET_ABI = [
|
||
|
|
{
|
||
|
|
inputs: [
|
||
|
|
{ name: 'account', type: 'address' },
|
||
|
|
{ name: 'asset', type: 'address' }
|
||
|
|
],
|
||
|
|
name: 'userCollateral',
|
||
|
|
outputs: [
|
||
|
|
{
|
||
|
|
components: [
|
||
|
|
{ name: 'balance', type: 'uint128' },
|
||
|
|
{ name: '_reserved', type: 'uint128' }
|
||
|
|
],
|
||
|
|
name: '',
|
||
|
|
type: 'tuple'
|
||
|
|
}
|
||
|
|
],
|
||
|
|
stateMutability: 'view',
|
||
|
|
type: 'function'
|
||
|
|
},
|
||
|
|
{
|
||
|
|
inputs: [{ name: 'account', type: 'address' }],
|
||
|
|
name: 'balanceOf',
|
||
|
|
outputs: [{ name: '', type: 'uint256' }],
|
||
|
|
stateMutability: 'view',
|
||
|
|
type: 'function'
|
||
|
|
},
|
||
|
|
{
|
||
|
|
inputs: [{ name: 'account', type: 'address' }],
|
||
|
|
name: 'borrowBalanceOf',
|
||
|
|
outputs: [{ name: '', type: 'uint256' }],
|
||
|
|
stateMutability: 'view',
|
||
|
|
type: 'function'
|
||
|
|
},
|
||
|
|
{
|
||
|
|
inputs: [
|
||
|
|
{ name: 'account', type: 'address' },
|
||
|
|
{ name: 'asset', type: 'address' }
|
||
|
|
],
|
||
|
|
name: 'collateralBalanceOf',
|
||
|
|
outputs: [{ name: '', type: 'uint128' }],
|
||
|
|
stateMutability: 'view',
|
||
|
|
type: 'function'
|
||
|
|
}
|
||
|
|
]
|
||
|
|
|
||
|
|
async function main() {
|
||
|
|
console.log('\n检查 Compound V3 存储结构\n')
|
||
|
|
console.log('合约:', LENDING_PROXY)
|
||
|
|
console.log('用户:', USER)
|
||
|
|
console.log('抵押品:', YT_A)
|
||
|
|
console.log()
|
||
|
|
|
||
|
|
// 测试 Compound V3 原生函数
|
||
|
|
for (const func of COMET_ABI) {
|
||
|
|
console.log(`--- ${func.name} ---`)
|
||
|
|
try {
|
||
|
|
let result
|
||
|
|
if (func.name === 'userCollateral') {
|
||
|
|
result = await client.readContract({
|
||
|
|
address: LENDING_PROXY,
|
||
|
|
abi: [func],
|
||
|
|
functionName: func.name,
|
||
|
|
args: [USER, YT_A]
|
||
|
|
})
|
||
|
|
console.log('✓ 成功!')
|
||
|
|
console.log(' balance:', result.balance.toString(), '(', Number(result.balance) / 1e18, 'YT-A )')
|
||
|
|
console.log(' _reserved:', result._reserved.toString())
|
||
|
|
} else if (func.name === 'collateralBalanceOf') {
|
||
|
|
result = await client.readContract({
|
||
|
|
address: LENDING_PROXY,
|
||
|
|
abi: [func],
|
||
|
|
functionName: func.name,
|
||
|
|
args: [USER, YT_A]
|
||
|
|
})
|
||
|
|
console.log('✓ 成功:', result.toString(), '(', Number(result) / 1e18, 'YT-A )')
|
||
|
|
} else if (func.name === 'balanceOf') {
|
||
|
|
result = await client.readContract({
|
||
|
|
address: LENDING_PROXY,
|
||
|
|
abi: [func],
|
||
|
|
functionName: func.name,
|
||
|
|
args: [USER]
|
||
|
|
})
|
||
|
|
console.log('✓ 成功:', result.toString(), '(', Number(result) / 1e6, 'USDC )')
|
||
|
|
} else {
|
||
|
|
result = await client.readContract({
|
||
|
|
address: LENDING_PROXY,
|
||
|
|
abi: [func],
|
||
|
|
functionName: func.name,
|
||
|
|
args: [USER]
|
||
|
|
})
|
||
|
|
console.log('✓ 成功:', result.toString())
|
||
|
|
}
|
||
|
|
} catch (error) {
|
||
|
|
console.log('✗ 失败:', error.message.split('\n')[0])
|
||
|
|
}
|
||
|
|
console.log()
|
||
|
|
}
|
||
|
|
|
||
|
|
console.log('=== 结论 ===')
|
||
|
|
console.log('如果上面的 Compound V3 原生函数能工作,')
|
||
|
|
console.log('说明合约使用了 Compound V3 的数据结构,')
|
||
|
|
console.log('前端应该调用这些函数而不是自定义的 getUserCollateral 等。\n')
|
||
|
|
}
|
||
|
|
|
||
|
|
main()
|