org.web3j.abi.EventEncoder Java Examples

The following examples show how to use org.web3j.abi.EventEncoder. 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: Contract.java    From client-sdk-java with Apache License 2.0 7 votes vote down vote up
public static EventValues staticExtractEventParameters(
        Event event, Log log) {

    List<String> topics = log.getTopics();
    String encodedEventSignature = EventEncoder.encode(event);
    if (topics == null || topics.size() == 0 || !topics.get(0).equals(encodedEventSignature)) {
        return null;
    }

    List<Type> indexedValues = new ArrayList<>();
    List<Type> nonIndexedValues = FunctionReturnDecoder.decode(
            log.getData(), event.getNonIndexedParameters());

    List<TypeReference<Type>> indexedParameters = event.getIndexedParameters();
    for (int i = 0; i < indexedParameters.size(); i++) {
        Type value = FunctionReturnDecoder.decodeIndexedValue(
                topics.get(i + 1), indexedParameters.get(i));
        indexedValues.add(value);
    }
    return new EventValues(indexedValues, nonIndexedValues);
}
 
Example #2
Source File: Contract.java    From etherscan-explorer with GNU General Public License v3.0 6 votes vote down vote up
public static EventValues staticExtractEventParameters(
        Event event, Log log) {

    List<String> topics = log.getTopics();
    String encodedEventSignature = EventEncoder.encode(event);
    if (!topics.get(0).equals(encodedEventSignature)) {
        return null;
    }

    List<Type> indexedValues = new ArrayList<>();
    List<Type> nonIndexedValues = FunctionReturnDecoder.decode(
            log.getData(), event.getNonIndexedParameters());

    List<TypeReference<Type>> indexedParameters = event.getIndexedParameters();
    for (int i = 0; i < indexedParameters.size(); i++) {
        Type value = FunctionReturnDecoder.decodeIndexedValue(
                topics.get(i + 1), indexedParameters.get(i));
        indexedValues.add(value);
    }
    return new EventValues(indexedValues, nonIndexedValues);
}
 
Example #3
Source File: SolidityFunctionWrapper.java    From etherscan-explorer with GNU General Public License v3.0 6 votes vote down vote up
MethodSpec buildDefaultEventObservableFunction(
        String responseClassName,
        String functionName) {

    String generatedFunctionName =
            Strings.lowercaseFirstLetter(functionName) + "EventObservable";
    ParameterizedTypeName parameterizedTypeName = ParameterizedTypeName.get(ClassName.get(rx
            .Observable.class), ClassName.get("", responseClassName));

    MethodSpec.Builder observableMethodBuilder =
            MethodSpec.methodBuilder(generatedFunctionName)
                    .addModifiers(Modifier.PUBLIC)
                    .addParameter(DefaultBlockParameter.class, START_BLOCK)
                    .addParameter(DefaultBlockParameter.class, END_BLOCK)
                    .returns(parameterizedTypeName);

    observableMethodBuilder.addStatement("$1T filter = new $1T($2L, $3L, "
            + "getContractAddress())", EthFilter.class, START_BLOCK, END_BLOCK)
            .addStatement("filter.addSingleTopic($T.encode("
                    + buildEventDefinitionName(functionName) + "))", EventEncoder.class)
            .addStatement("return " + generatedFunctionName + "(filter)");

    return observableMethodBuilder
            .build();
}
 
Example #4
Source File: PublicResolver.java    From etherscan-explorer with GNU General Public License v3.0 6 votes vote down vote up
public Observable<NameChangedEventResponse> nameChangedEventObservable(DefaultBlockParameter startBlock, DefaultBlockParameter endBlock) {
    final Event event = new Event("NameChanged", 
            Arrays.<TypeReference<?>>asList(new TypeReference<Bytes32>() {}),
            Arrays.<TypeReference<?>>asList(new TypeReference<Utf8String>() {}));
    EthFilter filter = new EthFilter(startBlock, endBlock, getContractAddress());
    filter.addSingleTopic(EventEncoder.encode(event));
    return web3j.ethLogObservable(filter).map(new Func1<Log, NameChangedEventResponse>() {
        @Override
        public NameChangedEventResponse call(Log log) {
            EventValues eventValues = extractEventParameters(event, log);
            NameChangedEventResponse typedResponse = new NameChangedEventResponse();
            typedResponse.node = (byte[]) eventValues.getIndexedValues().get(0).getValue();
            typedResponse.name = (String) eventValues.getNonIndexedValues().get(0).getValue();
            return typedResponse;
        }
    });
}
 
