导航

批量ETH地址余额查询

#1
因为有需要大量ETH地址查询余额和交易记录
发现中科链安和OKLink全部都不能大批量查询(10次以内)
然后百度了下各种接口都不行,我就自己动手丰衣足食吧
程序交流那个板块我也发不了,绝非水贴
请教师傅后,写个脚本,批量调用jsonrpc,掉节点rpc想查多少查多少啊
const getEthBalance = (address) => {
  const headers = new Headers();
  headers.append('Content-Type', 'application/json');

  const rawData = JSON.stringify({
    id: '1',
    jsonrpc: '2.0',
    method: 'eth_getBalance',
    params: [address, 'latest'],
  });

  const opts = {
    method: 'POST',
    headers,
    body: rawData,
    redirect: 'follow',
  };

  return fetch('https://cloudflare-eth.com/', opts)
    .then((response) => response.json())
    .then(({result}) => Number(result))
    .catch(console.error)
};

const getEthBalances = async addrs => {
    for (const addr of addrs) {
        const balanceWei = await getEthBalance(addr);
        const balanceEther = Number(balanceWei)/1e18;
        console.log(`${addr}: ${balanceEther} Ether`)
    }
}

await getEthBalances([
    "0xabea9132b05a70803a4e85094fd0e1800777fbef"
])

然后就写出来了,用法呢
cloudflare-eth 的接口允许跨域的,无所谓你从哪个网站发起这个请求
浏览器开个标签,访问 about:blank。
然后 F12打开开发者工具,执行就可以了
如果限制频率那就换其他节点,可以去 https://chainlist.org 找免费的。
如果是数量确实非常大,那就得更换为 multicall 智能合约来实现,调节点可能会太频繁。
然后发现这样速度还是一般般哈,我就直接改成python了

import requests

def get_eth_balance(address):
    headers = {
        'Content-Type': 'application/json'
    }

    data = {
        'id': '1',
        'jsonrpc': '2.0',
        'method': 'eth_getBalance',
        'params': [address, 'latest']
    }

    response = requests.post('https://cloudflare-eth.com/', headers=headers, json=data)
    response.raise_for_status()

    return response.json()['result']

def get_eth_balances():
    with open('addres.txt', 'r') as file:
        addresses = file.read().splitlines()

    results = []
    for address in addresses:
        balance = get_eth_balance(address)
        if balance != '0x0':
            balance = float(balance)
        else:
            balance = 0.0
        result = f'{address}: {balance / 1e18} Ether'
        results.append(result)
        print(result)

    with open('ethamount.txt', 'w') as file:
        file.write('\n'.join(results))

if __name__ == '__main__':
    get_eth_balances()
导出后文件为ethamount.txt
python3然后需要pip requests库
速度嘎嘎的
2023-12-16
#2
这有啥用?
2023-12-18
#3
回复 2# Ameng
用链必追配合这个快速找出收款eth账户啊!