使用Java对易点国际交易所钱包客户端进行操作(三)

在经过前面几篇文章的介绍后,我们对易点国际交易所平台的钱包客户端已经可以有一些基本的操作了。那么,在这篇文章中就将最后介绍几个基础功能,分别是:查询钱包余额转入/转出查询交易记录

查询钱包余额

    /*
     * 取得钱包余额
     * {"result":9.5E-5,"error":null,"id":1}
     * {"result":0,"error":null,"id":1}
     * 若获取失败,result为空,error信息为错误信息的编码
      /
    public JSONObject getbalance() throws Exception {
        String s = main("getbalance", "[]");
        JSONObject json = JSONObject.fromObject(s); 
        return json;
    }

此方法为查询钱包总余额,而不是查询钱包客户端中某个钱包地址的余额。
因钱包客户端中的所有子地址接收到的币都会汇总到总钱包中,而子地址只能查询到交易记录。
如果想知道某个子地址接收或转出多少币,我们只需查询出子地址的接收或转出记录再进行统计便可得知。

测试

public static void main(String args[]) throws Exception{
        BTCMessage message = new BTCMessage();
        message.setIP("192.168.0.250");
        message.setPORT("18083");
        message.setACCESS_KEY("admin");
        message.setSECRET_KEY("12345678");
        BTCUtils utils = new BTCUtils(message);
        JSONObject object = utils.getbalance();
        System.out.println(object);
        Map<String, Object> map = JSONObject.fromObject(object);
        if(map.get("error").equals(null)) {
            System.out.println("["+"-1"+"]查询成功,当前可用余额:"+map.get("result"));
        }else {
            System.out.println("["+map.get("error")+"]查询失败");
        }
    }

成功示例:{"result":9,"error":null,"id":1}
失败示例:{"result":null,"error":401,"id":1}

转入/转出

    /*
     * 根据钱包地址,转入/转出LMT
     * Returns the transaction ID <txid> if successful.
     * {"result":"2278857ca4ab04b41b36fe0f5ec8415d8e72c66a4688c60d8d2eefe994d3042c","error":null,"id":1}
     * 若获取失败,result为空,error信息为错误信息的编码
     * {"result":null,"error":500,"id":1}
      /
    public JSONObject sendtoaddress(String address,double account,String comment) throws Exception {
        if(PASSWORD != null && PASSWORD.trim().length() >0){
            walletpassphrase(30);
        }
        String s = main("sendtoaddress", "[""+address+"","+account+","+"""+comment+""]");
        if(PASSWORD != null && PASSWORD.trim().length() >0){
            walletlock();
        }
        JSONObject json = JSONObject.fromObject(s); 
        return json;
    }

address:钱包地址,这里因为受交易所的限制,钱包客户端内的钱包地址不可进行互转。
account:交易数量
comment:交易备注,非必传

此方法可同时完成转入/转出需求。
举例:
现有某钱包客户端为A,有用户user1和用户user2
由user1转入一定数量的币到客户端A,则为转入
由客户端A转出一定数量的币到user2,则为转出
请根据实际情况使用此方法。

测试

public static void main(String args[]) throws Exception{
        BTCMessage message = new BTCMessage();
        message.setIP("192.168.0.250");
        message.setPORT("18083");
        message.setACCESS_KEY("admin");
        message.setSECRET_KEY("12345678");
        BTCUtils utils = new BTCUtils(message);
        JSONObject object = utils.sendtoaddress(cel.getpAddress(), er.doubleValue() , "");
        Map<String, Object> map = JSONObject.fromObject(object);
        if(!map.get("error").equals(null)) {
            throw new DataCheckException("["+map.get("error")+"]出现异常");
        }
    }

成功示例:{"result":"2278857ca4ab04b41b36fe0f5ec8415d8e72c66a4688c60d8d2eefe994d3042c","error":null,"id":1}
失败示例:{"result":null,"error":500,"id":1}

查询交易记录

    /**
     * 根据用户ID,获取交易记录
     *  {"result":[{"account":"test","address":"i6W5Ng4X49gJDAVnPafFm3rQwEAfU28SUo",
     *  "category":"receive","amount":10,"confirmations":166,
     *  "blockhash":"9b227a05cce30b33d991199af734cdd72171c6db608ec36aa71f48952ad1a639",
     *  "blockindex":1,"txid":"4ed4de64672cb86cc3458a12a8a1d1c4a79478627a536ec62033367ba180ffc9",
     *  "time":1391943409,"comment":"","from":"","message":"","to":""}],"error":null,"id":1}
        没有交易记录的结果:{"result":[],"error":null,"id":1}
        若获取失败,result为空,error信息为错误信息的编码
      /
    public JSONObject listtransactions(int userId) throws Exception {
        String s = main("listtransactions", "[""+userId+""]");
        JSONObject json = JSONObject.fromObject(s); 
        return json;
    }

userId:用户ID,对应钱包客户端中接收里拉令牌中的标签字段

测试 - 查询所有交易记录

public static void main(String args[]) throws Exception{
        BTCMessage message = new BTCMessage();
        message.setIP("192.168.0.250");
        message.setPORT("18083");
        message.setACCESS_KEY("admin");
        message.setSECRET_KEY("12345678");
        BTCUtils utils = new BTCUtils(message);
        JSONObject object = utils.listtransactions(615311959);
    }

成功示例:{"result:[{"account":"615311959","address":"LYUUVn2jvJD63MU77f5L5ykQZCo6cnY7Ws","category":"receive","amount":1,
"confirmations":87209,"bcconfirmations":87209,"blockhash":"000000005f02f9670b77fecf4299ea2d1f09d1871b91
349a32bb5ba7fb06fcb8","blockindex":1,"blocktime":1542615400,"txid":"9b1ff6dfd02a88dd874e812971703e827c
6f60dd547872c612f4196d49bcc984","walletconflicts:[],"time":1542615347,"timereceived":1542615347}],"error":null,"id":1}
失败示例:{"result":[],"error":null,"id":1}

测试 - 查询所有接收的交易记录

public static void main(String args[]) throws Exception{
        BTCMessage message = new BTCMessage();
        message.setIP("192.168.0.250");
        message.setPORT("18083");
        message.setACCESS_KEY("admin");
        message.setSECRET_KEY("12345678");
        BTCUtils utils = new BTCUtils(message);
        JSONObject object = utils.listtransactions(615311959);
        List allResult = (List) object.get("result");
        for (Object object2 : allResult) {
            String json = JsonUtils.objectToJson(object2);
            System.out.println(JsonUtils.toMap(json, null));
            Map<String, Object> map = JsonUtils.toMap(json, null);
        }
    }

在区分交易类型时使用记录中的category字段,此字段有两个值,分别为:receive接收send发送
我们在查询某用户的转出记录或转入记录时,即可使用此字段进行查询统计

成功示例:{"result:[{"account":"615311959","address":"LYUUVn2jvJD63MU77f5L5ykQZCo6cnY7Ws","category":"receive","amount":1,
"confirmations":87209,"bcconfirmations":87209,"blockhash":"000000005f02f9670b77fecf4299ea2d1f09d1871b91
349a32bb5ba7fb06fcb8","blockindex":1,"blocktime":1542615400,"txid":"9b1ff6dfd02a88dd874e812971703e827c
6f60dd547872c612f4196d49bcc984","walletconflicts:[],"time":1542615347,"timereceived":1542615347}],"error":null,"id":1}
失败示例:{"result":[],"error":null,"id":1}

总结

至此,从易点国际交易所的安装配置到使用Java代码对钱包客户端进行基本操作已经全部完成,如有疑问可查阅前几篇文章或联系博主进行解答。在这个系列中所用到的代码已全部上传GitHub,其中还包括一些在文章中所没有介绍到的方法,有兴趣可前往GitHub下载使用,如有不足之处欢迎各位指出并一起探讨。
GitHub地址:https://github.com/J1AngRui/BTCUtils

Last modification:January 15th, 2019 at 04:38 pm
If you think my article is useful to you, please feel free to appreciate

Leave a Comment