From b3f621ef60d38e69996f32d5ff57656dfb5c0ee1 Mon Sep 17 00:00:00 2001 From: usii-004 Date: Mon, 6 May 2024 16:49:39 +0100 Subject: [PATCH] added sample js to interact with the smart contract --- lesson-2/chapter-14/sample.js | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 lesson-2/chapter-14/sample.js diff --git a/lesson-2/chapter-14/sample.js b/lesson-2/chapter-14/sample.js new file mode 100644 index 0000000..132ad1b --- /dev/null +++ b/lesson-2/chapter-14/sample.js @@ -0,0 +1,32 @@ +var abi = x;/* abi generated by the compiler */ +var ZombieFeedingContract = web3.eth.contract(abi) +var contractAddress = x;/* our contract address on Ethereum after deploying */ +var ZombieFeeding = ZombieFeedingContract.at(contractAddress) + +// Assuming we have our zombie's ID and the kitty ID we want to attack +let zombieId = 1; +let kittyId = 1; + +// To get the CryptoKitty's image, we need to query their web API. This +// information isn't stored on the blockchain, just their webserver. +// If everything was stored on a blockchain, we wouldn't have to worry +// about the server going down, them changing their API, or the company +// blocking us from loading their assets if they don't like our zombie game ;) +let apiUrl = "https://api.cryptokitties.co/kitties/" + kittyId +$.get(apiUrl, function(data) { + let imgUrl = data.image_url + // do something to display the image +}) + +// When the user clicks on a kitty: +$(".kittyImage").click(function(e) { + // Call our contract's `feedOnKitty` method + ZombieFeeding.feedOnKitty(zombieId, kittyId) +}) + +// Listen for a NewZombie event from our contract so we can display it: +ZombieFactory.NewZombie(function(error, result) { + if (error) return + // This function will display the zombie, like in lesson 1: + generateZombie(result.zombieId, result.name, result.dna) +}) \ No newline at end of file