Posted By : Suraj
INTRODUCTION:
Hashgraph is a distributed ledger technology. It uses the DAG (Directed Acyclic Graph). It has already been used in machine learning, compilers, and statistics. In this case, the data is not stored in blocks, but distributed by events. DAG system is also used to solve the Byzantine General Problems. As a rule, each project developing a distributed registry has its own way of synchronizing nodes, i.e their own consensus algorithm.
Hashgraph Consensus Algorithm:
Each validator collects its transactions, randomly chooses the other participant, and transfers the information about the transactions to the participant, as well as the information that the other nodes have informed when they connected to him.
It has simple functionality. Tthe validator “listens”, receives all messages that are coming to him, and afterwards, randomly chooses the other participant and sends him all the transactions along with his own. Hashgraph ensure that with steps' final number (rounds of protocol’s work), the same state is achieved by all nodes. Its protocol is fast and easily scalable. In the network, there is no leader. As long as 2/3 honest nodes remain, the protocol secures the high level of security. For nodes to be synchronized and to achieve the agreement between them, there is also an exchange process going between the other participants of the network randomly.
Registration (KYC) :
First, you have to register on portal and pass the identity proof. It is important. Registration at the portal is possible as part of the testing phase. Here are instructions for registration and the KYC process. link.
Network selection:
First of all you have to choose network, so that the smart contracts functionality is available at the moment only in testnet. Now, let’s choose the testnet.
Network access
The portal will request that you provide an access code to the network, which can be received via e-mail, by leaving the request at the website.
Upon entering the code, Hedera returns the following information:
This data will needed in the future.
Public and private keys:
First of all you have to add dependency in you pom.xml file is given below:
<dependency>
<groupId>com.hedera.hashgraph</groupId>
<artifactId>sdk</artifactId>
<version>0.7.0</version>
</dependency>
To generate public and private Keys you have to the write of some code in java which is given below:
Ed25519PrivateKey newKey = Ed25519PrivateKey.generate();
Ed25519PublicKey newPublicKey = newKey.getPublicKey();
Deploying of smart contract
To deploy a smart contract, first of all you have to compile your solidity code and get the bin file of that smart contract and your bin file contain some byte code. You can get bin file of your smart contract with the use of command and the exemple of byte code which is given below:-
solc SimpleToken.sol --allow-paths "." --bin --abi --optimize -o /home/suraj/Downloads/hedera1/
Byte code: 302e020100300506032b657004220420d396c78d1d4c31a9f3f5c16eb81eb542803c89beddf30e1e1a583dd2db433704
HelperClass:
package com.suraj.phicoin.service;
import com.hedera.hashgraph.sdk.Client;
import com.hedera.hashgraph.sdk.account.AccountId;
import com.hedera.hashgraph.sdk.crypto.ed25519.Ed25519PrivateKey;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
@Service
public class HederaClient {
@Value("${OPERATOR_ID}")
private String operatorId;
@Value("${OPERATOR_KEY}")
private String operatorKey;
@Value("${Node_Address_1}")
private String nodeAddress1;
@Value("${Node_Address_2}")
private String nodeAddress2;
@Value("${Node_Address_3}")
private String nodeAddress3;
@Value("${Node_Address_4}")
private String nodeAddress4;
@Value("${Node_Id_1}")
private String node1;
@Value("${Node_Id_2}")
private String node2;
@Value("${Node_Id_3}")
private String node3;
@Value("${Node_Id_4}")
private String node4;
public Client hederaClientSetup() {
AccountId operatorId1 = AccountId.fromString(operatorId);
AccountId node_1 = AccountId.fromString(node1);
AccountId node_2 = AccountId.fromString(node2);
AccountId node_3 = AccountId.fromString(node3);
AccountId node_4 = AccountId.fromString(node4);
Map<AccountId,String> nodeAddressMap = new HashMap<>();
nodeAddressMap.put(node_1,nodeAddress1);
nodeAddressMap.put(node_2,nodeAddress2);
nodeAddressMap.put(node_3,nodeAddress3);
nodeAddressMap.put(node_4,nodeAddress4);
System.out.println(nodeAddressMap);
Client hederaClient = new Client(nodeAddressMap);
Ed25519PrivateKey operatorKey1 = Ed25519PrivateKey.fromString(operatorKey);
hederaClient.setOperator(operatorId1,operatorKey1);
return hederaClient;
}
public String getOpreatorKey() {
return operatorKey;
}
public String getOperatorId() {
return operatorId;
}
}
package com.suraj.phicoin.service;
import com.hedera.hashgraph.sdk.*;
import com.hedera.hashgraph.sdk.contract.ContractCallQuery;
import com.hedera.hashgraph.sdk.contract.ContractCreateTransaction;
import com.hedera.hashgraph.sdk.contract.ContractDeleteTransaction;
import com.hedera.hashgraph.sdk.contract.ContractId;
import com.hedera.hashgraph.sdk.crypto.ed25519.Ed25519PrivateKey;
import com.hedera.hashgraph.sdk.file.FileCreateTransaction;
import com.hedera.hashgraph.sdk.file.FileId;
import com.hederahashgraph.api.proto.java.ResponseCodeEnum;
import java.io.IOException;
import java.time.Duration;
import java.time.Instant;
public class CreateSimpleContract {
private CreateSimpleContract() { }
public static void main(String[] args) throws HederaException, IOException {
HederaClient hederaClient = new HederaClient();
String byteCodeHex = "608060405234801561001057600080fd5b50336000806101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff1602179055506101cb806100606000396000f3fe608060405260043610610046576000357c01000000000000000000000000000000000000000000000000000000009004806341c0e1b51461004b578063cfae321714610062575b600080fd5b34801561005757600080fd5b506100606100f2565b005b34801561006e57600080fd5b50610077610162565b6040518080602001828103825283818151815260200191508051906020019080838360005b838110156100b757808201518184015260208101905061009c565b50505050905090810190601f1680156100e45780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b6000809054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff161415610160573373ffffffffffffffffffffffffffffffffffffffff16ff5b565b60606040805190810160405280600d81526020017f48656c6c6f2c20776f726c64210000000000000000000000000000000000000081525090509056fea165627a7a72305820ae96fb3af7cde9c0abfe365272441894ab717f816f07f41f07b1cbede54e256e0029";
byte[] byteCode = byteCodeHex.getBytes();
Ed25519PrivateKey operatorKey = Ed25519PrivateKey.fromString("302e020100300506032b657004220420d396c78d1d4c31a9f3f5c16eb81eb542803c89beddf30e1e1a583dd2db433704");
Client client = hederaClient.hederaClientSetup();
// create the contract's bytecode file
FileCreateTransaction fileTx = new FileCreateTransaction(client).setExpirationTime(
Instant.now()
.plus(Duration.ofSeconds(3600)))
// Use the same key as the operator to "own" this file
.addKey(operatorKey.getPublicKey())
.setContents(byteCode);
TransactionReceipt fileReceipt = fileTx.executeForReceipt();
FileId newFileId = fileReceipt.getFileId();
System.out.println("contract bytecode file: " + newFileId);
// create the contract itself
ContractCreateTransaction contractTx = new ContractCreateTransaction(client).setAutoRenewPeriod(Duration.ofHours(1))
.setGas(217000)
.setBytecodeFile(newFileId)
// set an admin key so we can delete the contract later
.setAdminKey(operatorKey.getPublicKey());
TransactionReceipt contractReceipt = contractTx.executeForReceipt();
System.out.println(contractReceipt.toProto());
ContractId newContractId = contractReceipt.getContractId();
System.out.println("new contract ID: " + newContractId);
FunctionResult contractCallResult = new ContractCallQuery(client).setGas(30000)
.setContractId(newContractId)
.setFunctionParameters(CallParams.function("greet"))
.execute();
if (contractCallResult.getErrorMessage() != null) {
System.out.println("error calling contract: " + contractCallResult.getErrorMessage());
return;
}
String message = contractCallResult.getString();
System.out.println("contract message: " + message);
// now delete the contract
TransactionReceipt contractDeleteResult = new ContractDeleteTransaction(client)
.setContractId(newContractId)
.executeForReceipt();
if (contractDeleteResult.getStatus() != ResponseCodeEnum.SUCCESS) {
System.out.println("error deleting contract: " + contractDeleteResult.getStatus());
return;
}
System.out.println("Contract successfully deleted");
}
}
September 8, 2024 at 02:29 am
Your comment is awaiting moderation.