Summary: In the last section, we introduced a smart contract development example. In this section, we will take a closer look at the Lib libraries commonly used in smart contract development. Due to the length of the article, we will cover the tool library in two parts. This article will cover LibInt and LibString.

  • Introduction to common Libraries

In the contract development specification, we see a subdirectory, utillib, under the contracts directory, which is the common tool library provided by the JUICE open service platform. These libraries provide methods that are business-related. It works like the various Util tools in JAVA. Commonly used are:

1. LibInt encapsulation for integer operations (support direct call, using calls) https://open.juzix.net/api_doc/contract/utillib/LibInt.html

2. LibString encapsulation for string operations (support direct call, using calls) https://open.juzix.net/api_doc/contract/utillib/LibString.html

3. LibJson encapsulation of the JSON format string manipulation (support direct call, using calls) https://open.juzix.net/api_doc/contract/utillib/LibJson.html

4. LibStack encapsulates the use of the stack (only supports direct calls) https://open.juzix.net/api_doc/contract/utillib/LibStack.html

5. LibLog encapsulation log operation (only supports direct calls) https://open.juzix.net/api_doc/contract/utillib/LibLog.html

6. Use method to insert library file in the header of business contract:

Pragma solidity ^ 0.4.2; import"./utillib/LibLog.sol";
Copy
Copy the code

Call directly in the contract:

function myLogger() constant public returns(bool _out) {
        LibLog.log("here is my logger message"); . . _out =true;
}
Copy
Copy the code

Using in a contract:

Pragma solidity ^ 0.4.2; import"./utillib/LibString.sol";

