本页面使用机器翻译自英语,可能包含错误或不清楚的语言。如需最准确的信息,请参阅英文原文。由于更新频繁,部分内容可能与英文原文有出入。请加入我们在 Crowdin 上的努力,帮助我们改进本页面的翻译。 (Crowdin translation page, Contributing guide)
Deploy
Web3 SDK 支持通过合约实例(而非底层事务)与智能合约进行交互。
从 Web3j 和 kaia 库(web3j-ext)中导入必要的类。
使用 Web3j 连接到 kaia 区块链并指定 URL
使用私钥创建 KlayCredentials
确定交易的gas价格和gas限额
获取发件人地址和非密码
定义 ** 合约字节码**,可从编译后的 solidity 代码中获取
从 Kaia 网络获取链 ID
为交易参数初始化变量
创建原始交易
使用 KlayRawTransaction.createTransaction 签署原始事务
将交易发送至 kaia 网络
获取交易收据并使用交易哈希值
关闭 Web3j 连接
解码原始交易以获取交易类型
smartContractDeploy.java
package org.web3j.example.contracts;import java.io.IOException;import java.math.BigInteger;import org.web3j.crypto.KlayCredentials;import org.web3j.crypto.KlayRawTransaction;import org.web3j.crypto.KlayTransactionEncoder;import org.web3j.crypto.transaction.type.TxType;import org.web3j.crypto.transaction.type.TxTypeSmartContractDeploy;import org.web3j.crypto.transaction.type.TxType.Type;import org.web3j.protocol.core.DefaultBlockParameterName;import org.web3j.protocol.core.methods.response.EthChainId;import org.web3j.protocol.core.methods.response.EthSendTransaction;import org.web3j.protocol.http.HttpService;import org.web3j.protocol.kaia.Web3j;import org.web3j.utils.Numeric;import org.web3j.protocol.kaia.core.method.response.TransactionReceipt;import org.web3j.tx.response.PollingTransactionReceiptProcessor;import org.web3j.tx.response.TransactionReceiptProcessor;import org.web3j.example.keySample;/** * */public class DeployContractExample implements keySample {        /**        *        */        public static void run() throws Exception {                Web3j web3j = Web3j.build(new HttpService(keySample.BAOBAB_URL));                KlayCredentials credentials = KlayCredentials.create(keySample.LEGACY_KEY_privkey);                BigInteger GAS_PRICE = BigInteger.valueOf(50000000000L);                BigInteger GAS_LIMIT = BigInteger.valueOf(6721950);                String from = credentials.getAddress();                BigInteger nonce = web3j.ethGetTransactionCount(from, DefaultBlockParameterName.LATEST).send()                                .getTransactionCount();                String data = "0x60c0604052600c60809081526b48656c6c6f20576f726c642160a01b60a05260009061002b90826100dd565b5034801561003857600080fd5b5061019c565b634e487b7160e01b600052604160045260246000fd5b600181811c9082168061006857607f821691505b60208210810361008857634e487b7160e01b600052602260045260246000fd5b50919050565b601f8211156100d857600081815260208120601f850160051c810160208610156100b55750805b601f850160051c820191505b818110156100d4578281556001016100c1565b5050505b505050565b81516001600160401b038111156100f6576100f661003e565b61010a816101048454610054565b8461008e565b602080601f83116001811461013f57600084156101275750858301515b600019600386901b1c1916600185901b1785556100d4565b600085815260208120601f198616915b8281101561016e5788860151825594840194600190910190840161014f565b508582101561018c5787850151600019600388901b60f8161c191681555b5050505050600190811b01905550565b61019a806101ab6000396000f3fe608060405234801561001057600080fd5b506004361061002b5760003560e01c8063cfae321714610030575b600080fd5b61003861004e565b60405161004591906100dc565b60405180910390f35b6000805461005b9061012a565b80601f01602080910402602001604051908101604052809291908181526020018280546100879061012a565b80156100d45780601f106100a9576101008083540402835291602001916100d4565b820191906000526020600020905b8154815290600101906020018083116100b757829003601f168201915b505050505081565b600060208083528351808285015260005b81811015610109578581018301518582016040015282016100ed565b506000604082860101526040601f19601f8301168501019250505092915050565b600181811c9082168061013e57607f821691505b60208210810361015e57634e487b7160e01b600052602260045260246000fd5b5091905056fea264697066735822122002272dad43feb87cde6d15be86d8d1af21672f2443deb524dce07ca0210d1cec64736f6c63430008120033";                EthChainId EthchainId = web3j.ethChainId().send();                long chainId = EthchainId.getChainId().longValue();                String to = null;                byte[] payload = Numeric.hexStringToByteArray(data);                BigInteger value = BigInteger.ZERO;                BigInteger codeFormat = BigInteger.ZERO;                TxType.Type type = Type.SMART_CONTRACT_DEPLOY;                KlayRawTransaction raw = KlayRawTransaction.createTransaction(                                type,                                nonce,                                GAS_PRICE,                                GAS_LIMIT,                                to,                                value,                                from,                                payload,                                codeFormat);                byte[] signedMessage = KlayTransactionEncoder.signMessage(raw, chainId, credentials);                String hexValue = Numeric.toHexString(signedMessage);                EthSendTransaction transactionResponse = web3j.ethSendRawTransaction(hexValue).send();                System.out.println("TxHash : \n " + transactionResponse.getResult());                String txHash = transactionResponse.getResult();                int DEFAULT_POLLING_ATTEMPTS_PER_TX_HASH = 40;                int DEFAULT_BLOCK_TIME = 1 * 1000;                long DEFAULT_POLLING_FREQUENCY = DEFAULT_BLOCK_TIME;                TransactionReceiptProcessor transactionReceiptProcessor = new PollingTransactionReceiptProcessor(web3j,                                DEFAULT_POLLING_FREQUENCY, DEFAULT_POLLING_ATTEMPTS_PER_TX_HASH);                org.web3j.protocol.core.methods.response.TransactionReceipt ethReceipt = transactionReceiptProcessor                                .waitForTransactionReceipt(txHash);                System.out.println("Receipt from eth_getTransactionReceipt : \n" + ethReceipt);                TransactionReceipt receipt = web3j.klayGetTransactionReceipt(txHash).send().getResult();                System.out.println("Receipt from klay_getTransactionReceipt : \n" + receipt);                web3j.shutdown();                TxTypeSmartContractDeploy rawTransaction = TxTypeSmartContractDeploy                                .decodeFromRawTransaction(signedMessage);                System.out.println("TxType : " + rawTransaction.getKlayType());        }}
output
❯ java smartContractDeploy.javaTxHash : 0xd780119c1c48519b348a653045acf28314a6da2be43dcf76c443390f0ef32a2creceipt :class TransactionReceipt {    blockHash: 0xf5d6fc86cc6dd1fab6e66e4436188b62302fe8ffd36fede94d42cf62a5f99e25    blockNumber: 0x8ded0a4    codeFormat: 0x0    contractAddress: 0x81fd08cfe4c37cc85fe7a33db9d493c99852dd55    feePayer: null    feePayerSignatures: []    feeRatio: null    from: 0xa2a8854b1802d8cd5de631e690817c253d6a9153    gas: 0x66919e    effectiveGasPrice: 0x5d21dba00    gasPrice: 0xba43b7400    gasUsed: 0x3af17    humanReadable: false    key: null    input: 0x60c0604052600c60809081526b48656c6c6f20576f726c642160a01b60a05260009061002b90826100dd565b5034801561003857600080fd5b5061019c565b634e487b7160e01b600052604160045260246000fd5b600181811c9082168061006857607f821691505b60208210810361008857634e487b7160e01b600052602260045260246000fd5b50919050565b601f8211156100d857600081815260208120601f850160051c810160208610156100b55750805b601f850160051c820191505b818110156100d4578281556001016100c1565b5050505b505050565b81516001600160401b038111156100f6576100f661003e565b61010a816101048454610054565b8461008e565b602080601f83116001811461013f57600084156101275750858301515b600019600386901b1c1916600185901b1785556100d4565b600085815260208120601f198616915b8281101561016e5788860151825594840194600190910190840161014f565b508582101561018c5787850151600019600388901b60f8161c191681555b5050505050600190811b01905550565b61019a806101ab6000396000f3fe608060405234801561001057600080fd5b506004361061002b5760003560e01c8063cfae321714610030575b600080fd5b61003861004e565b60405161004591906100dc565b60405180910390f35b6000805461005b9061012a565b80601f01602080910402602001604051908101604052809291908181526020018280546100879061012a565b80156100d45780601f106100a9576101008083540402835291602001916100d4565b820191906000526020600020905b8154815290600101906020018083116100b757829003601f168201915b505050505081565b600060208083528351808285015260005b81811015610109578581018301518582016040015282016100ed565b506000604082860101526040601f19601f8301168501019250505092915050565b600181811c9082168061013e57607f821691505b60208210810361015e57634e487b7160e01b600052602260045260246000fd5b5091905056fea264697066735822122002272dad43feb87cde6d15be86d8d1af21672f2443deb524dce07ca0210d1cec64736f6c63430008120033    logs: []    logsBloom: 0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000    nonce: 0x3c3    senderTxHash: 0xd780119c1c48519b348a653045acf28314a6da2be43dcf76c443390f0ef32a2c    signature: []    status: 0x1    txError: null    to: null    transactionHash: 0xd780119c1c48519b348a653045acf28314a6da2be43dcf76c443390f0ef32a2c    transactionIndex: 0x1    type: TxTypeSmartContractDeploy    typeInt: 40    value: 0x0}TxType : SMART_CONTRACT_DEPLOY