Update: The smart contract management interface is now accessible to all TokenGen users and it is the far easier way to interact with your deployed smart contract. For more info, go through the interactive tutorial for the Manage Project page on the official TokenGen website.
As mentioned in a previous article, we are developing a management interface that will make it very easy for TokenGen users to interact with their deployed smart contracts. Until then however, we wanted to write a detailed guide on how the interaction can be done performed at the moment.
Interacting with Deployed Smart Contracts
While the management interface we have planned will make the interaction process extremely simple, currently, the way to interact with your smart contract is not anything difficult either.
It just requires you to know a few things about smart contracts in general and understand the structure of the package generated by TokenGen. So let’s first lay the fundamental knowledge required in order for you to understand how the interaction process works.
The TokenGen Smart Contracts
Depending on the selected features, TokenGen will create up to five separate smart contracts when you deploy the bytecode:
- Fundraiser – this is the smart contract that deals with the funds and the creation and allocation of new tokens to participants.
- Token – this is the token contract itself.
- Token safe – if the token safe feature is selected, TokenGen will create a token safe contract where the specified amount of tokens will be locked until their release date.
- Whitelist – if the whitelistable fundraiser feature is selected, TokenGen will create a whitelist contract, which will allow the owner of the contract to whitelist/blacklist addresses of participants in the fundraiser.
- Refund safe – if the refundable fundraiser feature is selected, TokenGen will create a refund safe contract where all funds gathered in the fundraiser will be kept and later distributed to the beneficiary address or refunded to participants if the soft cap was not reached.
Those are the five separate smart contracts that TokenGen creates. Of course, if some of the features mentioned above are not selected, the related smart contract will not be there.
Interacting with a Smart Contract via MyEtherWallet & MetaMask
This is by far the simplest, fastest and most secure way to do it in my opinion. All you need to do is go to the Contracts page of MEW and fill in the required details:
Now, I have created a dummy smart contract via TokenGen (deployed on Ropsten) and have selected all features so I can show you how you can interact with each of the five separate smart contracts.
Interacting with the Fundraiser Smart Contract
Let’s first interact with the fundraiser smart contract. I have already filled in the details in the above screenshot, but let me clarify where I got the ABI from.
First of all, the ABI (application binary interface), in the context of Ethereum smart contracts, is quite simply the skeleton depicting the methods and variables that a certain smart contract has. Since there is no way for MEW to know what functions and variables a smart contract has based on the address provided, it needs the ABI so it can fill the “Select a function” dropdown with all of the methods and variables of the smart contract you are trying to interact with.
Once we fill-in the contract address and the ABI, which I got from the root of the TokenGen smart contract package (ExampleTokenFundraiser.abi.json), we click the Access button and we can now see all methods and variables available at our disposal. The functions that a user would usually need to call are all documented on the official TokenGen documentation page.
So for instance, if I want to change the beneficiary address, I can simply call the “setBeneficiary” method with the new beneficiary address as a parameter, connect to MetaMask, and then execute the transaction. Simple as that:
Once the transaction is confirmed, the address I provided will now be the new beneficiary of the fundraiser. So, once the fundraiser is concluded, all funds will be sent to the new beneficiary address I just set:
As you can see, the transaction is now confirmed and if we go back to MEW and check the “beneficiary” variable of the fundraiser contract, we will see that it is now set to the address we provided:
It is important to note here that all of the TokenGen smart contracts except for the token safe have owner accounts and so, you can execute administrative functions on them (such as “setBeneficiary”) only from the owner account. So make sure you have selected that same account from MetaMask when you connect it to MEW.
Another important thing to note here is the fact that, usually, if you try to do something that is not right i.e. try and call the “setBeneficiary” method from an address that is not the owner, MEW will not automatically fill-in the gas limit and MetaMask will also usually issue a warning that the transaction will most likely fail due to execution code.
Now, from the fundraiser contract, we can find all of the other four smart contracts as their addresses are saved as simple variables. Let’s start with the token.
Interacting with the Token Smart Contract
All we need to do in order to find the address of the token smart contract (remember that we need it so we can interact with it via MEW) is to select the “token” variable from the fundraiser contract:
And there it is. Now we open another MEW tab and we again go to the Contracts page. And we follow the same steps as with the fundraiser contract interaction. However, this time, we will use the ExampleToken.abi.json from the root of the TokenGen smart contract package. That is the ABI of the token contract:
And there you have it. We now have access to the token contract. From here, we can perform different tasks on the token itself such as pause/unpause transfers. But mostly, this contract contains information about the token you have created i.e. name, symbol, decimals, etc.
Interacting with the Token Safe Smart Contract
Again, the steps are identical to the way we found the token contract address. This time however, we select the “tokenSafe” variable from the fundraiser contract.
We open up another MEW tab and go to the Contracts page. We paste the address of the token safe contract and we find its ABI from the TokenGen smart contract package in the artifacts folder in the TokenSafe.json file. We copy the value of the “abi” variable in that file i.e. from “[” to “]” and we paste into MEW:
And there you have it. We are now interacting with the token safe smart contract. The token safe contract is actually quite simple – it only has one variable and one method.
The “groups” variable simply shows you all groups of addresses which have locked tokens in the safe i.e. core team, advisors, etc. Then there’s the “release” method which allows people to unlock their tokens once the release date is reached. That’s it.
On a quick side note, we will be introducing a small update to TokenGen soon, which will provide, in the root of the smart contract package, the necessary ABI files for all of the five smart contracts (if the relevant features are selected). Currently, only the ABIs for the token and fundraiser smart contracts are generated there while the rest can be found in the artifacts folder.
Interacting with the Whitelist Smart Contract
Similar to the token safe contract, we can interact with the whitelist smart contract. We get its address from the fundraiser contract’s “whitelist” variable and we get the ABI from the Whitelist.json file in the artifacts folder – again we copy the value of the “abi” variable in that file.
The whitelist contract is quite simple as well. Basically, what you need to do with it is use the “add” and “remove” methods (mostly) and then check if a certain address is whitelisted via the “whitelisted” variable.
For instance, if I want to whitelist a few addresses so they can participate in my fundraiser, I have to simply call the “add” method and provide a comma-separated list of the addresses, which I want to whitelist:
Again, I connect to MetaMask, making sure that I have selected the owner address, and then I execute the transaction. Once confirmed, I can go back to the MEW and check if all is as expected:
As you can see, one of the addresses I just whitelisted is now allowed to participate in the fundraiser. And if later, for whatever reason, I want to remove access to the fundraiser from that same address, I can simply call the “remove” method. That’s basically all there is to the whitelist contract.
Interacting with the Refund Safe Smart Contract
Again, completely similar to the token safe and whitelist contracts, we get access to the refund safe smart contract. Usually, you don’t really need to interact with the refund safe contract at all. If your fundraiser is successful i.e. you have reached your soft cap, then you can simply call the “finalize” method of the fundraiser smart contract and it will send all funds from the refund safe to the beneficiary address.
If however, the soft cap is not reached for the duration of the fundraiser, then participants will be able to interact with the refund safe contract and call the “refund” method to get their contributions back. That’s pretty much all there is to the refund safe smart contract.
In this article we showed how easy it is to interact with the contracts generated by TokenGen by using MyEtherWallet and MetaMask. Until we release the management interface, the above depicted method is the best way to go about interacting with your smart contracts.
If you have any questions regarding the use of TokenGen, please feel free to contact us via the TokenGen contact page. We have already set our eyes on the next features we will introduce into the tool as we are looking to turn it into a kind of all-in-one dashboard for token generation events.