Web3js

如何從安卓手機呼叫智能合約的功能?

  • May 31, 2018

我通過 Javascript 和 HTML 部署了一個可靠的智能合約以及一個 GUI。這個項目在我的電腦上執行良好。

現在我想從安卓手機呼叫智能合約的功能。

有沒有可能做到這一點?如果是這樣,怎麼辦?

Ans 我還需要寫我的智能合約Java嗎?還是Solidity版本好?

我的solidity智能合約程式碼:

pragma solidity 0.4.23; 

contract RFID {

   struct StateStruct {
       bytes32 description;
       mapping(bytes32 => bytes32) sub_state;
   }

   struct ObjectStruct {
       StateStruct state;
       address owner; 
       bool isObject;

       bytes32 review;
   }

   mapping(bytes32 => ObjectStruct) objectStructs;
   bytes32[] public objectList;

   event LogNewObject(address sender, bytes32 indexed id, bytes32 sub_states_types, bytes32 sub_states_values, address owner);
   event LogChangeObjectState(address sender, bytes32 indexed id, bytes32 sub_states_types, bytes32 sub_states_values);
   event LogChangeObjectOwner(address sender, bytes32 indexed id, address newOwner);

   event LogNewObjectReview(address sender, bytes32 indexed _id, bytes32 _review,  address _owner);
   event LogChangeObjectStateReview(address sender, bytes32 indexed id, bytes32 _review);

   function isObject(bytes32 _id) public view returns(bool isIndeed) {
       return objectStructs[_id].isObject;
   }

   function getObjectCount() public view returns(uint count) {
       return objectList.length;
   }

   /*function setArraySize(uint256 _number_of_sub_states) public {

       number_of_sub_states = _number_of_sub_states;

   }

   function getArraySize() view public returns (uint256) {
      return number_of_sub_states;
   }*/

   function newObject(bytes32 _id, uint256 number_of_sub_states, bytes32[10] sub_states_types, bytes32[10] sub_states_values, address _owner) public returns(bool success) {
       require(!isObject(_id));

       uint256 counter=0;
       for(counter; counter < number_of_sub_states; counter++) {

           objectStructs[_id].state.sub_state[sub_states_types[counter]] = sub_states_values[counter];

           emit LogNewObject(msg.sender, _id, bytes32(sub_states_types[counter]), bytes32(sub_states_values[counter]), _owner);

       }

       objectStructs[_id].owner = _owner;
       objectStructs[_id].isObject = true;

       objectList.push(_id);

       return true;
   }

   function newObjectReview(bytes32 _id, bytes32 _review,  address _owner) public returns(bool success) {
       require(!isObject(_id));

       objectStructs[_id].owner = _owner;
       objectStructs[_id].isObject = true;

       objectStructs[_id].review = _review;

       emit LogNewObjectReview(msg.sender, _id, _review, _owner);

       objectList.push(_id);

       return true;
   }

   function changeObjectState(bytes32 _id, uint256 number_of_sub_states, bytes32[10] sub_states_types, bytes32[10] sub_states_values) public returns(bool success) {
       require(isObject(_id));
       uint256 counter=0;
       for(counter; counter < number_of_sub_states; counter++) {

           objectStructs[_id].state.sub_state[sub_states_types[counter]] = sub_states_values[counter];

           emit LogChangeObjectState(msg.sender, _id, bytes32(sub_states_types[counter]), bytes32(sub_states_values[counter]));

       }

       return true;
   }

   function changeObjectStateReview(bytes32 _id, bytes32 _review) public returns(bool success) {
       require(isObject(_id));

       objectStructs[_id].review = _review;

       emit LogChangeObjectStateReview(msg.sender, _id, _review);

       return true;
   }

   function changeObjectOwner(bytes32 _id, address _newOwner) public returns(bool success) {
       require(isObject(_id));
       objectStructs[_id].owner = _newOwner;
       emit LogChangeObjectOwner(msg.sender, _id, _newOwner);
       return true;
   }

}

我想做的是通過android手機呼叫我的契約功能。

**注意:**我確實註冊了Infura,但老實說我不知道如何在我的安卓手機上使用它來從我的安卓手機呼叫這個智能合約的功能?

為此,您需要兩件事:

  1. 英富拉
  2. Web3js

Infura 將允許您連接到乙太坊網路(並且是免費的)。然後您可以使用 web3 與您的合約進行互動。

希望能幫助到你。

如果您詢問如何從 Android 手機執行 DApp,您可以載入Toshi應用程序。它基本上是一個帶有內置錢包和背面的乙太坊節點的瀏覽器,它將執行你所有基於 web3.js 的網頁。

引用自:https://ethereum.stackexchange.com/questions/48264