9.1 Interfacing Traditional Games into Cocos-BCX Chain System

  1. Import the relevant js files in order
    a. 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>

b. import local file
Download the above files and place them into the local folder, importing them one by one in the local import way.

<script src="bcx.min.js"></script>
<script src="core.min.js"></script>
<script src="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.)

if (window.BcxWeb) {
         // The injected object exists, covering the Sdk interface object.
          bcx = window.BcxWeb
          })
          return
        }
  // The injection object window.BcxWeb does not exist, the timer is started to perform timing detection.
  let timer = null
        clearInterval(timer)
        timer = setInterval(() => {
          if (window.BcxWeb) {
              // Cover the Sdk interface object and return
               bcx = window.BcxWeb       
                return
              } 
            })
            clearInterval(timer)
          }
        }, 1000)

 Cocosjs.plugins(new CocosBCX())
        await Cocosjs.cocos.connect('My-App').then(connected => {
          if (!connected) {
            return
          }
          //Connecting successfully and clearing timer
          clearInterval(timer)
          const cocos = Cocosjs.cocos
          //Overwrite the write SDK interface object,
          bcx = cocos.cocosBcx(bcx)
        })

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.