I’m looking to create 3 smart contracts. A Factory contract, a Token contract and a Dex (decentralised exchange) contract. The way it should work is that the Factory contract will have a function which when invoked will deploy a new token and a new Dex - where the Token, in its constructor, will call the mint function and deposit some tokens to the Dex address to facilitate its trading. So the steps are:
- Deploy the Factory contract
- Call the
createTokenAndDex
function, on the Factory contract, to deploy the Token contract and Dex contract. - On the Token contract constructor, it will call the
_mint
to deposit tokens to the Dex address (i.e. deposit the Dex an initial supply of tokens as soon as it’s created).
The challenge I have is in the createTokenAndDex
function, the Dex constructor is called, to create a new Dex, followed by the Token constructor, to create a new token. The problem here is the Token constructor will need the Dex address as an input, to pass to the _mint
function (inside the Token constructor), to be able to deposit the Dex with some Tokens. But the Dex address will not be available in time (to pass as an argument to the Token constructor), as the Dex contract deployment on the Ethereum network will take time. My initial ideal solution is to find some sort of Async/Await call-back type functionality, and I’m ready to be corrected here, but I couldn’t find such a built-in functionality in Solidity.
Please refer to my code snippet (below). Apologies in advance for the number of lines of code purely dedicated to storing various data in arrays and mappings. Even though these lines of code seem irrelevant to include for this question, I intentionally included them to demonstrate the importance of having both the Token constructor and Dex constructor called in the same function (createTokenAndDex
function) - i.e. because I need both the Dex and Token data linked to each other via mappings, when stored, in order to query them easily later on.
Also, please note I only included the createTokenAndDex
function from the Factory contract, in the code snippet for the sake of brevity.
Code to reproduce
Factory contract:
function createTokenAndDex(string tokenName, string tokenTicker,
uint initialSupply, uint initialPrice, bool isActive) public {
// Deploy the Dex
address newDexAddress = new Dex(initialPrice, isActive);
// Deploy the Token - the problem here is the `newDexAddress` is still not created yet, to pass to the Token constructor (need Async/Await ideally in Solidity)
address newTokenAddress = new Token(tokenName, tokenTicker, initialSupply, newDexAddress);
// Now store the Token info in various forms to provide various options for querying
DeployedTokenStruct memory newDeployedTokenStruct = DeployedTokenStruct({
tokenName: tokenName,
tokenTicker: tokenTicker,
initialSupply: initialSupply,
initialPrice: initialPrice,
});
mapTokenNameToTokenAddress[tokenName] = newTokenAddress;
mapTokenAddressToTokenStruct[newTokenAddress] = newDeployedTokenStruct;
mapTokenNameToTokenStruct[tokenName] = newDeployedTokenStruct;
deployedTokensAddr.push(newTokenAddress);
deployedTokensStruct.push(newDeployedTokenStruct);
// Now store the Dex info in various forms to provide various options for querying
deployedDexs.push(newDexAddress);
mapTokenAddrToDexAddr[newTokenAddress] = newDexAddress;
mapTokenNameToDexAddr[tokenName] = newDexAddress;
mapDexAddrToTokenStruct[newDexAddress] = newDeployedTokenStruct;
}
Token contract:
contract Token is ERC20, ERC20Detailed, ERC20Burnable, ERC20Mintable, ERC20Pausable {
function Token(tokenName, tokenTicker, initialSupply, dexAddress) ERC20Detailed(tokenName, tokenTicker, 18) internal {
_mint(dexAddress, 1000);
}
Environment
Visual Studio Code, Truffle-hdwallet-provider.