以太坊智能合约 Event 调用实例

先说一下event调用过程:首先需要在合约中定义event,并在某个函数中去触发它。当我们调用该函数的时候就会触发event,那么我们就可以通过在该次调用智能合约产生的block上建立filter来捕获event。具体代码如下:

合约

(部分截取):

pragma solidity ^0.4.23;
import "./Vote.sol";
import "./SafeMath.sol";

contract Deposit {
    using SafeMath for uint256;

    address admin;
    uint256 adminBalance;
    uint8 constant CHARGE_PERCENTAGE = 30;
    struct DepositRecord {
        uint id;
        address payer;
        address payee;
        // unit: wei
        uint money;
        // time when payment is created
        uint payTime;
        // duration before withdraw allowed, unit : min
        uint freezeDuration;
        // time withdraw happened.
        uint withdrawTime;
    }

    mapping(uint => DepositRecord) public depositRecords;
    mapping(address => uint[]) payerMapping;
    mapping(address => uint[]) payeeMapping;

    //定义事件
    event DepositRecordCreated (uint recordId);

    constructor() public {
        admin = msg.sender;
    }

    /**
     * create deposit record.
     */
    function pay(address _payee, uint _freezeDuration) public payable
    returns (uint) {
        require(msg.value > 0);
        require(msg.sender != _payee);
        ++globalId;
        DepositRecord memory record = DepositRecord(globalId,
            msg.sender, _payee, msg.value, now, _freezeDuration, 0, 0);
        depositRecords[globalId] = record;
        payerMapping[msg.sender].push(globalId);
        payeeMapping[_payee].push(globalId);

        emit DepositRecordCreated(globalId); //触发定义的事件
    }
}

合约编译生成的Java类

合约的编译请自行百度,对web3j API掌握很好的话可以执行编写

package io.shareblock.contract;

****
import org.web3j.abi.EventEncoder;
import org.web3j.abi.TypeReference;
import org.web3j.abi.datatypes.Address;
import org.web3j.abi.datatypes.DynamicArray;
import org.web3j.abi.datatypes.Event;
import org.web3j.abi.datatypes.Function;
import org.web3j.abi.datatypes.Type;
import org.web3j.abi.datatypes.Utf8String;
import org.web3j.abi.datatypes.generated.Uint256;
import org.web3j.abi.datatypes.generated.Uint8;
import org.web3j.crypto.Credentials;
import org.web3j.protocol.Web3j;
import org.web3j.protocol.core.DefaultBlockParameter;
import org.web3j.protocol.core.RemoteCall;
import org.web3j.protocol.core.methods.request.EthFilter;
import org.web3j.protocol.core.methods.response.Log;
import org.web3j.protocol.core.methods.response.TransactionReceipt;
import org.web3j.tuples.generated.Tuple8;
import org.web3j.tx.Contract;
import org.web3j.tx.TransactionManager;
import rx.Observable;
import rx.functions.Func1;

