0

I have a function as follows :

/// @notice send `_value` token to `_to` from `msg.sender`
  /// @notice send `Pl_value` token to `_to` from `msg.sender`
  /// @param _to The address of the recipient
  /// @param _value The amount of token to be transferred
  /// @param Pl_value The payload to be transferred
  /// @return Whether the transfer was successful or not
  function transfer(address _from, address _to, uint256 _value, string Pl_value) public returns (bool) {
    require(_to != address(0));

    // SafeMath.sub will throw if there is not enough balance.
    balances[_from] = balances[_from].sub(_value);
    balances[_to] = balances[_to].add(_value);

    Payload[_to] = Pl_value;

    return true;
  }

}

And I create an instance from my contract as follows :

var contractInstance = new web3.eth.Contract(abi, 'address-of-contract');

How can I call this function via :

contractInstance.methods.

What is its syntax exactly ? (I do it via command prompt and terminal)

Here is the entire .sol file :

import './SafeMath.sol';

contract BasicToken {

  // Using safe math library for all uint256 operations
  using SafeMath for uint256;

  // Total supply of token
  uint256 public totalSupply;

  // Balances for each account
  mapping(address => uint256) balances;

  mapping(address => string) Payload;

  /// @param _owner The address from which the balance will be retrieved
  /// @return The balance
  function balanceOf(address _owner) constant returns (uint256 balance) {
    return balances[_owner];
  }

  /// @param _owner The address from which the balance will be retrieved
  /// @return The payload
  function PayloadOf(address _owner) constant returns (string payload) {
    return Payload[_owner];
  }

  /// @notice send `_value` token to `_to` from `msg.sender`
  /// @notice send `Pl_value` token to `_to` from `msg.sender`
  /// @param _to The address of the recipient
  /// @param _value The amount of token to be transferred
  /// @param Pl_value The payload to be transferred
  /// @return Whether the transfer was successful or not
  function transfer(address _from, address _to, uint256 _value, string Pl_value) public returns (bool) {
    require(_to != address(0));

    // SafeMath.sub will throw if there is not enough balance.
    balances[_from] = balances[_from].sub(_value);
    balances[_to] = balances[_to].add(_value);

    Payload[_to] = Pl_value;

    return true;
  }

}
1

Using web3.js 1.0, you call it like this:

contractInstance.methods.transfer(<method parameters>).send(<options>)

where are the parameters of the transfer method: _from, _to, _value and Pl_value. With you can specify the transaction options, like he address the transaction should be sent from or the gas price. See documentation: http://web3js.readthedocs.io/en/1.0/web3-eth-contract.html#methods-mymethod-send

  • Thank you. I added entire .sol file in the question. If I need to add some thing else, please let me know. – Questioner Apr 19 '18 at 12:04
  • This command format is correct for this function? contract_Instance.methods.transfer('from_address', 'to_address', 'value_int', 'Pl_value_string').send({ from: 'contract_account_address' }, function(error, result) – Questioner Apr 19 '18 at 12:34
  • from should be the account who is going to call the smart contract. – lunr Apr 19 '18 at 14:40
  • If you mean "send({ from: 'contract_account_address' } ..." from is the account which deployed the contract. So, is it correct? Thanks – Questioner Apr 19 '18 at 15:10
  • It doesn't have to be. It can be any account. Also keep in mind that a smart contract cannot 'send' a transaction. See this: ethdocs.org/en/latest/contracts-and-transactions/… – lunr Apr 20 '18 at 16:44

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.