Africa's Blockchain Club (ABC)
  • Web 3.0
    • Web 3.0
      • Web 3.0 Roadmap
    • Todo List
  • DeFi
    • Decentralized Finance (DeFi)
    • Decentralised Exchange (DEX)
      • Automated Market Maker
      • Testing out a DEX
      • Create a Dapp
    • Non-fungible tokens (NFTs)
  • Research
    • Crypto Research
    • Federal Reserve
      • Inflation and Interest Rate
      • Debt Ceiling
      • Policies
    • World Reserve Currency
    • Glossary
Powered by GitBook
On this page
  • VendingMachine Code Snippet:
  • Lottery Sweepstake:
  1. DeFi
  2. Decentralised Exchange (DEX)

Create a Dapp

PreviousTesting out a DEXNextNon-fungible tokens (NFTs)

Last updated 1 year ago

To create a React app for your Solidity code, you can use the following steps. Please note that this example uses web3 to interact with the Ethereum blockchain. Make sure to have web3 installed in your project.

  1. Create a new Vite React app and add :

npm init vite@latest
npm install
npm run dev
npm install -D tailwindcss postcss autoprefixer
npx tailwindcss init
  1. Install web3:

npm install web3

VendingMachine Code Snippet:

```solidity
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.18;

contract VendingMachine {

    // state variables
    address public owner;
    mapping (address => uint) public cokeBalances;

    // set the owner as th address that deployed the contract
    // set the initial vending machine balance to 100
    constructor() {
        owner = msg.sender;
        cokeBalances[address(this)] = 100;
    }

    function getVendingMachineBalance() public view returns (uint) {
        return cokeBalances[address(this)];
    }

    // Let the owner restock the vending machine
    function restock(uint amount) public {
        require(msg.sender == owner, "Only the owner can restock.");
        cokeBalances[address(this)] += amount;
    }

    // Purchase donuts from the vending machine
    function purchase(uint amount) public payable {
        require(msg.value >= amount * 2 ether, "You must pay at least 2 ETH per donut");
        require(cokeBalances[address(this)] >= amount, "Not enough donuts in stock to complete this purchase");
        cokeBalances[address(this)] -= amount;
        cokeBalances[msg.sender] += amount;
    }
}
```

Lottery Sweepstake:

```remix-solidity
// SPDX-License-Identifier: MI
pragma solidity ^0.8.17;

contract Lottery {
    address public owner;
    address payable[] public players;
    uint public lotteryId;
    mapping (uint => address payable) public lotteryHistory;

    constructor() {
        owner = msg.sender;
        lotteryId = 1;
    }

    function getWinnerByLottery(uint lottery) public view returns (address payable) {
        return lotteryHistory[lottery];
    }

    function getBalance() public view returns (uint) {
        return address(this).balance;
    }

    function getPlayers() public view returns (address payable[] memory) {
        return players;
    }

    function enter() public payable {
        require(msg.value > .001 ether);
        players.push(payable(msg.sender));
    }

    function getRandomNumber() public view returns (uint) {
        return uint(keccak256(abi.encodePacked(owner, block.timestamp)));
    }

    function pickWinner() public onlyowner {
        uint index = getRandomNumber() % players.length;
        players[index].transfer(address(this).balance);

        lotteryHistory[lotteryId] = players[index];
        lotteryId++;
        

        // reset the state of the contract
        players = new address payable[](0);
    }

    modifier onlyowner() {
      require(msg.sender == owner);
      _;
    }
}
```

Lottery with Link:

tailwind