主页 > imtoken钱包官方网站 > peth:以太坊命令行工具
peth:以太坊命令行工具
imtoken钱包官方网站 2023-05-28 05:42:31
“不能安装seth,只能写peth”
在M1 mac上安装dapptools遇到各种奇怪的问题,其实我只是想用seth。 .
于是在复工的第一天,写了一个python版的seth,取名为peth。
与seth相比以太坊rpc,它做了很多简化。 首先,它不支持加载私钥,只允许eth_call调用合约。 不过我目前的需求主要是减少日常工作中打开etherscan.io的次数,所以基本可以用。
01
—
RPC调用
必须支持 ETH RPC API 调用。 其他兼容 EVM 的公链也是如此。 代码中内置了多个网络的 RPC 配置。
# Basic RPC call.
➜ peth-cli python peth.py -r eth_blockNumber
0xd80830
# -c to change network.
➜ peth-cli python peth.py -r eth_blockNumber -c bsc
0xe58858
# RPC call with arguments.
➜ peth-cli python peth.py -r eth_getBalance 0x0000000000000000000000000000000000000000 latest
0x2679ce59cfe4a16dd4e
02
—
智能合约调用
这里ABI定义如下形式,这样可以在命令行中保存一些字符。
function_name (argment_type, [...]) -> (return_value_type, [...])
使用如下:
# Call name() of USDT contract.
➜ peth-cli python main.py --to 0xdac17f958d2ee523a2206206994597c13d831ec7 -e "name()->(string)"
Tether USD
# Without return type, we get raw hex data.
➜ peth-cli python main.py --to 0xdac17f958d2ee523a2206206994597c13d831ec7 -e "name()"
0x0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000a5465746865722055534400000000000000000000000000000000000000000000
# When only function name provided, peth will try to auto-extract ABI via the Etherscan API.
➜ peth-cli python main.py --to 0xdac17f958d2ee523a2206206994597c13d831ec7 -e name
Tether USD
当然,大多数合约都支持直接从 etherscan 抓取 ABI。 在这种情况下,不需要编写完整的函数定义,只需标记一个函数名即可。 当然,代价是慢了几秒。
03
—
壳
对于一些比较常用的命令,为了进一步减少字符数,比如python xxx.py,直接集成了一个小shell,惊喜的发现python自带的cmd模块也可以自动补全。
➜ peth-cli python main.py --console
Welcome to the peth shell. Type help or ? to list commands.
peth > ?
Documented commands (type help
): ========================================
balance bye contract erc20 exit help nonce number quit storage
peth > number
14158806
peth > balance 0xdAC17F958D2ee523a2206206994597C13D831ec7
1 Wei( 0.0000 Ether)
peth > nonce 0xdAC17F958D2ee523a2206206994597C13D831ec7
1
peth > storage 0xdAC17F958D2ee523a2206206994597C13D831ec7 1
0x000000000000000000000000000000000000000000000000008d7b18430396d4
peth > contract 0xdAC17F958D2ee523a2206206994597C13D831ec7
SourceCode : pragma solidity ^0.4.17;
ABI : [{"constant":true,"inputs":[],"name":"name","outputs":[{"name":"","type":"string ...
ContractName : TetherToken
CompilerVersion : v0.4.18+commit.9cf6e910
OptimizationUsed : 0
Runs : 0
ConstructorArguments : 000000000000000000000000000000000000000000000000000000174876e8000000000000000000 ...
EVMVersion : Default
Library :
LicenseType :
Proxy : 0
Implementation :
SwarmSource : bzzr://645ee12d73db47fd78ba77fa1f824c3c8f9184061b3b10386beb4dc9236abb28
=== ABI ===
function name() returns(string ) view
function deprecate(address _upgradedAddress) returns() nonpayable
function approve(address _spender,uint256 _value) returns() nonpayable
...
peth > erc20 0xdAC17F958D2ee523a2206206994597C13D831ec7
totalSupply() -> (uint256) => 39823315849942740
name() -> (string) => Tether USD
symbol() -> (string) => USDT
decimals() -> (uint8) => 6
peth > erc20 0xdAC17F958D2ee523a2206206994597C13D831ec7 balanceOf 0xdAC17F958D2ee523a2206206994597C13D831ec7
644096679570
peth > exit
bye!
04
—
结尾
完整的可以直接查看
欢迎star、fork、提PR。
最后,区块链安全,区块链开发以太坊rpc,后台开发,安全合规招聘好久了,加油,大佬们。