首页 > 编程知识 正文

以太坊怎么做合约,什么叫以太坊合约赚钱快吗

时间:2023-05-05 20:30:31 阅读:207109 作者:4787

以太坊笔记:为什么合约要用到TransactionManager
先上代码:

package org.blockchain.services.ethereum;import org.blockchain.model.ethereum.ERC20;import org.web3j.crypto.Credentials;import org.web3j.crypto.ECKeyPair;import org.web3j.crypto.WalletFile;import org.web3j.protocol.Web3j;import org.web3j.protocol.Web3jService;import org.web3j.protocol.core.DefaultBlockParameterNumber;import org.web3j.protocol.core.Request;import org.web3j.protocol.core.methods.response.TransactionReceipt;import org.web3j.protocol.http.HttpService;import org.web3j.protocol.parity.Parity;import org.web3j.protocol.parity.methods.response.ParityExportAccount;import org.web3j.protocol.parity.methods.response.ParityTracesResponse;import org.web3j.protocol.parity.methods.response.Trace;import org.web3j.tx.FastRawTransactionManager;import org.web3j.tx.TransactionManager;import org.web3j.tx.gas.DefaultGasProvider;import org.web3j.tx.response.NoOpProcessor;import java.math.BigInteger;import java.util.List;public class Web3JClient { private static Web3jService service = new HttpService("http://localhost:8545"); private static Web3j web3j = Web3j.build(service); private static Parity parity = Parity.build(service); public static void main(String[] args) throws Exception { /** * TEST DEPLOY AND CALL CONTRACT FUNCTION */ // load private key into eckey to sign String privatekey = "***********************************"; BigInteger privkey = new BigInteger(privatekey, 16); ECKeyPair ecKeyPair = ECKeyPair.create(privkey); Credentials credentials = Credentials.create(ecKeyPair); NoOpProcessor processor = new NoOpProcessor(web3j); //deploy new contract TransactionManager txManager = new FastRawTransactionManager(web3j, credentials, processor); RemoteCall<ERC20> request = ERC20.deploy(web3j, txManager, DefaultGasProvider.GAS_PRICE, DefaultGasProvider.GAS_LIMIT); ERC20 token = request.send(); String contractAddress = token.getDeployedAddress("3"); // 3 is ropsten testnet // load existing contract by address // ERC20 token = ERC20.load(contractAddress, web3j, txManager, DefaultGasProvider.GAS_PRICE, DefaultGasProvider.GAS_LIMIT); // create transaction transfer token to receiver String receiver = "0xa107483c8a16a58871182a48d4ba1fbbb6a*****"; BigInteger value = new BigInteger("10000000000000"); TransactionReceipt receipt = token.transfer(receiver, value).send(); // get transaction result System.out.println(receipt.getTransactionHash()); }}

看了下源代码加载已有的合约应该是不需要的私钥的
ERC20 token = ERC20.load(contractAddress, web3j, txManager, DefaultGasProvider.GAS_PRICE, DefaultGasProvider.GAS_LIMIT);
私钥主要是用于部署
TransactionManager
可以看到
RawTransactionManager
实现了sendTransaction 的方法里面用了signAndSend的方法
本地ganache测试下的调用view合约方法的确不需要密钥
但调用更改状态的合约方法的确需要密钥

public EthSendTransaction sendTransaction( BigInteger gasPrice, BigInteger gasLimit, String to, String data, BigInteger value) throws IOException { return sendTransaction(gasPrice, gasLimit, to, data, value, false); }

RawTransactionManager

@Override public EthSendTransaction sendTransaction( BigInteger gasPrice, BigInteger gasLimit, String to, String data, BigInteger value, boolean constructor) throws IOException { BigInteger nonce = getNonce(); RawTransaction rawTransaction = RawTransaction.createTransaction(nonce, gasPrice, gasLimit, to, value, data); return signAndSend(rawTransaction); }

如何调用代码实例:https://medium.com/coinmonks/how-to-interact-with-smart-contract-using-java-f5b1ce7324e7

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。