Skip to content

Instantly share code, notes, and snippets.

@yhuag
Last active July 5, 2018 16:48
Show Gist options
  • Save yhuag/c77bc8eb0f5d53066e234aa1a0a7a1f5 to your computer and use it in GitHub Desktop.
Save yhuag/c77bc8eb0f5d53066e234aa1a0a7a1f5 to your computer and use it in GitHub Desktop.
Modularized smart contract: Library
pragma solidity ^0.4.24;
//// All Modules ////
contract Math {
// Module #0
function plus(uint _n1, uint _n2) public pure returns (uint) { return _n1 + _n2; }
// Module #1
function minus(uint _n1, uint _n2) public pure returns (uint ans) { return _n1 - _n2; }
// Module #2
function multiply(uint _n1, uint _n2) public pure returns (uint ans) { return _n1 * _n2; }
// Module #3
function divide(uint _n1, uint _n2) public pure returns (uint ans) { return _n1 / _n2; }
}
// @title Modularized smart contract: Library
// @author Hu Yao-Chieh (yhuag@ust.hk)
// @dev This smart contract is modularized via library call, in which the library stores all the modules.
contract Librarian{
address owner;
// Constructor
constructor() public { owner = msg.sender; }
//// Library ////
Math math = Math(0xe45BC404753c914e3B94Ddb0902B54027bE111A0);
function setMathLibrary(address _target) public { math = Math(_target); }
// Module #0
function plus(uint _n1, uint _n2) public view returns (uint) { return math.plus(_n1,_n2); }
// Module #1
function minus(uint _n1, uint _n2) public view returns (uint ans) { return math.minus(_n1,_n2); }
// Module #2
function multiply(uint _n1, uint _n2) public view returns (uint ans) { return math.multiply(_n1,_n2); }
// Module #3
function divide(uint _n1, uint _n2) public view returns (uint ans) { return math.divide(_n1,_n2); }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment