Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save rachmaninovquartet/4809d58d9541b3aa35e4c127384cb4dd to your computer and use it in GitHub Desktop.
Save rachmaninovquartet/4809d58d9541b3aa35e4c127384cb4dd to your computer and use it in GitHub Desktop.
Test Contracts
// Copyright SECURRENCY INC.
// SPDX-License-Identifier: MIT
pragma solidity >=0.7.0 <0.9.0;
/// ///
/// Task 1 ///
/// ///
/**
* Implemenet the function "doStringsMatch"
* if the strings match - return true
* else - return false
*/
contract StringMatch {
// first length check eliminates a lot of extra processing. Then bytes is faster than abi encoding
function doStringsMatch(string memory a, string memory b) external pure returns (bool)
{
return bytes(a).length == bytes(b).length && keccak256(bytes(a)) == keccak256(bytes(b));
}
}
/// ///
/// Task 2 ///
/// ///
/**
* Test task "Investor registration"
*
* Change the code so that investor details are tied to a "round"
* The caller should be able to set the investor details for the first round, then second round, to n... round
* The caller should be able to retrieve investor details by "round"
*
* All other modifications are allowed.
**/
contract InvestorRegistration {
uint public investmentRound = 0;
InvestorDetails[] roundsWithDetails;
//InvestorDetails public investorsDetails; //removed bc state var costs storage
struct InvestorDetails { //reorg vars to fewer slots and rename so function params match
address investor;
uint64 depositAmount;
uint8 age;
bool kycStatus;
bool isVerifiedInvestor;
bool isUSResident;
}
/**
* @notice Lead investor registration
*
* @param _investor A lead investor address
* @param _depositAmount The amount deposited by a investor
* @param _age Investor age
* @param _kycStatus Investor KYC status
* @param _isVerifiedInvestor Investor verification status
* @param _isUSResident Resident status
**/
function setLeadInvestorForARound(
address _investor,
uint64 _depositAmount,
uint8 _age,
bool _kycStatus,
bool _isVerifiedInvestor,
bool _isUSResident
)
external
{
require(_investor != address(0x00), "Invalid investor address");
require(_depositAmount > 0, "A deposit amount should be more than zero");
require(_age > 18, "The investor should be adult");
roundsWithDetails.push(InvestorDetails({ //remove unnecessary variable
investor: _investor,
depositAmount: _depositAmount,
age: _age,
kycStatus: _kycStatus,
isVerifiedInvestor: _isVerifiedInvestor,
isUSResident: _isUSResident
}));
investmentRound++;
}
/**
* @notice Returns a lead investor details
**/
function getInvestorDetailsByInvestmentRound(
uint round
)
external
view
returns (
address investor,
uint64 depositAmount,
uint8 age,
bool kycStatus,
bool isVerifiedInvestor, //fix name
bool isUSResident
)
{
require(round > 0 && round <= investmentRound, "invalid round"); //sanitize input
InvestorDetails memory investorDetails = roundsWithDetails[round - 1];
return (
investorDetails.investor, //consistent var names
investorDetails.depositAmount,
investorDetails.age,
investorDetails.kycStatus,
investorDetails.isVerifiedInvestor,
investorDetails.isUSResident
);
}
}
/// ///
/// Task 3 ///
/// ///
/**
* This contract should compile
* A stack overflow issue
*/
contract StackOverflow {
struct manyVariables{ //reorg for fewer slots, possibly types can be made smaller but business requirments are needed
bytes32 marketObjectCodeRateReset;
address currency;
address settlementCurrency;
uint contractDealDate;
uint statusDate;
uint initialExchangeDate;
uint maturityDate;
uint purchaseDate;
uint capitalizationEndDate;
uint cycleAnchorDateOfInterestPayment;
uint cycleAnchorDateOfRateReset;
uint cycleAnchorDateOfScalingIndex;
uint cycleAnchorDateOfFee;
int notionalPrincipal;
int nominalInterestRate;
int accruedInterest;
int rateMultiplier;
}
function createAssetDetails(
manyVariables calldata _manyVariables
)
external
{
require(_manyVariables.currency != address(0), "Invalid currency address"); // fixed this and next two (inefficient checks)
require(_manyVariables.settlementCurrency != address(0), "Invalid settlement currency address");
require(_manyVariables.marketObjectCodeRateReset != bytes32(0), "Code rate request is required");
require(_manyVariables.notionalPrincipal != 0, "notionalPrincipal can't be empty"); //fix typo
require(_manyVariables.nominalInterestRate != 0, "nominalInterestRate can't be empty");
require(_manyVariables.accruedInterest != 0, "accruedInterest can't be empty");
require(_manyVariables.rateMultiplier != 0, "rateMultiplier can't be empty");
require(_manyVariables.contractDealDate != 0, "Contract deal date can't be empty");
require(_manyVariables.statusDate != 0, "statusDate can't be empty");
require(_manyVariables.initialExchangeDate != 0, "initialExchangeDate can't be empty");
require(_manyVariables.maturityDate != 0, "maturityDate can't be empty");
require(_manyVariables.purchaseDate != 0, "purchaseDate can't be empty");
require(_manyVariables.capitalizationEndDate != 0, "capitalizationEndDate can't be empty");
require(_manyVariables.cycleAnchorDateOfInterestPayment != 0, "cycleAnchorDateOfInterestPayment can't be empty");
require(_manyVariables.cycleAnchorDateOfScalingIndex != 0, "cycleAnchorDateOfScalingIndex can't be empty");
require(_manyVariables.cycleAnchorDateOfFee != 0, "cycleAnchorDateOfFee can't be empty");
saveDetailsToStorage(
_manyVariables
);
}
function saveDetailsToStorage(
manyVariables calldata _manyVariables
)
internal
{
// Mock function
// skip implementation
}
}
@rachmaninovquartet
Copy link
Author

The thing that stands out most to me in regards to architecture is the callability of methods. I think OZ onlyOwner or RBAC would be used in real life. No events are emitted so this would not be easily visible from a microservice when a transaction is processed. There's some typos and the or requires should probably be condensed and potentially use errors instead of strings.
The last contract has business logic that could be potentially be improved to better sanitize the input. Also the details that are saved don's have any associated getter, so they'll be saved but inaccessible.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment