Java Code Examples for org.web3j.protocol.core.methods.response.TransactionReceipt#getLogs()

The following examples show how to use org.web3j.protocol.core.methods.response.TransactionReceipt#getLogs() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DelegateContract.java    From client-sdk-java with Apache License 2.0 6 votes vote down vote up
/**
 *  获得解除委托时所提取的委托收益(当减持/撤销委托成功时调用)
 *
 * @param transactionReceipt
 * @return
 * @throws TransactionException
 */
public BigInteger decodeUnDelegateLog(TransactionReceipt transactionReceipt) throws TransactionException {
    List<Log> logs = transactionReceipt.getLogs();
    if(logs==null||logs.isEmpty()){
        throw new TransactionException("TransactionReceipt logs is empty");
    }

    String logData = logs.get(0).getData();
    if(null == logData || "".equals(logData) ){
        throw new TransactionException("TransactionReceipt logs[0].data is empty");
    }

    RlpList rlp = RlpDecoder.decode(Numeric.hexStringToByteArray(logData));
    List<RlpType> rlpList = ((RlpList)(rlp.getValues().get(0))).getValues();
    String decodedStatus = new String(((RlpString)rlpList.get(0)).getBytes());
    int statusCode = Integer.parseInt(decodedStatus);

    if(statusCode != ErrorCode.SUCCESS){
        throw new TransactionException("TransactionResponse code is 0");
    }

    return  ((RlpString)((RlpList)RlpDecoder.decode(((RlpString)rlpList.get(1)).getBytes())).getValues().get(0)).asPositiveBigInteger();
}
 
Example 2
Source File: BaseContract.java    From client-sdk-java with Apache License 2.0 6 votes vote down vote up
private TransactionResponse getResponseFromTransactionReceipt(TransactionReceipt transactionReceipt) throws TransactionException {
	List<Log> logs = transactionReceipt.getLogs();
       if(logs==null||logs.isEmpty()){
       	 throw new TransactionException("TransactionReceipt logs is empty");
       }

	String logData = logs.get(0).getData();
	if(null == logData || "".equals(logData) ){
		throw new TransactionException("TransactionReceipt log data is empty");
	}

       RlpList rlp = RlpDecoder.decode(Numeric.hexStringToByteArray(logData));
       List<RlpType> rlpList = ((RlpList)(rlp.getValues().get(0))).getValues();
       String decodedStatus = new String(((RlpString)rlpList.get(0)).getBytes());
       int statusCode = Integer.parseInt(decodedStatus);
       
       TransactionResponse transactionResponse = new TransactionResponse();
       transactionResponse.setCode(statusCode);
       transactionResponse.setTransactionReceipt(transactionReceipt);
       
       return transactionResponse;
}
 
Example 3
Source File: RewardContract.java    From client-sdk-java with Apache License 2.0 5 votes vote down vote up
/**
 *  获得提取的明细(当提取账户当前所有的可提取的委托奖励成功时调用)
 *
 * @param transactionReceipt
 * @return
 * @throws TransactionException
 */
public List<Reward> decodeWithdrawDelegateRewardLog(TransactionReceipt transactionReceipt) throws TransactionException {
    List<Log> logs = transactionReceipt.getLogs();
    if(logs==null||logs.isEmpty()){
        throw new TransactionException("TransactionReceipt logs is empty");
    }

    String logData = logs.get(0).getData();
    if(null == logData || "".equals(logData) ){
        throw new TransactionException("TransactionReceipt logs[0].data is empty");
    }

    RlpList rlp = RlpDecoder.decode(Numeric.hexStringToByteArray(logData));
    List<RlpType> rlpList = ((RlpList)(rlp.getValues().get(0))).getValues();
    String decodedStatus = new String(((RlpString)rlpList.get(0)).getBytes());
    int statusCode = Integer.parseInt(decodedStatus);

    if(statusCode != ErrorCode.SUCCESS){
        throw new TransactionException("TransactionResponse code is 0");
    }

    List<Reward> rewards = new ArrayList<>();
    ((RlpList)((RlpList)RlpDecoder.decode(((RlpString)rlpList.get(1)).getBytes())).getValues().get(0)).getValues()
            .stream()
            .forEach(rl -> {
                RlpList rlpL = (RlpList)rl;
                Reward reward = new Reward();
                reward.setNodeId(((RlpString)rlpL.getValues().get(0)).asString());
                reward.setStakingNum(((RlpString)rlpL.getValues().get(1)).asPositiveBigInteger());
                reward.setRewardBigIntegerValue((((RlpString)rlpL.getValues().get(2)).asPositiveBigInteger()));
                rewards.add(reward);
            });

    return  rewards;
}
 
