Skip to content

Instantly share code, notes, and snippets.

@reselbob
Created March 25, 2023 20:54
Show Gist options
  • Save reselbob/b4118e0d1f49a5c20e8e3dc1b55c25b5 to your computer and use it in GitHub Desktop.
Save reselbob/b4118e0d1f49a5c20e8e3dc1b55c25b5 to your computer and use it in GitHub Desktop.
Solidity Smart Contracts for OOP Developers
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.9.0;
import "./MessageRepeater.sol";
contract AmazingMessageRepeater is MessageRepeater {
function repeatMessage(uint256 repetitions) public view virtual override returns (string[] memory){
string[] memory arr;
for (uint i = 0; i < repetitions; i++) {
arr = addElementToStringArray(arr, toUpperCase(getMessage()));
//arr.push(myMessage);
}
return arr;
}
function toUpperCase(string memory str) private pure returns (string memory) {
bytes memory strBytes = bytes(str);
for (uint i = 0; i < strBytes.length; i++) {
if (strBytes[i] >= 0x61 && strBytes[i] <= 0x7A) {
strBytes[i] = bytes1(uint8(strBytes[i]) - 32);
}
}
return string(strBytes);
}
}
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.9.0;
interface Messageable {
function setMessage(string memory message) external;
function getMessage() external returns (string memory) ;
function getMessage(uint256 repetitions) external returns (string[] memory );
function repeatMessage(uint256 repetitions) external returns (string[] memory ) ;
}
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.9.0;
import "./Messageable.sol";
contract MessageRepeater is Messageable {
string private myMessage;
address private creator;
constructor() {
creator = msg.sender;
}
function setMessage(string memory message) public {
myMessage = message;
}
function getMessage() public view returns (string memory) {
return myMessage;
}
function getMessage(uint256 repetitions) public view returns (string[] memory) {
return repeatMessage(repetitions);
}
function repeatMessage(uint256 repetitions) public virtual view returns (string[] memory){
string[] memory arr;
for (uint i = 0; i < repetitions; i++) {
arr = addElementToStringArray(arr, myMessage);
}
return arr;
}
function addElementToStringArray(string[] memory oldArray, string memory newElement) internal pure returns (string[] memory) {
string[] memory newArray = new string[](oldArray.length + 1);
for (uint i = 0; i < oldArray.length; i++) {
newArray[i] = oldArray[i];
}
newArray[oldArray.length] = newElement;
return newArray;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment