EOS JAVA 调用(离线签名)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/liu1765686161/article/details/83308819

前言:  之前一篇文章是用的在线签名,本地启用了keosd服务,来管理用户密钥和数据签名.

本篇文章,直接离线签名,无需自己再启动eos的相关服务.  (上一篇文章https://blog.csdn.net/liu1765686161/article/details/82180070)

用户充值:需要告知用户userId,用户充值时备注上userId,然后后台查询账户交易记录.判断memo是否为用户ID.是则代表用户充值.

1,下载eos4j.jar包(https://download.csdn.net/download/liu1765686161/10633193),加入项目中.  下载需要CSDN积分,没有的话可加QQ技术交流群629042605下载

2,直接调用

package com.tn.set.service.coin;
import io.eblock.eos4j.EosRpcService;
import io.eblock.eos4j.api.vo.action.Action;
import io.eblock.eos4j.api.vo.action.ActionTrace;
import io.eblock.eos4j.api.vo.action.Actions;
import io.eblock.eos4j.api.vo.transaction.Transaction;
import java.util.List;
import javax.annotation.PostConstruct;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.springframework.stereotype.Service;
import com.alibaba.fastjson.JSONObject;
import com.tn.base.Log;
import com.tn.util.BigDecimalUtil;
import com.tn.util.JsonUtil;

/**
 * eos操作
 * @author xhl
 * @create 2017/10/27
 **/
@Service
public class CopyOfCoinEosService {

    private Logger log = Log.get();
    
//      正式网络https://api-v2.eosasia.one
    private String chainUrl = "https://api-kylin.eosasia.one";//测试网络
    private String account = "eosqxyx22222";
//    private String activePrivateKey = "5JJB2oiCXwASK9fumjyTgbtHcwDVedVRaZda1kBhFuQcmjnrDWB";
    private String activePrivateKey = "5JbkSyGHeYRH2sa1aSh3Fg7ECdUQWpKM7mfgCi1HmyDK84RHHvT";
    
    private EosRpcService eosRpcService = null;
    private static final String EOS_TOKEN = "eosio.token";
    private static final String ACTION_TRANSFER = "transfer";

    @PostConstruct
    public void init(){
    	eosRpcService = new EosRpcService(chainUrl);
    }

	public static void main(String[] args) {
		CopyOfCoinEosService eosService = new CopyOfCoinEosService();
		eosService.eosRpcService = new EosRpcService(eosService.chainUrl);
		System.out.println(eosService.getBalance());
		eosService.send("eosqxyx11111", 10.00, "46");
	}
    
    public String getAddress(){
    	return account;
    }

    public double getBalance(){
    	List<String> list = eosRpcService.getCurrencyBalance(EOS_TOKEN, account, "EOS");
    	if (list != null && list.size() > 0) {
			return Double.parseDouble(list.get(0).replaceAll(" EOS", ""));
		}
    	return 0.00;
    }
    /**
     * 获取用户交易记录
     * @param index 获取到第几条记录
     * @return
     */
	public boolean getActions(int index){
		try {
			//最后一个参数代表每次获取几条记录,若是获取多条记录有可能会出现重复数据.
			Actions actions = eosRpcService.getActions(account, index,1);
			if (actions != null) {
				List<Action> list = actions.getActions();
				if (list==null || list.size() == 0) {
					return false;
				}
				for (Action action : list) {
					ActionTrace actionTrace = action.getActionTrace();
					String account = actionTrace.getAct().getAccount();
					if (!EOS_TOKEN.equals(account)) {
						log.info("非EOS交易记录:{}",account);
	                	return true;
					}
					String name = actionTrace.getAct().getName();
					if (!ACTION_TRANSFER.equals(name)) {
						log.info("非EOS转账交易记录:{}",account);
						return true;
					}
					//{from=eosqxyx11111, to=eosqxyx22222, quantity=10.0000 EOS, memo=test}
					log.info("交易详情:{}",actionTrace.getAct().getData().toString());
					JSONObject json = JSONObject.parseObject(JsonUtil.getSting(actionTrace.getAct().getData()));
					if (!account.equals(json.getString("to"))) {
						log.info("非充值记录:{}",actionTrace.getTrxId());
						return true;
					}
					String[] quantity = json.getString("quantity").split(" ");
					if (!"EOS".equals(quantity[1])) {
						log.info("非EOS充值记录:{}",json.getString("quantity"));
						return true;
					}
					String memo = json.getString("memo");
	                if (StringUtils.isEmpty(memo)) {
						log.info("记录TrxId:{}为空",actionTrace.getTrxId());
						return true;
					}
	                //判断用户是否存在
	                /*UserEntity user = userService.getUserById(Integer.parseInt(memo));
	                if (user == null) {
						log.info("用户信息不存在:memo:{}",memo);
						continue;
					}*/
	                //添加充值记录
	        		return true;
				}
			}
		} catch (Exception e1) {
			e1.printStackTrace();
			log.error("获取用户交易记录失败:{}",e1.getMessage());
		}
		return false;
	}
	/**
	 * 去重
	 * @param list
	 * @return
	 */
	public List<Action> removeDuplicate(List<Action> list) {
		for (int i = 0; i < list.size() - 1; i++) {
			for (int j = list.size() - 1; j > i; j--) {
				if (list.get(j).getActionTrace().getTrxId()
						.equals(list.get(i).getActionTrace().getTrxId())) {
					list.remove(j);
				}
			}
		}
		return list;
	}
	
	/**
     * 发送交易
     * @param toAccount
     * @param amount 保留4位小数点
     * @param memo
     * @return
     */
    public String send(String toAccount,double amount,String memo){
		try {
			String amt = BigDecimalUtil.getFourString(amount)+" EOS";
			System.out.println(amt);
//			Transaction t1 = rpc.transfer("5JJB2oiCXwASK9fumjyTgbtHcwDVedVRaZda1kBhFuQcmjnrDWB","eosio.token", "eosqxyx11111","eosqxyx22222", "10.0000 EOS", "te1");
			Transaction t1 = eosRpcService.transfer(activePrivateKey,EOS_TOKEN, account,toAccount, amt, memo);
	        if (t1 != null) {
	        	log.info("EOS转账成功:transactionId:{}",t1.getTransactionId());
	    		return t1.getTransactionId();
			}else {
				log.info("EOS转账失败");
			}
		} catch (Exception e) {
			e.printStackTrace();
			log.error("EOS转账失败:{}",e.getMessage());
		}
        return null;
    }

}

3,建立一个定时任务,扫描用户交易记录,并记录每次处理到用户第几条交易记录.

希望能帮到大家,欢迎大家一起分享。

觉得有用请打赏,你的鼓励就是我的动力!

有问题可以通过chat向我提问,共同进步

同时也可以加入我创建的技术交流群629042605

猜你喜欢

转载自blog.csdn.net/liu1765686161/article/details/83308819
eos