naszam / dai-faucet

Dai in Smart Contracts's Tutorial from the developerguides by MakerDao using Truffle & Remix

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

#ubuntu 18.04 #npm 12.14 #built_with_Truffle #solc 0.5.9 #testnet kovan

Dai Faucet

Dai in Smart Contracts's Tutorial from the developerguides by MakerDAO using Truffle & Remix.

Dai is the first decentralized stablecoin built on Ethereum by MakerDAO.
The Dai token is based on the DS-Token implementation which follow the ERC-20 token standard with minor additions that makes it fit nicely into the Maker system.
The Dai Stablecoin System involves a series of smart contracts that allows anyone to issue Dai by locking collateral (other valuable crypto assets) into the system.

Sections

Building Blocks of the DaiFaucet contract

Dai Token Interface

To enable our faucet contract to recognize and interact with the Dai token contract we need to write an interface that will map the Dai token functions that we'll use.
In this case that means the transfer() and balanceOf functions, since we will need our contract to transfer Dai to whomever requests it and also to check the balanceOf its Dai holdings to know if it can transfer in the first place.
We will need to instantiate this interface later in the codebase.

Contract Ownership

The Owned contract sets up the contract creator as the one in control, it sets the DaiToken daitoken variable and the owner variable.
It creates the onlyOwner modifier function to adds restrictions to who can call the other functions in our faucet contract.
When deployed on the Kovan network, the constructor function will set the owner variable to the address of the calling Ethereum account, and set the daitoken variable to the address of the Dai token contract on the Kovan network, which is 0x4f96fe3b7a6cf9725f59d353f723c1bdb64ca6aa.
Now the DaiToken interface will link the Dai token address on the kovan network. So when we call the transfer or balanceOf functions, they will call the functions of the Dai token contract.

Kill Switch

The Mortal contract inherit the Owned contract and give our contract a kill switch that will terminate it and return any funds back to the owner.
The destroy function can only be called by the owner, hence the onlyOwner modifier.
Here we use the daitoken interface, transfering any remaining Dai funds of the faucet contract to the owner.

Dai Faucet

The DaiFaucet contract inherits the Mortal contract, which in turn inherits the Owned contract, which finally inherits the DaiToken interface. This way, we have modularised our contracts for their specific functions and added our total control over it.
Inside the contract we have two events that will watch and log every time there is a Withdrawal and a Deposit to/from this contract.
We have added the withdraw function that will take care to send Dai to anyone who calls this function. As you can see, we have added 2 conditions for the withdrawal:

  • Require that the withdraw_amount is less or equal to 0.1 Dai;
  • Require that we have more Dai in the faucet than the withdraw_amount;

Only after these conditions are met we can transfer 0.1 Dai to the function caller. And of course, we log this transaction with the Withdrawal event. The way we send Dai to the function caller is by using the above defined DaiToken interface to allow us to make the transfer.
The fallback function is here to receive any incoming payments our contracts gets and log the Deposit event.

Setup

Clone this GitHub repository.

Steps to Compile and Deploy

  • Global dependencies
    • Truffle & Ganache:
    $ npm install -g truffle ganache-cli

Running the project with local test network (ganache-cli)

  • Start ganache-cli with the following command:
    $ ganache-cli
  • Compile the smart contract using Truffle with the following command:
    $ truffle compile
  • Deploy the smart contracts using Truffle & Ganache with the following command:
    $ truffle migrate

Deploy on Kovan Testnet

  • Get an Ethereum Account on Metamask.
  • On the landing page, click “Get Chrome Extension.”
  • Create a .secret file cointaining the menomic.
  • Get some test ether from a Kovan's faucet.
  • Signup Infura.
  • Create new project.
  • Copy the rinkeby URL into truffle-config.js.
  • Uncomment the following lines in truffle-config.js:
    // const HDWalletProvider = require("@truffle/hdwallet-provider");
    // const infuraKey = '...';
    // const infuraURL = 'https://kovan.infura.io/...';
    
    // const fs = require('fs');
    // const mnemonic = fs.readFileSync(".secret").toString().trim();
    
  • Install Truffle HD Wallet Provider:
    $ npm install @truffle/hdwallet-provider
  • Deploy the smart contract using Truffle & Infura with the following command:
    $ truffle migrate --network kovan

Use Faucet

The contract has been deployed on Kovan network using Infura and Remix.
Now that we have successfully deployed our smart contract on the Kovan network, we can interact with it.
The simplest way to interact with the contract is through Etherscan.
As you are the deployer of this contract, you have the source code.
This means that you can publish the source code to Etherscan and verify the contract.
This allows you to interact with the contract on Etherscan with just your MetaMask account. Easy right?

Verify Contract

To verify your contract and publish your source code, navigate to your contract address on Etherscan and click on the Verify and Publish link.
Select the compiler type to be Single File to be used for the Remix all in one file source code.
sure to select the right compiler version that your contract has been compiled against and then press continue.
Paste the Solidity code used in Remix, and then press Verify and Publish.

Congratulations! Your contract is verified, now you can interact with it from Etherscan with Metamask.

About

Dai in Smart Contracts's Tutorial from the developerguides by MakerDao using Truffle & Remix


Languages

Language:JavaScript 57.7%Language:Solidity 42.3%