Example 4
Source File: HumanStandardTokenIT.java    From etherscan-explorer with GNU General Public License v3.0 5 votes vote down vote up
private void sendTransferTokensTransaction(
        Credentials credentials, String to, String contractAddress, BigInteger qty)
        throws Exception {

    Function function = transfer(to, qty);
    String functionHash = execute(credentials, function, contractAddress);

    TransactionReceipt transferTransactionReceipt =
            waitForTransactionReceipt(functionHash);
    assertThat(transferTransactionReceipt.getTransactionHash(), is(functionHash));

    List<Log> logs = transferTransactionReceipt.getLogs();
    assertFalse(logs.isEmpty());
    Log log = logs.get(0);

    // verify the event was called with the function parameters
    List<String> topics = log.getTopics();
    assertThat(topics.size(), is(3));

    Event transferEvent = transferEvent();

    // check function signature - we only have a single topic our event signature,
    // there are no indexed parameters in this example
    String encodedEventSignature = EventEncoder.encode(transferEvent);
    assertThat(topics.get(0), is(encodedEventSignature));
    assertThat(new Address(topics.get(1)), is(new Address(credentials.getAddress())));
    assertThat(new Address(topics.get(2)), is(new Address(to)));

    // verify qty transferred
    List<Type> results = FunctionReturnDecoder.decode(
            log.getData(), transferEvent.getNonIndexedParameters());
    assertThat(results, equalTo(Collections.singletonList(new Uint256(qty))));
}
 
Example 5
Source File: HumanStandardTokenIT.java    From etherscan-explorer with GNU General Public License v3.0 5 votes vote down vote up
private void sendApproveTransaction(
        Credentials credentials, String spender, BigInteger value,
        String contractAddress) throws Exception {
    Function function = approve(spender, value);
    String functionHash = execute(credentials, function, contractAddress);

    TransactionReceipt transferTransactionReceipt =
            waitForTransactionReceipt(functionHash);
    assertThat(transferTransactionReceipt.getTransactionHash(), is(functionHash));

    List<Log> logs = transferTransactionReceipt.getLogs();
    assertFalse(logs.isEmpty());
    Log log = logs.get(0);

    // verify the event was called with the function parameters
    List<String> topics = log.getTopics();
    assertThat(topics.size(), is(3));

    // event Transfer(address indexed _from, address indexed _to, uint256 _value);
    Event event = approvalEvent();

    // check function signature - we only have a single topic our event signature,
    // there are no indexed parameters in this example
    String encodedEventSignature = EventEncoder.encode(event);
    assertThat(topics.get(0), is(encodedEventSignature));
    assertThat(new Address(topics.get(1)), is(new Address(credentials.getAddress())));
    assertThat(new Address(topics.get(2)), is(new Address(spender)));

    // verify our two event parameters
    List<Type> results = FunctionReturnDecoder.decode(
            log.getData(), event.getNonIndexedParameters());
    assertThat(results, equalTo(Collections.singletonList(new Uint256(value))));
}
 
Example 6
Source File: HumanStandardTokenIT.java    From etherscan-explorer with GNU General Public License v3.0 5 votes vote down vote up
public void sendTransferFromTransaction(
        Credentials credentials, String from, String to, BigInteger value,
        String contractAddress) throws Exception {

    Function function = transferFrom(from, to, value);
    String functionHash = execute(credentials, function, contractAddress);

    TransactionReceipt transferTransactionReceipt =
            waitForTransactionReceipt(functionHash);
    assertThat(transferTransactionReceipt.getTransactionHash(), is(functionHash));

    List<Log> logs = transferTransactionReceipt.getLogs();
    assertFalse(logs.isEmpty());
    Log log = logs.get(0);

    Event transferEvent = transferEvent();
    List<String> topics = log.getTopics();

    // check function signature - we only have a single topic our event signature,
    // there are no indexed parameters in this example
    String encodedEventSignature = EventEncoder.encode(transferEvent);
    assertThat(topics.get(0), is(encodedEventSignature));
    assertThat(new Address(topics.get(1)), is(new Address(from)));
    assertThat(new Address(topics.get(2)), is(new Address(to)));

    // verify qty transferred
    List<Type> results = FunctionReturnDecoder.decode(
            log.getData(), transferEvent.getNonIndexedParameters());
    assertThat(results, equalTo(Collections.singletonList(new Uint256(value))));
}
 
