Coder Social home page Coder Social logo

soliditool-solidity-utils-library's Introduction

Soliditool— Solidity utils library

This library holds pre-made functions to make your life easier when coding your smart contract.

Overview

This library can help you keep your code cleaner and avoid rewriting code.

Functions in the library

Those are the functions available in this library:

compareStrings()

Function to compare 2 strings. Use the compareStrings function with 2 parameters, it will compare them and return a bool value.

For example:

compareStrings("blue", "red") will return false.
    function compareStrings(string memory firstString, string memory secondString) internal pure returns (bool) {
        return keccak256(bytes(firstString)) == keccak256(bytes(secondString));
    }

pseudoRandom()

Function to generate a pseudo-random number.

WARNING: This function does not return a real random value; this function is to be used for testing ONLY and not in production where a real random number is needed.

    function pseudoRandom() public view returns (uint) {
        bytes32 blockHash = blockhash(block.number - 1);
        return uint(keccak256(abi.encodePacked(block.timestamp, uint(blockHash))));
    }

findString()

Function to find the index matching a string in an array. This function takes a string as a parameter and loops through an array of strings. It then returns the index of the matching string (if it exists), or the 420 code if it does not exist.

    function findString(string[] memory array, string memory _string) internal pure returns (uint) {
        for (uint i = 0; i < array.length; i++) {
            string memory stringToFind = array[i];
            bool exists = Utils.compareStrings(stringToFind, _string);
            if (exists == true) {
                return i;
            }
        }
        return uint(420);
    }

findUint()

Function to find the index matching a uint in an array. This function takes a uint as a parameter and loops through an array of units. It then returns the index of the matching unit (if it exists) or the 90909090 code if it does not exist.

    function findUint(uint[] memory array, uint _number) internal pure returns (uint) {
        for (uint i = 0; i < array.length; i++) {
            uint uintToFind = array[i];
            if (_number == uintToFind) {
                return i;
            }
        }
        return uint(90909090);
    }

findInt()

Function to find the index matching an int in an array. This function takes an int as a parameter and loops through an array of ints. It then returns the index of the matching int (if it exists) or the 90909090 code if it does not exist.

    function findInt(int[] memory array, int _number) internal pure returns (uint) {
        for (uint i = 0; i < array.length; i++) {
            int intToFind = array[i];
            if (_number == intToFind) {
                return i;
            }
        }
        return uint(90909090);
    }

Getting started

Download the Soliditool package into your project by running:

npm install soliditool

Then import it into your smart contract adding this import statement at the top:

import "soliditool/contracts/Utils.sol";

Here is an example:

// SPDX-License-Identifier: MIT

pragma solidity ^0.8.0;

import "soliditool/contracts/Utils.sol";

contract TryUtils {

    string[] public colors = ["blue", "yellow", "red"];
    uint[] public numbers = [1,4,7];
    int[] public moreNumbers = [-3,99,-5343];

    address public owner;

    function compareStrings(string memory firstString, string memory secondString) public pure returns(bool) {
        return Utils.compareStrings(firstString, secondString);
    }
}

soliditool-solidity-utils-library's People

Contributors

soos3d avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.