Integration of EOS Coin in java project
Posted By : Rahbar Ali | 04-Dec-2018
Introduction->
Eos is a newly launch blockchain(and a coin) that build smart contract like ethereum but its much faster than ethereum
the developer who want to create smart contract on eos network that much hold some eos on his account for use resources of eos blockchain
like network bandwidth, ram and cpu.this resources help user to create smart contract and distribute its on his users.
every user who want to use eos network for transfer eos (or make smart contract) from his account to another then it must be hold some
amount of eos for network bandwidth, ram and cpu . so that means at the time of transaction there are no fees deduct from his wallet
on behalf of this resources but this resources is stuck for some time (3 days) that is depends on transaction size and its execution time.
On Eos
Setup Eos in ubuntu os->
we have setup
for set
after setup you get two
1-
2-
now for
and add
<repositories> <repository> <id>oss.sonatype.org-snapshot</id> <url>http://oss.sonatype.org/content/repositories/snapshots</url> <releases> <enabled>false</enabled> </releases> <snapshots> <enabled>true</enabled> </snapshots> </repository> </repositories> <dependencies> <dependency> <groupId>io.jafka</groupId> <artifactId>jeos</artifactId> <version>1.0-SNAPSHOT</version> </dependency> </dependencies>
you can check above dependency on this url
https://github.com/adyliu/jeos that is open-source
For create an eos account you need an existing eos account because every eos account need some eos coin for purchase some amount
of ram, cpu and network bandwidth.
package com.eos.service; import java.time.ZoneId; import java.time.ZonedDateTime; import java.time.format.DateTimeFormatter; import java.time.temporal.ChronoUnit; import java.util.Arrays; import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; import javax.annotation.PostConstruct; import org.apache.log4j.BasicConfigurator; import com.eos.Dto.CreateAccountArg; import com.eos.Dto.Key; import com.eos.Dto.RequiredAuth; import com.fasterxml.jackson.databind.ObjectMapper; import client.EosApiClientFactory; import client.EosApiRestClient; import client.domain.common.WalletKeyType; import io.jafka.jeos.EosApi; import io.jafka.jeos.EosApiFactory; import io.jafka.jeos.core.common.transaction.PackedTransaction; import io.jafka.jeos.core.common.transaction.SignedPackedTransaction; import io.jafka.jeos.core.common.transaction.TransactionAction; import io.jafka.jeos.core.common.transaction.TransactionAuthorization; import io.jafka.jeos.core.request.chain.AbiJsonToBinRequest; import io.jafka.jeos.core.request.chain.json2bin.BuyRamArg; import io.jafka.jeos.core.request.chain.json2bin.DelegatebwArg; import io.jafka.jeos.core.response.chain.AbiJsonToBin; import io.jafka.jeos.core.response.chain.Block; import io.jafka.jeos.core.response.chain.transaction.PushedTransaction; import io.jafka.jeos.exception.EosApiException; import io.jafka.jeos.impl.EosApiServiceGenerator; public class CreateAccount { static void create(EosApi client) throws Exception { ObjectMapper mapper = EosApiServiceGenerator.getMapper(); final String newAccountName = "nitinoodles2"; String password = client.createWallet(newAccountName); System.out.println("password "+password); String pubKey = client.createKey(newAccountName, WalletKeyType.K1); System.out.println("pubKey "+pubKey); final String creator = "avnishpan124"; // ? build binary of create account CreateAccountArg createAccountArg = new CreateAccountArg(); { createAccountArg.setCreator(creator); createAccountArg.setNewact(newAccountName); // set the owner key RequiredAuth owner = new RequiredAuth(); owner.setThreshold(1L); owner.setAccounts(Collections.EMPTY_LIST); owner.setWaits(Collections.EMPTY_LIST); owner.setKeys(Arrays.asList(new Key(pubKey, 1L))); createAccountArg.setOwner(owner); // set the active key RequiredAuth active = new RequiredAuth(); active.setThreshold(1L); active.setAccounts(Collections.EMPTY_LIST); active.setWaits(Collections.EMPTY_LIST); active.setKeys(Arrays.asList(new Key(pubKey, 1L))); createAccountArg.setActive(active); } AbiJsonToBin createAccountData = client.abiJsonToBin("eosio", "newaccount", createAccountArg); System.out.println("create account bin= " + createAccountData.getBinargs()); // ? build binary of ram BuyRamArg buyRamArg = new BuyRamArg(creator,newAccountName, 1024 * 2);//8k memory AbiJsonToBin buyRamData = client.abiJsonToBin("eosio","buyrambytes", buyRamArg); System.out.println("buy ram bin= "+ buyRamData.getBinargs()); // ? delegatebw cpu and net DelegatebwArg delegatebwArg = new DelegatebwArg(creator, newAccountName, "0.1000 EOS", "0.1000 EOS", 0L); AbiJsonToBin delegatebwData = client.abiJsonToBin("eosio", "delegatebw", delegatebwArg); System.out.println("delegatebw bin= "+delegatebwData.getBinargs()); // ? get the latest block info Block block = client.getBlock(client.getChainInfo().getHeadBlockId()); System.out.println("blockNum=" + block.getBlockNum()); // ? create the authorization List<TransactionAuthorization> authorizations = Arrays.asList(new TransactionAuthorization(creator, "active")); // ? build the all actions List<TransactionAction> actions = Arrays.asList(// new TransactionAction("eosio","newaccount",authorizations, createAccountData.getBinargs())// ,new TransactionAction("eosio","buyrambytes",authorizations, buyRamData.getBinargs())// ,new TransactionAction("eosio","delegatebw",authorizations, delegatebwData.getBinargs())// ); // ? build the packed transaction PackedTransaction packedTransaction = new PackedTransaction(); packedTransaction.setRefBlockPrefix(block.getRefBlockPrefix()); packedTransaction.setRefBlockNum(block.getBlockNum()); // expired after 3 minutes String expiration = ZonedDateTime.now(ZoneId.of("GMT")).plusMinutes(2)// .truncatedTo(ChronoUnit.SECONDS).format(DateTimeFormatter.ISO_LOCAL_DATE_TIME); packedTransaction.setExpiration(expiration); packedTransaction.setRegion("0"); packedTransaction.setMaxNetUsageWords(0); packedTransaction.setMaxCpuUsageMs(0); packedTransaction.setActions(actions); // ? unlock the creator's wallet try { client.unlockWallet("avnishpan124", "PW5J8ZmL6x8D5BNpXpSSb9uWMrGeDP9xA9xhruBVMddjkahDM879n"); } catch (EosApiException ex) { System.err.println(ex.getMessage()); } // ? sign the transaction SignedPackedTransaction signedPackedTransaction = client.signTransaction(packedTransaction, // Arrays.asList("EOS8U5q3Ri1yhRZQ8Kyyoie8L5KQGhSQmPZ7bhWmqQG2qKyRoJ6jC"), // "038f4b0fc8ff18a4f0842a8f0564611f6e96e8535901dd45e43ac8691a1c4dca"); System.out.println("signedPackedTransaction=" + mapper.writeValueAsString(signedPackedTransaction)); System.out.println("\n--------------------------------\n"); // ? push the signed transaction PushedTransaction pushedTransaction = client.pushTransaction("none", signedPackedTransaction); System.out.println("pushedTransaction=" + mapper.writeValueAsString(pushedTransaction)); System.out.println("Transaction id .. "+pushedTransaction.getTransactionId()); } public static void main() throws Exception { BasicConfigurator.configure(); EosApi client = EosApiFactory.create("http://127.0.0.1:3000", // "http://jungle.cryptolions.io:38888",// "http://jungle.cryptolions.io:38888");; // ------------------------------------------------------------------------ create(client); } }
After successfull run above programe you will get Transaction id of newly created account.
Process of creating account name on eos blockchain-
1- we will use an existing eos account that will purchase ram, cpu and network-bandwidth for new account.
2- So for purchase of these assets from eos blockchain creator give some amount of eos-coin (fees of new account creating) this fees
are not fixed this will varry upon network uses.
3- After purchase of assets creator bind that newly created account with in PackedTransaction and broadcast into on eos-blockchain
if eos-blockchain accept this transaction then its gives an transaction id of related to that transaction.
2-> Send Eos from one account to another account.
package com.eos.service; import java.time.ZoneId; import java.time.ZonedDateTime; import java.time.format.DateTimeFormatter; import java.time.temporal.ChronoUnit; import java.util.Arrays; import java.util.HashMap; import java.util.List; import java.util.Map; import org.apache.log4j.BasicConfigurator; import com.fasterxml.jackson.databind.ObjectMapper; import io.jafka.jeos.EosApi; import io.jafka.jeos.EosApiFactory; import io.jafka.jeos.core.common.transaction.PackedTransaction; import io.jafka.jeos.core.common.transaction.SignedPackedTransaction; import io.jafka.jeos.core.common.transaction.TransactionAction; import io.jafka.jeos.core.common.transaction.TransactionAuthorization; import io.jafka.jeos.core.request.chain.json2bin.TransferArg; import io.jafka.jeos.core.response.chain.AbiJsonToBin; import io.jafka.jeos.core.response.chain.Block; import io.jafka.jeos.core.response.chain.transaction.PushedTransaction; import io.jafka.jeos.exception.EosApiException; import io.jafka.jeos.impl.EosApiServiceGenerator; public class TransferEOS { static void transfer(EosApi client) throws Exception { ObjectMapper mapper = EosApiServiceGenerator.getMapper(); final String from = "avnishpan124"; // ? pack transfer data TransferArg transferArg = new TransferArg(from, "shubhamgwa24", "0.9999 EOS", "hello"); AbiJsonToBin data = client.abiJsonToBin("eosio.token", "transfer", transferArg); System.out.println("data "+data.getBinargs()); //AbiJsonToBin data = client.abiJsonToBin("eosio.token", "transfer", transferArg); //System.out.println("bin= " + data.getBinargs()); // ? get the latest block info Block block = client.getBlock(client.getChainInfo().getHeadBlockId()); System.out.println("blockNum=" + block.getBlockNum()); // ? create the authorization List<TransactionAuthorization> authorizations = Arrays.asList(new TransactionAuthorization(from, "active")); // ? build the all actions List<TransactionAction> actions = Arrays.asList(// new TransactionAction("eosio.token", "transfer", authorizations, data.getBinargs())// ); // ? build the packed transaction PackedTransaction packedTransaction = new PackedTransaction(); packedTransaction.setRefBlockPrefix(block.getRefBlockPrefix()); packedTransaction.setRefBlockNum(block.getBlockNum()); // expired after 3 minutes String expiration = ZonedDateTime.now(ZoneId.of("GMT")).plusMinutes(2).truncatedTo(ChronoUnit.SECONDS).format(DateTimeFormatter.ISO_LOCAL_DATE_TIME); System.out.println("............expiration ...."+expiration); packedTransaction.setExpiration(expiration); packedTransaction.setRegion("0"); packedTransaction.setMaxNetUsageWords(0); packedTransaction.setMaxCpuUsageMs(0); packedTransaction.setActions(actions); // ? unlock the creator's wallet
// here we are unlocking wallet that will send coin try { client.unlockWallet("avnishpan124", "PW5J8ZmL6x8D5BNpXpSSb9uWMrGeDP9xA9xhruBVMddjkahDM879n"); } catch (EosApiException ex) { System.err.println(ex.getMessage()); } // ? sign the transaction SignedPackedTransaction signedPackedTransaction = client.signTransaction(packedTransaction, // Arrays.asList("EOS8U5q3Ri1yhRZQ8Kyyoie8L5KQGhSQmPZ7bhWmqQG2qKyRoJ6jC"), // "038f4b0fc8ff18a4f0842a8f0564611f6e96e8535901dd45e43ac8691a1c4dca"); System.out.println("signedPackedTransaction=" + mapper.writeValueAsString(signedPackedTransaction)); System.out.println("\n--------------------------------\n"); // ? push the signed transaction PushedTransaction pushedTransaction = client.pushTransaction("none", signedPackedTransaction); System.out.println("pushedTransaction=" + mapper.writeValueAsString(pushedTransaction)); System.out.println(pushedTransaction.getTransactionId()); } public static void main() throws Exception { BasicConfigurator.configure(); EosApi client = EosApiFactory.create("http://127.0.0.1:3000", "http://jungle.cryptolions.io:38888", "http://jungle.cryptolions.io:38888"); transfer(client); } }
in method
TransferArg(from, "shubhamgwa24", "0.9999 EOS", "hello");
we are passing sender wallet name , reciever wallet name, number of coin that we are sending, and memo tag (it could me String ).
above send method send coin from wallet that you have created on your local server that's available on your machine location you can see this on localtion like
rahbar@rahbar:/opt/Jungle2Testnet/Wallet$
after successfully send you will get transaction id .
System.out.println(pushedTransaction.getTransactionId());
If you have any query regarding above statement then you can simple put a comment in comment section.
Source -> https://github.com/adyliu/jeos
Thanks for read this
Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.
About Author
Rahbar Ali
Rahbar Ali is bright Java Developer and keen to learn new skills.