1

In the Truffle documentation, for interaction with contracts, the returned contract instance is assigned to meta before being interacted with:

var account_one = "0x1234..."; // an address
var account_two = "0xabcd..."; // another address

var meta;
MetaCoin.deployed().then(function(instance) {
  meta = instance;                               // <- Reasignment!
  return meta.sendCoin(account_two, 10, {from: account_one});
}).then(function(result) {
  // If this callback is called, the transaction was successfully processed.
  alert("Transaction successful!")
}).catch(function(e) {
  // There was an error! Handle it.
})

Why do this?

1

So that you can chain another function call using the same instance. For example to elaborate on their example:

var account_one = "0x1234..."; // an address
var account_two = "0xabcd..."; // another address

var meta;
MetaCoin.deployed().then(function(instance) {
  meta = instance;                               // <- Reasignment!
  return meta.sendCoin(account_two, 10, {from: account_one});
}).then(function(result) {
  // If this callback is called, the transaction was successfully processed.
  console.log("Transaction successful!");
  return meta.getBalance.call(account_two)
}).then(function(balance) {
  // can call more functions using the same instance of meta
  alert("Balance = " + balance);
}).catch(function(e) {
  // There was an error! Handle it.
})
| improve this answer | |
0

It isn't necessary to assign it to a global variable in the context of the code you've shared. It's only useful if you want to use it outside the scope of the first callback (the first .then()) which is probably what they're trying to demonstrate.

| improve this answer | |

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.