SaveText.Ru

dsadad
  1. if (window.ethereum) { // Modern dapp browsers
  2. App.web3Provider = window.ethereum;
  3. try {
  4. await window.ethereum.enable(); // Request account access
  5. } catch (error) { // User denied account access...
  6. console.error("User denied account access")
  7. }
  8. } else if (window.web3) { // Legacy dapp browsers
  9. App.web3Provider = window.web3.currentProvider;
  10. } else { // If no injected web3 instance is detected, fall back to Ganache
  11. App.web3Provider = new Web3.providers.HttpProvider('http://localhost:7545');
  12. }
  13. web3 = new Web3(App.web3Provider);
  14.  
  15. ----------------------------------------
  16.  
  17. $.getJSON('Adoption.json', function(data) {
  18. // Get the necessary contract artifact file and instantiate it with truffle-contract
  19. var AdoptionArtifact = data;
  20. App.contracts.Adoption = TruffleContract(AdoptionArtifact);
  21. // Set the provider for our contract
  22. App.contracts.Adoption.setProvider(App.web3Provider);
  23. // Use our contract to retrieve and mark the adopted pets
  24. return App.markAdopted();
  25. });
  26.  
  27. ---------------------------------------------
  28. var adoptionInstance;
  29. App.contracts.Adoption.deployed().then(function(instance) {
  30. adoptionInstance = instance;
  31. return adoptionInstance.getAdopters.call();
  32. }).then(function(adopters) {
  33. for (i = 0; i < adopters.length; i++) {
  34. if (adopters[i] !== '0x0000000000000000000000000000000000000000') {
  35. $('.panel-pet').eq(i).find('button').text('Success').attr('disabled', true);
  36. }
  37. }
  38. }).catch(function(err) {
  39. console.log(err.message);
  40. });
  41.  
  42. -----------------------------
  43. var adoptionInstance;
  44. web3.eth.getAccounts(function(error, accounts) {
  45. if (error) {
  46. console.log(error);
  47. }
  48. var account = accounts[0];
  49. App.contracts.Adoption.deployed().then(function(instance) {
  50. adoptionInstance = instance;
  51. // Execute adopt as a transaction by sending account
  52. return adoptionInstance.adopt(petId, {from: account});
  53. }).then(function(result) {
  54. return App.markAdopted();
  55. }).catch(function(err) {
  56. console.log(err.message);
  57. });
  58. });

Share with your friends:

Print