Challenge: Create a Contract with Attributes and Functions

Test yourself on Solidity by creating contracts with different parameters and coding the respective run scripts.

Problem statement

Create a contract named Challenge with the following attributes and functions:

  • A private variable called targetAmount, which is equal to 2.

  • The constructor function that should accept an argument called amount.

  • The constructor should check if the amount is equal to the targetAmount. If not, it should fail with the error message Error.

Write a main function for a Hardhat run script that would load the Challenge contract you created earlier and deploy with the Hardhat generated address.

Input

There is no input for this challenge.

Output

The code will display the error message.

Try it yourself

Write the code for the contract in the contracts/contract.sol file and the run script in the scripts/run.js file. Click the “Run” button in the code widget below to test your solution:

Get hands-on with 1200+ tech skills courses.