Skip to content

Latest commit

 

History

History
118 lines (80 loc) · 2.49 KB

README.md

File metadata and controls

118 lines (80 loc) · 2.49 KB

SmartPy Utilities

Usage

import smartpy as sp

Utils = sp.io.import_script_from_url("https://raw.githubusercontent.com/RomarQ/tezos-sc-utils/main/smartpy/utils.py")

class Contract(sp.Contract):

    @sp.entry_point
    def test(self):
        # Converts an int to a string (fails if invalid)
        sp.verify(Utils.String.of_int(-1) == "-1")
        # Converts a string to an int (fails if invalid)
        sp.verify(Utils.Int.of_string("1") == 1)
        # Splits a string
        sp.verify_equal(Utils.String.split("a b", " "), ["a","b"])
        # Checks if a string starts with a given substring
        sp.verify(Utils.String.starts_with("abc", "a") == True)
        # Checks if a string ends with a given substring
        sp.verify(Utils.String.ends_with("abc", "c") == True)
        # Computes the median
        sp.verify(Utils.Math.median([1, 2, 3, 4, 5]) == 3)

Documentation

  • Bytes

    • pad_start(bytes, fill, length)

      Utils.Bytes.pad_start(sp.bytes("0x11"), sp.bytes("0x00"), 3) # "0x000011"
    • pad_end(bytes, fill, length)

      Utils.Bytes.pad_end(sp.bytes("0x11"), sp.bytes("0x00"), 3) # "0x110000"
    • of_string(str)

      Utils.Bytes.of_string("TEST_STRING") # "0x544553545f535452494e47"
    • of_nat(number)

      Utils.Bytes.of_nat(56) # "0x36"
  • String

    • of_bytes(bytes)

      Utils.String.of_bytes(sp.bytes("0x544553545f535452494e47")) # "TEST_STRING"
    • of_int(number)

      Utils.String.of_int(1) # "1"
    • split(str, token)

      Utils.String.split("a b", " ") # ["a","b"]
    • starts_with(str, substr)

      Utils.String.starts_with("ab", "a") # True
    • ends_with(str, substr)

      Utils.String.ends_with("ab", "b") # True
  • Int

    • of_string(str)

      Utils.Int.of_string("1") # 1
    • of_bytes(b)

      Utils.Int.of_bytes(sp.bytes("0x0100")) # 256
  • Address

    • is_kt1(address)

      Utils.Address.is_kt1(sp.address("tz28QJHLyqvaY2rXAoFZTbxrXeD88NA8wscC")) # False
      Utils.Address.is_kt1(sp.address("KT18hYjnko76SBVv6TaCT4kU6B32mJk6JWLZ")) # True
  • Math

    • median(list)

      Utils.Math.median([1, 2, 3, 4, 5]) # 3