您正在查看: Ethereum-新手教程 分类下的文章

web3j 监听 Solidity Funcion event

假设您有一个非常简单的合同,可以递增和递减部署到该地址的计数器 CONTRACT_X

pragma solidity ^0.4.20;

contract Counter {
    int counter; // Global state

    event CounterIncremented(address indexed _by, int _newValue);
    event CounterDecremented(address indexed _by, int _newValue);

    function increment(int _value) public {
        counter = counter + _value;
        emit CounterIncremented(msg.sender, counter);
    }

    function deincrement(int _value) public {
        counter = counter - _value;
        emit CounterDecremented(msg.sender, counter);
    }
}

事件定义哈希

首先,您必须定义每个事件(定义),以便计算代表该事件在网络上的唯一哈希。

// Definition of event CounterIncremented(address indexed _by, int _newValue)
public static final Event INCREMENT_EVENT = new Event("CounterIncremented", 
    Arrays.<TypeReference<?>>asList(new TypeReference<Address>(true) {}, new TypeReference<Int>(false) {}}));

// Definition of event CounterDecremented(address indexed _by, int _newValue)
public static final Event DECREMENT_EVENT = new Event("CounterDecremented", 
    Arrays.<TypeReference<?>>asList(new TypeReference<Address>(true) {}, new TypeReference<Int>(false) {}}));

然后,要计算事件的哈希,您只需要执行以下操作:

private static final String INCREMENT_EVENT_HASH = EventEncoder.encode(INCREMENT_EVENT);

private static final String DECREMENT_EVENT_HASH = EventEncoder.encode(DECREMENT_EVENT);

筛选

在订阅事件之前,可以应用过滤器,因为根据定义,可以查询以太坊事件日志(网络所有智能合约的所有事件)。

按区块范围和智能合约过滤

返回智能合约的所有不同类型的事件CONTRACT_X。在我们的情况下,我们将获得CounterIncremented和CounterDecremented事件。

EthFilter filter = new EthFilter(
    DefaultBlockParameterName.EARLIEST, // From block 0
    DefaultBlockParameterName.LATEST,  // To last block
    CONTRACT_X); // Unique Smart Contract

EthFilter filter = new EthFilter(
    DefaultBlockParameterName.LATEST, // only the latest block  
    DefaultBlockParameterName.LATEST,  // 
    CONTRACT_X); // Unique Smart Contract

EthFilter filter = new EthFilter(
    DefaultBlockParameterName.valueOf("2222"), // From block no 2222
    DefaultBlockParameterName.LATEST,  // To latest
    Arrays.asList(address1, address2, address3)); // List of Smart Contracts

LATEST意味着您将提取订阅期间出现的事件。

  • 按事件过滤
    如果要按事件类型过滤,例如仅获取CounterIncremented事件,则可以使用属性addSingleTopic
    EthFilter filter = new EthFilter(
      DefaultBlockParameterName.EARLIEST, // From block 0 
      DefaultBlockParameterName.LATEST,  // To latest
      CONTRACT_X) // Unique Smart Contract
    .addSingleTopic(INCREMENT_EVENT_HASH);
  • 按事件参数过滤
    最后,您可能希望按事件参数进行过滤,例如,CounterIncremented由给定地址触发的所有事件(按事件的参数_by进行过滤CounterIncremented)
    EthFilter filter = new EthFilter(
      DefaultBlockParameterName.EARLIEST, // From block 0
      DefaultBlockParameterName.LATEST,  // To latest
      CONTRACT_X) // Unique Smart Contract
    .addSingleTopic(INCREMENT_EVENT_HASH)
    .addOptionalTopic("0xdDd6427Aaf3766DC97A9cA9deDD3e7911b085B6B");

    事件参数过滤器仅适用于“索引”参数。

    订阅活动

    最后,最后一步是订阅节点的事件:

    web3.ethLogFlowable(filter).subscribe(event -> { 
      event.getAddress(); // Smart contract address
      event.getBlockNumber(); // Block number
      event.getTransactionHash(); // Transaction that emitted the event
      event.getTopics().get(0); // Event hash
      event.getTopics().get(1-n); // Event parameter (1) _by, (2) _value
    }); 

    参考

    https://ethereum.stackexchange.com/questions/66379/recover-solidity-funcion-event-in-web3j
    http://docs.web3j.io/latest/advanced/filters_and_events/

MyEtherWallet 本地部署

编译运行

git clone https://github.com/MyEtherWallet/MyEtherWallet.git
cd MyEtherWallet/
npm i
npm run start:ci

外网访问

https://github.com/MyEtherWallet/MyEtherWallet/blob/1e76e24312d21a447d35f0810c5431dbdb7432ee/vueConfig/mew.js#L10

 devServer: {
    https: true, // 是否默认https
    host: 'localhost', // 访问地址
    hotOnly: true,
    port: 8080,  //端口
    headers: defaultConfigs.headers // 删掉
  },

eth poa 链加油站gasprice

部署安装

git clone https://github.com/bcskill/eth-gasprices.git
cd eth-gasprices
pip3 install -r requirements.txt

服务运行

export ETH_RPC_URL=http://ip:8545
python3 gasprice.py -h 0.0.0.0 -p 8000
服务启动
cp gasprice.service /etc/systemd/system/gasprice.service
systemctl daemon-reload
systemctl start gasprice.service
systemctl status gasprice.service

或者

python3 gasprice.py -h 0.0.0.0 -p 8000 "$@" > ./stdout.txt 2> ./stderr.txt &  echo $! > ./nodeos.pid
tail -f stderr.txt