Example #5
Source File: PublicResolver.java    From etherscan-explorer with GNU General Public License v3.0 6 votes vote down vote up
public Observable<ABIChangedEventResponse> aBIChangedEventObservable(DefaultBlockParameter startBlock, DefaultBlockParameter endBlock) {
    final Event event = new Event("ABIChanged", 
            Arrays.<TypeReference<?>>asList(new TypeReference<Bytes32>() {}, new TypeReference<Uint256>() {}),
            Arrays.<TypeReference<?>>asList());
    EthFilter filter = new EthFilter(startBlock, endBlock, getContractAddress());
    filter.addSingleTopic(EventEncoder.encode(event));
    return web3j.ethLogObservable(filter).map(new Func1<Log, ABIChangedEventResponse>() {
        @Override
        public ABIChangedEventResponse call(Log log) {
            EventValues eventValues = extractEventParameters(event, log);
            ABIChangedEventResponse typedResponse = new ABIChangedEventResponse();
            typedResponse.node = (byte[]) eventValues.getIndexedValues().get(0).getValue();
            typedResponse.contentType = (BigInteger) eventValues.getIndexedValues().get(1).getValue();
            return typedResponse;
        }
    });
}
 
Example #6
Source File: PublicResolver.java    From etherscan-explorer with GNU General Public License v3.0 6 votes vote down vote up
public Observable<PubkeyChangedEventResponse> pubkeyChangedEventObservable(DefaultBlockParameter startBlock, DefaultBlockParameter endBlock) {
    final Event event = new Event("PubkeyChanged", 
            Arrays.<TypeReference<?>>asList(new TypeReference<Bytes32>() {}),
            Arrays.<TypeReference<?>>asList(new TypeReference<Bytes32>() {}, new TypeReference<Bytes32>() {}));
    EthFilter filter = new EthFilter(startBlock, endBlock, getContractAddress());
    filter.addSingleTopic(EventEncoder.encode(event));
    return web3j.ethLogObservable(filter).map(new Func1<Log, PubkeyChangedEventResponse>() {
        @Override
        public PubkeyChangedEventResponse call(Log log) {
            EventValues eventValues = extractEventParameters(event, log);
            PubkeyChangedEventResponse typedResponse = new PubkeyChangedEventResponse();
            typedResponse.node = (byte[]) eventValues.getIndexedValues().get(0).getValue();
            typedResponse.x = (byte[]) eventValues.getNonIndexedValues().get(0).getValue();
            typedResponse.y = (byte[]) eventValues.getNonIndexedValues().get(1).getValue();
            return typedResponse;
        }
    });
}
 
Example #7
Source File: PublicResolver.java    From etherscan-explorer with GNU General Public License v3.0 6 votes vote down vote up
public Observable<TextChangedEventResponse> textChangedEventObservable(DefaultBlockParameter startBlock, DefaultBlockParameter endBlock) {
    final Event event = new Event("TextChanged", 
            Arrays.<TypeReference<?>>asList(new TypeReference<Bytes32>() {}, new TypeReference<Utf8String>() {}),
            Arrays.<TypeReference<?>>asList(new TypeReference<Utf8String>() {}));
    EthFilter filter = new EthFilter(startBlock, endBlock, getContractAddress());
    filter.addSingleTopic(EventEncoder.encode(event));
    return web3j.ethLogObservable(filter).map(new Func1<Log, TextChangedEventResponse>() {
        @Override
        public TextChangedEventResponse call(Log log) {
            EventValues eventValues = extractEventParameters(event, log);
            TextChangedEventResponse typedResponse = new TextChangedEventResponse();
            typedResponse.node = (byte[]) eventValues.getIndexedValues().get(0).getValue();
            typedResponse.indexedKey = (String) eventValues.getIndexedValues().get(1).getValue();
            typedResponse.key = (String) eventValues.getNonIndexedValues().get(0).getValue();
            return typedResponse;
        }
    });
}
 
