1. 程式人生 > 其它 >MIMIC 以太坊醫療專案開發(6)Web3智慧合約API

MIMIC 以太坊醫療專案開發(6)Web3智慧合約API

技術標籤:MIMIC以太坊醫療專案

web3.js是一個庫集合,你可以使用HTTP或IPC連線本地或遠端以太它節點進行互動。 它具備如下性質:
•通過JSON-RPC與Ethereum客戶端進行互動
•支援所有的JSON-RPC方法型別
•支援所有Geth和Parity方法,用於管理賬戶和簽署交易
•同步或非同步的傳送客戶端請求
•可從Solidity ABI檔案自動生成智慧合約功能包

1. H5前端

在瀏覽器安裝了MetaMask外掛後

1.1 得到智慧合約的物件

if (window.ethereum) {
      this.web3 = new Web3(window.ethereum)
; window.ethereum.enable(); this.account = window.ethereum.selectedAddress; alert("RC loaded"); alert(this.account); if (this.account != "0x3da8b61d0133cad0c736a2cd3b9d05c8312e6a19") { alert("Metamask account incorrect.") }
// load registrar contract // RCabi 是智慧合約的字串程式碼 // 0xa98E67c2Bc21023a58a58d1ae9f053f77D15bADc是建立智慧合約的賬號地址 this.contractInstance = web3.eth.contract(JSON.parse(RCabi)). at("0xa98E67c2Bc21023a58a58d1ae9f053f77D15bADc");

1.2 呼叫智慧合約的函式

this.contractInstance.registerUser(this.username, this.ethAddr, this.description, 
this.profileType,
{from: this.account}, (err, result)=> {

2. truffle專案中呼叫智慧合約

 //得到智慧合約物件
 const Registrar = artifacts.require("Registrar");
let RC = await Registrar.deployed();
let accounts = await web3.eth.getAccounts();
let auth = accounts[0];

 //呼叫智慧合約的函式
await RC.registerUser(patient_id, patients[counter], "", 0, {from: auth});

【引用】

[1]: TRUFFLE AND METAMASK