# Truffle tips and hacks

By [N00b21337](https://paragraph.com/@n00b21337) · 2023-01-11

---

### Installing truffle globally, permission errors

If you are using Truffle and want to install it globally with NPM but having some permission errors like _permission denied, open '/root/.config/truffle/config.json'_  you can use unsafe and allow root parameters to achieve that, like with the line below

`sudo npm install -g truffle --unsafe-perm=true --allow-root`

### Enable optimizer for your truffle project (when contract is larger then 24kb)

You will need to update your truffle-config.js configuration file to include the following:

    module.exports = {
    ...
        solc: {
          settings: {
            optimizer: {
              enabled: true,
              runs: 1500
            }
          }
        }
    }
    

### Truffle deployment/migration snippets and helpers

To deploy contracts from start but not compile them we can use  `yarn truffle deploy --reset --compile-none --network live`

To make dry run and you dont want to deploy contracts `yarn truffle deploy --dry-run --reset --network live`

To reset migration state on network where you canceled transaction you need to reset it. This is because truffle remembers previous migrations you made but you are on a new test network. You can see registered contract addresses by running **truffle networks.**

You can delete these information by running **truffle networks --clean** before truffle migrate.

The easiest way to get compiler version with truffle is to find it in your project directory `./build/contracts/YourContractName.json` near the bottom of the file. It looks like

     "compiler": {
        "name": "solc",
        "version": "0.7.18+commit.9cf6e910.Emscripten.clang"
      }
    

### Upgrading truffle - unexpected error with YARN

I tried to upgrade truffle and install new version but I kept getting some kind of errors like

_An unexpected error occurred: "_[_https://registry.yarnpkg.com/truffle:_](https://registry.yarnpkg.com/truffle:) _getaddrinfo EAI\_AGAIN registry.yarnpkg.com"_

and just couldn't upgrade truffle to latest version. What helped is that I used **yarn cache clean** and then resolved the problems I was having. But some other solution would also be to change the registry of yarn. If you run command **yarn config get registry** you would probably get [https://registry.yarnpkg.com](https://registry.yarnpkg.com/) some people say they change it to  **yarn config set registry** [**https://registry.npmjs.org**](https://registry.npmjs.org/) and that helped with fetching proper repos.

### Check if contract is deployed via truffle

After running **truffle console --network rinkeby**  you can use below line to check if contract address is deployed, you would get bytecode returned if it is

`web3.eth.getCode("0x0de378AbDB252E1d63Cd3d956343E76640F720Ed", function(err, code) { console.log(code);})`

### Exiting truffle console JS script

When I run some script in truffle console with "exec" it would finish but would be stuck and no ctrl+d or +c could unstuck it, I needed to close the terminal window and reopen it. To solve this I needed to put **process.exit();** in a script so what I ended up with was something like this.

        const checkURI = await nftContract.methods.tokenURI(1).call(function(err, res){
            console.log(res);
            process.exit(0);
        });
    

### Truffle console calling contract functions on testnet or live

First to connect to network of your choice add flag to console **truffle console --network rinkeby** After that make an instance of deployed contract with **let instance = await myContract.deployed()** and then you can call functions like some getters or setters on contract **instance.baseTokenURI()**

You can also use console to get quick estimates of gas used for functions, so you run  **instance.lazyMintAdditional.estimateGas(600,700)** and you will get estimate for function lazyMintAdditional with parameters 600 and 700. or to get estimate of contract creation, you will need to run contract name with NEW method and constructor parameters in parenthesese like **Creature.new.estimateGas("0xf57b2c51ded3a29e6891aba85459d600256cf317")**

getting current price of Gas **web3.eth.getGasPrice(function(error, result){ console.log(result)})**

getting current balance of added account to network settings **let balance = await web3.eth.getBalance(accounts\[0\]);**

get nonce of current wallet **var firstNonce = web3.eth.getTransactionCount(yourSender)**

---

*Originally published on [N00b21337](https://paragraph.com/@n00b21337/truffle-tips-and-hacks)*