Example #8
Source File: EthereumUtils.java    From jbpm-work-items with Apache License 2.0 6 votes vote down vote up
public static void observeContractEvent(Web3j web3j,
                                        String contractEventName,
                                        String contractAddress,
                                        List<TypeReference<?>> indexedParameters,
                                        List<TypeReference<?>> nonIndexedParameters,
                                        String eventReturnType,
                                        rx.functions.Action1 action1) {

    Event event = new Event(contractEventName,
                            indexedParameters,
                            nonIndexedParameters);

    EthFilter filter = new EthFilter(DefaultBlockParameterName.EARLIEST,
                                     DefaultBlockParameterName.LATEST,
                                     contractAddress);

    filter.addSingleTopic(EventEncoder.encode(event));

    Class<Type> type = (Class<Type>) AbiTypes.getType(eventReturnType);
    TypeReference<Type> typeRef = TypeReference.create(type);

    web3j.ethLogObservable(filter).subscribe(action1);
}
 
Example #9
Source File: ENS.java    From etherscan-explorer with GNU General Public License v3.0 6 votes vote down vote up
public Observable<TransferEventResponse> transferEventObservable(DefaultBlockParameter startBlock, DefaultBlockParameter endBlock) {
    final Event event = new Event("Transfer", 
            Arrays.<TypeReference<?>>asList(new TypeReference<Bytes32>() {}),
            Arrays.<TypeReference<?>>asList(new TypeReference<Address>() {}));
    EthFilter filter = new EthFilter(startBlock, endBlock, getContractAddress());
    filter.addSingleTopic(EventEncoder.encode(event));
    return web3j.ethLogObservable(filter).map(new Func1<Log, TransferEventResponse>() {
        @Override
        public TransferEventResponse call(Log log) {
            EventValues eventValues = extractEventParameters(event, log);
            TransferEventResponse typedResponse = new TransferEventResponse();
            typedResponse.node = (byte[]) eventValues.getIndexedValues().get(0).getValue();
            typedResponse.owner = (String) eventValues.getNonIndexedValues().get(0).getValue();
            return typedResponse;
        }
    });
}
 
Example #10
Source File: ENS.java    From etherscan-explorer with GNU General Public License v3.0 6 votes vote down vote up
public Observable<NewResolverEventResponse> newResolverEventObservable(DefaultBlockParameter startBlock, DefaultBlockParameter endBlock) {
    final Event event = new Event("NewResolver", 
            Arrays.<TypeReference<?>>asList(new TypeReference<Bytes32>() {}),
            Arrays.<TypeReference<?>>asList(new TypeReference<Address>() {}));
    EthFilter filter = new EthFilter(startBlock, endBlock, getContractAddress());
    filter.addSingleTopic(EventEncoder.encode(event));
    return web3j.ethLogObservable(filter).map(new Func1<Log, NewResolverEventResponse>() {
        @Override
        public NewResolverEventResponse call(Log log) {
            EventValues eventValues = extractEventParameters(event, log);
            NewResolverEventResponse typedResponse = new NewResolverEventResponse();
            typedResponse.node = (byte[]) eventValues.getIndexedValues().get(0).getValue();
            typedResponse.resolver = (String) eventValues.getNonIndexedValues().get(0).getValue();
            return typedResponse;
        }
    });
}
 