contract StringTest is OwnerNamed {
        using LibString for*;function myString() constant public returns(bool _out) {
                LibLog.log("here is my string test");

                string memory _string = "hello world"; //using bool result = _string.compare("hello world"); //bool result = LiString.compare(_string,"hello world"); _out = result ; }}Copy the code

Note:

LibJson supports both direct and using calls, but the using calls are slightly different. See the LibJson library for details. https://open.juzix.net/api_doc/contract/utillib/LibJson.html

The following examples will all use using for *; Method to call

LibInt

LibInt mainly encapsulates a series of operations for shaping;

Support direct calls, using for *; call

Uint is converted to a string

Description: Converts an integer to a string of specified length

Structure definition

function toString(uint _self, uint width) internal returns (string _ret);
Copy the code

The sample

uint _uint = 1000;
string memory _ret= _uint.toString(3);        //  _ret = 100
Copy the code

Uint to a string

Description: The uint type is changed to string

Structure definition

function toString(uint _self) internal returns (string _ret) ;
Copy the code

The sample

uint _uint = 1000;
string memory _ret= _uint.toString();        //  _ret = 1000
Copy the code

Uint to hexadecimal

Description: uint Data converted to a hexadecimal string

Structure definition

function toHexString(uint _self) internal returns (string _ret) ;
Copy the code

The sample

uint _uint = 1000;
string memory _ret= _uint.toHexString();        //  _ret = 0x3e8
Copy the code

Uint to hexadecimal (32 bytes)

Description: Convert each byte of a uint to a hexadecimal string. A uint is 32 bytes and requires 64 characters to represent it

Structure definition

function toHexString64(uint _self) internal returns (string _ret) ;
Copy the code

The sample

uint _uint = 1000;
string memory _ret= _uint.toHexString64();        //  _ret = 0x0000000000000000000000000000000000000000000000000000000003e8
Copy the code

Int to string

Description: Converted from int to string

Structure definition

function toString(int _self) internal returns (string _ret);
Copy the code

The sample

int _int = 1000;
string memory _ret= _uint.toString();        //  _ret = "1000"
Copy the code

Uint (address value) to string

Description: Uint Transfer address a character string

Structure definition

function toAddrString(uint _self) internal returns (string _ret);
Copy the code

The sample

address _address =0x8affd1952705d8a908e016695c6e454ad39a1c6f ;
uint _uint = uint(_address);
string memory _ret= _uint.toAddrString();        //  _ret = "0x8affd1952705d8a908e016695c6e454ad39a1c6f"
Copy the code

Uint Transfer key pair k-V

Description: Uint is converted to a k-V key-value pair

Structure definition

function toKeyValue(uint _self, string _key) internal returns (string _ret);
Copy the code

The sample

uint _uint = 1000;
string memory _ret= _uint.toKeyValue("key");        
//  -> _ret = "key" : 1000
Copy the code

Int key pair k-v

Description: Int converted to a k-V key-value pair

Structure definition

function toKeyValue(int _self, string _key) internal returns (string _ret);
Copy the code

The sample

int _int = 1000;
string memory _ret= _int.toKeyValue("key");        
//  -> _ret = "key" : 1000
Copy the code

Addrerss transfer key value pair k- V

Description: The address type is converted to a k-V key-value pair

Structure definition

function toKeyValue(address _self, string _key) internal returns (string _ret);
Copy the code

The sample

address _address =0x8affd1952705d8a908e016695c6e454ad39a1c6f ;
string memory _ret= _address.toKeyValue("key");        
//  -> _ret = "key" : "0x8affd1952705d8a908e016695c6e454ad39a1c6f"
Copy the code

Uint Memory data copy

This method is mainly because strings cannot be used for inter-contract calls, so characters are passed as 32-byte integers

Description: Uint memory data, aligned 32 bytes to the right, copied to an integer

If you want to pass a string of less than 32 bytes, you align the memory of the string to the right, store it in the integer, and then return it to the string

Structure definition

functionRecoveryToString (uint _self) Internal returns (string _ret);Copy the code

The sample

uint _uint = 1000;
string memory _ret= _uint.recoveryToString();
Copy the code

Uint [] Array judgment

Description: Check whether an element of type uint exists in an array of type uint[]. Note that the type of uint[] must be storage.

Structure definition

function inArray(uint _self,uint[] storage _array) internal returns (bool _ret);
Copy
Copy the code

The sample

Pragma solidity ^ 0.4.2; import"LibInt.sol";

contract TestManager {

    using LibInt for *;

    uint[] public _arruint;

    function test(uint _uint) constant returns(string _ret) {
             // if exists , isExists = true, / /if not ,isExists = false; bool isExists = _uint.inArray(_arruint); }}Copy the code

The following examples will all use the library library using for *; Method to call

LibString

LibString encapsulates a series of general operations on strings;

Support direct calls, using for *; call

String comparison

Description: Compares two strings for equality

Structure definition

function equals(string _self, string _str) internal returns (bool _ret);
Copy the code

The sample

string memory _str1 = "1000";
string memory _str2 = "200";
bool _ret= _str1.equal(_str2);        //  _ret = falses
Copy the code

String comparison (ignoring case)

Description: Compares two strings for equality

Structure definition

function equals(string _self, string _str) internal returns (bool _ret);
Copy the code

The sample

string memory _str1 = "ABC";
string memory _str2 = "abc";
bool _ret= _str1.equalsNoCase(_str2);        //  _ret = true
Copy the code

String interception

Description: Intercepts a string of specified length

Structure definition

function substr(string _self, uint _start, uint _len) internal returns (string _ret);
Copy the code

The sample

string memory _str1 = "abcdefg"; String _ret = _str1. Substr (0, 3); // _ret ="abc"

Copy the code

String splicing

Description: Concatenated string

Structure definition

function concat(string _self, string _str) internal returns (string _ret)
Copy the code

The sample

string memory _str1 = "abcdefg";
string _ret= _str1.concat("123");        //  _ret = "abcdefg123"
Copy the code

String concatenation (multiple arguments)

Description: Concatenate string, can pass in more than one string to concatenate

Structure definition

function concat(string _self, string _str1, string _str2)internal returns (string _ret);

function concat(string _self, string _str1, string _str2,string _str3)internal returns (string _ret);
Copy the code

The sample

string memory _str1 = "abcdefg";
_str1 = _str1.concat("123"."456");        //  _str1 = "abcdefg123456"
_str1 = _str1.concat("A"."B"."C");         //  _str1 = "abcdefg123456ABC"
Copy the code

Remove string whitespace

Description: Remove string Spaces

Structure definition

function trim(string _self) internal returns (string _ret) ;
Copy the code

The sample

string memory _str1 = "abcdefg";
string _ret= _str1.trim("123");        //  _ret = "abcdefg123"
Copy the code

Removes the specified character of a string

Description: Removes the specified character of the string

Structure definition

function trim(string _self,string _chars) internal returns (string _ret) ;
Copy the code

The sample

string memory _str1 = "abcdefg";
string _ret= _str1.trim("a");        //  _ret = "bcdefg"
Copy the code

The string is cut into an array of specified characters

Description: The string is cut into arrays according to the specified characters. Note that the array type must be the state variable storage

Structure definition

function split(string _self, string _delim, string[] storage _array);
Copy the code

The sample

Pragma solidity ^ 0.4.2; import"LibString.sol";

contract TestManager {

    using LibString for *;

    striing[] public _arr;

    function test(uint _uint) constant returns(string _ret) {
        string memory _str = "A&B&C";
        _str.split("&",_arr);             // _arr = ["A"."B"."C"]}}Copy the code

Character index

Description: Finds the index position of the specified character in a string, starting at 0, or -1 if none exists

Structure definition

function indexOf(string _self, string _str) internal returns (int _ret);
Copy the code

The sample

string memory _str1 = "abcdefg";
int _ret= _str1.indexOf("b");        //  _ret = 1
Copy the code

Character index (specify subscript)

Description: Finds the index of the specified character in the string and specifies the starting position, starting at 0, or -1 if none exists

Structure definition

function indexOf(string _self, string _str,uint pos) internal returns (int _ret);
Copy the code

The sample

string memory _str1 = "abcdefg";
int _ret= _str1.indexOf("b", 0); // _ret = 1Copy the code

String to int

Description: String type to integer type

Structure definition

function toInt(string _self) internal returns (int _ret) ;
Copy the code

The sample

string memory _str1 = "1111";
int _ret= _str1.toInt();        //  _ret = 1111
Copy the code

String to address

Description: Change the string type to address type

Structure definition

function toAddress(string _self) internal returns (address _ret);
Copy the code

The sample

string memory _str1 = "0x8affd1952705d8a908e016695c6e454ad39a1c6f";
address _ret= _str1.toAddress();        //  _ret = 0x8affd1952705d8a908e016695c6e454ad39a1c6f
Copy the code

String to K-V key-value pair (memory type)

Description: String assembled into K-V key-value pairs

Structure definition

function toKeyValue(string _self, string _key) internal returns (string _ret)
Copy the code

The sample

string memory _str1 = "aaaa";
address _ret= _str1.toKeyValue("name");        //  _ret = "name":"aaaa"
Copy the code

String to K-V key-value pair (storage type)

Description: String assembled into K-V key-value pairs for storage type variables;

Note: the variable of life outside the function is the state variable storage;

Structure definition

function toKeyValue(string storage _self, string _key) internal returns (string _ret)
Copy the code

The sample

Pragma solidity ^ 0.4.2; import"LibString.sol";

contract TestManager {

    using LibString for *;

    string public _str;

    function test() constant returns(string _ret) {
         _str = "hello";
         _ret = _str.toKeyValue("name");             // _ret ="name":"hello"}}Copy the code

Characters are replaced by uint

Description: A string whose length is less than 32 bytes is represented by uint.

Structure definition

function storageToUint(string _self) internal returns (uint _ret);
Copy the code

The sample

string memory _str1 = "aaaa";
uint _ret= _str1.storageToUint();        //  _ret =
Copy the code

Checks if the string is in an array

Description: Determines whether a string is in a specified array.

Structure definition

function inArray(string _self, string[] storage _array) internal returns (bool _ret);
Copy the code

The sample

Pragma solidity ^ 0.4.2; import"LibString.sol";

contract TestManager {

    using LibString for *;

    string[] public _arr;

    function test() constant returns(bool _ret) {
        _arr.push("hello1");
        _arr.push("hello2");
         string memory _str = "hello1";
        _ret = _str.inArray(_arr);             // _ret = true}}Copy the code

Determines whether a string is in an array (case ignored)

Description: Determines whether a string is in a specified array, regardless of case;

Structure definition

function inArrayNoCase(string _self, string[] storage _array) internal returns (bool _ret);
Copy the code

The sample

Pragma solidity ^ 0.4.2; import"LibString.sol";

contract TestManager {

    using LibString for *;

    string[] public _arr;

    function test() constant returns(bool _ret) {
        _arr.push("Hello1");
        _arr.push("hello2");
         string memory _str = "hello1";
        _ret = _str.inArrayNoCase(_arr);             // _ret = true}}Copy the code

Uppercase string

Description: Represents all elements of a string in uppercase

Structure definition

function toUpper(string _self) internal returns (string _ret);
Copy the code

The sample

string memory _str1 = "aaaa";
string memory _ret= _str1.toUpper();        //  _ret = "AAAA"
Copy the code

String to lowercase

Description: Converts all characters to lowercase

Structure definition

function toLower(string _self) internal returns (string _ret);
Copy the code

The sample

string memory _str1 = "AAAA";
string memory _ret= _str1.toLower();        //  _ret = "aaaa"
Copy the code

The string type is uint

Description: Convert a string type to uint. The content of the string itself is uint. For example, “AA123” is not convertible, but “123” is convertible.

Structure definition

function toUint(string _self) internal returns (uint _ret);
Copy the code

The sample

string memory _str1 = "111";
uint _ret= _str1.toUint();        //  _ret = 111
Copy the code

The content of the reference: https://open.juzix.net/doc

Smart Contract Development Tutorial video: Introduction to Smart contracts in the blockchain video series