Skip to content

Latest commit

 

History

History
141 lines (91 loc) · 3.71 KB

README.md

File metadata and controls

141 lines (91 loc) · 3.71 KB

Blockchain Programming Cheat sheet

1. Metamask

This can be used to hold an account inside Chrome for easier access to test wallet accounts - Use Rinkeby for testing

1.1 Funding account
  1. Create social post with address
  2. Copy the post's link inside https://faucet.rinkeby.io/ to fund your test account

2. Language

The programming language to be used is called Solidity. The documentation can be found at https://docs.soliditylang.org/en/v0.8.0/.

3. Local Setup

  1. Install NodeJS 14.15.0
  2. Create folder
  3. Go into folder
  4. npm init (creates package.json)
  5. npm install -g truffle
  6. truffle init

3.1 To create a smart contract

truffle create contract %contractName%

3.2 To compile

truffle compile

3.3 Develop

You can also run truffle develop and then run commands such as compile and migrate inside it.

3.4 Migrate

Running migrate in develop migrates contracts to the blockchain.

3.4.1 To create a migration

truffle create migration %migrationName%.

Then you can add the following code to newly added file in the migrations directory.

const ContractName = artifacts.require('ContractName')

module.exports = function(deployer){
    deployer.deploy(ContractName)
}
3.4.2 If already migrated

Add --reset to restart the migration

3.5 Other notes

3.5.1 Issues on windows

Check out https://www.trufflesuite.com/docs/truffle/reference/configuration#resolvingnaming-conflicts-on-windows

3.5.2 Changing solc version

After running truffle init, one should find a file named truffle-config.json. Change the version under compilers > solc to 0.6.6.

compilers: {
    solc: {
      version: "0.6.6"
    },
  }

4. Testing

4.1 Mocha

truffle develop uses Mocha for tests, whose documentation can be found at https://mochajs.org/.

4.2 Chai

truffle develop uses Chai for assertions, whose documentation can be found at https://www.chaijs.com/api/assert/ .

5. Deploying Smart Contract To Rinkeby Network

To deploy a smart contract once can use a public node.

5.1 Infura

  1. Go to infura.io
  2. Sign Up
  3. Create new project
  4. Get the endpoint from the project's settings under KEYS. Make sure Rinkeby is selected in the selectbox near ENDPOINTS

5.2 Setup

  1. Go into the directory of the newly created project(locally not on Infura)
  2. Run npm install @truffle/hdwallet-provider or npm install truffle-hdwallet-provider for older versions.
  3. Open truffle-config.js
  4. Get the mnemonic/seed from the MetaMask setting (Click top-right circle > settings > Security & Privacy > Reveal Seed Phrase). This should be like a long phrase with different words making no sense.
  5. Make sure the truffle-config.js file looks like this:
const metaMaskMnemonic = "seed obtained from metamask's settings";

const HDWalletProvider = require('@truffle/hdwallet-provider');

module.exports = {
  networks: {
    rinkeby: {
      network_id: 4,
      provider: new HDWalletProvider(metaMaskMnemonic, "endpoint obtained from infura's settings")
    }
  },

  // Set default mocha options here, use special reporters etc.
  mocha: {
    // timeout: 100000
  },

  // Configure your compilers
  compilers: {
    solc: {
      version: "0.6.6"
    },
  },
};
  1. Run truffle migrate --network rinkeby

5.3 Checking Deployment

The migrate command should return a contract address and this can be checked by going into https://rinkeby.etherscan.io/ and pasting the address there.

6. Online IDE

If you wish to test smart contracts quickly, one can also make use of an online IDE which can be found in this link, https://remix.ethereum.org/.