Example #11
Source File: ENS.java    From etherscan-explorer with GNU General Public License v3.0 6 votes vote down vote up
public Observable<NewTTLEventResponse> newTTLEventObservable(DefaultBlockParameter startBlock, DefaultBlockParameter endBlock) {
    final Event event = new Event("NewTTL", 
            Arrays.<TypeReference<?>>asList(new TypeReference<Bytes32>() {}),
            Arrays.<TypeReference<?>>asList(new TypeReference<Uint64>() {}));
    EthFilter filter = new EthFilter(startBlock, endBlock, getContractAddress());
    filter.addSingleTopic(EventEncoder.encode(event));
    return web3j.ethLogObservable(filter).map(new Func1<Log, NewTTLEventResponse>() {
        @Override
        public NewTTLEventResponse call(Log log) {
            EventValues eventValues = extractEventParameters(event, log);
            NewTTLEventResponse typedResponse = new NewTTLEventResponse();
            typedResponse.node = (byte[]) eventValues.getIndexedValues().get(0).getValue();
            typedResponse.ttl = (BigInteger) eventValues.getNonIndexedValues().get(0).getValue();
            return typedResponse;
        }
    });
}
 
Example #12
Source File: PublicResolver.java    From etherscan-explorer with GNU General Public License v3.0 6 votes vote down vote up
public Observable<AddrChangedEventResponse> addrChangedEventObservable(DefaultBlockParameter startBlock, DefaultBlockParameter endBlock) {
    final Event event = new Event("AddrChanged", 
            Arrays.<TypeReference<?>>asList(new TypeReference<Bytes32>() {}),
            Arrays.<TypeReference<?>>asList(new TypeReference<Address>() {}));
    EthFilter filter = new EthFilter(startBlock, endBlock, getContractAddress());
    filter.addSingleTopic(EventEncoder.encode(event));
    return web3j.ethLogObservable(filter).map(new Func1<Log, AddrChangedEventResponse>() {
        @Override
        public AddrChangedEventResponse call(Log log) {
            EventValues eventValues = extractEventParameters(event, log);
            AddrChangedEventResponse typedResponse = new AddrChangedEventResponse();
            typedResponse.node = (byte[]) eventValues.getIndexedValues().get(0).getValue();
            typedResponse.a = (String) eventValues.getNonIndexedValues().get(0).getValue();
            return typedResponse;
        }
    });
}
 
Example #13
Source File: PublicResolver.java    From etherscan-explorer with GNU General Public License v3.0 6 votes vote down vote up
public Observable<ContentChangedEventResponse> contentChangedEventObservable(DefaultBlockParameter startBlock, DefaultBlockParameter endBlock) {
    final Event event = new Event("ContentChanged", 
            Arrays.<TypeReference<?>>asList(new TypeReference<Bytes32>() {}),
            Arrays.<TypeReference<?>>asList(new TypeReference<Bytes32>() {}));
    EthFilter filter = new EthFilter(startBlock, endBlock, getContractAddress());
    filter.addSingleTopic(EventEncoder.encode(event));
    return web3j.ethLogObservable(filter).map(new Func1<Log, ContentChangedEventResponse>() {
        @Override
        public ContentChangedEventResponse call(Log log) {
            EventValues eventValues = extractEventParameters(event, log);
            ContentChangedEventResponse typedResponse = new ContentChangedEventResponse();
            typedResponse.node = (byte[]) eventValues.getIndexedValues().get(0).getValue();
            typedResponse.hash = (byte[]) eventValues.getNonIndexedValues().get(0).getValue();
            return typedResponse;
        }
    });
}
 
Example #14
Source File: TokenERC20.java    From Android-Wallet-Token-ERC20 with Apache License 2.0 6 votes vote down vote up
public Observable<BurnEventResponse> burnEventObservable(DefaultBlockParameter startBlock, DefaultBlockParameter endBlock) {
    final Event event = new Event("Burn", 
            Arrays.<TypeReference<?>>asList(new TypeReference<Address>() {}),
            Arrays.<TypeReference<?>>asList(new TypeReference<Uint256>() {}));
    EthFilter filter = new EthFilter(startBlock, endBlock, getContractAddress());
    filter.addSingleTopic(EventEncoder.encode(event));
    return web3j.ethLogObservable(filter).map(new Func1<Log, BurnEventResponse>() {
        @Override
        public BurnEventResponse call(Log log) {
            EventValues eventValues = extractEventParameters(event, log);
            BurnEventResponse typedResponse = new BurnEventResponse();
            typedResponse.from = (String) eventValues.getIndexedValues().get(0).getValue();
            typedResponse.value = (BigInteger) eventValues.getNonIndexedValues().get(0).getValue();
            return typedResponse;
        }
    });
}
 
