Represents a smart contract (chaincode) instance in a network. Applications should get a Contract instance using the networks's getContract method.
The Contract allows applications to:
- Submit transactions that store state to the ledger using submitTransaction.
- Evaluate transactions that query state from the ledger using evaluateTransaction.
- Listen for new chaincode events and replay previous chaincode events emitted by the smart contract using addContractListener.
If more control over transaction invocation is required, such as including transient data, createTransaction can be used to build a transaction request that is submitted to or evaluated by the smart contract.
Methods
-
addContractListener(listener [, options])
-
Add a listener to receive all chaincode events emitted by the smart contract as part of successfully committed transactions. The default is to listen for full contract events from the current block position.
Parameters:
Name Type Argument Description listener
module:fabric-network.ContractListener A contract listener callback function. options
module:fabric-network.ListenerOptions <optional>
Listener options. Returns:
The added listener.- Type
- Promise.<module:fabric-network.ContractListener>
Example
const listener: ContractListener = async (event) => { if (event.eventName === 'newOrder') { const details = event.payload.toString('utf8'); // Run business process to handle orders } }; contract.addContractListener(listener);
-
addDiscoveryInterest(interest)
-
Provide a Discovery Interest settings to help the peer's discovery service build an endorsement plan. This chaincode Id will be include by default in the list of discovery interests. If this contract's chaincode is in one or more collections then use this method with this chaincode Id to change the default discovery interest to include those collection names.
Parameters:
Name Type Description interest
DiscoveryInterest These will be added to the existing discovery interests and used when module:fabric-network.Transaction#submit is called. Returns:
This Contract instance- Type
- Contract
-
createTransaction(name)
-
Create an object representing a specific invocation of a transaction function implemented by this contract, and provides more control over the transaction invocation. A new transaction object must be created for each transaction invocation.
Parameters:
Name Type Description name
string Transaction function name. Returns:
A transaction object. -
deserializeTransaction(data)
-
Deserialize a transaction from previously saved state.
Parameters:
Name Type Description data
Buffer Serialized transaction data. Returns:
A transaction object. -
evaluateTransaction(name [, args])
-
Evaluate a transaction function and return its results. The transaction function
name
will be evaluated on the endorsing peers but the responses will not be sent to the ordering service and hence will not be committed to the ledger. This is used for querying the world state. This function is equivalent to callingcreateTransaction(name).evaluate()
.Parameters:
Name Type Argument Description name
string Transaction function name. args
string <optional>
<repeatable>
Transaction function arguments. Returns:
Payload response from the transaction function.- Type
- Buffer
-
getDiscoveryInterests()
-
Retrieve the Discovery Interest settings that will help the peer's discovery service build an endorsement plan.
Returns:
- An array of DiscoveryInterest- Type
- Array.<DiscoveryInterest>
-
removeContractListener(listener)
-
Remove a previously added contract listener.
Parameters:
Name Type Description listener
module:fabric-network.ContractListener A contract listener callback function. -
resetDiscoveryInterests()
-
reset Discovery interest to default of this contracts chaincode name and no collection names and no other chaincode names.
Returns:
This Contract instance- Type
- Contract
-
submitTransaction(name [, args])
-
Submit a transaction to the ledger. The transaction function
name
will be evaluated on the endorsing peers and then submitted to the ordering service for committing to the ledger. This function is equivalent to callingcreateTransaction(name).submit()
.Parameters:
Name Type Argument Description name
string Transaction function name. args
string <optional>
<repeatable>
Transaction function arguments. Throws:
-
If the transaction was successfully submitted to the orderer but timed out before a commit event was received from peers.
Returns:
Payload response from the transaction function.- Type
- Buffer
-