Skip to content

Instantly share code, notes, and snippets.

@renso3x
Created March 10, 2026 13:50
Show Gist options
  • Select an option

  • Save renso3x/de1cd14c68d5df9369ff716347e070a5 to your computer and use it in GitHub Desktop.

Select an option

Save renso3x/de1cd14c68d5df9369ff716347e070a5 to your computer and use it in GitHub Desktop.
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.31;
/**
Activity 1: Caller and Receiver Contracts
Instructions
Create two contracts
The first contract should serve as a caller
The second contract should contain the function to compute sum, difference, products and quotient of two numbers that will come from the caller.
*/
contract Calculator {
function add(uint256 a, uint256 b) external pure returns (uint256) {
return a + b;
}
function subtract(uint256 a, uint256 b) external pure returns (uint256) {
require(a >= b, "Result would be negative");
return a - b;
}
function multiply(uint256 a, uint256 b) external pure returns (uint256) {
return a * b;
}
function divide(uint256 a, uint256 b) external pure returns (uint256) {
require(b != 0, "Cannot be divisible by 0");
return a / b;
}
}
interface ICalculator {
function add(uint256 a, uint256 b) external pure returns (uint256);
function subtract(uint256 a, uint256 b) external pure returns (uint256);
function multiply(uint256 a, uint256 b) external pure returns (uint256);
function divide(uint256 a, uint256 b) external pure returns (uint256);
}
contract MyCalculator {
ICalculator public calculator;
constructor(address _icalculator) {
calculator = ICalculator(_icalculator);
}
function getSum(uint256 a, uint256 b) external view returns (uint256) {
return calculator.add(a, b);
}
function getDifference(uint256 a, uint256 b) external view returns (uint256) {
return calculator.subtract(a, b);
}
function getMultiply(uint256 a, uint256 b) external view returns (uint256) {
return calculator.multiply(a, b);
}
function getQuotient(uint256 a, uint256 b) external view returns (uint256) {
return calculator.divide(a, b);
}
}
/**
Activity 2: :Convert This Contract into an Interface
Below is a regular contract called StudentRegistry.
Your task:
Create an interface named IStudentRegistry based on this contract.
Rules for your interface:
Copy only the function signatures (no function bodies or logic).
All functions must be marked external.
Do not include state variables, events, or constructors.
Save it in a separate file or above the contract.
*/
interface IStudentRegistry {
function register(string memory _name, uint256 _age) external;
function getStudent(address _addr) external view returns (string memory, uint);
function isRegistered(address _addr) external view returns (bool);
}
contract StudentRegistry is IStudentRegistry {
struct Student {
string name;
uint age;
}
mapping(address => Student) public students;
function register(string memory _name, uint256 _age) public {
students[msg.sender] = Student(_name, _age);
}
function getStudent(address _addr) public view returns (string memory, uint) {
Student memory s = students[_addr];
return (s.name, s.age);
}
function isRegistered(address _addr) public view returns (bool) {
return bytes(students[_addr].name).length > 0;
}
}
// Inheritance
contract BaseOne {
function firstFunction() public pure returns (string memory) {
return "First Function";
}
function secondFunction() public pure returns (string memory) {
return "Second Function";
}
}
contract BaseTwo {
function thirdFunction() public pure returns (string memory) {
return "Third Function";
}
}
contract FinalBase is BaseOne, BaseTwo {
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment