Menu

Blogchain

A blog about Blockchain technology

Blockchain Integration Patterns – Outbound

Smart contracts operate on data contained within a blockchain, but there may be situations when events generated within a blockchain, like a payment or a change of state of a smart contract, require some activities to happen in the outside world, off-chain. Azure Blockchain Workbench facilitates interaction of smart contracts with systems, services and devices when an event is raised in a smart contract and it needs to be handled externally. This form of messaging integration focuses on two types of messages: messages requesting transactions to execute on a distributed ledger, and events exposed by that ledger when transactions have taken place.
 
Raise events in a Smart Contract
In this scenario, an event occurs within a smart contract, for example, a state change or the execution of a specific type of transaction. This event is broadcast via an Azure Event Grid to downstream consumers, and those consumers then take appropriate actions. An example of this scenario is that when a transaction occurs, a consumer would be alerted and could take action, such as recording the information in a SQL database or the Common Data Service. This scenario is the same pattern that Azure Blockchain Workbench follows to populate its off-chain SQL database.
Another would be if a smart contract transitions to a particular state, for example when a contract goes out of compliance. When this state change happens, the smart contract could trigger an alert to be sent to an administrator's mobile phone.
 
 
This scenario occurs using the process depicted in the previous diagram, where:
  • The smart contract transitions to a new state and sends an event to the ledger.
  • The ledger receives and delivers the event to Azure Blockchain Workbench.
  • Azure Blockchain Workbench is subscribed to events from the ledger and receives the event.
  • Azure Blockchain Workbench publishes the event to subscribers on the Event Grid.
  • External systems are subscribed to the Event Grid, consume the message, and take the appropriate actions.
 
Using Azure Blockchain Workbench, it is possible to raise events from within a smart contract by invoking the ContractCreated() or ContractUpdated() methods in the WorkbenchBase base class.
This class is declared as:
 
contract WorkbenchBase {
    event WorkbenchContractCreated(string applicationName, string workflowName, address originatingAddress);
    event WorkbenchContractUpdated(string applicationName, string workflowName, string action, address originatingAddress);
 
    string internal ApplicationName;
    string internal WorkflowName;
 
    function WorkbenchBase(string applicationName, string workflowName) internal {
        ApplicationName = applicationName;
        WorkflowName = workflowName;
    }
 
    function ContractCreated() internal {
        WorkbenchContractCreated(ApplicationName, WorkflowName, msg.sender);
    }
 
    function ContractUpdated(string action) internal {
        WorkbenchContractUpdated(ApplicationName, WorkflowName, action, msg.sender);
    }
}
 
Any public smart contract running in Azure Blockchain Workbench should extend this base class, and then have the opportunity to raise an event invoking the relevant method. For example:
 
contract AssetTransfer is WorkbenchBase('AssetTransfer', 'AssetTransfer')
{
   function AssetTransfer(string description, uint256 price) public
    {
        InstanceOwner = msg.sender;
        AskingPrice = price;
        Description = description;
        State = StateType.Active;
        ContractCreated();
    }
 
    function Terminate() public
    {
        if (InstanceOwner != msg.sender)
        {
            revert();
        }
 
        State = StateType.Terminated;
        ContractUpdated('Terminate');
    }
 
 
The AssetTransfer smart contract raises the ContractCreated event in its constructor, and the ContractUpdated event in the Terminate method. These events are picked up by the Azure Blockchain Workbench runtime and published on an Azure Event Grid, from which they can be handled by an external system.
 

Go Back

Comment