
Python調(diào)用Google Bard API 完整指南
Etherscan API 可以被一些替代方案替代,它們?nèi)缦拢?/p>
要開始使用 Etherscan API,您需要在 Etherscan 上創(chuàng)建一個(gè)帳戶并獲取 API 密鑰。要創(chuàng)建帳戶,請轉(zhuǎn)到此鏈接并點(diǎn)擊屏幕右上角的“登錄”按鈕。
之后,點(diǎn)擊登錄框下方的藍(lán)色“注冊”按鈕。要注冊新賬戶,您需要提供電子郵件地址并創(chuàng)建唯一的用戶名和密碼。
完成后,點(diǎn)擊藍(lán)色的“創(chuàng)建帳戶”按鈕。之后,轉(zhuǎn)到您提供的電子郵件地址并打開確認(rèn)電子郵件。確認(rèn)后,登錄您新創(chuàng)建的帳戶。
登錄后,導(dǎo)航到“API-KEY”下拉菜單,然后單擊藍(lán)色的“+添加”按鈕來創(chuàng)建一個(gè)新的API密鑰,如下圖所示:
命名您的 API 密鑰,然后您就可以開始使用和探索它了,這將在以下文章標(biāo)題中展示。
要使用 Etherscan API 獲取單個(gè)地址的以太幣余額,您需要使用提供的地址和 API 密鑰向帳戶模塊和余額操作端點(diǎn)發(fā)出請求。
為此,我們將使用 Python 請求庫,并且為了增加趣味性,我們將使用可在網(wǎng)上找到的已知鯨魚地址:
requests.get('https://api.etherscan.io/api?module=account&action=balance&address=0x4976a4a02f38326660d17bf34b431dc6e2eb2327&tag=latest&apikey=A1C15S3AXQHQ7PVVDX63VVK2IBAECS448Z').json()
{'status': '1', 'message': 'OK', 'result':'29724721687169089009293'}
要使用 Etherscan API 獲取正常交易列表,您需要向帳戶模塊和 txlist 操作端點(diǎn)發(fā)送請求,每次調(diào)用將返回最多 10k 條記錄。
此端點(diǎn)將與 pandas 庫一起使用來創(chuàng)建數(shù)據(jù)框。由于端點(diǎn)有點(diǎn)混亂,我將以更清晰的方式展示它們,調(diào)用它們的方式與我們之前的示例相同。
https://api.etherscan.io/api
?module=account
&action=txlist
&address=0xddbd2b932c763ba5b1b7ae3b362eac3e8d40121a
&startblock=0
&endblock=99999999
&page=1
&offset=10
&sort=asc
&apikey=YourApiKeyToken
這將存儲在數(shù)據(jù)變量內(nèi)部,我們將響應(yīng)轉(zhuǎn)換為 pandas 數(shù)據(jù)框并以轉(zhuǎn)置的方式打印出最后 5 個(gè)點(diǎn)以查看所有返回參數(shù)。
data = pd.DataFrame(data['result'])
data.tail().T
要使用 Etherscan API 按地址獲取 ERC20 代幣轉(zhuǎn)移事件列表,您需要向帳戶模塊和 tokentx 操作端點(diǎn)發(fā)送請求。此端點(diǎn)需要合約的地址和地址端點(diǎn)。
https://api.etherscan.io/api
?module=account
&action=tokentx
&contractaddress=0x9f8f72aa9304c8b593d555f12ef6589cc3a579a2
&address=0x4e83362442b8d1bec281594cea3050c8eb01311c
&page=1
&offset=100
&startblock=0
&endblock=27025780
&sort=asc
&apikey=YourApiKeyToken
我們將響應(yīng)放入數(shù)據(jù)框中,就像我們在前面的例子中所做的那樣。
當(dāng)合約模塊和 getabi 操作端點(diǎn)被請求時(shí),Etherscan API 可以獲取合約數(shù)據(jù)。此端點(diǎn)還需要 API 密鑰和地址參數(shù)。
https://api.etherscan.io/api
?module=contract
&action=getabi
&address=0xBB9bc244D798123fDe783fCc1C72d3Bb8C189413
&apikey=YourApiKeyToken
要使用 Etherscan API 檢查交易狀態(tài),用戶需要向交易模塊和gettxreceiptstatus
操作發(fā)出請求。交易哈希碼也需要作為參數(shù)發(fā)送。
https://api.etherscan.io/api
?module=transaction
&action=gettxreceiptstatus
&txhash=0x513c1ba0bebf66436b5fed86ab668452b7805593c05073eb2d51d3a52f480a76
&apikey=YourApiKeyToken
{'status': '1', 'message': 'OK', 'result': {'status': '1'}}
要使用 Etherscan API 獲取塊數(shù)據(jù),您需要getblockreward
使用提供的塊號作為請求參數(shù)來訪問塊模塊和操作端點(diǎn)。
https://api.etherscan.io/api
?module=block
&action=getblockreward
&blockno=2165403
&apikey=YourApiKeyToken
要使用 Etherscan API 獲取日志,您需要向日志模塊和getLogs
操作端點(diǎn)發(fā)出 API 請求。所需的參數(shù)是發(fā)件人/收件人區(qū)塊、地址、主題和 API 密鑰。以下是示例請求:
https://api.etherscan.io/api
?module=logs
&action=getLogs
&fromBlock=379224
&toBlock=latest
&address=0x33990122638b9132ca29c723bdf037f1a891a70c
&topic0=0xf63780e752c6a54a94fc52715dbc5518a3b4c3c2833d301a204226548a2a8545
&apikey=YourApiKeyToken
要使用帶有 Etherscan API 的 Geth/Parity 代理,您需要訪問代理模塊端點(diǎn)。此模塊下有多個(gè)端點(diǎn),例如eth_blockNumber
、、等等eth_getBlockByNumber
。eth_call
例如,我們將獲取eth_getStorageAt
從指定地址的存儲位置返回值的端點(diǎn):
https://api.etherscan.io/api
?module=proxy
&action=eth_getStorageAt
&address=0x6e03d9cce9d60f3e9f2597e13cd4c54c55330cfd
&position=0x0
&tag=latest
&apikey=YourApiKeyToken
{'jsonrpc': '2.0',
'id': 1,
'result': '0x0000000000000000000000003d0768da09ce77d25e2d998e6a7b6ed4b9116c2d'}
要使用 Etherscan API 獲取代幣數(shù)據(jù),您將可以訪問多個(gè)端點(diǎn),這些端點(diǎn)可以返回 ERC20 代幣供應(yīng)、賬戶余額、歷史數(shù)據(jù)等內(nèi)容。
為了獲得代幣供應(yīng),我們執(zhí)行以下操作:
https://api.etherscan.io/api
?module=stats
&action=tokensupply
&contractaddress=0x57d90b64a1a57749b0f932f1a3395792e12e7055
&apikey=YourApiKeyToken
{'status': '1', 'message': 'OK', 'result': '21265524714464'}
要將 gas 跟蹤器與 Etherscan API 結(jié)合使用,您必須查看可用 gas 端點(diǎn)列表。Etherscan API 提供的端點(diǎn)包括 gas 估算、gas 預(yù)言機(jī)、每日 gas 限額、總 gas 使用量、gas 價(jià)格等。
例如,讓我們獲得氣體確認(rèn)時(shí)間的估計(jì):
https://api.etherscan.io/api
?module=gastracker
&action=gasestimate
&gasprice=2000000000
&apikey=YourApiKeyToken
{'status': '1', 'message': 'OK', 'result': '3615'}
Etherscan API 可以獲得各種統(tǒng)計(jì)數(shù)據(jù),例如以太幣的總供應(yīng)量、最新價(jià)格、節(jié)點(diǎn)總數(shù)、節(jié)點(diǎn)大小、平均網(wǎng)絡(luò)哈希率、歷史市值、交易數(shù)量、新地址數(shù)量、交易費(fèi)用等。
例如,我們來看看 Ether 2.0 的總供應(yīng)量:
https://api.etherscan.io/api
?module=stats
&action=ethsupply2
&apikey=YourApiKeyToken
{'status': '1',
'message': 'OK',
'result': {'EthSupply': '118194367499000000000000000',
'Eth2Staking': '309618220365297000000000',
'BurntFees': '738514872810670998934509'}}