# Smart contract

W Chain utilizes the Ethereum Virtual Machine (EVM) in the backend. Smart contracts are written in Solidity and they can function on W Chain as they do on Ethereum. To deploy a smart contract, you send a transaction containing your bytecode without specifying any recipients. After the contract is deployed, it will be available to all users of the W Chain network.&#x20;

You don't have to write and deploy your smart contract manually like that, there many tools or frameworks that will help you write, test and deploy your smart contracts in better ways.

### Foundry

Foundry is an open-source, modern development framework designed for writing, testing, and deploying Solidity smart contracts for Ethereum and other EVM-compatible blockchains. Written in Rust, it offers a fast, efficient, and developer-friendly environment with tools like Forge for compiling, testing, and deploying contracts, Cast for interacting with the blockchain via the command line, and Anvil for running a local Ethereum node for testing.&#x20;

Foundry emphasizes simplicity, speed, and modularity, allowing developers to write robust tests in Solidity, debug contracts easily, and integrate with existing Ethereum tools. Its focus on a test-driven development approach and seamless integration with Git makes it a popular choice for building secure and scalable decentralized applications.

We recommend Foundry, as it natively use Solidity in its scripting, testing and deploying functionalities. You will never need to switch between different programming languages, use Solidity everywhere!

Here is the recommended settings for your <mark style="color:yellow;">`foundry.toml`</mark>:

```toml
[profile.default]
src = "src"
out = "out"
libs = ["lib"]
solc = '0.8.19'
optimizer = true
optimizer_runs = 10_000

[etherscan]
wchain = { key = "verifyContract", chain = 171717, url = 'https://scan.w-chain.com/api' }
wchain-test = { key = "verifyContract", chain = 71117, url = 'https://scan-testnet.w-chain.com/api' }

[rpc_endpoints]
wchain = "https://rpc.w-chain.com"
wchain-test = "https://rpc-testnet.w-chain.com"
```

Using this settings, you will be able to use the RPC in your CLI commands. Here are some examples:

```bash
cast send 0x6807dc923806fE8Fd134338EABCA509979a7e0cB --value 1ether --rpc-url wchain --private-key $PRIVATE_KEY
```

```bash
cast balance 0x6807dc923806fE8Fd134338EABCA509979a7e0cB --rpc-url wchain
```

```
forge create src/SomeContract.sol:MyContract --rpc-url wchain --verify --chain 171717 --private-key $PRIVATE_KEY
```

### Hardhat

Hardhat is an open-source, flexible, and extensible development environment for writing, compiling, testing, and deploying Solidity smart contracts for Ethereum and EVM-compatible blockchains. Built with JavaScript and Node.js, it provides a robust framework for developers to create, debug, and deploy smart contracts efficiently.&#x20;

Key features include the Hardhat Network for local blockchain testing, a powerful task runner for automating workflows, and support for plugins to extend functionality. Hardhat’s intuitive API, detailed error reporting, and integration with tools like TypeScript and Ethers.js make it ideal for both beginners and experienced developers building secure, scalable decentralized applications.

Compared to Foundry, Hardhat will require you to switch contexts between Solidity and Javascript/TypeScript. This benefits developers with good Javascript background as it will use familiar testing modules, and share same contexts with Ethers JS, building early connection to UI/Frontend side of the dApp.

Here is a settings you can start from:

```javascript
module.exports = {
  networks: {
    w_chain_testnet: {
      url: "https://rpc-testnet.w-chain.com",
      accounts: [process.env.PRIVATE_KEY],
    },
    w_chain: {
      url: "https://rpc.w-chain.com",
      accounts: [process.env.PRIVATE_KEY],
    }
  },
  etherscan: {
    apiKey: "free-key",
    customChains: [
      {
        network: "W Chain Testnet",
        chainId: 71117,
        urls: {
          apiURL: "https://scan-testnet.w-chain.com/api",
          browserURL: "https://scan-testnet.w-chain.com"
        }
      },
      {
        network: "W Chain",
        chainId: 171717,
        urls: {
          apiURL: "https://scan.w-chain.com/api",
          browserURL: "https://scan.w-chain.com"
        }
      }
    ]
  }
};
```

&#x20;

### Verifying Smart Contract

We recommend you to use Foundry or Hardhat to build, deploy and to verify the source code on W Chain Block Explorer. If there's any situation you want to manually verify a smart contract, you can send the POST request to W Chain Block Explorer's API. Here's an example:

```bash
curl --location 'https://scan.w-chain.com/api?module=contract&action=verifysourcecode' \
--form 'contractaddress="0xYourContractAddress"' \
--form 'sourceCode="// SPDX-License-Identifier: GPL-3.0 pragma solidity ^0.8.17; contract Storage { uint256 number; function store(uint256 num) public { number = num; } function retrieve() public view returns (uint256){ return number; } }"' \
--form 'contractname="Storage"' \
--form 'codeformat="solidity-single-file"' \
--form 'compilerversion="v0.8.17+commit.8df45f5f"' \
--form 'optimizationUsed="1"' \
--form 'runs="200"' \
--form 'constructorArguements=""' \
--form 'evmversion="london"' \
--form 'licenseType="3"'
```

{% hint style="warning" %}
It's very important to ensure the exact code format, source code, compiler version, constructor arguments (if any), optimizations configs and EVM version.
{% endhint %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.w-chain.com/tutorial/smart-contract.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