Example #15
Source File: Contract.java    From web3j with Apache License 2.0 6 votes vote down vote up
public static EventValues staticExtractEventParameters(Event event, Log log) {
    final List<String> topics = log.getTopics();
    String encodedEventSignature = EventEncoder.encode(event);
    if (topics == null || topics.size() == 0 || !topics.get(0).equals(encodedEventSignature)) {
        return null;
    }

    List<Type> indexedValues = new ArrayList<>();
    List<Type> nonIndexedValues =
            FunctionReturnDecoder.decode(log.getData(), event.getNonIndexedParameters());

    List<TypeReference<Type>> indexedParameters = event.getIndexedParameters();
    for (int i = 0; i < indexedParameters.size(); i++) {
        Type value =
                FunctionReturnDecoder.decodeIndexedValue(
                        topics.get(i + 1), indexedParameters.get(i));
        indexedValues.add(value);
    }
    return new EventValues(indexedValues, nonIndexedValues);
}
 
Example #16
Source File: TokenERC20.java    From Android-Wallet-Token-ERC20 with Apache License 2.0 6 votes vote down vote up
public Observable<TransferEventResponse> transferEventObservable(DefaultBlockParameter startBlock, DefaultBlockParameter endBlock) {
    final Event event = new Event("Transfer", 
            Arrays.<TypeReference<?>>asList(new TypeReference<Address>() {}, new TypeReference<Address>() {}),
            Arrays.<TypeReference<?>>asList(new TypeReference<Uint256>() {}));
    EthFilter filter = new EthFilter(startBlock, endBlock, getContractAddress());
    filter.addSingleTopic(EventEncoder.encode(event));
    return web3j.ethLogObservable(filter).map(new Func1<Log, TransferEventResponse>() {
        @Override
        public TransferEventResponse call(Log log) {
            EventValues eventValues = extractEventParameters(event, log);
            TransferEventResponse typedResponse = new TransferEventResponse();
            typedResponse.from = (String) eventValues.getIndexedValues().get(0).getValue();
            typedResponse.to = (String) eventValues.getIndexedValues().get(1).getValue();
            typedResponse.value = (BigInteger) eventValues.getNonIndexedValues().get(0).getValue();
            return typedResponse;
        }
    });
}
 
Example #17
Source File: SolidityFunctionWrapper.java    From client-sdk-java with Apache License 2.0 6 votes vote down vote up
MethodSpec buildDefaultEventObservableFunction(
        String responseClassName,
        String functionName) {

    String generatedFunctionName =
            Strings.lowercaseFirstLetter(functionName) + "EventObservable";
    ParameterizedTypeName parameterizedTypeName = ParameterizedTypeName.get(ClassName.get(rx
            .Observable.class), ClassName.get("", responseClassName));

    MethodSpec.Builder observableMethodBuilder =
            MethodSpec.methodBuilder(generatedFunctionName)
                    .addModifiers(Modifier.PUBLIC)
                    .addParameter(DefaultBlockParameter.class, START_BLOCK)
                    .addParameter(DefaultBlockParameter.class, END_BLOCK)
                    .returns(parameterizedTypeName);

    observableMethodBuilder.addStatement("$1T filter = new $1T($2L, $3L, "
            + "getContractAddress())", PlatonFilter.class, START_BLOCK, END_BLOCK)
            .addStatement("filter.addSingleTopic($T.encode("
                    + buildEventDefinitionName(functionName) + "))", EventEncoder.class)
            .addStatement("return " + generatedFunctionName + "(filter)");

    return observableMethodBuilder
            .build();
}
 
