diff --git a/macros/core/functions.py.sql b/macros/core/functions.py.sql index 3e6c389..8474cb4 100644 --- a/macros/core/functions.py.sql +++ b/macros/core/functions.py.sql @@ -229,4 +229,53 @@ def transform_hex_to_bech32(hex, hrp=''): return hrp + '1' + ''.join([CHARSET[d] for d in data5bit + checksum]) +{% endmacro %} + +{% macro create_udf_int_to_binary() %} + +def int_to_binary(num): + num = int(num) + is_negative = num < 0 + if is_negative: + num = -num + + binary_string = bin(num)[2:] + + if is_negative: + inverted_string = "".join("1" if bit == "0" else "0" for bit in binary_string) + + carry = 1 + result = "" + for i in range(len(inverted_string) - 1, -1, -1): + if inverted_string[i] == "1" and carry == 1: + result = "0" + result + elif inverted_string[i] == "0" and carry == 1: + result = "1" + result + carry = 0 + else: + result = inverted_string[i] + result + + binary_string = result + + return binary_string + +{% endmacro %} + +{% macro create_udf_binary_to_int() %} + +def binary_to_int(binary): + + for char in binary: + if char not in "01": + raise ValueError("Input string must be a valid binary string.") + + integer = 0 + + for i, digit in enumerate(binary[::-1]): + digit_int = int(digit) + + integer += digit_int * 2**i + + return str(integer) + {% endmacro %} \ No newline at end of file diff --git a/macros/core/utils.yaml.sql b/macros/core/utils.yaml.sql index 2f6b71b..a2a9774 100644 --- a/macros/core/utils.yaml.sql +++ b/macros/core/utils.yaml.sql @@ -272,4 +272,26 @@ sql: | {{ create_udf_hex_to_bech32() | indent(4) }} +- name: {{ schema }}.udf_int_to_binary + signature: + - [num, STRING] + return_type: TEXT + options: | + LANGUAGE PYTHON + RUNTIME_VERSION = '3.8' + HANDLER = 'int_to_binary' + sql: | + {{ create_udf_int_to_binary() | indent(4) }} + +- name: {{ schema }}.udf_binary_to_int + signature: + - [binary, STRING] + return_type: TEXT + options: | + LANGUAGE PYTHON + RUNTIME_VERSION = '3.8' + HANDLER = 'binary_to_int' + sql: | + {{ create_udf_binary_to_int() | indent(4) }} + {% endmacro %} \ No newline at end of file diff --git a/models/deploy/core/utils.yml b/models/deploy/core/utils.yml index c0fdd47..659b025 100644 --- a/models/deploy/core/utils.yml +++ b/models/deploy/core/utils.yml @@ -197,6 +197,34 @@ models: [['array', [0, 1, 2]]] assertions: - result = 'array=%5B0%2C+1%2C+2%5D' + - name: udf_int_to_binary + tests: + - test_udf: + name: test_utils__udf_int_to_binary + args: 123456789 + assertions: + - result = '111010110111100110100010101' + - name: udf_int_to_binary + tests: + - test_udf: + name: test_utils__udf_int_to_binary_large_number + args: "'123456789123456789123456789123456789123456789'" + assertions: + - result = '101100010010011011011100101001111010001001110011010111111101111101010111011001001101000001111110001010100001011011010000100000001000101111100010101' + - name: udf_binary_to_int + tests: + - test_udf: + name: test_utils__udf_binary_to_int + args: '111010110111100110100010101' + assertions: + - result = '123456789' + - name: udf_binary_to_int + tests: + - test_udf: + name: test_utils__udf_binary_to_int_large_number + args: "'110110110100110110100101110101100110100000101111100010101'" + assertions: + - result = '123456789123456789' - name: udf_evm_decode_log tests: - test_udf: