Web3j вызывает переменную в смарт-контракте

Я пытаюсь получить значение переменной в смарт-контракте, используя solidity, geth и web3j.

Контракт HelloWorld очень прост:

pragma solidity ^0.6.10;
   contract HelloWorld {
   uint256 public counter = 5;
  
   function add() public {  //increases counter by 1
       counter++;
   }
 
   function subtract() public { //decreases counter by 1
       counter--;
   }
   
   function getCounter() public view returns (uint256) {
       return counter;
   }
}

web3j не имеет функции call (), только send (), что удивительно.

когда я пытаюсь получить счетчик, следуя инструкциям web3j:

contract.getCounter().send()

Я получаю квитанцию ​​о транзакции, а не значение uint256.

Кто-нибудь может помочь?

Спасибо

Воля


person user1640943    schedule 18.07.2020    source источник
comment
В Web3Js есть метод вызова . Чтобы вызвать ваш метод getCounter (), используйте этот синтаксис: contract.methods.getCounter().call()...   -  person Emmanuel Collin    schedule 23.07.2020


Ответы (1)


Вам необходимо изменить функцию getCounter () в сгенерированном файле HelloWorld.java.

public RemoteCall<Type> getCounter() {
    final Function function = new Function(
            FUNC_GETCOUNTER, 
            Arrays.<Type>asList(), 
            Arrays.<TypeReference<?>>asList(new TypeReference<Uint>() {}));
    return executeRemoteCallSingleValueReturn(function);
}

И чтобы получить значение, используйте следующий код:

Type message = contract.getCounter().send();
System.out.println(message.getValue()); 
person dev13    schedule 22.09.2020