Wie rufe ich eine Funktion auf?

Ich habe folgende Funktion:

/// @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;
  }

}

Und ich erstelle eine Instanz aus meinem Vertrag wie folgt:

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

Wie kann ich diese Funktion aufrufen über:

contractInstance.methods.

Was ist seine Syntax genau? (Ich mache es über die Eingabeaufforderung und das Terminal)

Hier ist die gesamte .sol-Datei:

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;
  }

}

Antworten (1)

Mit web3.js 1.0 nennen Sie es so:

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

Wo sind die Parameter der Übertragungsmethode: _from, _to, _valueund Pl_value. Mit können Sie die Transaktionsoptionen angeben, wie die Adresse, von der die Transaktion gesendet werden soll, oder den Gaspreis. Siehe Dokumentation: http://web3js.readthedocs.io/en/1.0/web3-eth-contract.html#methods-mymethod-send

Danke schön. Ich habe die gesamte .sol-Datei in der Frage hinzugefügt. Wenn ich noch etwas hinzufügen muss, lassen Sie es mich bitte wissen.
Dieses Befehlsformat ist für diese Funktion richtig? contract_Instance.methods.transfer('from_address', 'to_address', 'value_int', 'Pl_value_string').send({ from: 'contract_account_address' }, function(error, result)
fromsollte das Konto sein, das den Smart Contract aufrufen wird.
Wenn Sie "send({ from: 'contract_account_address' } ..." from" meinen, ist das Konto, das den Vertrag bereitgestellt hat. Also, ist es richtig? Danke
Es muss nicht sein. Es kann ein beliebiges Konto sein. Denken Sie auch daran, dass ein Smart Contract keine Transaktion „senden“ kann. Siehe dies: ethdocs.org/en/latest/contracts-and-transactions/…
Ich habe mehrmals einige Transaktionen über ein auf Ethereum gegründetes Konsortium gesendet, aber keine davon wurde abgebaut oder bestätigt. Wissen Sie, was der Grund sein könnte?