All Downloads are FREE. Search and download functionalities are using the official Maven repository.

vite.ViteSubscribeRpc Maven / Gradle / Ivy

The newest version!
package vite;


import com.alibaba.fastjson.JSONObject;
import org.apache.commons.codec.binary.Base64;
import org.java_websocket.WebSocket;
import vite.api.service.RpcViteService;
import vite.api.service.ViteWebSocket;
import vite.api.service.ViteWebSocketDemo;
import vite.api.utils.Generator;
import vite.api.vo.RequestJson;
import vite.api.vo.SnapshotBlockSubscriptions;

import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.List;

public class ViteSubscribeRpc {

    private RpcViteService rpcViteService;

    public ViteSubscribeRpc(String baseUrl) {
        rpcViteService = Generator.createService(RpcViteService.class, baseUrl);
    }


    public String subscribeSnapshotBlock() {

        RequestJson requestJson = new RequestJson();
        requestJson.setJsonrpc("2.0");
        requestJson.setMethod("subscribe_newSnapshotBlocksFilter");
        JSONObject jsonObject = Generator.executeSync(rpcViteService.postRequest(requestJson));

        if (jsonObject.getString("result") != null) {
            return jsonObject.getString("result");
        }
        return null;

    }


    public SnapshotBlockSubscriptions getSnapshotBlockFilterChanges(String filterId) {

        RequestJson requestJson = new RequestJson();
        requestJson.setMethod("subscribe_getFilterChanges");
        List parems = new ArrayList<>();
        requestJson.setParams(parems);
        parems.add(filterId);
        JSONObject jsonObject = Generator.executeSync(rpcViteService.postRequest(requestJson));

        if (jsonObject.getJSONObject("result") != null) {
            return jsonObject.getJSONObject("result").toJavaObject(SnapshotBlockSubscriptions.class);
        }
        return null;
    }


