Fixed length strings vs bytes5 in solidity

Hello, I have some strings of 3 to 5 characters in my smart contract. I have defined function signatures and variables using string and it was fine, eg

function deposit(string memory _name, uint256 _amount) internal { ... }

I also created chai test invoking the functions in my contract passing strings as arguments to the calls and also was fine. No issues:

    await contract.deposit('John', 10);

This works perfectly.

My concern is that I know all my string values are 3 to 5 characters long. I have replaced the functions signature of my contract by bytes5 as:

function deposit(bytes5 _name, uint256 _amount) internal { ... }

and this also compiles ok.

What i do not know is:

  • is it worth to use bytes5 instead string in function signatures and variables in terms of gas / contract size?
  • how to pass the values of bytes5 in the chai tests? If I maintain the same call
  await contract.deposit('John', 10);

I get the following error.

Error: invalid arrayify value (argument=“value”, value=“John”, code=INVALID_ARGUMENT, version=bytes/5.6.1)

so the test does not work anymore.

Thanks

This seems to work

let stringToBytes5 = function (str: string) {
	return ethers.utils.hexZeroPad(ethers.utils.toUtf8Bytes(str), 5);
}
let bytes5ToString = function (hexString: string) {
	return ethers.utils.toUtf8String(hexString);
}
2 Likes