6

sorry if this is really basic but I'm a bit stuck. I'm playing with the default MetaCoin contract in Truffle and testing it against testrpc.

Reading through the truffle docs (http://truffle.readthedocs.io/en/beta/getting_started/contracts/), they mention executing contract functions using the abstraction - which is available in the frontend.

By frontend here, do they mean the truffle console? If no, where is it? Also, where does the output of my commands get printed?

At the moment I have a truffle console open in a powershell and testrpc running on a separate powershell. Any help is greatly appreciated!

11

This is probably a duplicate, but I think I can open a door for you, for as long as it lasts.

Front-end could be anything using Web3 and Truffle's wrapper.

Truffle lets you interact with the contract with less fussing about the low-level implementation details. Two areas stand out.

  1. Knowing the contract address after it got deployed.
  2. Knowing the ABI details for the contract.

At a low level, both are important, since all transactions are ByteCode sent to the contract for consideration. The ByteCode for a transaction is formed with hashes of the friendly functions in the source, truncated to 4-byte signatures, followed by arguments. Suffice it to say it's a process to correctly put it together.

Truffle deals with both issues in a developer-friendly way.

Try this ...

truffle development > var meta;
truffle development > var balance;
truffle development > var accounts;
truffle development > var account;
truffle development > web3.eth.getAccounts(function(e,a) { accounts=a; });
truffle development > account = accounts[0];

The node uses callbacks that truffle transforms into promises (thenable), so we've just initialized some vars we'll use next.

truffle development > MetaCoin.deployed().then(function(instance) { meta = instance; });
truffle development > meta

You should see some familiar functions, the deployed address and other details about MetaCoin.

truffle development > meta.getBalance.call(account).then(function(myBal) { balance = myBal; });
truffle development > balance;

You should see it in BigNumber format.

truffle development > balance.toString(10);

Or human-readable.

Truffle keeps track of the deployed address (.deployed()) for you. With testRPC this is a little glitchy because each time you restart testRPC it's an empty blockchain. Truffle's recollection of the deployed address misleads it. Resolve that with.

$ testrpc

Different window:

$ truffle migrate --reset

Here's a console showing it working.

enter image description here

  • 1
    Just a couple points to clarify: When I run truffle development > meta.balance().then(function(myBal) { balance = myBal; }); it returns meta.balance() is not a function Also, when I try to console log balance it doesn't return anything. Do I need to do something else to balance after declaring it in order to use it in the function? Thanks again for your help. – Pryce Turner Apr 12 '17 at 10:21
  • Hey Rob, I'm slowly getting all this. Is MetaCoin.deployed().then(function(instance) { meta = instance; }); the same as var meta = MetaCoin.deployed();? If yes, what is the advantage of your method? Thanks again. – Pryce Turner Apr 16 '17 at 5:41
  • Glad to hear it. Very possible you'll find similar results either way in many circumstances, but I'm finding the thennable syntax more reliable, so forming the habit. In summary, the difference is about waiting for a response. Truffle 3.x is rather new. Have a look here for guidance suggesting the new approach. truffleframework.com/tutorials/… – Rob Hitchens - B9lab Apr 16 '17 at 6:18
  • Can I interact from file (not directly in console) – sharif2008 Jun 28 '18 at 13:14
1

Here you have short and easy tutorial about truffle console and using it to interact with smart contracts.

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.