    public static void main(String[] args) {
      /** ViteSubscribeRpc viteSubscribeRp = new ViteSubscribeRpc("http://148.70.30.139:48132");

         String filterId = viteSubscribeRp.subscribeSnapshotBlock();

         System.out.println(filterId);


         while (true) {
         SnapshotBlockSubscriptions subscriptions = viteSubscribeRp.getFilterChanges(filterId);

         System.out.println(JSONObject.toJSONString(subscriptions));
         try {
         Thread.sleep(5000L);
         } catch (Exception e) {


         }



         }**/


      String a= "pragma soliditypp ^0.4.2;\n" +
              "contract ViteBet{\n" +
              "    address owner;\n" +
              "\n" +
              "    struct BetLimit {\n" +
              "        uint256 lowerLimit;\n" +
              "        uint256 upperLimit;\n" +
              "        uint256 tipPer;\n" +
              "    }\n" +
              "\n" +
              "    tokenId[] tokens = [\"tti_5649544520544f4b454e6e40\"];\n" +
              "    mapping(tokenId => BetLimit) public tokenMap;\n" +
              "\n" +
              "    event win(address indexed addr, uint256 rollTarget, uint256 betAmount, uint64 rollNum, uint256 winAmount);\n" +
              "    event lose(address indexed addr, uint256 rollTarget, uint256 betAmount, uint64 rollNum);\n" +
              "    event suspendBet(address indexed addr, uint256 rollTarget, uint256 betAmount);\n" +
              "\n" +
              "    constructor() public {\n" +
              "        owner = msg.sender;\n" +
              "        tokenMap[\"tti_5649544520544f4b454e6e40\"].lowerLimit = 1 vite;\n" +
              "        tokenMap[\"tti_5649544520544f4b454e6e40\"].upperLimit = 100 vite;\n" +
              "        tokenMap[\"tti_5649544520544f4b454e6e40\"].tipPer = 5;\n" +
              "    }\n" +
              "\n" +
              "    onMessage () payable {\n" +
              "    }\n" +
              "\n" +
              "    // 配置token赌注上下限\n" +
              "    // 配置抽成比例(0到20)\n" +
              "    onMessage configBetLimit(uint256 ll, uint256 ul, uint256 tp) {\n" +
              "        require(owner == msg.sender);\n" +
              "        require(ll > 0 && ll <= ul);\n" +
              "        require(tp >= 0 && tp <= 20);\n" +
              "        if (tokenMap[msg.tokenid].lowerLimit == 0)\n" +
              "            tokens.push(msg.tokenid);\n" +
              "        tokenMap[msg.tokenid].lowerLimit = ll;\n" +
              "        tokenMap[msg.tokenid].upperLimit = ul;\n" +
              "        tokenMap[msg.tokenid].tipPer = tp;\n" +
              "    }\n" +
              "\n" +
              "    onMessage DrawMoney(uint256 amount) {\n" +
              "        require(owner == msg.sender);\n" +
              "        require(amount <= balance(msg.tokenid));\n" +
              "        msg.sender.transfer(msg.tokenid, amount);\n" +
              "    }\n" +
              "\n" +
              "    // 获取token的上下限和费率\n" +
              "    getter getBetLimit(tokenId token) returns(uint256 ll, uint256 ul, uint256 tipPer) {\n" +
              "        return (tokenMap[token].lowerLimit, tokenMap[token].upperLimit, tokenMap[token].tipPer);\n" +
              "    }\n" +
              "\n" +
              "    // 获取token的列表\n" +
              "    getter getTokenList() returns(tokenId[] memory) {\n" +
              "        return tokens;\n" +
              "    }\n" +
              "\n" +
              "    onMessage BetAndRoll(uint256 rollTargets) payable {\n" +
              "        uint256 betAmount = msg.amount;\n" +
              "        address betAddr = msg.sender;\n" +
              "        uint256 ll = tokenMap[msg.tokenid].lowerLimit;\n" +
              "        uint256 ul = tokenMap[msg.tokenid].upperLimit;\n" +
              "        require(ll > 0 && ll <= ul);\n" +
              "        require(betAmount >= ll && betAmount <= ul);\n" +
              "        require(rollTargets > 0 && rollTargets < 100000);\n" +
              "\n" +
              "        uint64 randomNumber = random64();\n" +
              "        uint64 rollNum = randomNumber % 6 + 1;\n" +
              "        bool winBet = false;\n" +
              "        uint64 count = 0;\n" +
              "        uint256 tempRollTargets = rollTargets;\n" +
              "        while(tempRollTargets % 10 > 0) {\n" +
              "            count++;\n" +
              "            uint64 rollTarget = uint64(tempRollTargets % 10);\n" +
              "            require(rollTarget > 0 && rollTarget <= 6);\n" +
              "            if(rollTarget == rollNum) {\n" +
              "                winBet = true;\n" +
              "            }\n" +
              "            tempRollTargets = tempRollTargets / 10;\n" +
              "        }\n" +
              "        uint256 winAmount = calcWinAmount(betAmount, count, msg.tokenid);\n" +
              "\n" +
              "        if(winBet == false) {\n" +
              "            // betAddr.transfer(msg.tokenId, 0);\n" +
              "            emit lose(betAddr, rollTargets, betAmount, rollNum);\n" +
              "        } else if(winBet == true && winAmount > balance(msg.tokenid)) {\n" +
              "            betAddr.transfer(msg.tokenid, betAmount);\n" +
              "            emit suspendBet(betAddr, rollTargets, betAmount);\n" +
              "        } else {\n" +
              "            betAddr.transfer(msg.tokenid, winAmount);\n" +
              "            emit win(betAddr, rollTargets, betAmount, rollNum, winAmount);\n" +
              "        }\n" +
              "\n" +
              "    }\n" +
              "\n" +
              "    function calcWinAmount(uint256 betAmount, uint256 length, tokenId token) public view returns(uint256) {\n" +
              "        uint256 bonus = betAmount * 6 / length;\n" +
              "        return betAmount + (bonus - betAmount) * (100 - tokenMap[token].tipPer) / 100;\n" +
              "    }\n" +
              "\n" +
              "}";

        Base64 base64 =new Base64();


    try {
         ViteWebSocket viteWebSocket =new ViteWebSocketDemo("ws://148.70.30.139:41423");
         viteWebSocket.connect();
         while (!viteWebSocket.getReadyState().equals(WebSocket.READYSTATE.OPEN)) {//获取连接状态
         System.out.println("还没有打开");
         }
         System.out.println("建立websocket连接");
         viteWebSocket.send("{\"jsonrpc\": \"2.0\",\"id\": 1,\"method\": \"subscribe_subscribe\",\"params\": [\"newSnapshotBlocks\"]}\n");



         } catch (URISyntaxException e) {
         e.printStackTrace();
         }

        ViteRpc viteRpc=new ViteRpc("http://148.70.30.139:48132");
        //viteRpc.getBlockByHeight()


    }
}