Skip to content

Instantly share code, notes, and snippets.

@liuhaidong
Forked from nguyer/build_web3_apps.md
Last active May 28, 2022 12:46
Show Gist options
  • Select an option

  • Save liuhaidong/3b185d87f5acfba3ed7c110f442475b5 to your computer and use it in GitHub Desktop.

Select an option

Save liuhaidong/3b185d87f5acfba3ed7c110f442475b5 to your computer and use it in GitHub Desktop.

Revisions

  1. liuhaidong revised this gist May 28, 2022. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion build_web3_apps.md
    Original file line number Diff line number Diff line change
    @@ -103,7 +103,7 @@ Note the contract addresses printed from the output of the command above. We wil
    Now that we have an ERC-20 contract on chain, let's play around in the FireFly Sandbox! The Sandbox is entirely web based, and has a very intuitive interface. You can read up about the types of things you can do in the Getting Started guide to [Use the Sandbox](https://hyperledger.github.io/firefly/gettingstarted/sandbox.html). For this section of the workshop, you can simply follow along in your browser.

    ### Create a contract interface
    In this step, we're going to tell FireFly about our custom ERC-20 contract. To do this, we look into the `./solidity/artifacts/build-info` directory and open the `.json` file there. We need to copy the ABI from our contract, so search the file for the string `"contracts/ERC20WithData.sol"`. The ABI should be right after the second occurrence of this string.
    In this step, we're going to tell FireFly about our custom ERC-20 contract. To do this, we look into the `./solidity/artifacts/build-info` directory and open the `.json` file there. **We need to copy the ABI from our contract, so search the file for the string `"contracts/ERC20WithData.sol"`.** The ABI should be right after the second occurrence of this string.

    For convenience, I've also included a copy of it here, which you can copy/paste without hunting through a large JSON file:
    ```json
  2. @nguyer nguyer revised this gist Apr 19, 2022. 1 changed file with 496 additions and 39 deletions.
    535 changes: 496 additions & 39 deletions build_web3_apps.md
    Original file line number Diff line number Diff line change
    @@ -19,7 +19,6 @@ For details on how to get started with Docker, please see: https://www.docker.co
    > **NOTE:** For Windows users, we recommend that you use Windows Subsystem for Linux 2 (WSL2). FireFly binaries provided for Linux will work in this environment.
    To verify installation, you can run:

    ```
    docker --version
    Docker version 20.10.14, build a224086
    @@ -46,7 +45,6 @@ We will be using Hardhat to compile and deploy a custom smart contract during th
    To install Node.js for your platform please visit: https://nodejs.org/en/

    To verify installation you can run:

    ```
    node --version
    v16.14.0
    @@ -57,56 +55,515 @@ npm --version
    8.3.1
    ```

    ## Creating a custom smart contract with Hardhat
    To bootstrap our custom smart contract, we will follow Hardhat's Getting Started guide here: https://hardhat.org/getting-started/

    ### Add an event to our contract
    Let's add an event that will fire any time someone updates the greeting. Add a line somewhere inside the `contract` like this:
    ## Getting started with FireFly
    For this portion of this workshop we will be going through the first section of the [Getting Started Guide](https://hyperledger.github.io/firefly/gettingstarted/gettingstarted.html) in the FireFly Docs to [Install the FireFly CLI](https://hyperledger.github.io/firefly/gettingstarted/firefly_cli.html).

    ```solidity
    event GreetingUpdated(string _greeting);
    We will then follow a similar approach to that which is outlined guide to [Start your environment](https://hyperledger.github.io/firefly/gettingstarted/setup_env.html). However, I am going to use a slightly different command to initialize my FireFly Stack:
    ```
    ff init workshop 3 -t erc20_erc721 --prompt-names
    ```

    Now we will change the `setGreeting` function to emit the event, right at the end of the function:
    I'm going to name each of my members a color, `Red`, `Green`, and `Blue` so that I can keep track of them a bit better, and I'm just going to name their nodes `Red_Node`, `Green_Node`, and `Blue_Node`.

    ```solidity
    function setGreeting(string memory _greeting) public {
    console.log("Changing greeting from '%s' to '%s'", greeting, _greeting);
    greeting = _greeting;
    emit GreetingUpdated(greeting);
    }
    After this, you can start up your FireFly stack by running:
    ```
    ff start workshop
    ```

    ### Update `hardhat.config.js`
    After creating our project directory, we need to update the `hardhat.config.js` file which has a `module.exports` section at the bottom. To get Hardhat to talk to the same blockchain node that our FireFly network is using, make the bottom of the file look like this:

    ```javascript
    module.exports = {
    solidity: "0.8.4",
    defaultNetwork: "firefly",
    networks: {
    firefly: {
    url: "http://127.0.0.1:5100"
    }
    }
    };
    ## Deploy an ERC-20 contract with Hardhat
    Next, we're going to compile and deploy an ERC-20 contract that we will use with FireFly. For this workshop, we're just going to deploy an ERC-20 contract in the FireFly token connector Git repo. Start by cloning that repo:
    ```
    git clone [email protected]:hyperledger/firefly-tokens-erc20-erc721.git
    ```

    ###
    This repo contains a few different things, but we're only concerned with the smart contracts that are in the `contracts` directory for now. Change to that directory:
    ```
    cd firefly-tokens-erc20-erc721/solidity
    ```

    ### Compile the contract
    Now let's install dependencies:
    ```
    npx hardhat compile
    Compiled 2 Solidity files successfully
    npm install
    ```

    ### Deploy the contract
    Lastly, compile and deploy the contract with Hardhat:
    ```
    npx hardhat run scripts/sample-script.js
    Greeter deployed to: 0x3FA91D0571e8C99A88A7155138293c691bf935bb
    npx hardhat run scripts/deploy.ts
    Generating typings for: 18 artifacts in dir: typechain for target: ethers-v5
    Successfully generated 33 typings!
    Compiled 18 Solidity files successfully
    ERC-20 contract deployed to: 0xA8e0C061C56430cf89243CEB19f8dbD68D1D8Fd4
    ERC-721 contract deployed to: 0x44282af89ACc6FE5AcA828fF4D1F87CDC377f0Ee
    ```

    In this case, the hex string `0x3FA91D0571e8C99A88A7155138293c691bf935bb` is our deployed contract address. This is what we will use when creating a Contract API in the FireFly Sandbox.
    Note the contract addresses printed from the output of the command above. We will use the address for the ERC-20 contract in the next step.

    ## Getting started with FireFly
    For this portion of this workshop we will be going through the first two sections of the [Getting Started Guide](https://hyperledger.github.io/firefly/gettingstarted/gettingstarted.html) in the FireFly Docs to [Install the FireFly CLI](https://hyperledger.github.io/firefly/gettingstarted/firefly_cli.html) and [Start your environment](https://hyperledger.github.io/firefly/gettingstarted/setup_env.html). Please follow along there.
    ## Use the FireFly Sandbox
    Now that we have an ERC-20 contract on chain, let's play around in the FireFly Sandbox! The Sandbox is entirely web based, and has a very intuitive interface. You can read up about the types of things you can do in the Getting Started guide to [Use the Sandbox](https://hyperledger.github.io/firefly/gettingstarted/sandbox.html). For this section of the workshop, you can simply follow along in your browser.

    ### Create a contract interface
    In this step, we're going to tell FireFly about our custom ERC-20 contract. To do this, we look into the `./solidity/artifacts/build-info` directory and open the `.json` file there. We need to copy the ABI from our contract, so search the file for the string `"contracts/ERC20WithData.sol"`. The ABI should be right after the second occurrence of this string.

    For convenience, I've also included a copy of it here, which you can copy/paste without hunting through a large JSON file:
    ```json
    [
    {
    "inputs": [
    {
    "internalType": "string",
    "name": "name",
    "type": "string"
    },
    {
    "internalType": "string",
    "name": "symbol",
    "type": "string"
    }
    ],
    "stateMutability": "nonpayable",
    "type": "constructor"
    },
    {
    "anonymous": false,
    "inputs": [
    {
    "indexed": true,
    "internalType": "address",
    "name": "owner",
    "type": "address"
    },
    {
    "indexed": true,
    "internalType": "address",
    "name": "spender",
    "type": "address"
    },
    {
    "indexed": false,
    "internalType": "uint256",
    "name": "value",
    "type": "uint256"
    }
    ],
    "name": "Approval",
    "type": "event"
    },
    {
    "anonymous": false,
    "inputs": [
    {
    "indexed": true,
    "internalType": "address",
    "name": "previousOwner",
    "type": "address"
    },
    {
    "indexed": true,
    "internalType": "address",
    "name": "newOwner",
    "type": "address"
    }
    ],
    "name": "OwnershipTransferred",
    "type": "event"
    },
    {
    "anonymous": false,
    "inputs": [
    {
    "indexed": true,
    "internalType": "address",
    "name": "from",
    "type": "address"
    },
    {
    "indexed": true,
    "internalType": "address",
    "name": "to",
    "type": "address"
    },
    {
    "indexed": false,
    "internalType": "uint256",
    "name": "value",
    "type": "uint256"
    }
    ],
    "name": "Transfer",
    "type": "event"
    },
    {
    "inputs": [
    {
    "internalType": "address",
    "name": "owner",
    "type": "address"
    },
    {
    "internalType": "address",
    "name": "spender",
    "type": "address"
    }
    ],
    "name": "allowance",
    "outputs": [
    {
    "internalType": "uint256",
    "name": "",
    "type": "uint256"
    }
    ],
    "stateMutability": "view",
    "type": "function"
    },
    {
    "inputs": [
    {
    "internalType": "address",
    "name": "spender",
    "type": "address"
    },
    {
    "internalType": "uint256",
    "name": "amount",
    "type": "uint256"
    }
    ],
    "name": "approve",
    "outputs": [
    {
    "internalType": "bool",
    "name": "",
    "type": "bool"
    }
    ],
    "stateMutability": "nonpayable",
    "type": "function"
    },
    {
    "inputs": [
    {
    "internalType": "address",
    "name": "spender",
    "type": "address"
    },
    {
    "internalType": "uint256",
    "name": "amount",
    "type": "uint256"
    },
    {
    "internalType": "bytes",
    "name": "data",
    "type": "bytes"
    }
    ],
    "name": "approveWithData",
    "outputs": [
    {
    "internalType": "bool",
    "name": "",
    "type": "bool"
    }
    ],
    "stateMutability": "nonpayable",
    "type": "function"
    },
    {
    "inputs": [
    {
    "internalType": "address",
    "name": "account",
    "type": "address"
    }
    ],
    "name": "balanceOf",
    "outputs": [
    {
    "internalType": "uint256",
    "name": "",
    "type": "uint256"
    }
    ],
    "stateMutability": "view",
    "type": "function"
    },
    {
    "inputs": [
    {
    "internalType": "address",
    "name": "from",
    "type": "address"
    },
    {
    "internalType": "uint256",
    "name": "amount",
    "type": "uint256"
    },
    {
    "internalType": "bytes",
    "name": "data",
    "type": "bytes"
    }
    ],
    "name": "burnWithData",
    "outputs": [],
    "stateMutability": "nonpayable",
    "type": "function"
    },
    {
    "inputs": [],
    "name": "decimals",
    "outputs": [
    {
    "internalType": "uint8",
    "name": "",
    "type": "uint8"
    }
    ],
    "stateMutability": "view",
    "type": "function"
    },
    {
    "inputs": [
    {
    "internalType": "address",
    "name": "spender",
    "type": "address"
    },
    {
    "internalType": "uint256",
    "name": "subtractedValue",
    "type": "uint256"
    }
    ],
    "name": "decreaseAllowance",
    "outputs": [
    {
    "internalType": "bool",
    "name": "",
    "type": "bool"
    }
    ],
    "stateMutability": "nonpayable",
    "type": "function"
    },
    {
    "inputs": [
    {
    "internalType": "address",
    "name": "spender",
    "type": "address"
    },
    {
    "internalType": "uint256",
    "name": "addedValue",
    "type": "uint256"
    }
    ],
    "name": "increaseAllowance",
    "outputs": [
    {
    "internalType": "bool",
    "name": "",
    "type": "bool"
    }
    ],
    "stateMutability": "nonpayable",
    "type": "function"
    },
    {
    "inputs": [
    {
    "internalType": "address",
    "name": "to",
    "type": "address"
    },
    {
    "internalType": "uint256",
    "name": "amount",
    "type": "uint256"
    },
    {
    "internalType": "bytes",
    "name": "data",
    "type": "bytes"
    }
    ],
    "name": "mintWithData",
    "outputs": [],
    "stateMutability": "nonpayable",
    "type": "function"
    },
    {
    "inputs": [],
    "name": "name",
    "outputs": [
    {
    "internalType": "string",
    "name": "",
    "type": "string"
    }
    ],
    "stateMutability": "view",
    "type": "function"
    },
    {
    "inputs": [],
    "name": "owner",
    "outputs": [
    {
    "internalType": "address",
    "name": "",
    "type": "address"
    }
    ],
    "stateMutability": "view",
    "type": "function"
    },
    {
    "inputs": [],
    "name": "renounceOwnership",
    "outputs": [],
    "stateMutability": "nonpayable",
    "type": "function"
    },
    {
    "inputs": [],
    "name": "symbol",
    "outputs": [
    {
    "internalType": "string",
    "name": "",
    "type": "string"
    }
    ],
    "stateMutability": "view",
    "type": "function"
    },
    {
    "inputs": [],
    "name": "totalSupply",
    "outputs": [
    {
    "internalType": "uint256",
    "name": "",
    "type": "uint256"
    }
    ],
    "stateMutability": "view",
    "type": "function"
    },
    {
    "inputs": [
    {
    "internalType": "address",
    "name": "to",
    "type": "address"
    },
    {
    "internalType": "uint256",
    "name": "amount",
    "type": "uint256"
    }
    ],
    "name": "transfer",
    "outputs": [
    {
    "internalType": "bool",
    "name": "",
    "type": "bool"
    }
    ],
    "stateMutability": "nonpayable",
    "type": "function"
    },
    {
    "inputs": [
    {
    "internalType": "address",
    "name": "from",
    "type": "address"
    },
    {
    "internalType": "address",
    "name": "to",
    "type": "address"
    },
    {
    "internalType": "uint256",
    "name": "amount",
    "type": "uint256"
    }
    ],
    "name": "transferFrom",
    "outputs": [
    {
    "internalType": "bool",
    "name": "",
    "type": "bool"
    }
    ],
    "stateMutability": "nonpayable",
    "type": "function"
    },
    {
    "inputs": [
    {
    "internalType": "address",
    "name": "newOwner",
    "type": "address"
    }
    ],
    "name": "transferOwnership",
    "outputs": [],
    "stateMutability": "nonpayable",
    "type": "function"
    },
    {
    "inputs": [
    {
    "internalType": "address",
    "name": "from",
    "type": "address"
    },
    {
    "internalType": "address",
    "name": "to",
    "type": "address"
    },
    {
    "internalType": "uint256",
    "name": "amount",
    "type": "uint256"
    },
    {
    "internalType": "bytes",
    "name": "data",
    "type": "bytes"
    }
    ],
    "name": "transferWithData",
    "outputs": [],
    "stateMutability": "nonpayable",
    "type": "function"
    }
    ]
    ```

    ### Mint some tokens
    Now let's mint some tokens. Open the Swagger UI for the `mintWithData` endpoint on our custom contract:

    http://127.0.0.1:5000/api/v1/namespaces/default/apis/erc20-with-data/api#/default/invoke_mintWithData

    Click the **Try It Out** button, and set the request body to the following:
    ```json
    {
    "input": {
    "amount": 1000000000000000000,
    "data": "0x00",
    "to": "0xa403f0ab1824b1a1577f94eb235b9a250fd21dea"
    }
    }
    ```
  3. @nguyer nguyer revised this gist Apr 19, 2022. 1 changed file with 28 additions and 7 deletions.
    35 changes: 28 additions & 7 deletions build_web3_apps.md
    Original file line number Diff line number Diff line change
    @@ -4,7 +4,7 @@ Welcome! We're glad you're here! This is the guide that we will be going through

    ## Before the workshop

    > **IMPORTANT:** Please make sure you have done the following ***before*** the workshop so that we can hit the ground running when the workshop starts.
    > **IMPORTANT:** Please make sure you have installed the software listed in this section ***before*** the workshop so that we can hit the ground running when the workshop starts.
    ### Install Docker

    @@ -30,7 +30,7 @@ docker-compose --version
    Docker Compose version v2.4.1
    ```

    ### Open SSL
    ### Install Open SSL
    The FireFly CLI also uses Open SSL for certificate generation. You probably already have a version of it installed. If not, please use your system's package manager to install Open SSL

    To verify installation you can run:
    @@ -57,12 +57,27 @@ npm --version
    8.3.1
    ```

    ## Getting Started
    For this portion of this workshop we will be going through the first two sections of the [Getting Started Guide](https://hyperledger.github.io/firefly/gettingstarted/gettingstarted.html) in the FireFly Docs to [Install the FireFly CLI](https://hyperledger.github.io/firefly/gettingstarted/firefly_cli.html) and [Start your environment](https://hyperledger.github.io/firefly/gettingstarted/setup_env.html). Please follow along there.
    ## Creating a custom smart contract with Hardhat
    To bootstrap our custom smart contract, we will follow Hardhat's Getting Started guide here: https://hardhat.org/getting-started/

    ## Deploying a custom smart contract with Hardhat
    To boostrap our custom smart contract, we will follow Hardhat's Getting Started guide here: https://hardhat.org/getting-started/
    ### Add an event to our contract
    Let's add an event that will fire any time someone updates the greeting. Add a line somewhere inside the `contract` like this:

    ```solidity
    event GreetingUpdated(string _greeting);
    ```

    Now we will change the `setGreeting` function to emit the event, right at the end of the function:

    ```solidity
    function setGreeting(string memory _greeting) public {
    console.log("Changing greeting from '%s' to '%s'", greeting, _greeting);
    greeting = _greeting;
    emit GreetingUpdated(greeting);
    }
    ```

    ### Update `hardhat.config.js`
    After creating our project directory, we need to update the `hardhat.config.js` file which has a `module.exports` section at the bottom. To get Hardhat to talk to the same blockchain node that our FireFly network is using, make the bottom of the file look like this:

    ```javascript
    @@ -77,9 +92,12 @@ module.exports = {
    };
    ```

    ###

    ### Compile the contract
    ```
    npx hardhat compile
    Compiled 2 Solidity files successfully
    ```

    ### Deploy the contract
    @@ -88,4 +106,7 @@ npx hardhat run scripts/sample-script.js
    Greeter deployed to: 0x3FA91D0571e8C99A88A7155138293c691bf935bb
    ```

    In this case, the hex string `0x3FA91D0571e8C99A88A7155138293c691bf935bb` is our deployed contract address. This is what we will use when creating a Contract API in the FireFly Sandbox.
    In this case, the hex string `0x3FA91D0571e8C99A88A7155138293c691bf935bb` is our deployed contract address. This is what we will use when creating a Contract API in the FireFly Sandbox.

    ## Getting started with FireFly
    For this portion of this workshop we will be going through the first two sections of the [Getting Started Guide](https://hyperledger.github.io/firefly/gettingstarted/gettingstarted.html) in the FireFly Docs to [Install the FireFly CLI](https://hyperledger.github.io/firefly/gettingstarted/firefly_cli.html) and [Start your environment](https://hyperledger.github.io/firefly/gettingstarted/setup_env.html). Please follow along there.
  4. @nguyer nguyer revised this gist Apr 19, 2022. 1 changed file with 41 additions and 6 deletions.
    47 changes: 41 additions & 6 deletions build_web3_apps.md
    Original file line number Diff line number Diff line change
    @@ -40,17 +40,52 @@ openssl version
    LibreSSL 2.8.3
    ```

    ### Install `solc`
    During the workshop we will compile and deploy a custom smart contract. We will use the `solc` solidty compiler for this. Please use your system's package manager to install `solc`.
    ### Install Node.js
    We will be using Hardhat to compile and deploy a custom smart contract during the workshop, so you'll need to make sure you have a recent version of Node.js installed.

    To install Node.js for your platform please visit: https://nodejs.org/en/

    To verify installation you can run:

    ```
    solc --version
    solc, the solidity compiler commandline interface
    Version: 0.8.11+commit.d7f03943.Darwin.appleclang
    node --version
    v16.14.0
    ```

    ```
    npm --version
    8.3.1
    ```

    ## Getting Started
    For this portion of this workshop we will be going through the first two sections of the [Getting Started Guide](https://hyperledger.github.io/firefly/gettingstarted/gettingstarted.html) in the FireFly Docs to [Install the FireFly CLI](https://hyperledger.github.io/firefly/gettingstarted/firefly_cli.html) and [Start your environment](https://hyperledger.github.io/firefly/gettingstarted/setup_env.html). Please follow along there.

    ## Deploying a custom smart contract with Hardhat
    To boostrap our custom smart contract, we will follow Hardhat's Getting Started guide here: https://hardhat.org/getting-started/

    After creating our project directory, we need to update the `hardhat.config.js` file which has a `module.exports` section at the bottom. To get Hardhat to talk to the same blockchain node that our FireFly network is using, make the bottom of the file look like this:

    ```javascript
    module.exports = {
    solidity: "0.8.4",
    defaultNetwork: "firefly",
    networks: {
    firefly: {
    url: "http://127.0.0.1:5100"
    }
    }
    };
    ```

    ### Compile the contract
    ```
    npx hardhat compile
    ```

    ### Deploy the contract
    ```
    npx hardhat run scripts/sample-script.js
    Greeter deployed to: 0x3FA91D0571e8C99A88A7155138293c691bf935bb
    ```

    For this portion of this workshop we will be going through the [Getting Started Guide](https://hyperledger.github.io/firefly/gettingstarted/gettingstarted.html) in the FireFly Docs. Please follow along there.
    In this case, the hex string `0x3FA91D0571e8C99A88A7155138293c691bf935bb` is our deployed contract address. This is what we will use when creating a Contract API in the FireFly Sandbox.
  5. @nguyer nguyer revised this gist Apr 18, 2022. 1 changed file with 56 additions and 1 deletion.
    57 changes: 56 additions & 1 deletion build_web3_apps.md
    Original file line number Diff line number Diff line change
    @@ -1 +1,56 @@
    TBD
    # Workshop Guide - Building Apps on FireFly

    Welcome! We're glad you're here! This is the guide that we will be going through during the workshop.

    ## Before the workshop

    > **IMPORTANT:** Please make sure you have done the following ***before*** the workshop so that we can hit the ground running when the workshop starts.
    ### Install Docker

    The FireFly CLI uses `docker` and `docker-compose` to create a local development environment on your machine for offline development. Please make sure you have the latest (or very recent) version of Docker and `docker-compose` on your machine.

    For details on how to get started with Docker, please see: https://www.docker.com/get-started/

    #### Linux Users
    > **NOTE:** For Linux users, it is recommended that you add your user to the docker group so that you do not have to run ff or docker as root or with sudo. For more information about Docker permissions on Linux, please see Docker’s documentation on the topic.
    #### Windows Users
    > **NOTE:** For Windows users, we recommend that you use Windows Subsystem for Linux 2 (WSL2). FireFly binaries provided for Linux will work in this environment.
    To verify installation, you can run:

    ```
    docker --version
    Docker version 20.10.14, build a224086
    ```

    ```
    docker-compose --version
    Docker Compose version v2.4.1
    ```

    ### Open SSL
    The FireFly CLI also uses Open SSL for certificate generation. You probably already have a version of it installed. If not, please use your system's package manager to install Open SSL

    To verify installation you can run:

    ```
    openssl version
    LibreSSL 2.8.3
    ```

    ### Install `solc`
    During the workshop we will compile and deploy a custom smart contract. We will use the `solc` solidty compiler for this. Please use your system's package manager to install `solc`.

    To verify installation you can run:

    ```
    solc --version
    solc, the solidity compiler commandline interface
    Version: 0.8.11+commit.d7f03943.Darwin.appleclang
    ```

    ## Getting Started

    For this portion of this workshop we will be going through the [Getting Started Guide](https://hyperledger.github.io/firefly/gettingstarted/gettingstarted.html) in the FireFly Docs. Please follow along there.
  6. @nguyer nguyer renamed this gist Mar 11, 2022. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  7. @nguyer nguyer created this gist Mar 11, 2022.
    1 change: 1 addition & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1 @@
    TBD