Example #18
Source File: TestnetConfig.java    From web3j with Apache License 2.0 5 votes vote down vote up
@Override
public String encodedEvent() {
    Event event =
            new Event(
                    "Notify",
                    Arrays.asList(
                            new TypeReference<Uint>(true) {}, new TypeReference<Uint>() {}));

    return EventEncoder.encode(event);
}
 
Example #19
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 #20
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 #21
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 #22
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 #23
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 #24
Source File: DepositContract.java    From teku with Apache License 2.0 5 votes vote down vote up
public SafeFuture<List<DepositEventEventResponse>> depositEventInRange(
    DefaultBlockParameter startBlock, DefaultBlockParameter endBlock) {
  final EthFilter filter = new EthFilter(startBlock, endBlock, getContractAddress());
  filter.addSingleTopic(EventEncoder.encode(DEPOSITEVENT_EVENT));
  return SafeFuture.of(
      web3j
          .ethGetLogs(filter)
          .sendAsync()
          .thenApply(
              logs ->
                  logs.getLogs().stream()
                      .map(log -> (Log) log.get())
                      .map(this::convertLogToDepositEventEventResponse)
                      .collect(Collectors.toList())));
}
 
Example #25
Source File: ContractTest.java    From client-sdk-java with Apache License 2.0 5 votes vote down vote up
@Test
public void testExtractEventParametersWithLogGivenATransactionReceipt() {

    final java.util.function.Function<String, Event> eventFactory = name ->
            new Event(name, emptyList());

    final BiFunction<Integer, Event, Log> logFactory = (logIndex, event) ->
            new Log(false, "" + logIndex, "0", "0x0", "0x0", "0", "0x" + logIndex, "", "",
                    singletonList(EventEncoder.encode(event)));

    final Event testEvent1 = eventFactory.apply("TestEvent1");
    final Event testEvent2 = eventFactory.apply("TestEvent2");

    final List<Log> logs = Arrays.asList(
            logFactory.apply(0, testEvent1),
            logFactory.apply(1, testEvent2)
    );

    final TransactionReceipt transactionReceipt = new TransactionReceipt();
    transactionReceipt.setLogs(logs);

    final List<Contract.EventValuesWithLog> eventValuesWithLogs1 =
            contract.extractEventParametersWithLog(testEvent1, transactionReceipt);

    assertEquals(eventValuesWithLogs1.size(), 1);
    assertEquals(eventValuesWithLogs1.get(0).getLog(), logs.get(0));

    final List<Contract.EventValuesWithLog> eventValuesWithLogs2 =
            contract.extractEventParametersWithLog(testEvent2, transactionReceipt);

    assertEquals(eventValuesWithLogs2.size(), 1);
    assertEquals(eventValuesWithLogs2.get(0).getLog(), logs.get(1));
}
 
Example #26
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 #27
Source File: ContractTest.java    From etherscan-explorer with GNU General Public License v3.0 5 votes vote down vote up
@Test
public void testExtractEventParametersWithLogGivenATransactionReceipt() {

    final java.util.function.Function<String, Event> eventFactory = name ->
            new Event(name, emptyList(), emptyList());

    final BiFunction<Integer, Event, Log> logFactory = (logIndex, event) ->
            new Log(false, "" + logIndex, "0", "0x0", "0x0", "0", "0x" + logIndex, "", "",
                    singletonList(EventEncoder.encode(event)));

    final Event testEvent1 = eventFactory.apply("TestEvent1");
    final Event testEvent2 = eventFactory.apply("TestEvent2");

    final List<Log> logs = Arrays.asList(
            logFactory.apply(0, testEvent1),
            logFactory.apply(1, testEvent2)
    );

    final TransactionReceipt transactionReceipt = new TransactionReceipt();
    transactionReceipt.setLogs(logs);

    final List<Contract.EventValuesWithLog> eventValuesWithLogs1 =
            contract.extractEventParametersWithLog(testEvent1, transactionReceipt);

    assertEquals(eventValuesWithLogs1.size(), 1);
    assertEquals(eventValuesWithLogs1.get(0).getLog(), logs.get(0));

    final List<Contract.EventValuesWithLog> eventValuesWithLogs2 =
            contract.extractEventParametersWithLog(testEvent2, transactionReceipt);

    assertEquals(eventValuesWithLogs2.size(), 1);
    assertEquals(eventValuesWithLogs2.get(0).getLog(), logs.get(1));
}
 
