Each contract instance is different based on the source Solidity contract, and the API is created dynamically. Here are the various the APIs of a contract instance:
- allEvents: This is a function of a contract instance that takes a callback that is invoked whenever an event is fired by the contract matching the event signature under the current network ID in the contract artifacts object. You can also use event name-specific functions to catch specific events instead of all of them. In the preceding contract, to catch ping events, you can use SampleContract_Instance.ping(function(e, r){}).
- send: This function is used to send ether to the contract. It takes two arguments; that is, the first argument is the amount of wei to transfer and the second argument is an optional object that can be used to set the from of the transaction, which indicates from which address the ether is being sent. This call returns a promise, and the promise resolves to the details about the transaction when its mined.
- We can invoke any method of the contract using SampleContract.functionName() or SampleContract.functionName.call(). The first one sends a transaction, whereas the second one invokes the method on the EVM only, and the changes are not persistent. Both of these methods return a promise. In the first case, the promise resolves to the result of the transaction, that is, an object holding a transaction hash, logs, and transaction receipt. And in the second case, it resolves to the return value of the method call. Both the methods take function arguments and an optional last argument, which is an object to set from, gas, value, and so on of the transaction.