I'd like to save the contract addresses from DAI and UNI into a mapping when calling the smart contract constructor depending on the network. Below is a minimalistic example that works fine:
contract TokenManager {
mapping(string => address) public contractERC20;
address private ZERO_ADDRESS = 0x0000000000000000000000000000000000000000;
// Contract constructor
constructor(string memory network) public {
if (compareStrings(network, "ropsten")) {
registerERC20("DAI", 0xaD6D458402F60fD3Bd25163575031ACDce07538D);
registerERC20("UNI", 0x1f9840a85d5aF5bf1D1762F925BDADdC4201F984);
}
}
// Compare two strings
function compareStrings(string memory s1, string memory s2) private pure returns(bool){
return keccak256(abi.encodePacked(s1)) == keccak256(abi.encodePacked(s2));
}
// Register a ERC20 contract address
function registerERC20(string memory _tokenName, address _tokenContract) public {
require(bytes(_tokenName).length != 0,
"Token name can't be empty");
require(contractERC20[_tokenName] == ZERO_ADDRESS,
"ERC20 contract address already registered");
contractERC20[_tokenName] = _tokenContract;
}
// ...
}
However, I believe this is not the best approach since the addresses are hardcoded in the contract, and the full list of tokens multiplied by the number of networks can be big. In addition, it would be better to choose the tokens to be created directly from a migrations file in Truffle (for instance), instead of updating the contract for every deployment.
I was considering passing an array of objects to the constructor with all the addresses, so that the contract loops through this array and assigns each address, but didn't find a way to send such array of objects/mappings to the constructor from Truffle.
Therefore, is there a better way to handle this assignment of addresses during the contract deployment?