0

Here is my contract example.

pragma solidity ^0.4.18;
contract LocalEthereum {

    address public owner;
    event Created(bytes32 _tradeHash);
    function createEvent() onlyOwner external {
        Created(0x01);
    } 
}

version one

import {default as contract} from 'truffle-contract';
import Web3 from 'web3';
const contractAbstraction = contract(abi);
contractAbstraction.setProvider(this.web3.currentProvider);
contractAbstraction.deployed().then(function(contractInstance) {

    }).catch(function(e) {
      console.log(e);
    })

version 2

const contractAbstraction = contract(abi);
contractAbstraction.setProvider(this.web3.currentProvider);
contractAbstraction.new().then(instance){}.catch(err){}

}

version 3

const contractAbstraction = web3.eth.contract(abi); 
var contractInstance = contractAbstraction.at(address);

My first question is compared with code 1 and 2, if I am using the truffle framework to deploy the contract, then I just need to contractAbstraction.deployed() for each call, so what is the use of new().

My assumption of new() will make a new instance of the contract, which initialize the contract, the owner address in the contract is the address of those who uses new(). But why do some examples I see don't use new() at all?

Secondly, compared with code 1 and 3, what is the difference with them, I see different examples using the two, I assume they are the same?

1

The function new() is used to deploy a new instance of a contract. For example you can design your contract to have multiple instances you will need new to create them.

The funciton at() is used to reference an already deployed contract. For example if you want to interact with a third party contract, and you only have the contract ABI and address, in those cases deployed() will not work and you have to use at to interact with it.

  • as new() is to deploy a new instance of a contract, is it the same with turffle.migrate() command? moreover, when deploying a contract the network cost the gas and eth, why would we have multiple instance of contract, any reason behind it? – user824624 Nov 30 '17 at 7:04
  • @user824624 Truffle's migrate is not the same that new(), trufle updates the contract's artifacts and mantain a record of deployed contract. It depends on your contract requirementes and design, for a store we have a big contract that handles payments, and we deploy a small contract for each user that forward payments. The user doesn't know it is a contract and can use they favorite wallet to send payments. We use the contract address to identify the user, so it can pay with any address, ie from an exchange for example. – Ismael Nov 30 '17 at 19:40

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.