Class: Contract

Contract

The main Contact class that all code working within a Chaincode Container must be extending. Overriding of the `beforeTransaction`, `afterTransaction`, `aroundTransaction`, `unknownTransaction` and `createContext` are all optional Supplying a name within the constructor is also option and will default to ''

new Contract(name)

Constructor - supplying a name is recommended but is not mandatory.
Parameters:
Name Type Description
name String name for the logic within this contract

Methods


<static> _isContract(obj)

isContract provides functionality to check if a passed object is a contract type. Enables checking if its a contract for when contract-api is "required" by different modules
Parameters:
Name Type Description
obj Object

<async> afterTransaction(ctx, result)

'afterTransaction' will be called before any of the transaction functions within your contract Override this method to implement your own processing. Examples of what you may wish to code are Logging, Event Publishing If an error is thrown, the whole transaction will be rejected
Parameters:
Name Type Description
ctx Context the transactional context
result Object value that is returned from the transaction function

<async> aroundTransaction(ctx, fn, paramters)

'aroundTransaction' wraps the call to the transaction function within your contract, allowing you to encapsulate it into a code block. Examples of what you could do overriding this include, but are not limited to: catching exceptions, logging, use a thread-store. When overriding this function, remember to call `super.aroundTransaction(ctx, fn, parameters)`! If you don't, the contract won't be able to run any transaction. If an error is thrown, the whole transaction will be rejected
Parameters:
Name Type Description
ctx Context the transactional context
fn function the contract function to invoke
paramters any the parameters for the function to invoke

<async> beforeTransaction(ctx)

'beforeTransaction' will be called before any of the transaction functions within your contract Override this method to implement your own processing. Examples of what you may wish to code are Logging, Event Publishing or Permissions checks If an error is thrown, the whole transaction will be rejected
Parameters:
Name Type Description
ctx Context the transactional context

createContext()

'createContext' is called before any after, before, unknown or user defined transaction function. This permits contracts to use their own subclass of context to add additinal processing. After this function returns, the chaincodeStub and client identity objects will be injected. No chaincode apis are available for calling directly within this function. Nor should the constructor of the subclasses context assume any other setup.
Returns:
a context implementation that must subclass context
Type
Context

getName()

Returns:
returns the namespace
Type
String

<async> unknownTransaction(ctx)

'unknownTransaction' will be called if the required transaction function requested does not exist Override this method to implement your own processing. If an error is thrown, the whole transaction will be rejected
Parameters:
Name Type Description
ctx Context the transactional context