我是靠谱客的博主 爱撒娇板凳,最近开发中收集的这篇文章主要介绍用everitoken新建FungibleToken到转移FungibleToken的例子,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

首先上代码:

 public static void main(String[] args) throws ApiResponseException {
// Init test net
PrivateKey privateKey = PrivateKey.randomPrivateKey();
PublicKey publicKey = privateKey.toPublicKey();
NetParams netParams = new TestNetNetParams();
// get node info for building the transaction
NodeInfo nodeInfo = (new Info()).request(RequestParams.of(netParams));
String FTName = getRandomString(5);
int id=129;
NewFungibleAction newFungibleAction=NewFungibleAction.of(Symbol.of(id,5),FTName,
publicKey.toString(),
JSONObject.parseObject("{"name":"issue","threshold":1,"authorizers":[{"ref":"[A]"+" "+publicKey.toString()+"",""+"weight":"
+"1}]}"),
JSONObject.parseObject("{"name":"manage","threshold":1,"authorizers":[{"ref":"[A]"+" "+publicKey.toString()+"",""+"weight":"
+"1}]}"),
"10000.00000 S#"+id
);
KeyProvider provider = KeyProvider.of(privateKey.toWif());
try{
TransactionService transactionService1 = TransactionService.of(netParams);
TransactionConfiguration trxConfiguration = TransactionConfiguration.of(nodeInfo,1000000,publicKey);
TransactionData NewFT =transactionService1.push(trxConfiguration,Arrays.asList(newFungibleAction),false,provider);
System.out.println(NewFT.getTrxId());
}catch (ApiResponseException ex){
System.out.println(ex.getRaw());
}
//issue this Fungible
IssueFungibleAction issueFungibleAction = IssueFungibleAction.of("100.00000 S#"+id,
publicKey.toString(), "test from java");
try {
TransactionService transactionService = TransactionService.of(netParams);
TransactionConfiguration trxConfig = TransactionConfiguration.of(nodeInfo, 1000000,
PublicKey.of(publicKey.toString()));
TransactionData txData = transactionService.push(trxConfig, Arrays.asList(issueFungibleAction), false,
provider);
System.out.println(txData.getTrxId());
} catch (ApiResponseException ex) {
System.out.println(ex.getRaw());
}
//transfer the FungibleToken
TransferFungibleAction transferFungibleAction = TransferFungibleAction.of("1.00000 S#"+id,
publicKey.toString(),
"EVT8aNw4NTvjBL1XR6hgy4zcA9jzh1JLjMuAw85mSbW68vYzw2f9H", "test java");
// Init transaction service with net parameters
TransactionService transactionService = TransactionService.of(netParams);
KeyProvider keyProvider = KeyProvider.of(privateKey.toWif());
try {
// Construct transaction configuration
TransactionConfiguration trxConfig1 = TransactionConfiguration.of(nodeInfo, 100000, PublicKey.of(publicKey.toString()), false, null);
TransactionData push = transactionService.push(trxConfig1, Arrays.asList(transferFungibleAction), false,
KeyProvider.of(privateKey.toWif()));
System.out.println(JSON.toJSONString(push));
}catch (ApiResponseException ex){
System.out.println(nodeInfo);
}
}
public static String getRandomString(int length){
String str="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
Random random=new Random();
StringBuffer sb=new StringBuffer();
for(int i=0;i<length;i++){
int number=random.nextInt(62);
sb.append(str.charAt(number));
}
return sb.toString();
}

然后便是解释一下这个代码:

首先开始的

这里是新建一个私钥并把私钥转换成公钥的操作,相当于建立了一个可以新建Token的账户

这里是网络方面的配置,更加直白一点的说是选择一个节点

everitoken有十多个节点分布在世界各地,其中包含一个测试节点,像这里我们选择的就是测试节点(不要钱)

另外这里就是获取这个节点的信息,几乎所有的动作都需要节点的信息

接下来就是新建FungibleToken了()以下简称FT

 

