Using Interfaces & Inheritance with Solidity
Dr. Joe Shepherd
I help companies infuse AI into their products, transform processes, and innovate in ways that will uniquely accelerate your organization.
There is a lot of information out there on #Blockchain but not very much on #Solidity the programming language used to create Smart Contracts for Blockchain. My background is largely Object Oriented Development and I like to make heavy use of Interfaces when I build applications. I wanted to understand how to do this with Solidity and share the process here.
It turns out that this isn't really all that hard. Since Solidity is Javascript based and object oriented its fairly straight forward. The below code shows a StatementOfWork Solidity contract that pulls in a single function from an iStateMachine interface. The first image defines the interface.
The second image is the StatementOfWork contract.
You can clearly see that on line 28 we are able to implement our interface function thus enforcing our interface contract. The only thing you need is the import statement on line 2 (import "./iStateMachine.sol";) and the contract definition on line 5 (contract StatementOfWork is iStateMachine). The important part is the 'is iStateMachine".
Now inheritance works the exact same way. You just use the Contract keyword as opposed to Interface. I am using Visual Studio Code on a Mac for this. Super light weight and very simple.
Ref: https://solidity.readthedocs.io/en/develop/common-patterns.html#state-machine