测试访问

http://127.0.0.1:8000/

测试数据

{
    "health": true,
    "block_number": 26972,
    "block_time": 2.261,
    "slow": 24,
    "standard": 25,
    "fast": 54.955,
    "instant": 100
}
数据解释
  • slow 慢
  • standard 标准
  • fast 快速
  • instant 即时

“慢”,“标准”,“快速”和“即时”值代表最近200个区块的最低汽油价格。 默认情况下,慢表示30%的概率,标准为60%,快为90%,即时为100%。

常见问题

numpy: Something is wrong with the numpy installation.
python3 gasprice.py
Traceback (most recent call last):
  File "gasprice.py", line 4, in <module>
    import pandas as pd
  File "/home/surou/.local/lib/python3.6/site-packages/pandas/__init__.py", line 17, in <module>
    "Unable to import required dependencies:\n" + "\n".join(missing_dependencies)
ImportError: Unable to import required dependencies:
numpy: Something is wrong with the numpy installation. While importing we detected an older version of numpy in ['/home/surou/.local/lib/python3.6/site-packages/numpy']. One method of fixing this is to repeatedly uninstall numpy until none is found, then reinstall this version.
解决方法
rm -rf ~/.local/lib/python3.6/site-packages/numpy
pip install numpy
No module named 'click'
ModuleNotFoundError: No module named 'click'
解决方法
pip3 install click

Web3.py 安装和使用

安装python3

 sudo apt-get install python3-dev -y

设置python3默认

sudo update-alternatives --install /usr/bin/python python /usr/bin/python3 150
sudo update-alternatives --config python

安装pip3

sudo apt-get install python3-pip

安装web3

pip3 install web3

测试合约代码

import time
from web3 import Web3
from web3.contract import Contract, ContractFunctions
from web3 import Web3

false = False
true = True
CONTRACT_ADDRESS = "0xD8B0c34024deA0B9ABaf187E58Dbb3e81a033367"
CONTRACT_ABI = [{"constant":true,"inputs":[],"name":"name","outputs":[{"name":"","type":"string"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"_spender","type":"address"},{"name":"_value","type":"uint256"}],"name":"approve","outputs":[{"name":"success","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"totalSupply","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"_from","type":"address"},{"name":"_to","type":"address"},{"name":"_value","type":"uint256"}],"name":"transferFrom","outputs":[{"name":"success","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"decimals","outputs":[{"name":"","type":"uint8"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"_value","type":"uint256"}],"name":"burn","outputs":[{"name":"success","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[{"name":"","type":"address"}],"name":"balanceOf","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"_from","type":"address"},{"name":"_value","type":"uint256"}],"name":"burnFrom","outputs":[{"name":"success","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"symbol","outputs":[{"name":"","type":"string"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"_to","type":"address"},{"name":"_value","type":"uint256"}],"name":"transfer","outputs":[{"name":"","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"name":"_spender","type":"address"},{"name":"_value","type":"uint256"},{"name":"_extraData","type":"bytes"}],"name":"approveAndCall","outputs":[{"name":"success","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[{"name":"","type":"address"},{"name":"","type":"address"}],"name":"allowance","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"inputs":[{"name":"initialSupply","type":"uint256"},{"name":"tokenName","type":"string"},{"name":"tokenSymbol","type":"string"}],"payable":false,"stateMutability":"nonpayable","type":"constructor"},{"anonymous":false,"inputs":[{"indexed":true,"name":"from","type":"address"},{"indexed":true,"name":"to","type":"address"},{"indexed":false,"name":"value","type":"uint256"}],"name":"Transfer","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"owner","type":"address"},{"indexed":true,"name":"spender","type":"address"},{"indexed":false,"name":"value","type":"uint256"}],"name":"Approval","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"from","type":"address"},{"indexed":false,"name":"value","type":"uint256"}],"name":"Burn","type":"event"}]

w3 = Web3(Web3.HTTPProvider('http://。。。:8545'))
acct = w3.eth.account.privateKeyToAccount("e9bc9ae61053。。。。。69fd59d7986b9f4f49c025cc")

if __name__ == "__main__":
    print("getTransactionCount:", w3.eth.getTransactionCount(acct.address))
    print("gasPrice:", w3.eth.gasPrice)

    contract = w3.eth.contract(address=Web3.toChecksumAddress(CONTRACT_ADDRESS), abi=CONTRACT_ABI)
    TokenConstruct_txn = contract.functions.transfer(Web3.toChecksumAddress("0xc65dD4299C682f335d6e15e2B5774D015E01E479"), w3.toWei(2, 'ether')).buildTransaction({
        'from': acct.address,
        'nonce': w3.eth.getTransactionCount(acct.address),
        'gas': 100000,
        'gasPrice': w3.eth.gasPrice})
    TokenSigned = acct.signTransaction(TokenConstruct_txn)
    print("Pre-calculated transactionHash:", w3.toHex(w3.sha3(TokenSigned.rawTransaction)))
    TokenTx_hash = w3.eth.sendRawTransaction(TokenSigned.rawTransaction)
    print("transactionHash:", TokenTx_hash.hex())
    #TokenTx_receipt = w3.eth.waitForTransactionReceipt(TokenTx_hash)
    #TokenContract_address = TokenTx_receipt['contractAddress']
    #print("Contract Deployed At:", TokenContract_address)

参考

https://web3py.readthedocs.io/en/latest/quickstart.html

常见问题

  1. Command "python setup.py egg_info" failed with error code 1
    pip3 install --upgrade setuptools
    python3 -m pip install --upgrade pip