Created
April 27, 2018 21:47
-
-
Save usmanbashir/6bc83d16d2ec70f3ad8fda19ae93057c to your computer and use it in GitHub Desktop.
How To Write A Smart Contract - Blockchain Day @ Wadi Makkah
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
}); | |
}); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
}, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
initWeb3: function() { | |
// Is there an injected web3 instance? | |
if (typeof web3 !== 'undefined') { | |
App.web3Provider = web3.currentProvider; | |
} else { | |
// If no injected web3 instance is detected, fall back to Ganache | |
App.web3Provider = new Web3.providers.HttpProvider('http://localhost:7545'); | |
} | |
web3 = new Web3(App.web3Provider); | |
return App.initContract(); | |
}, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
}); | |
}, |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment