Deploy
Web3 SDKs support to interact with Smart Contract via contract instance, not low-level transaction.
Import necessary classes from the Web3j and kaia libraries(web3j-ext
).
Connect to the kaia blockchain using Web3j and specify the URL
Create KlayCredentials using the private key
Define gas price and gas limit for the transaction
Get the sender's address and nonce
Define contract bytecode, you can get it from compiled solidity code
Get the chain ID from the Kaia network
Initialize variables for transaction parameters
Create a raw transaction
Sign the raw transaction using KlayRawTransaction.createTransaction
Send the transaction to kaia network
Get the transaction receipt and using the transaction hash
Shut down the Web3j connection
Decode the raw transaction to get the transaction type
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()); }}
❯ 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