NewFungibleAction newFungibleAction=NewFungibleAction.of(Symbol.of(id,5),FTName,
publicKey.toString(),
JSONObject.parseObject("{"name":"issue","threshold":1,"authorizers":[{"ref":"[A]"+" "+publicKey.toString()+"",""+"weight":"
+"1}]}"),
JSONObject.parseObject("{"name":"manage","threshold":1,"authorizers":[{"ref":"[A]"+" "+publicKey.toString()+"",""+"weight":"
+"1}]}"),
"10000.00000 S#"+id
);

这里就是构造一个Action这个Action就是新建一个FT,配置一个新建FT的动作,里面的格式需要正确,可以发现这里格式有点难看清楚我把格式单独拿出来一下:

{"name":"Action","threshold":num(int),"authorizers":[{"ref":"[A] publicKey","weight":num(int)}]}

其中Action可以是issue或者manage

配置好这个FT和动作之后我们就需要把他发送到everitoken那里了


KeyProvider provider = KeyProvider.of(privateKey.toWif());
try{
TransactionService transactionService1 = TransactionService.of(netParams);
TransactionConfiguration trxConfiguration = TransactionConfiguration.of(nodeInfo,1000000,publicKey);
TransactionData NewFT =transactionService1.push(trxConfiguration,Arrays.asList(newFungibleAction),false,provider);
System.out.println(NewFT.getTrxId());
}catch (ApiResponseException ex){
System.out.println(ex.getRaw());
}

首先KeyProvider是新建一个私钥提供者,因为在everitoken上每个Action都需要签名

然后便是建立一个TransactionService类的对象,这个对象负责之后的push操作,即把newFungibleAction给push到everitoken上

之后配置trx

然后push

一般按照这个步骤就可以完成一个Action和everitoken的互动

到这里就已经完成了新建FT了

 

接着如果要交易还需要issue该FT才能进行交易,我们这里issue到刚刚创建的那个人的地址

issue操作大同小异:

//issue this Fungible
IssueFungibleAction issueFungibleAction = IssueFungibleAction.of("100.00000 S#"+id,
publicKey.toString(), "test from java");
try {
TransactionService transactionService = TransactionService.of(netParams);
TransactionConfiguration trxConfig = TransactionConfiguration.of(nodeInfo, 1000000,
PublicKey.of(publicKey.toString()));
TransactionData txData = transactionService.push(trxConfig, Arrays.asList(issueFungibleAction), false,
provider);
System.out.println(txData.getTrxId());
} catch (ApiResponseException ex) {
System.out.println(ex.getRaw());
}

首先创建一个Action

然后按照刚刚的步骤

Service->Configuration->push

的方式完成该动作

这里要提的一个是100.00000

这里的小数点后面的“0”的个数就是我们创建FT时候Symbol里面的precesion(精度)的值否则会报错

然后就是交易了

TransferFungibleAction transferFungibleAction = TransferFungibleAction.of("1.00000 S#"+id,
publicKey.toString(),
"EVT8aNw4NTvjBL1XR6hgy4zcA9jzh1JLjMuAw85mSbW68vYzw2f9H", "test java");
TransactionService transactionService = TransactionService.of(netParams);
KeyProvider keyProvider = KeyProvider.of(privateKey.toWif());
try {
TransactionConfiguration trxConfig1 = TransactionConfiguration.of(nodeInfo, 100000,
PublicKey.of(publicKey.toString()), false, null);
TransactionData push = transactionService.push(trxConfig1, Arrays.asList(transferFungibleAction), false,
KeyProvider.of(privateKey.toWif()));
System.out.println(JSON.toJSONString(push));
}catch (ApiResponseException ex){
System.out.println(nodeInfo);
}

这里那一串数字是Demo里面提供的交易的对象我不知道卖给谁就直接用Demo里面提供的对象了

可以发现还是老套路:

新建一个Action

然后KeyProvider

Service->Configuration->push

 

 

最后

以上就是爱撒娇板凳为你收集整理的用everitoken新建FungibleToken到转移FungibleToken的例子的全部内容,希望文章能够帮你解决用everitoken新建FungibleToken到转移FungibleToken的例子所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(36)

评论列表共有 0 条评论

立即
投稿
返回
顶部