LogoLogo
Phron AIopenPhronDocs
  • Build with Phron AI
  • Quick Start
    • Rust toolchain
    • Install
    • Developer CLI Tools
  • Learn
    • Why Substrate
    • Ethereum Compatible
    • Governance on PhronAI
  • Use
    • Wallets
    • Explorer
    • Bridge
    • Staking
      • Menu Overview
      • How to Start Staking with the Phron Dashboard
      • How to Change Nominations
      • How to Stop Staking
      • Staking Rewards
    • Validate
    • Dashboard
  • Build with PhronAI
    • SDKs and Tools
      • Ethereum
        • Contracts
        • Libraries
          • Ethers.js
          • Ethers.rs
          • Web3.js
          • Web3.py
        • Dev Environments
          • OpenZeppelin
            • Overview
            • Contract Wizard
            • Defender
          • Ape
          • Brownie
          • Foundry
          • Hardhat
          • Remix
          • Scaffold-PHR
          • Tenderly
          • thirdweb
          • Waffle & Mars
        • Contract Verification
          • PhronScan
        • RPC APIs
          • Standard Ethereum
          • Non-standard Ethereum: Tracing
      • Substrate
        • Libraries
          • Polkadot.js
          • Py substrate interface
        • Dev Environments
          • Chopsticks by Acala
    • Smart Contracts Development
      • Solidity Contracts
        • Phron API
          • Project Overview
          • Prerequisites
          • Steps to Set Up
          • Contract Architecture
          • Contract Functions
          • Testing
          • Deployment
          • Security Considerations
          • Using Foundry Start to End with Phron
          • How to Build a DApp: Complete DApp Architecture
        • Phron Toolkit
          • Libraries
            • Ethers.js
            • Ethers.rs
            • viem
            • Web3.js
            • Web3.py
          • Dev Environments
            • OpenZeppelin
              • Overview
              • Contract Wizard
              • Defender
            • Ape
            • Brownie
            • Foundry
            • Hardhat
            • Remix
            • Scaffold-PHR
            • Tenderly
            • thirdweb
            • Waffle & Mars
          • Verify Contracts
            • PhronScan
          • JSON-RPC APIs
            • Standard Ethereum
            • Non-standard Ethereum: Tracing
      • Rust Contracts
        • Phron smart contracts basics
          • Setting up a Testnet account
          • Installing required tools
          • Creating your first contract
          • Deploying your contract to Phron Testnet
          • Extending your contract
        • Cross contract calls
          • Using references
          • Using dynamic calls
Powered by GitBook
On this page
Export as PDF
  1. Build with PhronAI
  2. Smart Contracts Development
  3. Solidity Contracts
  4. Phron API

Steps to Set Up

Setting Up the Development Environment

1. Initialize the Project

Create a new directory for your project and initialize it with npm:

mkdir MySolidityProject
cd MySolidityProject
npm init -y

This will generate a package.json file for managing dependencies and scripts.

2. Install Solidity and Hardhat Dependencies

Hardhat is a powerful development environment for compiling, deploying, and testing Ethereum smart contracts. Install Hardhat along with ethers.js for interacting with Ethereum, and the necessary plugins:

npm install --save-dev hardhat @nomiclabs/hardhat-ethers ethers

This command installs:

  • Hardhat: For development, testing, and deployment.

  • ethers.js: A library for interacting with Ethereum.

  • @nomiclabs/hardhat-ethers: A plugin that integrates Hardhat with ethers.js.

3. Create the Hardhat Configuration

Run Hardhat’s initialization command to generate the basic configuration and project files:

npx hardhat

You will be prompted to select a task. Choose Create a basic sample project or Create an advanced project, depending on your needs. This will generate:

  • hardhat.config.js: Your Hardhat configuration file.

  • Sample contract, test, and script files.

4. Set Up the Folder Structure

MySolidityProject/
├── contracts/           # Contains all the Solidity contracts
│   └── MyContract.sol   # Example contract
├── scripts/             # Scripts for deployment and contract interaction
│   └── deploy.js        # Script for contract deployment
├── test/                # Contains unit tests for smart contracts
│   └── test.js          # Example test file
├── artifacts/           # Generated files (compiled contracts, ABIs, etc.)
├── cache/               # Cached files for faster compilation
├── node_modules/        # Installed npm packages
├── hardhat.config.js    # Hardhat configuration file
└── package.json         # Project dependencies and scripts

5. Creating a Sample Solidity Contract

Inside the contracts/ folder, create a simple Solidity contract, e.g., MyContract.sol:

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

contract MyContract {
    uint256 public value;

    function setValue(uint256 _value) public {
        value = _value;
    }

    function getValue() public view returns (uint256) {
        return value;
    }
}

6. Creating a Deployment Script

In the scripts/ folder, create a deployment script (deploy.js):

javascriptCopy codeasync function main() {
  const [deployer] = await ethers.getSigners();
  console.log("Deploying contracts with the account:", deployer.address);

  const MyContract = await ethers.getContractFactory("MyContract");
  const myContract = await MyContract.deploy();

  console.log("MyContract deployed to:", myContract.address);
}

main()
  .then(() => process.exit(0))
  .catch(error => {
    console.error(error);
    process.exit(1);
  });

7. Running the Deployment

To compile and deploy your contract locally:

  1. Start a local Ethereum node using Hardhat:

    npx hardhat node
  2. Deploy your contract:

    npx hardhat run scripts/deploy.js --network localhost

This will deploy your contract to a local test network.

8. Setting Up a Test File

In the test/ folder, create a test script (test.js) using Hardhat's testing framework and ethers.js:

const { expect } = require("chai");

describe("MyContract", function () {
  it("Should set and get the correct value", async function () {
    const MyContract = await ethers.getContractFactory("MyContract");
    const myContract = await MyContract.deploy();
    await myContract.deployed();

    // Set a value
    await myContract.setValue(42);

    // Test the value
    expect(await myContract.getValue()).to.equal(42);
  });
});

9. Running the Tests

To run your tests:

npx hardhat test

10. Hardhat Network Configuration (Optional for Testnets)

To deploy on a real test network , configure your hardhat.config.js:

require("@nomiclabs/hardhat-ethers");

module.exports = {
  solidity: "0.8.0",
  networks: {
    phron: {
      url: `https://testnet.phron.ai`,
    },
  },
};
PreviousPrerequisitesNextContract Architecture