区块链3 pet-shop

mkdir pet-shop-zw
truffle init 
// 如果 truffle test 报 VM Exception,执行下一步也可以解决问题
// migrations/2_test_migration.js
var Adoption = artifacts.require("./Adoption.sol");

module.exports = function(deployer) {
  deployer.deploy(Adoption);
};

npm init npm install -g lite-server

# bs-config.json
{
    "server": {
        "baseDir": ["./src", "./build/contracts"]
    }
}
// truffle.js
module.exports = {
  networks: {
    development: {
      host: "127.0.0.1",
      port: 8545,
      network_id: "*" // Match any network id
    }
  }
};

打开ganache-cli

truffle migrate // 在build/contracts/Adoption.js 最后networks部分就有数据了


```javascript
# package.json 中,修改"scripts" 部分,新增"dev" 项
  "scripts": {
    "dev": "lite-server",
    "test": "echo \"Error: no test specified\" && exit 1"
  },
App = {
    web3Provider: null,
    contracts: {},
  
    init: function() {
      // Load pets.
      $.getJSON('../pets.json', function(data) {
        var petsRow = $('#petsRow');
        var petTemplate = $('#petTemplate');
  
        for (i = 0; i < data.length; i ++) {
          petTemplate.find('.panel-title').text(data[i].name);
          petTemplate.find('img').attr('src', data[i].picture);
          petTemplate.find('.pet-breed').text(data[i].breed);
          petTemplate.find('.pet-age').text(data[i].age);
          petTemplate.find('.pet-location').text(data[i].location);
          petTemplate.find('.btn-adopt').attr('data-id', data[i].id);
  
          petsRow.append(petTemplate.html());
        }
      });
  
      return App.initWeb3();
    },
  
    initWeb3: function() {
        
    if (typeof web3 !== 'undefined') {
        App.web3Provider = web3.currentProvider;
      } else {
        App.web3Provider = new Web3.prviders.HttpProvider("http://127.0.0.1:7545");
      }
      web3 = new Web3(App.web3Provider);
 
      return App.initContract();
    },
  
    initContract: function() {
        $.getJSON('Adoption.json', function(data) {
            // Get the necessary contract artifact file and instantiate it with truffle-contract
            var AdoptionArtifact = data;
            App.contracts.Adoption = TruffleContract(AdoptionArtifact);
          
            // Set the provider for our contract
            App.contracts.Adoption.setProvider(App.web3Provider);
          
            // Use our contract to retrieve and mark the adopted pets
            return App.markAdopted();
          });
  
      return App.bindEvents();
    },
  
    bindEvents: function() {
      $(document).on('click', '.btn-adopt', App.handleAdopt);
    },
  
    markAdopted: function(adopters, account) {
        var adoptionInstance;
        App.contracts.Adoption.deployed().then(function(instance) {
          adoptionInstance = instance;
          return adoptionInstance.getAdopters.call();
        }).then(function(adopters) {
          for (i = 0; i < adopters.length; i++) {
            if (adopters[i] !== '0x0000000000000000000000000000000000000000') {
              $('.panel-pet').eq(i).find('button').text('Success').attr('disabled', true);
            }
          }
        }).catch(function(err) {
          console.log(err.message);
        });
    },
  
    handleAdopt: function(event) {
      event.preventDefault();
  
      var petId = parseInt($(event.target).data('id'));
  
      var adoptionInstance;
      web3.eth.getAccounts(function(error, accounts) {
        if (error) {
          console.log(error);
        }
      
        var account = accounts[0];
        App.contracts.Adoption.deployed().then(function(instance) {
          adoptionInstance = instance;
      
          // Execute adopt as a transaction by sending account
          return adoptionInstance.adopt(petId, {from: account});
        }).then(function(result) {
          return App.markAdopted();
        }).catch(function(err) {
          console.log(err.message);
        });
      });
    }
  
  };
  
  $(function() {
    $(window).load(function() {
      App.init();
    });
  });
  

转载于:https://my.oschina.net/corleone/blog/1823413

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值