3

I am following a tutorial.

contract

contract Auction {
  event AuctionClosed (uint highestBid);  //declare eventto be triggered when Auction closes
  address public creator;
  function Auction () { //Auction Constructor to register the creator of the contract
    creator = msg.sender;
  }
  function closeAuction (uint someRandomBid) {
    if (msg.sender == creator) {  //make sure that auction is being ended by the creator themselves
      AuctionClosed (someRandomBid);  //trigger the event to notify the listeneres that the auction has ended
      return;
    }
    throw;
  }
}

js

window.onload = function () {
  let accounts = web3.eth.accounts; //create local variable for easy access
  let maxBid = Math.ceil (Math.random () * 1000); //the maximum bid placed by the end of the auction
  let status = document.getElementById ('status');
  Auction.new ({from: accounts [0]}) //create new contract object
    .then ( (contract) => {
      contract.AuctionClosed ().watch ( (err, response) => {  //set up listener for the AuctionClosed Event
        //once the event has been detected, take actions as desired
        status.innerHTML = 'The auction has ended! Highest Bid is ' + response.args.highestBid;
      });
      setTimeout ( () => {  //simulate an auction for 3 seconds, after which the creator closes the auction
        contract.closeAuction (maxBid, {from: accounts [0]});
      }, 3000);
    })
    .catch ( (err) => {
      status.innerHTML = 'Some error occured. I guess shit happens =(';
    });
};

My question is on the JS app, every time the user is using the app, should we use
Auction.new to generate a new contract object?

or using new web3.eth.Contract(jsonInterface[, address][, options]) to load a existing contract? is this code a form of creating a contract instance?

1

This answer might help https://ethereum.stackexchange.com/a/34374/20276

Along with this snippet:

const Web3 = require('web3')
const web3 = new Web3('https://mainnet.infura.io')

function myFunction(myParam) {
    var contractAddress = '<ENTER-YOUR-CONTRACT-ADDRESS-HERE>'
    var contractABI = [<ENTER-YOUR-ABI-HERE>]
    var contract = new web3.eth.Contract(contractABI, contractAddress)
    var callData = contract.methods.<CONTRACT-FUNCTION>(myParam).call()
    callData.then(function(result) {
        console.log(result)
    })
}

myFunction(<MY-PARAM>)
  • thanks, wondering new web3.eth.Contract(contractABI, contractAddress) is creating a contract instance or not? – user824624 Dec 28 '17 at 11:39
  • yes. you can connect to multiple contracts simultaneously. – nyusternie Dec 28 '17 at 11:41
  • 1
    Re: contract address, I answered your other question: ethereum.stackexchange.com/questions/34570/… Also wanted to clarify, "creating a contract instance" means just creating some kind of "instance", which is not a separate contract or entirely new thing. It's simply an object that refers to, links, and allows you to interact with the contract itself on the blockchain. – carlolm Dec 28 '17 at 11:50
  • I have a another question, if the contract has two roles, seller and buyer. In the js app, both seller and buyer are accessing the application in their own browser which runs the js app. Does both apps be all initialized with the contract instance? – user824624 Dec 28 '17 at 16:22
  • FYI - Contract methods are NOT real-time. Meaning that the actions of either user of your dapp will not get written/confirmed until they're added in the next block. This creates a race, if 2 users perform a conflicting action on the same contract as to who will win and get "their" action confirmed (the other action will "fail"). – nyusternie Dec 28 '17 at 18:55
0

Usually you deploy a contract only once and obtain the address of it in the network. Then you use that address and ABI of the contract to interact with it. You could deploy a contract via JS code or use existing tools such as Remix/Parity to do so.

Code in question refers to accessing an already deployed contract.

PS. One contract could be deployed multiple times to the network and will have a unique address and independent instance of it in the network.

  • I have a another question, if the contract has two roles, seller and buyer. In the js app, both seller and buyer are accessing the application in their own browser which runs the js app. Does both apps be all initialized with the contract instance or one side will initialise a instance? – user824624 Dec 29 '17 at 11:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.