135 lines
4.4 KiB
JavaScript
135 lines
4.4 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 main() {
|
|||
|
|
console.log('\n测试合约文档中的正确函数名\n')
|
|||
|
|
console.log('合约:', LENDING_PROXY)
|
|||
|
|
console.log('用户:', USER)
|
|||
|
|
console.log('抵押品:', YT_A)
|
|||
|
|
console.log()
|
|||
|
|
|
|||
|
|
// 1. 测试 getCollateral(正确的函数名)
|
|||
|
|
console.log('=== 1. getCollateral(account, asset) ===')
|
|||
|
|
try {
|
|||
|
|
const collateral = await client.readContract({
|
|||
|
|
address: LENDING_PROXY,
|
|||
|
|
abi: [{
|
|||
|
|
inputs: [
|
|||
|
|
{ internalType: 'address', name: 'account', type: 'address' },
|
|||
|
|
{ internalType: 'address', name: 'asset', type: 'address' }
|
|||
|
|
],
|
|||
|
|
name: 'getCollateral',
|
|||
|
|
outputs: [{ internalType: 'uint256', name: '', type: 'uint256' }],
|
|||
|
|
stateMutability: 'view',
|
|||
|
|
type: 'function'
|
|||
|
|
}],
|
|||
|
|
functionName: 'getCollateral',
|
|||
|
|
args: [USER, YT_A]
|
|||
|
|
})
|
|||
|
|
console.log('✓ 成功!')
|
|||
|
|
console.log(' 抵押品余额:', Number(collateral) / 1e18, 'YT-A')
|
|||
|
|
console.log(' 原始值:', collateral.toString())
|
|||
|
|
} catch (error) {
|
|||
|
|
console.log('✗ 失败:', error.message.split('\n')[0])
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// 2. 测试 getBalance(用户USDC余额)
|
|||
|
|
console.log('\n=== 2. getBalance(account) ===')
|
|||
|
|
try {
|
|||
|
|
const balance = await client.readContract({
|
|||
|
|
address: LENDING_PROXY,
|
|||
|
|
abi: [{
|
|||
|
|
inputs: [{ internalType: 'address', name: 'account', type: 'address' }],
|
|||
|
|
name: 'getBalance',
|
|||
|
|
outputs: [{ internalType: 'int256', name: '', type: 'int256' }],
|
|||
|
|
stateMutability: 'view',
|
|||
|
|
type: 'function'
|
|||
|
|
}],
|
|||
|
|
functionName: 'getBalance',
|
|||
|
|
args: [USER]
|
|||
|
|
})
|
|||
|
|
console.log('✓ 成功!')
|
|||
|
|
console.log(' USDC 余额:', Number(balance) / 1e6, 'USDC')
|
|||
|
|
console.log(' 原始值:', balance.toString())
|
|||
|
|
} catch (error) {
|
|||
|
|
console.log('✗ 失败:', error.message.split('\n')[0])
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// 3. 测试 borrowBalanceOf
|
|||
|
|
console.log('\n=== 3. borrowBalanceOf(account) ===')
|
|||
|
|
try {
|
|||
|
|
const borrowBalance = await client.readContract({
|
|||
|
|
address: LENDING_PROXY,
|
|||
|
|
abi: [{
|
|||
|
|
inputs: [{ internalType: 'address', name: 'account', type: 'address' }],
|
|||
|
|
name: 'borrowBalanceOf',
|
|||
|
|
outputs: [{ internalType: 'uint256', name: '', type: 'uint256' }],
|
|||
|
|
stateMutability: 'view',
|
|||
|
|
type: 'function'
|
|||
|
|
}],
|
|||
|
|
functionName: 'borrowBalanceOf',
|
|||
|
|
args: [USER]
|
|||
|
|
})
|
|||
|
|
console.log('✓ 成功!')
|
|||
|
|
console.log(' 借款余额:', Number(borrowBalance) / 1e6, 'USDC')
|
|||
|
|
} catch (error) {
|
|||
|
|
console.log('✗ 失败:', error.message.split('\n')[0])
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// 4. 测试系统函数
|
|||
|
|
console.log('\n=== 4. getTotalBorrow() ===')
|
|||
|
|
try {
|
|||
|
|
const totalBorrow = await client.readContract({
|
|||
|
|
address: LENDING_PROXY,
|
|||
|
|
abi: [{
|
|||
|
|
inputs: [],
|
|||
|
|
name: 'getTotalBorrow',
|
|||
|
|
outputs: [{ internalType: 'uint256', name: '', type: 'uint256' }],
|
|||
|
|
stateMutability: 'view',
|
|||
|
|
type: 'function'
|
|||
|
|
}],
|
|||
|
|
functionName: 'getTotalBorrow'
|
|||
|
|
})
|
|||
|
|
console.log('✓ 成功!')
|
|||
|
|
console.log(' 系统总借款:', Number(totalBorrow) / 1e6, 'USDC')
|
|||
|
|
} catch (error) {
|
|||
|
|
console.log('✗ 失败:', error.message.split('\n')[0])
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
console.log('\n=== 5. getTotalSupply() ===')
|
|||
|
|
try {
|
|||
|
|
const totalSupply = await client.readContract({
|
|||
|
|
address: LENDING_PROXY,
|
|||
|
|
abi: [{
|
|||
|
|
inputs: [],
|
|||
|
|
name: 'getTotalSupply',
|
|||
|
|
outputs: [{ internalType: 'uint256', name: '', type: 'uint256' }],
|
|||
|
|
stateMutability: 'view',
|
|||
|
|
type: 'function'
|
|||
|
|
}],
|
|||
|
|
functionName: 'getTotalSupply'
|
|||
|
|
})
|
|||
|
|
console.log('✓ 成功!')
|
|||
|
|
console.log(' 系统总供应:', Number(totalSupply) / 1e6, 'USDC')
|
|||
|
|
} catch (error) {
|
|||
|
|
console.log('✗ 失败:', error.message.split('\n')[0])
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
console.log('\n=== 结论 ===')
|
|||
|
|
console.log('如果上面的函数都能工作,说明:')
|
|||
|
|
console.log('1. 合约使用的是文档中的函数名(getCollateral 等)')
|
|||
|
|
console.log('2. 前端 ABI 需要更新为文档中的正确函数名')
|
|||
|
|
console.log('3. getUserCollateral 和 getUserAccountData 不存在于合约中\n')
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
main()
|