One simple advice for all exchanges

in crypto •  7 days ago 

Making the MEMO field non-numeric is an excellent way to reduce the likelihood of accidental swaps with the amount field. Here's a general refactor strategy:

Validation Rules for MEMO

  • Restrict MEMO to alphanumeric characters or specific formats (e.g., UUID, prefixed strings like "REF-123").
  • Reject purely numeric values during validation.

UI/UX Improvements

  • Add placeholders or labels to clarify the difference between MEMO and amount fields (e.g., "Enter Reference Code" for MEMO and "Enter Amount" for amount).
  • Highlight invalid input in real-time.

Back-End Changes

  • Update input validation logic in your back-end API or contract logic to enforce non-numeric MEMO.
  • Add error handling to return clear messages if users enter a numeric MEMO.

Example

Frontend:

function validateMemo(memo) {
    const isNonNumeric = /^[A-Za-z][A-Za-z0-9_-]*$/.test(memo); // Alphanumeric, must start with a letter
    return isNonNumeric;
}

// Example Usage
const memoInput = "REF-12345";
if (!validateMemo(memoInput)) {
    console.error("Invalid MEMO: Must not be purely numeric.");
}

Backend:

def validate_memo(memo: str):
    if memo.isnumeric():
        raise ValueError("MEMO must not be numeric. Please use an alphanumeric format.")
    return True

# Example Usage
try:
    validate_memo("12345")
except ValueError as e:
    print(e)  # Output: MEMO must not be numeric. Please use an alphanumeric format.

Smart Contract:

require(bytes(memo).length > 0, "MEMO cannot be empty");
require(!isNumeric(memo), "MEMO cannot be numeric");

function isNumeric(string memory str) private pure returns (bool) {
    bytes memory b = bytes(str);
    for (uint i; i < b.length; i++) {
        if (b[i] < 0x30 || b[i] > 0x39) return false;
    }
    return true;
}

By combining front-end, back-end, and smart contract validations, you can ensure the MEMO field remains distinct and avoids confusion with the amount.

Steem to the Moon🚀!

Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE BLURT!