Eos是一个比较新的区块链项目,可以像以太坊一样建立智能合约,但它比以太坊快得多。希望在eos网络上创建智能合约的开发人员,在eos区块链的使用资源上拥有一些eos比如网络带宽,ram和cpu。这些资源帮助用户创建智能合约并将其分发给他的用户。
每个想要使用eos网络从他的帐户转移eos(或制作智能合约)到另一个用户的用户必须持有一定数量的eos网络带宽,RAM和CPU。这意味着在交易时没有从他的钱包中扣除任何费用代表这些资源,但是这些资源会停留一段时间(3天),这取决于交易大小及其执行时间。
在Eos平台上,用户可以轻松创建代币,如ethereum erc-20代币。
我们设置了来自 jungle-testnet区块链 的eos-blockchain用于测试目的。你可以简单地按照上面链接给出的设置步骤,安装完成后,你会得到两个网址:
现在,为了在java项目中实现这一点,我们将在spring-tool(sts)中创建一个简单的Maven项目。并在我们的pom.xml文件中添加这些依赖项。
<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>
你可以查看上面 这个网址 的依赖关系,是开源的。
要创建一个eos帐户,你需要一个现有的eos帐户,因为每个eos帐户都需要一些eos代币才能购买一些金额的ram,cpu和网络带宽。
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); } }
成功运行以上程序后,你将获得新创建的帐户的交易ID。在eos区块链上创建帐户名称的过程:
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); } }
在方法 TransferArg(from, "shubhamgwa24", "0.9999 EOS", "hello");
中,我们正在传递发件人钱包名称,收件人钱包名称,我们发送的代币数量,以及备忘录标签(可能是我的字符串)。
以上发送方法从你在本地服务器上创建的钱包发送代币,该代币在你的机器位置可用,你可以在本地查看类似:
rahbar@rahbar:/opt/Jungle2Testnet/Wallet$
成功发送后,你将获得交易ID。
System.out.println(pushedTransaction.getTransactionId());
如果你对上述陈述有任何疑问,那么你可以联系我们。感谢你阅读此内容。github源码在 这里 。
======================================================================
分享一些比特币、以太坊、EOS等区块链相关的交互式在线编程实战教程:
汇智网原创翻译,转载请标明出处。这里是 在Java项目中集成EOS代币