axelar-cgp-sui

Module 0xa5::utils

use 0x1::ascii;
use 0x1::vector;
use 0x2::address;
use 0x2::hash;

Constants

const ALPHABET_LENGTH: u8 = 26;

const LOWERCASE_START: u8 = 97;

const NUMBERS_LENGTH: u8 = 10;

const NUMBERS_START: u8 = 48;

const SPACE: u8 = 32;

const UNDERSCORE: u8 = 95;

const UPPERCASE_START: u8 = 65;

Function is_lowercase

entry fun is_lowercase(c: u8): bool
Implementation
entry fun is_lowercase(c: u8): bool {
    c >= LOWERCASE_START && c < LOWERCASE_START + ALPHABET_LENGTH
}

Function is_uppercase

entry fun is_uppercase(c: u8): bool
Implementation
entry fun is_uppercase(c: u8): bool {
    c >= UPPERCASE_START && c < UPPERCASE_START + ALPHABET_LENGTH
}

Function is_number

entry fun is_number(c: u8): bool
Implementation
entry fun is_number(c: u8): bool {
    c >= NUMBERS_START && c < NUMBERS_START + NUMBERS_LENGTH
}

Function module_from_symbol

public(friend) fun module_from_symbol(symbol: &ascii::String): ascii::String
Implementation
public(package) fun module_from_symbol(symbol: &ascii::String): ascii::String {
    let symbolBytes = ascii::as_bytes(symbol);
    let mut moduleName = vector[];

    let (mut i, length) = (0, vector::length(symbolBytes));
    while (is_number(*vector::borrow(symbolBytes, i))) {
        i = i + 1;
    };
    while (i < length) {
        let b = *vector::borrow(symbolBytes, i);
        if (is_lowercase(b) || is_number(b)) {
            moduleName.push_back(b);
        } else if (is_uppercase(b)) {
            moduleName.push_back(b - UPPERCASE_START + LOWERCASE_START);
        } else if (b == UNDERSCORE || b == SPACE) {
            moduleName.push_back(UNDERSCORE);
        };

        i = i + 1;
    };
    ascii::string(moduleName)
}

Function hash_coin_info

public(friend) fun hash_coin_info(symbol: &ascii::String, decimals: &u8): address
Implementation
public(package) fun hash_coin_info(
    symbol: &ascii::String,
    decimals: &u8,
): address {
    let mut v = vector[*decimals];
    v.append(*symbol.as_bytes());
    address::from_bytes(keccak256(&v))
}

Function decode_metadata

public(friend) fun decode_metadata(metadata: vector<u8>): (u32, vector<u8>)
Implementation
public(package) fun decode_metadata(
    mut metadata: vector<u8>,
): (u32, vector<u8>) {
    if (metadata.length() < 4) {
        (0, vector[])
    } else {
        let mut i = 0;
        let mut version: u32 = 0;
        while (i < 4) {
            version =
                (version << (8 as u8) as u32) + (metadata.remove<u8>(0) as u32);
            i = i + 1;
        };

        (version, metadata)
    }
}

Function pow

public(friend) fun pow(base: u256, exponent: u8): u256
Implementation
public(package) fun pow(mut base: u256, mut exponent: u8): u256 {
    let mut res: u256 = 1;
    while (exponent > 0) {
        if (exponent % 2 == 0) {
            base = base * base;
            exponent = exponent / 2;
        } else {
            res = res * base;
            exponent = exponent - 1;
        }
    };
    res
}