2

I have a contract and I'm super confused how to interact with it. The submitting part works fine and I get a contract id. After that, I don't really understand if I'm supposed to send it a transaction or do something else? What's the lifecycle? It says the constructor is executed when the contract runs. When is that? When it's mined? Everytime a person sends it a message. How do I send it a message? Does the consumer of a contract send gas? The creator? Both? If two people send messages are there two contracts or one?

Using this as a base: https://gist.github.com/tomconte/4edb83cf505f1e7faf172b9252fff9bf#file-web3-solc-contract-compile-deploy-js-L20

Have this function:

function testContract(address) {
    // Reference to the deployed contract
    const greeter = contract.at(address);
    console.log(greeter);
    //console.log(greeter("bob"));
    //var msg = greeter.greet.estimateGas();
    //const destAccount = '0x002D61B362ead60A632c0e6B43fCff4A7a259285';
    //console.log(greeter.greet.sendTransaction(destAccount));    
}

I've tried greeter.greet, greeter.almosteverything.

The object for greeter looks like this:

   {_eth:...
     abi:
       [ { constant: false,
           inputs: [],
           name: 'kill',
           outputs: [],
           payable: false,
           type: 'function' },
         { constant: true,
           inputs: [],
           name: 'greet',
           outputs: [Object],
           payable: false,
           type: 'function' },
         { inputs: [Object], payable: false, type: 'constructor' } ],
      kill:
       { [Function: bound ]
         request: [Function: bound ],
         call: [Function: bound ],
         sendTransaction: [Function: bound ],
         estimateGas: [Function: bound ],
         getData: [Function: bound ],
         '': [Circular] },
      greet:
       { [Function: bound ]
         request: [Function: bound ],
         call: [Function: bound ],
         sendTransaction: [Function: bound ],
         estimateGas: [Function: bound ],
         getData: [Function: bound ],
         '': [Circular] },
      allEvents: [Function: bound ] }

Sol code:

pragma solidity ^0.4.10;

contract mortal {
    /* Define variable owner of the type address*/
    address owner;

    /* this function is executed at initialization and sets the owner of the contract */
    function mortal() { owner = msg.sender; }

    /* Function to recover the funds on the contract */
    function kill() { if (msg.sender == owner) selfdestruct(owner); }
}

contract greeter is mortal {
    /* define variable greeting of the type string */
    string greeting;

    /* this runs when the contract is executed */
    function greeter(string _greeting) public {
        greeting = _greeting;
    }

    /* main function */
    function greet() constant returns (string) {
        return greeting;
    }
}
2
  • How do you deploy and plan to interact with the contract? Geth? Parity? Solidity+metamask? Private testnet or public? If public: would you share the address that you got upon successful deploy?
    – SCBuergel
    Commented May 26, 2017 at 8:38
  • I'm using a geth server with the web3 node.js library. The code I have above calls the contract. I can't see any output for it Commented May 27, 2017 at 4:58

3 Answers 3

1

Generally there will be one node which initiates the contract. and when it is mined, the contract would be added to the blockchain which returns a contract address.

The peer nodes which are interested in the contract, must know two things that needs to be shared.

  1. the contract address
  2. the contract abi

with the help of this two the other peer nodes could interact with the contract.

6
  • Do you need gas to interact with the contract? What should happen when I call greet? Commented May 27, 2017 at 5:04
  • @JustinThomas, For deploying a contract we required some gas. But to just call a contract data, we don't require any gas. Once the contract is deployed and if you are able to connect to it. you can directly call the greet method which will return the greeting message. Commented May 27, 2017 at 6:45
  • But what if the contract updates a mapping or stores data? Like in the example of creating a new token and transferring to another user. Commented May 30, 2017 at 3:06
  • @JustinThomas, Then we will surely need to have some ether to make such type of transactions. Commented May 30, 2017 at 4:38
  • The person calling the contract? Or the contract creator? Commented May 31, 2017 at 6:08
1

From this video: https://www.youtube.com/watch?v=8jI1TuEaTro

I discovered Truffle (https://github.com/trufflesuite/truffle)

Couple of things on life cycle:

  1. The contract creator pays gas to get the contract into the block chain.
  2. If the method or access is constant, the client and do the operation. Constant operations do not modify the block chain, so they are free.
  3. If the method is not constant, then it costs gas to run it.

Some code using web3 on node console:

# Beware the promise
> var helloWorld; HelloWorld.deployed().then((a) => helloWorld = a);
# Free
> hello.balance.call()
# Not free
> hello.getBalanceBaller.sendTransaction()
'0x7acb7577a8623090b4f648203f2ecfc2e0689e011f100edef212872fe9e917f7'
> hello.balance.call()
{ [String: '412'] s: 1, e: 2, c: [ 412 ] }
# Account value
> web3.eth.getBalance(account)
{ [String: '4.824337705048486307205e+21'] s: 1, e: 21, c: [ 48243377, 5048486307205 ] }
# Block was mined
> hello.getBalance.call()
{ [String: '824'] s: 1, e: 2, c: [ 824 ] }
# Balance of account is deducted
> web3.eth.getBalance(account)
{ [String: '4.829335031748486307205e+21'] s: 1, e: 21, c: [ 48293350, 31748486307205 ] }
0

I was struggling with similar questions. I have just put this tutorial online, in order to explain to get started with contract creation, and interaction from a web page : https://steemit.com/ethereum/@yalb/hello-world-ethereum-create-an-ethereum-smart-contract-and-interact-with-it-from-a-website

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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