Example 7
Source File: HumanStandardTokenIT.java    From web3j with Apache License 2.0 5 votes vote down vote up
private void sendTransferTokensTransaction(
        Credentials credentials, String to, String contractAddress, BigInteger qty)
        throws Exception {

    Function function = transfer(to, qty);
    String functionHash = execute(credentials, function, contractAddress);

    TransactionReceipt transferTransactionReceipt = waitForTransactionReceipt(functionHash);
    assertEquals(transferTransactionReceipt.getTransactionHash(), (functionHash));

    List<Log> logs = transferTransactionReceipt.getLogs();
    assertFalse(logs.isEmpty());
    Log log = logs.get(0);

    // verify the event was called with the function parameters
    List<String> topics = log.getTopics();
    assertEquals(topics.size(), (3));

    Event transferEvent = transferEvent();

    // check function signature - we only have a single topic our event signature,
    // there are no indexed parameters in this example
    String encodedEventSignature = EventEncoder.encode(transferEvent);
    assertEquals(topics.get(0), (encodedEventSignature));
    assertEquals(new Address(topics.get(1)), (new Address(credentials.getAddress())));
    assertEquals(new Address(topics.get(2)), (new Address(to)));

    // verify qty transferred
    List<Type> results =
            FunctionReturnDecoder.decode(
                    log.getData(), transferEvent.getNonIndexedParameters());
    assertEquals(results, (Collections.singletonList(new Uint256(qty))));
}
 
Example 8
Source File: HumanStandardTokenIT.java    From web3j with Apache License 2.0 5 votes vote down vote up
private void sendApproveTransaction(
        Credentials credentials, String spender, BigInteger value, String contractAddress)
        throws Exception {
    Function function = approve(spender, value);
    String functionHash = execute(credentials, function, contractAddress);

    TransactionReceipt transferTransactionReceipt = waitForTransactionReceipt(functionHash);
    assertEquals(transferTransactionReceipt.getTransactionHash(), (functionHash));

    List<Log> logs = transferTransactionReceipt.getLogs();
    assertFalse(logs.isEmpty());
    Log log = logs.get(0);

    // verify the event was called with the function parameters
    List<String> topics = log.getTopics();
    assertEquals(topics.size(), (3));

    // event Transfer(address indexed _from, address indexed _to, uint256 _value);
    Event event = approvalEvent();

    // check function signature - we only have a single topic our event signature,
    // there are no indexed parameters in this example
    String encodedEventSignature = EventEncoder.encode(event);
    assertEquals(topics.get(0), (encodedEventSignature));
    assertEquals(new Address(topics.get(1)), (new Address(credentials.getAddress())));
    assertEquals(new Address(topics.get(2)), (new Address(spender)));

    // verify our two event parameters
    List<Type> results =
            FunctionReturnDecoder.decode(log.getData(), event.getNonIndexedParameters());
    assertEquals(results, (Collections.singletonList(new Uint256(value))));
}
 
Example 9
Source File: HumanStandardTokenIT.java    From web3j with Apache License 2.0 5 votes vote down vote up
public void sendTransferFromTransaction(
        Credentials credentials,
        String from,
        String to,
        BigInteger value,
        String contractAddress)
        throws Exception {

    Function function = transferFrom(from, to, value);
    String functionHash = execute(credentials, function, contractAddress);

    TransactionReceipt transferTransactionReceipt = waitForTransactionReceipt(functionHash);
    assertEquals(transferTransactionReceipt.getTransactionHash(), (functionHash));

    List<Log> logs = transferTransactionReceipt.getLogs();
    assertFalse(logs.isEmpty());
    Log log = logs.get(0);

    Event transferEvent = transferEvent();
    List<String> topics = log.getTopics();

    // check function signature - we only have a single topic our event signature,
    // there are no indexed parameters in this example
    String encodedEventSignature = EventEncoder.encode(transferEvent);
    assertEquals(topics.get(0), (encodedEventSignature));
    assertEquals(new Address(topics.get(1)), (new Address(from)));
    assertEquals(new Address(topics.get(2)), (new Address(to)));

    // verify qty transferred
    List<Type> results =
            FunctionReturnDecoder.decode(
                    log.getData(), transferEvent.getNonIndexedParameters());
    assertEquals(results, (Collections.singletonList(new Uint256(value))));
}
 
