Web3j -无凭证如何读取合同?



我正在使用Web3j与我部署的智能合约交互。

我已经将我的合约部署到测试网络上,并且可以与它交互,但是使用生成的包装器代码,它需要我的钱包。

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();

如果没有credentials,我怎么从合同中读取?

我相信您可以使用encodedFunctions来读取而不需要凭据,但是对于写入,您需要凭据。

Ex: balances是合约中的方法之一,只需要输入一个地址

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();

最新更新