9.1 Interfacing Traditional Games into Cocos-BCX Chain System

  1. Import the relevant js files in order
    import in cdn way
// Import JSSDK files, links and signature toolkit 
 <script src="https://jdi.cocosbcx.net/static/js/bcx.min.js"></script>
 <script src="https://jdi.cocosbcx.net/static/js/core.min.js"></script>
 <script src="https://jdi.cocosbcx.net/static/js/plugin.min.js"></script>
  1. Find SDK objects
    a. Build a link and initialize the Sdk interface object that interacts with the chain
var _configParams = {
      ws_node_list: [{
        url: "ws://39.106.126.54:8049",
        name: "COCOS3.0节点2"
      }],
      networks: [{
        core_asset: "COCOS",
        chain_id: '7d89b84f22af0b150780a2b121aa6c715b19261c8b7fe0fda3a564574ed7d3e9'
      }],
      // faucet_url:"http://47.93.62.96:3000",
      faucetUrl: 'http://47.93.62.96:8041',
      auto_reconnect: true,
      worker: false,
      real_sub: true,
      check_cached_nodes_data: true,
      // app_keys: ["5HxzZncKDjx7NEaEv989Huh7yYY7RukcJLKBDQztXAmZYCHWPgd"]
    };
    bcx = new BCX(_configParams);

b. Detect whether the Sdk object injected by the mobile wallet (AndroidWallet, iOSWallet or Goole plugin wallet) exists, and if it exists, overwrite the Sdk interface object created in 1. (Mobile wallet and Goole plugin wallet will mount a BcxWeb object on the window by injection.)

try {
    if (window.BcxWeb) {
        bcx = window.BcxWeb
        bcx.getAccountInfo().then(res => {
            console.log("getAccountInfo---res", res);
        })
        return
    }
    let timer = null
    clearInterval(timer)
    timer = setInterval(() => {
        if (window.BcxWeb) {
            bcx = window.BcxWeb
            bcx.getAccountInfo().then(res => {
                if (res.locked) {
                    Message({
                        duration: 1200,
                        message: 'Account Locked',
                        type: 'error',
                    })
                    return
                }
            })
            clearInterval(timer)
        }
    }, 1000)

    Cocosjs.plugins(new CocosBCX())
    Cocosjs.cocos.connect('My-App').then(connected => {
        if (!connected) {
            return
        }
        clearInterval(timer)
        const cocos = Cocosjs.cocos
        bcx = cocos.cocosBcx(bcx)
        bcx.getAccountInfo().then(res => {
            console.log("getAccountInfo---res", res);
        })
    })
} catch (e) {
    console.log("error----",e);
}

After starting the detection, it will try to link your desktop wallet. (If the user installs both the Goole plugin and the desktop wallet:windows, mac, the wallet that first gets the bcx object will interact with the DApp)

  1. Call SDK Object Example
// Get Account Balance
bcx.queryAccountBalances({
  assetId: "COCOS",//Asset Tag
  account: 'test1' // Username
}).then(()=>{})

// Way to call contract
bcx.callContractFunction({
  nameOrId: "contract.dicegame", // contract
  functionName: "bet", // operation
  valueList: [rollUnder, cocos], //rollUnder and coin
  runtime: 10,
  onlyGetFee: false
}).then(()=>{})
  1. DApp Demo
    Demo

9.1 Interfacing Traditional Games into Cocos-BCX Chain System


Suggested Edits are limited on API Reference Pages

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