Solidity Smart Contracts Walk-through Series Part-3
This is part 3 of Smart contract walk-through series. In the following post I will give you an in-depth overview over the technical implementation details of our business partnership smart contract. Code repository can be found here.
Business partnership use case ::
Two users (or business partners) can get into partnership by signing the contract. Once the contract is signed both the partners will get following privileges :
- Rights to add the funds in contract.
- Rights to transfer the fund from contract to third party.
- Propose an item to be shared among them with percentage allocation.(Sum of both the partners allocation should be 100%)
- Approve a proposed item. Once an item gets approved by both the partners it will be committed to blockchain with allocation details.
- Remove an item. Once both the partner agrees to remove a certain item, it will be marked as removed on blockchain.
- Right to break the partnership. Once both the partner agrees to get separated, above mentioned rights will no more be applicable for any of them. Also the funds stored in contract will get equally divided among them.
Let’s start …
Here is our data structure defined :
We will store addresses of both the partners (partnerOne & partnerTwo). Two boolean flags (signed & separated) will track the status of partnership. Both the mappings (hasSeparated & hasSigned) will store the approval from partners regarding their partnership or separation. Item struct will help us to store the items to be shared among partners with their respective share percentage. Items array will store the list of items. ItemIds array will store the indexes of items.
We will initialise wallet address of both the users (who will become partner in future once they sign the contract) during contract deployment itself (in constructor). We will also use modifiers such as onlyPartner , isSigned & areNotSeparated to restrict various contract functionalities.
signContract method will allow both the users to sign the contract. Once both have signed the contract, contract’s status will change to in partnership (by marking signed flag as true).
Note- A method specified as external can be called from outside the contract. To call it inside contract use this keyword(this.externalMethodName()). Methods specified as public can be called outside or inside the contract. Internal methods can only be called internally. Private methods will not be available inside derived contracts.
proposeItem will let both the partners to propose any item to be shared among them. This transaction will be successful only when –
- Both users are in partnership(signed = true).
- Each partner share should be non-negative & total share sum should be equal to 100%.
proposeItem method will also mark item approved by the user who has proposed the item(item.hasApprovedAdd[msg.sender]=true).It will then store the item in items list & will also maintain its index in itemIds array.
approveItem will accept an itemId, to determine the item to be approved from the list. Before approving from the sender, it will validate if the item was added, removed or was approved earlier. Once both partners have approved item will be marked as added.
RemoveItem will accept an itemId, to determine the item to be removed. Once both partners agreed , item will be marked as removed.
Note- address.send() & address.transfer() methods are safe as they forward 23000 gas to external call which is sufficient only to log something. Whereas address.value.call(msg.value)() is unsafe as it forwards complete gas to external call, making contract vulnerable for hacking.
Anonymous payable method will allow partners to store the fund in contract. pay method will allow them to transfer any fund to third parties.
Note — Anonymous fallback function may get called after any other function. If it has to be used only for transaction (plain ether transfer) then you should validate if msg.data.length == 0 (Best practise).
getSeparated allows both partners to apply for ending the partnership. If both have approved for separation, contract will be end the partnership. It won’t allow them to make any other transactions on blockchain. Also all funds stored in contract will get equally divided among them.