How to do it...

  1. Consider the following contract as an example for this recipe. This is a simple token contract with a feature to transfer tokens and check the balance of each address. The contract is only for illustration purposes, so don't use it in your production application:
pragma solidity ^0.4.23;

contract TokenContract {
mapping (address => uint) balances;

event Transfer(address indexed _from, address indexed _to, uint256 _value);

constructor() public {
balances[msg.sender] = 100000;
}

function sendToken(address receiver, uint amount) public returns(bool) {
require(balances[msg.sender] < amount);
balances[msg.sender] -= amount;
balances[receiver] += amount;
emit Transfer(msg.sender, receiver, amount);
return true;
}

function getBalance(address addr) public view returns(uint) {
return balances[addr];
}
}
  1. This contract contains a constructor, a state changing method called sendToken, and a read-only method, getBalance. The constructor will be executed during the contract deployment and other functions have to be called with either a transaction or a call.
  2. Truffle will create a JavaScript object for the contract. Use the deployed() function to interact with the deployed version of the contract:
TokenContract.deployed().then(function(instance) { 
console.log(instance);
});

Make sure that you have migrated your contract before trying to interact with them. If you are using truffle console, use truffle migrate from the root of your project.

  1. The sendToken function will try to send some tokens from one account to another. This results in a state change, and we need to use a transaction for this.

  2. Call the function directly and it will result in a transaction by default, instead of a call:
TokenContract.sendToken();
  1. Pass the parameters that are required to execute the function. Use an object as the optional last parameter that lets you configure specific details about the transaction, such as from address, value, gas, and so on:
var from_address = "0xa...";
var to_address = "0xb...";

TokenContract.sendToken(to_address, 500, {
from: from_address
});
  1. Use the promise functionality to fire callbacks on success and failure. With this, you don't have to check the status of the transaction yourself:
TokenContract.sendToken(to_address, 500, {
from: from_address
}).then(function(result) {
console.log(result);
})
  1. The whole operation will look something as follows:
var from_address = "0xa...";
var to_address = "0xb...";

var tokenContract;

TokenContract.deployed().then(function(instance) {
tokenContract = instance;
return tokenContract.sendToken(to_address, 500, {
from: from_address
});
}).then(function(result) {
// Transaction successful!
console.log(result);
}).catch(function(e) {
// Transaction failed
console.log(e);
})
  1. The getBalance function is used for reading the balance of a specific address. Note that this does not modify any state. So, execute the function using the call method:
var account = "0xa...";
tokenContract.getBalance.call(account);
  1. Use the promise method to read data immediately from the execution. It will return the result instead of the transaction hash. It will also return a BigNumber object, which you may need to convert:
var account = "0xf17f52151EbEF6C7334FAD080c5704D77216b732";

TokenContract.deployed().then(function(instance) {
return instance.getBalance.call(account);
}).then(function(result) {
// Returns result
console.log(result.toNumber())
}).catch(function(e) {
// Exception
console.log(e);
});
  1. Now, you know how to make a transaction and a call. Let's look deep into the various things you can do with the transaction result.

  1. The transaction returns an object with the transaction hash, receipt, and array of events logged during the transaction:
var from_address = "0xa...";
var to_address = "0xb...";

var tokenContract;

TokenContract.deployed().then(function(instance) {
tokenContract = instance;
return tokenContract.sendToken(to_address, 500, {
from: from_address
});
}).then(function(result) {
console.log(result.tx); // Transaction hash
console.log(result.logs); // Event logs
console.log(result.receipt); // Transaction receipt
})
  1. Use the logs array to locate any event and then perform tasks based on that. Try catching the Transfer event during token transfer:
var from_address = "0xa...";
var to_address = "0xb...";

var tokenContract;

TokenContract.deployed().then(function(instance) {
tokenContract = instance;
return tokenContract.sendToken(to_address, 500, {
from: from_address
});
}).then(function(result) {
for(var i = 0; i < result.logs.length; i++) {
if(result.logs[i].event == "Transfer") {
console.log("Event raised!");
}
}
})
  1. Send Ether directly to a contract to trigger the fallback function. The contractInstance provides the sendTransaction method, which accepts the standard transaction object for this operation:
contractInstance.sendTransaction({
from: "0x..",
value: 10000 // in wei
}).then(function(result) {
// Transaction obj
console.log(result);
});
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.138.35.255