112 lines
3.6 KiB
JavaScript
112 lines
3.6 KiB
JavaScript
|
|
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 LENDING_PROXY = getAddress('0xCb4E7B1069F6C26A1c27523ce4c8dfD884552d1D')
|
||
|
|
const USER = getAddress('0xa013422A5918CD099C63c8CC35283EACa99a705d')
|
||
|
|
const YT_A = getAddress('0x97204190B35D9895a7a47aa7BaC61ac08De3cF05')
|
||
|
|
|
||
|
|
async function testFunction(name, abi, args) {
|
||
|
|
try {
|
||
|
|
const result = await client.readContract({
|
||
|
|
address: LENDING_PROXY,
|
||
|
|
abi: [abi],
|
||
|
|
functionName: name,
|
||
|
|
args: args
|
||
|
|
})
|
||
|
|
console.log(`✓ ${name} 成功:`, result)
|
||
|
|
return { success: true, result }
|
||
|
|
} catch (error) {
|
||
|
|
console.log(`✗ ${name} 失败:`, error.message.split('\n')[0])
|
||
|
|
return { success: false, error: error.message }
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
async function main() {
|
||
|
|
console.log('\n测试不同的抵押品查询函数...\n')
|
||
|
|
console.log('合约:', LENDING_PROXY)
|
||
|
|
console.log('用户:', USER)
|
||
|
|
console.log('抵押品:', YT_A, '\n')
|
||
|
|
|
||
|
|
// 测试 1: userCollateral (Compound V3 标准)
|
||
|
|
console.log('--- 测试 1: userCollateral(address, address) ---')
|
||
|
|
await testFunction('userCollateral', {
|
||
|
|
inputs: [
|
||
|
|
{ name: 'account', type: 'address' },
|
||
|
|
{ name: 'asset', type: 'address' }
|
||
|
|
],
|
||
|
|
name: 'userCollateral',
|
||
|
|
outputs: [
|
||
|
|
{ name: 'balance', type: 'uint128' },
|
||
|
|
{ name: '_reserved', type: 'uint128' }
|
||
|
|
],
|
||
|
|
stateMutability: 'view',
|
||
|
|
type: 'function'
|
||
|
|
}, [USER, YT_A])
|
||
|
|
|
||
|
|
// 测试 2: getUserCollateral
|
||
|
|
console.log('\n--- 测试 2: getUserCollateral(address, address) ---')
|
||
|
|
await testFunction('getUserCollateral', {
|
||
|
|
inputs: [
|
||
|
|
{ name: '_user', type: 'address' },
|
||
|
|
{ name: '_collateralAsset', type: 'address' }
|
||
|
|
],
|
||
|
|
name: 'getUserCollateral',
|
||
|
|
outputs: [{ name: '', type: 'uint256' }],
|
||
|
|
stateMutability: 'view',
|
||
|
|
type: 'function'
|
||
|
|
}, [USER, YT_A])
|
||
|
|
|
||
|
|
// 测试 3: collateralBalanceOf
|
||
|
|
console.log('\n--- 测试 3: collateralBalanceOf(address, address) ---')
|
||
|
|
await testFunction('collateralBalanceOf', {
|
||
|
|
inputs: [
|
||
|
|
{ name: 'account', type: 'address' },
|
||
|
|
{ name: 'asset', type: 'address' }
|
||
|
|
],
|
||
|
|
name: 'collateralBalanceOf',
|
||
|
|
outputs: [{ name: '', type: 'uint256' }],
|
||
|
|
stateMutability: 'view',
|
||
|
|
type: 'function'
|
||
|
|
}, [USER, YT_A])
|
||
|
|
|
||
|
|
// 测试 4: getUserAccountData
|
||
|
|
console.log('\n--- 测试 4: getUserAccountData(address) ---')
|
||
|
|
const result = await testFunction('getUserAccountData', {
|
||
|
|
inputs: [{ name: '_user', type: 'address' }],
|
||
|
|
name: 'getUserAccountData',
|
||
|
|
outputs: [
|
||
|
|
{ name: 'totalCollateralValue', type: 'uint256' },
|
||
|
|
{ name: 'totalBorrowValue', type: 'uint256' },
|
||
|
|
{ name: 'availableToBorrow', type: 'uint256' },
|
||
|
|
{ name: 'healthFactor', type: 'uint256' }
|
||
|
|
],
|
||
|
|
stateMutability: 'view',
|
||
|
|
type: 'function'
|
||
|
|
}, [USER])
|
||
|
|
|
||
|
|
if (result.success) {
|
||
|
|
console.log('\n详细结果:')
|
||
|
|
console.log(' totalCollateralValue:', result.result[0].toString())
|
||
|
|
console.log(' totalBorrowValue:', result.result[1].toString())
|
||
|
|
console.log(' availableToBorrow:', result.result[2].toString())
|
||
|
|
console.log(' healthFactor:', result.result[3].toString())
|
||
|
|
}
|
||
|
|
|
||
|
|
// 测试 5: borrowBalanceOf (检查借款)
|
||
|
|
console.log('\n--- 测试 5: borrowBalanceOf(address) ---')
|
||
|
|
await testFunction('borrowBalanceOf', {
|
||
|
|
inputs: [{ name: 'account', type: 'address' }],
|
||
|
|
name: 'borrowBalanceOf',
|
||
|
|
outputs: [{ name: '', type: 'uint256' }],
|
||
|
|
stateMutability: 'view',
|
||
|
|
type: 'function'
|
||
|
|
}, [USER])
|
||
|
|
}
|
||
|
|
|
||
|
|
main()
|