1. 程式人生 > >EOS JAVA 呼叫(離線簽名)

EOS JAVA 呼叫(離線簽名)

前言:  之前一篇文章是用的線上簽名,本地啟用了keosd服務,來管理使用者金鑰和資料簽名.

本篇文章,直接離線簽名,無需自己再啟動eos的相關服務.

使用者充值:需要告知使用者userId,使用者充值時備註上userId,然後後臺查詢賬戶交易記錄.判斷memo是否為使用者ID.是則代表使用者充值.

1,下載eos4j.jar包,加入專案中

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,建立一個定時任務,掃描使用者交易記錄,並記錄每次處理到使用者第幾條交易記錄.