# Getting Started in Smart Contract Development with Solidity

This guide is prepared by @BunHouth.

## [Solidity](https://docs.soliditylang.org/)

Solidity is an object-oriented, high-level language for implementing smart contracts. Smart contracts are programs which govern the behaviour of accounts within the Ethereum state. Solidity is a [curly-bracket language](https://en.wikipedia.org/wiki/List_of_programming_languages_by_type#Curly-bracket_languages). It is influenced by C++, Python and JavaScript, and is designed to target the Ethereum Virtual Machine (EVM).

## [Web3](https://web3js.readthedocs.io/en/v1.2.11/index.html)

web3.js is a collection of libraries that allow you to interact with a local or remote ethereum node using HTTP, IPC or WebSocket.

## Setup Development

#### Prerequisites

* Operating system: OSX or Linux(Ubuntu, Cenos, ...etc)
* Software: node js(npm, yarn) or python

#### Install node js

**Ubuntu**

```
 curl -sL https://dl.yarnpkg.com/debian/pubkey.gpg | sudo apt-key add -
 echo "deb https://dl.yarnpkg.com/debian/ stable main" | sudo tee /etc/apt/sources.list.d/yarn.list
 sudo apt-get update
 sudo apt-get install nodejs
 sudo apt install yarn
```

**OSX**

```
brew install node
brew install yarn
```

### Setup smart contract project

[**Install Truffle**](https://trufflesuite.com/docs/truffle/getting-started/compiling-contracts.html)

```
npm install -g truffle
```

[**Install Ganache Local Network**](https://github.com/trufflesuite/ganache)

```
npm install -g ganache-cli
```

### Generate smart contract

[Reference](https://medium.com/haloblock/deploy-your-own-smart-contract-with-truffle-and-ganache-cli-beginner-tutorial-c46bce0bd01e) **generate blank project**

```
mkdir web3together && cd web3together
truffle init
```

**Structures:**

![structure](/files/8AoXfFZhujIdA1eJen4j)

1. **/contracts:** store original codes of the smart contract. We will place our HelloWorld.sol file here.
2. **/migrations**: deploy the smart contract in the “contracts” folder.
3. **/test**: test codes for your smart contract, support both JavaScript and Solidity.
4. **truffle.js**: configuration document.
5. **truffle-config.js**: configuration of deployment.

**Step 2: Create HelloWeb3Together contract.** There are two ways to create a new contract: Directly place HelloWeb3Together.sol” file under “contracts” folder. In the “web3together” folder, run command:

```
truffle create contract Web3Together
```

Copy the following codes into HelloWeb3Together.sol”:

```
pragma solidity >=0.4.22 <0.9.0;

contract HelloWeb3Together {
    function ping() public pure returns (string memory) {
        return ("Web3Together - https://github.com/web3together/web3together");
    }

    function clock() public view returns (uint) {
        return block.timestamp;
    }
}

```

**Step 3: Compile “HelloWeb3Together” with the following command.**

```
truffle compile
```

This compiles the original code into Ethereum bytecode. If everything goes well, it will create .json file under build/contracts folder. **Step 4: Deploy “HelloWeb3Together” contract.**

1. Create `2_deploy_contracts.js` under migrations folders. Truffle is run following order
2. Copy and past the following deploying content into the “2\_deploy\_contracts.js”.

```
const HelloWeb3Together = artifacts.require("./HelloWeb3Together.sol");
module.exports = (deployer) => {
  deployer.deploy(HelloWeb3Together);
}
```

1. Modify truffle-config.js and add following content to network section. If you are using public chain or private chain please add many section as you want.

```
development: {
     host: "127.0.0.1",
     port: 8545,
     network_id: "*",
},
```

1. Start Ganache Local network and run following command on other tab or new terminal

```
ganache-cli
```

1. Deploy smart contract to local network

```
truffle migrate
```

**if contract already deploy and want to redeploy please add `--reset` option** After run `truffle migrate` we will see deploy log in the same terminal. 6. Additional setup for deploy contract to public chain testnet([rinkeby](https://rinkeby.etherscan.io/))

Additional dependency

```
yarn add @truffle/hdwallet-provider dotenv
```

Add following content into network sections. [reference](https://www.geeksforgeeks.org/deploying-smart-contract-on-test-main-network-using-truffle/)

```
    rinkeby: {
      provider: () => new HDWalletProvider("PrivateKeyToUnlockYourWallet", 'RPC_URL'),
      network_id: 4, //check here https://chainlist.org/
      gas: 5500000,
      confirmations: 2,
      timeoutBlocks: 200,
      skipDryRun: true,
    },
```

**RPC Service**

1. [infura](https://infura.io/)
2. [alchemy](http://alchemy.com)
3. [build your own](https://geth.ethereum.org/docs/rpc/server)

**🎉Congrats! The “Web3Together” smart contract has been successfully deployed to Ganache.**

### Running Smart Contract online tool

[Remix](https://remix.ethereum.org/)


---

# 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.tenten.co/awesome/web3/web3together-main/sessions/2021-12-11_getting_started_in_smart_contract_development_with_solidity.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.
