Step1: Develop Contract

The contract has two interfaces, one for placing a bet and the other for drawing a lottery

1) Placing a bet
a. When a user places a bet, the interface is called for inputting the information about the user’s bet and the round of the bet. 
b. After the contract interprets the information about the bet, the related amount is transferred from the user’s account to the contract account.

2. Drawing a lottery 
a. When a lottery is drawn, the dealer inputs the round of the lottery and the information of bets from all the players. 
b. The random function is called to generate three random numbers with three dices and the outcomes are computed. 
c. Based on the bets from the players, a lottery is drawn for each player and their income is calculated. The amount is transferred from the dealer’s account to the players’.

Step2:Client Connection To BCX SDK

Log in: call passwordLogin interface for login

Placing a bet and drawing a lottery

a. call callContractFunction interface and then the functions for placing a bet and drawing a lottery respectively 

b. the outcomes shall be acquired by analyzing the message returned by the interface

Example of Testing

test.js contains the following information

Initialize bcx example

Call passwordLogin interface for login

Call callContractFunction interface to call contract interface

/**
 * Copyright (c) 2018 Xiamen Yaji Software Co.Ltd. All rights reserved.
 * Created by lizhiyi on 2018/10/25.
 */


var constants = require('./../shared/constants');
var _ = require('lodash');
var bclLibs = require('./../libs/bcx.min');

var CONTRACT_NAME = 'contract.tychem';

module.exports = function (app) {
    return new ModuleService(app);
};

var ModuleService = function (app) {
    this.app = app;
    this.isLoginBcl = false;
};

var moduleService = ModuleService.prototype;

moduleService.init = function () {
    var _configParams={
        api_node:{
            url:"ws://39.106.139.132:8010",
            name:"xxxxxxxx"
        },
        networks:[
        {
            core_asset:"COCOS",
            chain_id:"52e65ef663454f910ba3fe5f0b97a359f6a15aa50a329ae8de4d2b38eb0ee7a1"
        }],
        faucet_url:"http://39.106.139.132:4000",
        auto_reconnect:true,
        worker:false
        //app_keys:["5HxzZncKDjx7NEaEv989Huh7yYY7RukcJLKBDQztXAmZYCHWPgd"]
    };

    var _this = this;
    this.bcl = new BCX(_configParams);
    this.bcl.init(function (res) {
        console.log('init finish:', res);
        _this.login();
    });
};

moduleService.login = function () {
    var _this = this;
    this.bcl.passwordLogin({account: 'xxxxx', password:'xxxxxxxx',  callback: function (result) {
        _this.account = null;
        if (result.code === 1) {
            _this.isLoginBcl = true;
            _this.account = result.data.account_name;
            _this.userId = result.data.account_id;
        }
        console.log('login result', result);
    }});
};

moduleService.dice = function (roomId, roundId, objBetInfo, callback) {
    this.bcl.callContractFunction({
        nameOrId: CONTRACT_NAME,
        functionName: 'dice',//["1",1000001,'COCOS']
        valueList:[roomId + '@' + roundId, JSON.stringify(objBetInfo)],////
        callback:function(res){
            console.info("dice res",res);

            if (res.code === 1) {
                //Interpret results and directly return the corresponding numbers
                var arrAffect = res.data[0].contract_affecteds;
                console.log(arrAffect);
                for (var idx = 0; idx < arrAffect.length; idx++) {
                    if (arrAffect[idx].type === "contract_affecteds_log") {
                        var text = arrAffect[idx].raw_data.message;
                        var key = "##result##:";
                        var idxFind = text.indexOf(key);
                        if (idxFind !== -1) {
                            var jsonStr = text.slice(idxFind + key.length);
                            var result = JSON.parse(jsonStr);

                            result.trx_id = res.trx_data.trx_id;

                            if (callback) {
                                callback(null, result);
                                return;
                            }
                            break;
                        }
                    }
                }

                if (callback) {
                    callback('result formate was error!', res);
                }
            } else if (callback) {
                callback(res.message, res);
            }
        }
    });
};

/**
 * Query the information about the bets from players according to transaction ID
 * @param {String} TXID transaction ID
 * @param {Function} callback
 */
moduleService.queryBetInfo = function (TXID, callback) {
    this.bcl.queryTransaction({
        transactionId: TXID,
        callback:function (res) {
            if (res.code !== 1) {
                callback(res.message, res);
                return;
            }

            if (!res.data || !res.data.parse_ops || !res.data.parse_ops[0]) {
                callback('result formate was error!', res);
            }

            var parse =  res.data.parse_ops[0];
            var arrAffect = parse.result.contract_affecteds;

            for (var idx = 0; idx < arrAffect.length; idx++) {
                if (arrAffect[idx].type === "contract_affecteds_log") {
                    var text = arrAffect[idx].parse_operations_text;
                    var key = "##result##:";
                    var idxFind = text.indexOf(key);

                    if (idxFind !== -1) {
                        var jsonStr = text.slice(idxFind + key.length);
                        var result = JSON.parse(jsonStr);

                        if (callback) {
                            callback(null, result);
                            return;
                        }
                        break;
                    }
                }
            }

            if (callback) {
                callback('result formate was error!', res);
            }
        },
    });
};

Dicing


Suggested Edits are limited on API Reference Pages

You can only suggest edits to Markdown body content, but not to the API spec.