Example 10
Source File: EventFilterIT.java    From etherscan-explorer with GNU General Public License v3.0 4 votes vote down vote up
@Test
public void testEventFilter() throws Exception {
    unlockAccount();

    Function function = createFibonacciFunction();
    String encodedFunction = FunctionEncoder.encode(function);

    BigInteger gas = estimateGas(encodedFunction);
    String transactionHash = sendTransaction(ALICE, CONTRACT_ADDRESS, gas, encodedFunction);

    TransactionReceipt transactionReceipt =
            waitForTransactionReceipt(transactionHash);

    assertFalse("Transaction execution ran out of gas",
            gas.equals(transactionReceipt.getGasUsed()));

    List<Log> logs = transactionReceipt.getLogs();
    assertFalse(logs.isEmpty());

    Log log = logs.get(0);

    List<String> topics = log.getTopics();
    assertThat(topics.size(), is(1));

    Event event = new Event("Notify",
            Collections.emptyList(),
            Arrays.asList(new TypeReference<Uint256>() {}, new TypeReference<Uint256>() {}));

    // check function signature - we only have a single topic our event signature,
    // there are no indexed parameters in this example
    String encodedEventSignature = EventEncoder.encode(event);
    assertThat(topics.get(0),
            is(encodedEventSignature));

    // verify our two event parameters
    List<Type> results = FunctionReturnDecoder.decode(
            log.getData(), event.getNonIndexedParameters());
    assertThat(results, equalTo(Arrays.asList(
            new Uint256(BigInteger.valueOf(7)), new Uint256(BigInteger.valueOf(13)))));

    // finally check it shows up in the event filter
    List<EthLog.LogResult> filterLogs = createFilterForEvent(
            encodedEventSignature, CONTRACT_ADDRESS);
    assertFalse(filterLogs.isEmpty());
}
 
Example 11
Source File: EventFilterIT.java    From web3j with Apache License 2.0 4 votes vote down vote up
@Test
public void testEventFilter() throws Exception {
    unlockAccount();

    Function function = createFibonacciFunction();
    String encodedFunction = FunctionEncoder.encode(function);

    BigInteger gas = estimateGas(encodedFunction);
    String transactionHash = sendTransaction(ALICE, CONTRACT_ADDRESS, gas, encodedFunction);

    TransactionReceipt transactionReceipt = waitForTransactionReceipt(transactionHash);

    assertFalse(gas.equals(transactionReceipt.getGasUsed()));

    List<Log> logs = transactionReceipt.getLogs();
    assertFalse(logs.isEmpty());

    Log log = logs.get(0);

    List<String> topics = log.getTopics();
    assertEquals(topics.size(), (1));

    Event event =
            new Event(
                    "Notify",
                    Arrays.asList(
                            new TypeReference<Uint256>() {}, new TypeReference<Uint256>() {}));

    // check function signature - we only have a single topic our event signature,
    // there are no indexed parameters in this example
    String encodedEventSignature = EventEncoder.encode(event);
    assertEquals(topics.get(0), (encodedEventSignature));

    // verify our two event parameters
    List<Type> results =
            FunctionReturnDecoder.decode(log.getData(), event.getNonIndexedParameters());
    assertEquals(
            results,
            (Arrays.asList(
                    new Uint256(BigInteger.valueOf(7)), new Uint256(BigInteger.valueOf(13)))));

    // finally check it shows up in the event filter
    List<EthLog.LogResult> filterLogs =
            createFilterForEvent(encodedEventSignature, CONTRACT_ADDRESS);
    assertFalse(filterLogs.isEmpty());
}