/**
 * 

Auto generated code. *

Do not modify! *

Please use the web3j command line tools, * or the org.web3j.codegen.SolidityFunctionWrapperGenerator in the * codegen module to update. * *

Generated with web3j version 3.4.0. */ public class Deposit extends Contract { private static final String BINARY = "608060405234801561001057600080fd5b5060028054600160a060020a031916331790556112ae806100326000396000f3006080604052600436106100cf5763ffffffff7c01000000000000000000000000000000000000000000000000000000006000350416632e1a7d4d81146100d457806334461067146100ee5780633c594059146102325780636c6324521461026057806372605228146102fe57806380f5fb12146103635780638d6191641461041c578063937cabed1461048d578063c407687614610526578063ca6668231461053d578063e4471c9714610552578063f18d20be14610573578063f7e3301214610588578063feeeed9c146105a0575b600080fd5b3480156100e057600080fd5b506100ec6004356105be565b005b3480156100fa57600080fd5b50610106600435610711565b6040518089600160a060020a0316600160a060020a0316815260200188600160a060020a0316600160a060020a031681526020018060200180602001878152602001868152602001858152602001848152602001838103835289818151815260200191508051906020019080838360005b8381101561018f578181015183820152602001610177565b50505050905090810190601f1680156101bc5780820380516001836020036101000a031916815260200191505b5083810382528851815288516020918201918a019080838360005b838110156101ef5781810151838201526020016101d7565b50505050905090810190601f16801561021c5780820380516001836020036101000a031916815260200191505b509a505050505050505050505060405180910390f35b34801561023e57600080fd5b5061024a600435610872565b6040805160ff9092168252519081900360200190f35b34801561026c57600080fd5b5060408051602060046024803582810135601f81018590048502860185019096528585526100ec95833595369560449491939091019190819084018382808284375050604080516020601f89358b018035918201839004830284018301909452808352979a99988101979196509182019450925082915084018382808284375094975050933594506108e69350505050565b34801561030a57600080fd5b50610316600435610997565b60408051988952600160a060020a0397881660208a015295909616878601526060870193909352608086019190915260a085015260c084015260e083019190915251908190036101000190f35b34801561036f57600080fd5b5060408051602060046024803582810135601f810185900485028601850190965285855261040a958335600160a060020a031695369560449491939091019190819084018382808284375050604080516020601f89358b018035918201839004830284018301909452808352979a99988101979196509182019450925082915084018382808284375094975050933594506109e79350505050565b60408051918252519081900360200190f35b34801561042857600080fd5b5061043d600160a060020a0360043516610af6565b60408051602080825283518183015283519192839290830191858101910280838360005b83811015610479578181015183820152602001610461565b505050509050019250505060405180910390f35b34801561049957600080fd5b506040805160206004803580820135601f810184900484028501840190955284845261040a94369492936024939284019190819084018382808284375050604080516020601f89358b018035918201839004830284018301909452808352979a9998810197919650918201945092508291508401838280828437509497505093359450610b629350505050565b61040a600160a060020a0360043516602435610c6b565b34801561054957600080fd5b5061040a610e8a565b34801561055e57600080fd5b5061043d600160a060020a0360043516610e90565b34801561057f57600080fd5b506100ec610efa565b34801561059457600080fd5b5061024a600435610f55565b3480156105ac57600080fd5b506100ec60043560ff60243516610fc4565b600080548211156105ce57600080fd5b600082815260046020526040902060020154600160a060020a031633146105f457600080fd5b6000828152600460205260409020600601541561061057600080fd5b6000828152600460208190526040909120600581015491015442603c909202011061063a57600080fd5b6000828152600460205260409020600701541580610675575060008281526004602052604090206007015461066e90610872565b60ff166002145b151561068057600080fd5b50600081815260046020526040902060038101546002909101546064601e83020491600160a060020a03909116906108fc906106bc908461110f565b6040518115909202916000818181858888f193505050501580156106e4573d6000803e3d6000fd5b506003546106f8908263ffffffff61112116565b5050600090815260046020526040902042600690910155565b6001602081815260009283526040928390208054818401546002808401805488516101009882161598909802600019011691909104601f8101869004860287018601909752868652600160a060020a0392831696929091169492939091908301828280156107c05780601f10610795576101008083540402835291602001916107c0565b820191906000526020600020905b8154815290600101906020018083116107a357829003601f168201915b5050505060038301805460408051602060026001851615610100026000190190941693909304601f81018490048402820184019092528181529495949350908301828280156108505780601f1061082557610100808354040283529160200191610850565b820191906000526020600020905b81548152906001019060200180831161083357829003601f168201915b5050505050908060040154908060050154908060060154908060070154905088565b60008181526001602052604081206004015415806108a9575060008281526001602052604090206005810154600490910154429101115b156108b6575060006108e1565b6000828152600160205260409020600781015460069091015411156108dd575060016108e1565b5060025b919050565b600080548511156108f657600080fd5b600085815260046020526040902060010154600160a060020a0316331461091c57600080fd5b6000858152600460205260409020600601541561093857600080fd5b6000858152600460205260409020600701541561095457600080fd5b60008581526004602052604090206002015461097b90600160a060020a03168585856109e7565b6000958652600460205260409095206007019490945550505050565b6004602081905260009182526040909120805460018201546002830154600384015494840154600585015460068601546007909601549496600160a060020a039485169693909416949192909188565b60006109f1611137565b506040805161010081018252338152600160a060020a0387811660208084019182528385018981526060850189905242608086015260a08501889052600060c0860181905260e086018190528054600190810180835582528084529690208551815473ffffffffffffffffffffffffffffffffffffffff199081169187169190911782559351968101805490941696909416959095179091559251805192938493610aa2926002850192019061118f565b5060608201518051610abe91600384019160209091019061118f565b506080820151600482015560a0820151600582015560c0820151600682015560e0909101516007909101555050600054949350505050565b600160a060020a038116600090815260066020908152604091829020805483518184028101840190945280845260609392830182828015610b5657602002820191906000526020600020905b815481526020019060010190808311610b42575b50505050509050919050565b6000610b6c611137565b506040805161010081018252338152600060208083018281528385018981526060850189905242608086015260a0850188905260c0850184905260e08501849052835460019081018086558552808452959093208451815473ffffffffffffffffffffffffffffffffffffffff19908116600160a060020a0392831617835592519682018054909316961695909517905590518051929384939092610c1892600285019291019061118f565b5060608201518051610c3491600384019160209091019061118f565b506080820151600482015560a0820151600582015560c0820151600682015560e09091015160079091015550506000549392505050565b6000610c7561120d565b60003411610c8257600080fd5b33600160a060020a0385161415610c9857600080fd5b60008081546001019190508190555061010060405190810160405280600054815260200133600160a060020a0316815260200185600160a060020a0316815260200134815260200142815260200184815260200160008152602001600081525090508060046000805481526020019081526020016000206000820151816000015560208201518160010160006101000a815481600160a060020a030219169083600160a060020a0316021790555060408201518160020160006101000a815481600160a060020a030219169083600160a060020a03160217905550606082015181600301556080820151816004015560a0820151816005015560c0820151816006015560e082015181600701559050506005600033600160a060020a0316600160a060020a0316815260200190815260200160002060005490806001815401808255809150509060018203906000526020600020016000909192909190915055506006600085600160a060020a0316600160a060020a0316815260200190815260200160002060005490806001815401808255809150509060018203906000526020600020016000909192909190915055507fd015cd2314a5fcb9425e530b948f723a2387f4f90e2a50aebb5607fca44123bd6000546040518082815260200191505060405180910390a15092915050565b60005481565b600160a060020a038116600090815260056020908152604091829020805483518184028101840190945280845260609392830182828015610b565760200282019190600052602060002090815481526020019060010190808311610b425750505050509050919050565b600254600160a060020a03163314610f1157600080fd5b600254600354604051600160a060020a039092169181156108fc0291906000818181858888f19350505050158015610f4d573d6000803e3d6000fd5b506000600355565b6000818152600160205260408120600401548110610f7257600080fd5b600082815260016020908152604080832033845260080190915290205460ff161515610fa0575060006108e1565b50600090815260016020908152604080832033845260080190915290205460ff1690565b60008281526001602052604081206004015411610fe057600080fd5b60ff811660011480610ff5575060ff81166002145b151561100057600080fd5b600082815260016020526040902054600160a060020a031633141561102457600080fd5b60008281526001602081905260409091200154600160a060020a031633141561104c57600080fd5b600082815260016020526040902060058101546004909101544291011161107257600080fd5b600082815260016020908152604080832033845260080190915290205460ff161561109c57600080fd5b6000828152600160208181526040808420338552600801909152909120805460ff191660ff841690811790915514156110ef5760008281526001602081905260409091206006018054909101905561110b565b6000828152600160208190526040909120600701805490910190555b5050565b60008282111561111b57fe5b50900390565b60008282018381101561113057fe5b9392505050565b610100604051908101604052806000600160a060020a031681526020016000600160a060020a031681526020016060815260200160608152602001600081526020016000815260200160008152602001600081525090565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f106111d057805160ff19168380011785556111fd565b828001600101855582156111fd579182015b828111156111fd5782518255916020019190600101906111e2565b50611209929150611265565b5090565b61010060405190810160405280600081526020016000600160a060020a031681526020016000600160a060020a0316815260200160008152602001600081526020016000815260200160008152602001600081525090565b61127f91905b80821115611209576000815560010161126b565b905600a165627a7a72305820d59bf2a11e5908bb5b02e6d6057e1865b7e52a3c2099630fbe2e39bcf33f22570029"; public static final String FUNC_WITHDRAW = "withdraw"; public static final String FUNC_RECORDS = "records"; public static final String FUNC_RESULT = "result"; public static final String FUNC_PAYERCREATEARBITRATION = "payerCreateArbitration"; public static final String FUNC_DEPOSITRECORDS = "depositRecords"; public static final String FUNC_GETPAYEEMAPPING = "getPayeeMapping"; public static final String FUNC_CREATEVOTE = "createVote"; public static final String FUNC_PAY = "pay"; public static final String FUNC_GLOBALID = "globalId"; ****省略N多与event无关代码***** public RemoteCall createArbitration(String defendant, String topic, String description, BigInteger duration) { final Function function = new Function( FUNC_CREATEARBITRATION, Arrays.asList(new org.web3j.abi.datatypes.Address(defendant), new org.web3j.abi.datatypes.Utf8String(topic), new org.web3j.abi.datatypes.Utf8String(description), new org.web3j.abi.datatypes.generated.Uint256(duration)), Collections.>emptyList()); return executeRemoteCallTransaction(function); } public static class DepositRecordCreatedEventResponse { public Log log; public BigInteger recordId; } public Observable depositRecordCreatedEventObservable(DefaultBlockParameter startBlock, DefaultBlockParameter endBlock) { EthFilter filter = new EthFilter(startBlock, endBlock, getContractAddress()); filter.addSingleTopic(EventEncoder.encode(DEPOSITRECORDCREATED_EVENT)); return depositRecordCreatedEventObservable(filter); } /** * 合约中的方法执行完毕,拿到transactionReceipt 后可以调用该方法get event response */ public List getDepositRecordCreatedEvents(TransactionReceipt transactionReceipt) { List valueList = extractEventParametersWithLog(DEPOSITRECORDCREATED_EVENT, transactionReceipt); ArrayList responses = new ArrayList(valueList.size()); for (Contract.EventValuesWithLog eventValues : valueList) { DepositRecordCreatedEventResponse typedResponse = new DepositRecordCreatedEventResponse(); typedResponse.log = eventValues.getLog(); typedResponse.recordId = (BigInteger) eventValues.getNonIndexedValues().get(0).getValue(); responses.add(typedResponse); } return responses; }

调用合约,捕获event返回值

//调用合约
 TransactionReceipt txReceipt = contract.pay(toAddress, _freezeDuration, ethAmount).send();

        String status = txReceipt.getStatus();
        if (!StringUtils.isEmpty(status) && status.equals("0x1")){//调用合约成功
            List depositRecordCreatedEvents = contract.getDepositRecordCreatedEvents(txReceipt);
            log.error("deposit to Address:"+toAddress+" end success ");
            return Response.ok(depositRecordCreatedEvents.get(0).recordId);
        }

你可能感兴趣的:(blockchain)