2

I'm using Web3j to interact with my deployed smart contract.

I've deployed my contract onto the testnet and can interact with it, however using the generated wrapper code, it requires my wallet.

Web3j web3 = Web3j.build(new HttpService("https://testnet-endpoint-rpc.com"));
            Credentials credentials = WalletUtils.loadCredentials(
                    "coolpassword1", "src\\main\\resources\\path-to-key-store.keystore"
            );

            FastRawTransactionManager txMananger = new FastRawTransactionManager(web3, credentials, 365);
            MySmartContract contract = MySmartContract.load(
                    "0x73292b80f99ffdc4e9a908865ce1d35fde7b736f", //Address for smartcontract
                    web3,
                    txMananger,
                    new DefaultGasProvider()
            );

//Reading from the contract
String contractName = contract.contractName(BigInteger.valueOf(0)).send();

How can I read from the contract without credentials?

DIRTY DAVE
  • 2,523
  • 2
  • 20
  • 83
  • 1
    It seems that `web3j` uses the `send()` method for both (read-write) transactions and (read-only) calls - [docs](https://docs.web3j.io/4.8.7/smart_contracts/interacting_with_smart_contract/#calling-constant-methods) - and that it decides which RPC method (`eth_sendTransaction` or `eth_call`) to use based on the contract ABI. If that's the case, you should be able to perform readonly calls, not requiring any credentials, to Solidity functions with modifiers `view` and `pure` (should not modify the contract state - [docs](https://docs.soliditylang.org/en/v0.8.9/contracts.html#state-mutability)). – Petr Hejda Oct 23 '21 at 13:44
  • @PetrHejda is it possible to sign a transaction with the mnemonic for the wallet? https://stackoverflow.com/q/69832708/11110509 – DIRTY DAVE Nov 03 '21 at 23:05
  • 1
    I don't know the specifics to the Java web3 implementation, my expertise is with the JS implementation. But generally, you can generate multiple private keys (for example using the BIP39 standard) from one mnemonic phrase. So first you need to get the desired private key, and then sign the transaction with the private key. But maybe the web3j does this operation in the background using the `Credentials` class, I'm not sure. – Petr Hejda Nov 04 '21 at 08:27

1 Answers1

0

I believe you can use the encodedFunctions to read without credentials but for write, you need the credentials.

Ex: balances is one of the methods in the contract with a single input of an address

Function function = 
  new Function("balances",
               Arrays.asList(new org.web3j.abi.datatypes.Address(walletAddress)),  
               new ArrayList());

String encodedFunction = FunctionEncoder.encode(function);
            org.web3j.protocol.core.methods.response.EthCall response = this.web3.ethCall(
                Transaction.createEthCallTransaction(walletAddress, ContractAddress, encodedFunction),
            DefaultBlockParameterName.LATEST)
        .sendAsync().get();