View Categories

Solidity

6 articles

How to log to console with Solidity

Last Updated: July 9, 2023

Most times, it’s convenient to know display certain information on the console to ease debugging work when working with Solidity code. To log to console when using Remix IDE, please follow the steps below. Step 1: Include the import line as shown below. import "hardhat/console.sol"; Step 2: To log to console, include the code example below. console.log("contractBaseURI:", baseURI ); where baseURI is the variable you wish to display. Result: Your console should now output the variable data, as depicted below. console.log: contractBaseURI: https://example.io/file/{id}.json

What IDE is the best to start Solidity coding?

Last Updated: July 10, 2023

Without a doubt, Remix IDE is the most recommended IDE (integrated development environment) to use when you’re starting Solidity programming. Because it is lightweight, easy to use, and works with off-the-shelf. There’s no installation required and everything is handled online. You can even easily import OpenZeppelin industrial-grade smart contract code into Remix with a few clicks via the OpenZeppelin Wizard. You don’t have to mess with Hardhat or Truffle, or any other plugins or coding frameworks. Just fire away! Remix IDE works with or without popular wallets such as MetaMask. If you don’t have a wallet ready, you may use the Remix VM to test out your Solidity code. Once you got the hang of Solidity coding, you may always upgrade to a more advanced IDE. The only catch is your code may disappear after you cleared your browser’s cookies. But there’s a convenient backup feature that lets you backup all your code and restore it without a hassle.

What is the difference between leading and trailing underscore in variables in Solidity?

Last Updated: July 10, 2023

Variables with a leading underscore usually represent an internal variable. While those with a trailing underscore mean they are external. Internal means the variable is used internally within the class or smart contract while external means it is used outside the contract such as accepting data from user input. Below is an example of variables with underscore in front (leading) and at the back (trailing). The variables _name and _symbol are both internal or private. The external variables name_ and symbol_ are variables that work as the constructor function parameter.

How to resolve error – DeclarationError: Identifier already declared

Last Updated: July 11, 2023

You might encounter this error “DeclarationError: Identifier already declared” while compiling Solidity code. The error may be caused by the collision between the use of imported files such as in this case between ERC1155Contract.sol and Authorizable.sol, where ERC155Contract.sol is importing Authorizable.sol as shown below.

How to read all the tokens ids right after minting?

Last Updated: July 16, 2023

Token IDs are stored under the log’s topics 3rd array element such as below. receipt.logs[i].topics[3] where receipt is the receipt variable for your transaction. So, if you’re only minting one token with one token ID, you should read receipt.logs[0].topics[3] and to convert the value to integer, you wrap it with BigInt(receipt.logs[0].topics[3]).toString(). If you are doing bulk minting, to read multiple token IDs, you may use the Javascript code below. let logId = []; // Take only array element with index of even number logId = receipt.logs.filter((element, index) => index % 2 == 0); // Read each token Id from receipt and save into tokenId array for (let i = 0; i < recipientAddressValue.length; i++) { tokenId.push(BigInt(logId[i].topics[3]).toString()); }

How to get or grant roles in OpenZeppelin Solidity contract code with access control?

Last Updated: August 6, 2023

If you’re using OpenZeppelin smart contract code with access control, you will encounter a function called grantRole (see red box) depicted below on Remix on how to assign a role to a wallet address. The question you might ask is what should I fill in the role parameter. You actually have to fill in the role such as MINTER_ROLE to assign the minting privilege to an address, or DEFAULT_ADMIN_ROLE to assign a wallet to be the admin of the contract. But the parameter only accepts bytes32 input. Thus, to obtain the bytes32 data, scroll down on the list of functions you have and find the variable MINTER_ROLE or DEFAULT_ADMIN_ROLE and click on them, as shown below, in red boxes. And you will get the bytes32 version such as 0x0000000000000000000000000000000000000000000000000000000000000000 for DEFAULT_ADMIN_ROLE and 0x9f2df0fed2c77648de5860a4cc508cd0818c85b8b8a1ab4ceeef8d981c8956a6 for MINTER_ROLE. You then just have to copy these data types and paste them on the role parameter in the grantRole function and also include a wallet address. Then, hit the call button.