How to take symbol like USD as bytes32 in solidity?

help needed.

mapping(bytes32 => address) tokenSymboltoAddress;

  1. The function saves tokenAddress on the tokenSymbol key in a mapping.
function addAddresses(bytes32 _name, address _tokenAddress ) {

tokenSymboltoAddress[bytes32] = _tokenAddress;

}
  1. Returns the address added in the mapping on the given token symbol.
function getAddress(bytes32 name) returns (address) {
	return tokenSymboltoAddress[name];
}

Can someone help me how to write or run these functions. How do we write symbol as bytes32 in function argument?

Can we convert bytes32 to string in solidity ?

Hi, @waqaswahid1,

You could use the following function to convert to string –

function bytes32ToString(bytes32 _bytes32) public pure returns (string memory) {
        uint8 i = 0;
        while(i < 32 && _bytes32[i] != 0) {
            i++;
        }
        bytes memory bytesArray = new bytes(i);
        for (i = 0; i < 32 && _bytes32[i] != 0; i++) {
            bytesArray[i] = _bytes32[i];
        }
        return string(bytesArray);
    }

Source of this answer – https://ethereum.stackexchange.com/questions/2519/how-to-convert-a-bytes32-to-string/2834

Hope this helps.

    const name = "Billy";
    useApiContract({
		address,
		abi,
		functionName: 'getAddress',
		params: {
			name: ethers.utils.formatBytes32String(name)
		},
	})

Is there a bytes32 string converter in moralis? In order not to connect ethers.js.