Fix Truffle REACT Project for Smart Contracts with MetaMask and Solidity
Now, grabbing the react sample from truffle (yes, you need GIT installed), trying to run the normal truffle develop, compile, migrate, you’ll run into a bit of an issue… You’re likely running a too new version of Solidity (as you should, security is vital) than what your Smart contract is set up to handle and if you’re not using and defining the same version of Solidity, you won’t be doing much of anything.
Installation (Taken & Modified from Github Repo)
- Install Truffle globally.
npm install -g truffle
- Download the box. This also takes care of installing the necessary dependencies.
truffle unbox react-auth
- Run the development console.
- Compile and migrate the smart contracts.
- Run the webpack server for front-end hot reloading (outside the development console). Smart contract changes must be manually recompiled and migrated.
npm run start
- Once this is running, let’s go forth and run the truffle test.
truffle test (if outside of truffle)
Now, this will NOT work if you run the latest and greatest of NPM, Node, Truffle, as it’s irrelevant in a way as the issue here is, your Smart Contract is requiring a specific version of Solidity. Depending on your version (as of writing likely to be 0.5.16).
What do we need to modify first? Let us go for the Truffle Test file, TestSimpleStorage.sol (/project-folder/test/TestSimpleStorage.sol) and using nano (or vi), open it like so:
At the top of the file, look for the pragma solidity definition and modify it so it shows as follows:
pragma solidity ^0.5.16
Smart Contract Files
Now that we modified the Test file, we need to modify the most important files, the smart contract files themselves that define the version of solidity required to even run.
Make sure you modify the contract Migrations file by doing the following:
Now, modify the pragma again, so it reflects as follows:
pragma solidity ^0.5.16;
After saving it, we’ll need to open the SimpleStorage.sol file.
After you’ve opened this file, look again at the top and change the pragma again to be:
pragma solidity ^0.5.16;
So you’ve done it, you modified all the needed files to get your smart contract working, well the react GitHub project. To some, this may have been a no-brainer but to several folks I have spoken with, it wasn’t an obvious thing for them, being new to Truffle and Smart Contracts in general. Happy contracting!