Example #28
Source File: EthereumUtils.java    From jbpm-work-items with Apache License 2.0 5 votes vote down vote up
public static void observeContractEvent(Web3j web3j,
                                        String contractEventName,
                                        String contractAddress,
                                        List<TypeReference<?>> indexedParameters,
                                        List<TypeReference<?>> nonIndexedParameters,
                                        String eventReturnType,
                                        KieSession kieSession,
                                        String signalName,
                                        boolean doAbortOnUpdate,
                                        WorkItemManager workItemManager,
                                        WorkItem workItem) {

    Event event = new Event(contractEventName,
                            indexedParameters,
                            nonIndexedParameters);

    EthFilter filter = new EthFilter(DefaultBlockParameterName.EARLIEST,
                                     DefaultBlockParameterName.LATEST,
                                     contractAddress);

    filter.addSingleTopic(EventEncoder.encode(event));

    Class<Type> type = (Class<Type>) AbiTypes.getType(eventReturnType);
    TypeReference<Type> typeRef = TypeReference.create(type);

    web3j.ethLogObservable(filter).subscribe(
            eventTrigger -> {
                kieSession.signalEvent(signalName,
                                       FunctionReturnDecoder.decode(
                                               eventTrigger.getData(),
                                               Arrays.asList(typeRef)).get(0).getValue());
                if (doAbortOnUpdate) {
                    workItemManager.completeWorkItem(workItem.getId(),
                                                     null);
                }
            }
    );
}
 
Example #29
Source File: SolidityFunctionWrapper.java    From web3j with Apache License 2.0 5 votes vote down vote up
MethodSpec buildDefaultEventFlowableFunction(String responseClassName, String functionName) {

        String generatedFunctionName = Strings.lowercaseFirstLetter(functionName) + "EventFlowable";
        ParameterizedTypeName parameterizedTypeName =
                ParameterizedTypeName.get(
                        ClassName.get(Flowable.class), ClassName.get("", responseClassName));

        MethodSpec.Builder flowableMethodBuilder =
                MethodSpec.methodBuilder(generatedFunctionName)
                        .addModifiers(Modifier.PUBLIC)
                        .addParameter(DefaultBlockParameter.class, START_BLOCK)
                        .addParameter(DefaultBlockParameter.class, END_BLOCK)
                        .returns(parameterizedTypeName);

        flowableMethodBuilder
                .addStatement(
                        "$1T filter = new $1T($2L, $3L, " + "getContractAddress())",
                        EthFilter.class,
                        START_BLOCK,
                        END_BLOCK)
                .addStatement(
                        "filter.addSingleTopic($T.encode("
                                + buildEventDefinitionName(functionName)
                                + "))",
                        EventEncoder.class)
                .addStatement("return " + generatedFunctionName + "(filter)");

        return flowableMethodBuilder.build();
    }
 
Example #30
Source File: Web3jUtil.java    From eventeum with Apache License 2.0 5 votes vote down vote up
public static String getSignature(ContractEventSpecification spec) {

        final List<ParameterDefinition> allParameterDefinitions = new ArrayList<>();
        addAllDefinitions(allParameterDefinitions, spec.getIndexedParameterDefinitions());
        addAllDefinitions(allParameterDefinitions, spec.getNonIndexedParameterDefinitions());
        Collections.sort(allParameterDefinitions);

        final Event event = new Event(spec.getEventName(),
                getTypeReferencesFromParameterDefinitions(allParameterDefinitions));

        return EventEncoder.encode(event);
    }