NFT數字藏品質押借貸挖礦dapp系統開發智慧合約詳情

丸子qy發表於2023-05-10

  

  目前,NFT(Non-Fungible Tokens)最為主流有三種合約:ERC-721、ERC-1155和ERC-998。

  

  在NFT的最初期,大家嚴格遵守NFT的定義規範,也就是ERC-721規範,早年非常火熱的加密貓系列就是基於該規範開發的。從 ERC-721 協議標準來看,每一個基於ERC-721建立的NFT都是獨一無二、不可複製的。使用者可以在智慧合約中編寫一段程式碼來建立自己的NFT,該段程式碼遵循一個比較基礎的通用模版格式,可透過該程式碼新增關於NFT的所有者名稱、後設資料或安全檔案連結等細節。現在我們來開始編寫第一個智慧合約程式碼,solidity程式碼如下:

  

  p`js

  

  ragma solidity ^0.4.2;

  

  contract hello {

  

  string greeting;

  

  function hello(string _greeting) public {

  

  greeting = _greeting;

  

  }

  

  系搭建唯:wwqqyy420

  

  function say() constant public returns (string) {

  

  return greeting;

  

  }

  

  }

  

  在編寫完成程式碼,IDE進行編譯時會出現錯誤,注意要選擇正確的編譯器版本,如圖:

  

  簡單解釋下,我們定義了一個名為hello的合約,在合約初始化時儲存了一個字串(我們會傳入hello world),每次呼叫say返回字串。

  

  把這段程式碼寫(複製)到Browser-Solidity,如果沒有錯誤,點選Details獲取部署程式碼,如:

  

  部署合約

  

  Browser-Solidity生成的程式碼,複製到編輯器裡修改後的程式碼如下:

  

  var _greeting = "Hello World";

  

  var helloContract = web3.eth.contract([{"constant":true,"inputs":[],"name":"say","outputs":[{"name":"","type":"string"}],"payable":false,"type":"function","stateMutability":"view"},{"inputs":[{"name":"_greeting","type":"string"}],"type":"constructor","payable":true,"stateMutability":"payable"}]);

  

  var hello = helloContract.new(

  

  _greeting,

  

  {

  

  from: web3.eth.accounts[2],

  

  data: '0x6060604052604051610250380380610250833981016040528080518201919060200150505b8060006000509080519060200190828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f10607157805160ff1916838001178555609f565b82800160010185558215609f579182015b82811115609e5782518260005055916020019190600101906082565b5b50905060c6919060aa565b8082111560c2576000818150600090555060010160aa565b5090565b50505b50610178806100d86000396000f360606040526000357c010000000000000000000000000000000000000000000000000000000090048063954ab4b21461003c57610037565b610002565b346100025761004e60048050506100bc565b60405180806020018281038252838181518152602001915080519060200190808383829060006004602084601f0104600302600f01f150905090810190601f1680156100ae5780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b602060405190810160405280600081526020015060006000508054600181600116156101000203166002900480601f0160208091040260200160405190810160405280929190818152602001828054600181600116156101000203166002900480156101695780601f1061013e57610100808354040283529160200191610169565b820191906000526020600020905b81548152906001019060200180831161014c57829003601f168201915b50505050509050610175565b9056',

  

  gas: '4700000'

  

  }, function (e, contract){

  

  console.log(e, contract);

  

  if (typeof contract.address !== 'undefined') {

  

  console.log('Contract mined! address: ' + contract.address + ' transactionHash: ' + contract.


來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/70029396/viewspace-2951277/,如需轉載,請註明出處,否則將追究法律責任。

相關文章