블록체인 TIL-15Week-100Day

디오·2023년 6월 20일
0

DApp 개발 프로젝트

목록 보기
2/4

✅오늘 뭐했니?

  • 참고자료.
  1. Flashot: https://buff.ly/45OAgwT
  2. DeFiRanger: https://buff.ly/3MGCkOx
  3. Curve Math: https://buff.ly/3OKY6TN
  4. Maximizing Extractable Value from Automated Market Makers: https://buff.ly/3qiiiCF
  5. Empirical Evidence from four Governance Token Distributions: https://buff.ly/3OEGusW
  6. Measuring Asset Composability as a Proxy for DeFi Integration: https://buff.ly/3C20Cxr
  7. From banks to DeFi: the evolution of the lending market: https://buff.ly/3qiiim9
  8. SoK: Decentralized Finance (DeFi): https://buff.ly/3C20C0p
  9. Attacking the DeFi Ecosystem with Flash Loans for Fun and Profit: https://buff.ly/3P2IVG3
  10. MakerDAO whitepaper (stables lending): https://buff.ly/3MGCl53
  11. Compound whitepaper (money markets): https://buff.ly/45OAh3V
  12. Aave whitepaper (lending pools): https://buff.ly/3C20CgV
  13. Frax whitepaper (algo stablecoin): https://buff.ly/3MHcA4r
  14. Curve intro (stables DEX, steeper AMM): https://buff.ly/3OGZS8J
  15. Liquidations: DeFi on a Knife-edge: https://buff.ly/3OJbjN2
  16. Cyclic Arbitrage in Decentralized Exchange Markets: https://buff.ly/3MDD72U
  17. Governance issues at DEXs (the “Curve Wars”): https://buff.ly/3qiiiTb
  18. The Decentralized Financial Crisis: https://buff.ly/45OAhAX
  19. Dynamic Curves for Decentralized Autonomous Cryptocurrency Exchanges: https://buff.ly/3C20CNX
  20. High-Frequency Trading on Decentralized On-Chain Exchanges: https://buff.ly/45OAhkr
  21. Behavior of Liquidity Providers in Decentralized Exchanges: https://buff.ly/45OAi7Z
  22. Blockchain Oracle Design Patterns: https://buff.ly/3OL7ved
  23. SoK: Oracles from the Ground Truth to Market Manipulation: https://buff.ly/3OHfGIz
  24. Tokenizing real-world assets primer: https://buff.ly/45OAjc3
  25. The Adoption of Blockchain-based Decentralized Exchanges: https://buff.ly/45OAhRt
  26. An analysis of Uniswap markets: https://buff.ly/3MDD7jq
  27. Finance 4.0: Design principles for a value-sensitive cryptoecnomic system to address sustainability: https://buff.ly/45OAiF1
  28. Composing Networks of Automated Market Makers: https://buff.ly/45OAiov
  29. DEX business models & moats: https://buff.ly/3OJHVWU
  30. Original Tether whitepaper: https://buff.ly/3C20D4t





  • 이더리움 보내기
import React from "react";
import { useEffect, useState } from "react";
import Web3 from "web3";
import { ethers } from "ethers";

function App() {
  const [blockNumber, setBlockNumber] = useState();
  const [balance, setBalance] = useState();
  const [account, setAccount] = useState();
  const [chainId, setChainId] = useState();

  const web3 = new Web3("wss://goerli.infura.io/ws/v3/c9272c6607724aa08e2432def393cb43");

  async function connect() {
    if(window.ethereum) {
      try {
        const res = await window.ethereum.request({
          method : "eth_requestAccounts",
        });
        setAccount(res[0]);

        getBalance()
      
      } catch(err) {
        console.error(err);
      }
    } else {
      console.log("Install metamask");
    }
  }

  useEffect(()=>{
    async function getBlock() {
      const blockNumber = await web3.eth.getBlockNumber();
      setBlockNumber(Number(blockNumber));
    }

    getBlock();
  })
  
  
  async function getBalance() {
    const res = await window.ethereum.request({
      method : "eth_requestAccounts",
    });

    if(account) {
      const _balance = await window.ethereum.request({
        method : "eth_getBalance",
        params : [res[0].toString(), "latest"]
      });
    
      setBalance(ethers.formatEther(_balance));
    } else {
      console.log("wallet is not connected");
    }
  }
  /*async function getBalance() {
    if(account) {
      const _balance = await web3.eth.getBalance(account);
      setBalance(ethers.formatEther(_balance));
    } else {
      console.log("wallet is not connected");
    }
  }*/

  useEffect(()=> {
     
    getBalance();

    async function subscribeBlock() {
      const subscription = await web3.eth.subscribe("newHeads");
      subscription.on("data", async(blockHead) => {
        setBlockNumber(Number(blockHead.number));
      });
    }

    subscribeBlock();
  })

  async function getChainId() {
    if(window.ethereum) {
      const ID = await window.ethereum.request({
        method : "eth_chainId",
      });
      setChainId(ID);
    }
  }

  getChainId();

  async function chainChanged() {
    if(window.ethereum) {
      setAccount(null);
      setBalance(null);
      connect();
      getChainId();
    }
  }

  useEffect(()=> {
    if(window.ethereum) {
      window.ethereum.on("chainChanged", chainChanged)
    }
  })

  useEffect(()=> {
    if(window.ethereum) {
      window.ethereum.on("accountsChanged", connect)
    }
  })

  async function sendTx(e) {
    e.preventDefault();
    const data = new FormData(e.target);
    console.log(typeof(data.get("amount")));
    /*var a = Number(data.get("amount"));
    a = web3.utils.numberToHex(a);*/
    var a = web3.utils.numberToHex(Number(data.get("amount")));
    console.log(a, typeof(a));

    await window.ethereum.request({
      method : "eth_sendTransaction",
      params : [{from : account, to : data.get("address"), value : a}]
    })
  }

  /*async function sendTx(e) {
    e.preventDefault();
    const data = new FormData(e.target);
    const params = {
      from : account,
      to : data.get("address"),
      value : data.get("amount")
    }
    await web3.eth.sendTransaction(params);
  }*/

  return (
    <div className="App">
      <div onClick={()=>{connect();}}>CONNECT WALLET</div>
      <li>current Block number : {blockNumber}</li>
      <li>current Address : {account}</li>
      <li>currnet balance : {balance} eth</li>
      <li>current chainId : {chainId} </li>
      <form onSubmit={sendTx}>
        <input type="text" name="address" placeholder="write address"></input>
        <input type="text" name="amount" placeholder="write amount"></input>
        <button type="submit">Send TX</button>
      </form>
    </div>
  );
}

export default App;






  • ERC-20 토큰 보내기
import React from "react";
import { useEffect, useState } from "react";
import Web3 from "web3";
import { ethers } from "ethers";
import abi from './abi.json';

function App() {
  const [blockNumber, setBlockNumber] = useState();
  const [balance, setBalance] = useState();
  const [account, setAccount] = useState();
  const [chainId, setChainId] = useState();
  const [tbalance, setTbalance] = useState();

  const web3 = new Web3("wss://goerli.infura.io/ws/v3/02a93bfdcfab460c97fe7ece41eb38d5");
  const web3_2 = new Web3("https://goerli.infura.io/v3/02a93bfdcfab460c97fe7ece41eb38d5");

  var c_addr = '0xf7389e84220FF1165842e38C8e92772846e61A9d';
  var contract = new web3_2.eth.Contract(abi, c_addr);

  async function connect() {
    if(window.ethereum) {
      try {
        const res = await window.ethereum.request({
          method : "eth_requestAccounts",
        });
        setAccount(res[0]);

        getBalance()
        getTbalance();
      
      } catch(err) {
        console.error(err);
      }
    } else {
      console.log("Install metamask");
    }
  }

  async function getTbalance() {
    if(account) {
      try {
        var a = await contract.methods.balanceOf(account).call()
        setTbalance(a);
      } catch(err) {
        console.error(err);
      }
    } else {
      console.log("connect the wallet");
    }
  }
  // getTbalance()

  useEffect(()=>{
    async function getBlock() {
      const blockNumber = await web3.eth.getBlockNumber();
      setBlockNumber(Number(blockNumber));
    }

    getBlock();
  })
  
  
  async function getBalance() {
    const res = await window.ethereum.request({
      method : "eth_requestAccounts",
    });

    if(account) {
      const _balance = await window.ethereum.request({
        method : "eth_getBalance",
        params : [res[0].toString(), "latest"]
      });
    
      setBalance(ethers.formatEther(_balance));
    } else {
      console.log("wallet is not connected");
    }
  }
  /*async function getBalance() {
    if(account) {
      const _balance = await web3.eth.getBalance(account);
      setBalance(ethers.formatEther(_balance));
    } else {
      console.log("wallet is not connected");
    }
  }*/

  useEffect(()=> {

    async function subscribeBlock() {
      const subscription = await web3.eth.subscribe("newHeads");
      subscription.on("data", async(blockHead) => {
        setBlockNumber(Number(blockHead.number));
      });
    }

    subscribeBlock();

    getTbalance();
    getBalance();
  })

  async function getChainId() {
    if(window.ethereum) {
      const ID = await window.ethereum.request({
        method : "eth_chainId",
      });
      setChainId(ID);
    }
  }

  getChainId();

  async function chainChanged() {
    if(window.ethereum) {
      setAccount(null);
      setBalance(null);
      connect();
      getChainId();
    }
  }

  useEffect(()=> {
    if(window.ethereum) {
      window.ethereum.on("chainChanged", chainChanged)
    }
  })

  useEffect(()=> {
    if(window.ethereum) {
      window.ethereum.on("accountsChanged", connect)
    }
  })

  async function sendTx(e) {
    e.preventDefault();
    const data = new FormData(e.target);
    console.log(typeof(data.get("amount")));
    /*var a = Number(data.get("amount"));
    a = web3.utils.numberToHex(a);*/
    var a = web3.utils.numberToHex(Number(data.get("amount")));
    console.log(a, typeof(a));

    await window.ethereum.request({
      method : "eth_sendTransaction",
      params : [{from : account, to : data.get("address"), value : a}]
    })
  }

  /*async function sendTx(e) {
    e.preventDefault();
    const data = new FormData(e.target);
    const params = {
      from : account,
      to : data.get("address"),
      value : data.get("amount")
    }
    await web3.eth.sendTransaction(params);
  }*/

  async function sendERC(e) {
    e.preventDefault();
    const data = new FormData(e.target);
    var a = web3.utils.numberToHex(Number(data.get("amount_2")));

    await window.ethereum.request({
      method : "eth_sendTransaction",
      params : [{from : account, to : c_addr, data : contract.methods.transfer(data.get("address_2"), a).encodeABI()}]
    })
  }

  return (
    <div className="App">
      <div onClick={()=>{connect();}}>CONNECT WALLET</div>
      <li>current Block number : {blockNumber}</li>
      <li>current Address : {account}</li>
      <li>currnet balance : {balance} eth</li>
      <li>current token balance : {tbalance} </li>
      <li>current chainId : {chainId} </li>
      <form onSubmit={sendTx}>
        <input type="text" name="address" placeholder="write address"></input>
        <input type="text" name="amount" placeholder="write amount"></input>
        <button type="submit">Send TX</button>
      </form>
      <form onSubmit={sendERC}>
        <input type="text" name="address_2" placeholder="write address"></input>
        <input type="text" name="amount_2" placeholder="write amount"></input>
        <button type="submit">Send ERC</button>
      </form>
    </div>
  );
}

export default App;






  • ico 해보기
import React from "react";
import { useEffect, useState } from "react";
import Web3 from "web3";
import { ethers } from "ethers";
import abi from './abi.json';
import abi2 from './abi2.json';

function App() {
  const [blockNumber, setBlockNumber] = useState();
  const [balance, setBalance] = useState();
  const [account, setAccount] = useState();
  const [chainId, setChainId] = useState();
  const [tbalance, setTbalance] = useState();

  const web3 = new Web3("wss://goerli.infura.io/ws/v3/02a93bfdcfab460c97fe7ece41eb38d5");
  const web3_2 = new Web3("https://goerli.infura.io/v3/02a93bfdcfab460c97fe7ece41eb38d5");

  var c_addr = '0xf7389e84220FF1165842e38C8e92772846e61A9d'; // 1. 공짜 민팅
  var c_addr_2 = '0x127c6Abf99a85f8852352Bf269ad1073b6F21417'; // 2. 유료 민팅 
  var contract = new web3_2.eth.Contract(abi, c_addr); // 1번
  var contract2 = new web3_2.eth.Contract(abi2, c_addr_2); // 2번

  async function connect() {
    if(window.ethereum) {
      try {
        const res = await window.ethereum.request({
          method : "eth_requestAccounts",
        });
        setAccount(res[0]);

        getBalance()
        getTbalance();
      
      } catch(err) {
        console.error(err);
      }
    } else {
      console.log("Install metamask");
    }
  }

  async function getTbalance() {
    if(account) {
      try {
        var a = await contract2/*필요에 따라 1번, 2번 바꾸기*/.methods.balanceOf(account).call()
        setTbalance(a); /*안되면 setTbalance(Number(a))*/
      } catch(err) {
        console.error(err);
      }
    } else {
      console.log("connect the wallet");
    }
  }
  // getTbalance()

  useEffect(()=>{
    async function getBlock() {
      const blockNumber = await web3.eth.getBlockNumber();
      setBlockNumber(Number(blockNumber));
    }

    getBlock();
  })
  
  
  async function getBalance() {
    const res = await window.ethereum.request({
      method : "eth_requestAccounts",
    });

    if(account) {
      const _balance = await window.ethereum.request({
        method : "eth_getBalance",
        params : [res[0].toString(), "latest"]
      });
    
      setBalance(ethers.formatEther(_balance));
    } else {
      console.log("wallet is not connected");
    }
  }
  /*async function getBalance() {
    if(account) {
      const _balance = await web3.eth.getBalance(account);
      setBalance(ethers.formatEther(_balance));
    } else {
      console.log("wallet is not connected");
    }
  }*/

  useEffect(()=> {

    async function subscribeBlock() {
      const subscription = await web3.eth.subscribe("newHeads");
      subscription.on("data", async(blockHead) => {
        setBlockNumber(Number(blockHead.number));
      });
    }

    subscribeBlock();

    getTbalance();
    getBalance();
  })

  async function getChainId() {
    if(window.ethereum) {
      const ID = await window.ethereum.request({
        method : "eth_chainId",
      });
      setChainId(ID);
    }
  }

  getChainId();

  async function chainChanged() {
    if(window.ethereum) {
      setAccount(null);
      setBalance(null);
      connect();
      getChainId();
    }
  }

  useEffect(()=> {
    if(window.ethereum) {
      window.ethereum.on("chainChanged", chainChanged)
    }
  })

  useEffect(()=> {
    if(window.ethereum) {
      window.ethereum.on("accountsChanged", connect)
    }
  })

  async function sendTx(e) {
    e.preventDefault();
    const data = new FormData(e.target);
    console.log(typeof(data.get("amount")));
    /*var a = Number(data.get("amount"));
    a = web3.utils.numberToHex(a);*/
    var a = web3.utils.numberToHex(Number(data.get("amount")));
    console.log(a, typeof(a));

    await window.ethereum.request({
      method : "eth_sendTransaction",
      params : [{from : account, to : data.get("address"), value : a}]
    })
  }

  /*async function sendTx(e) {
    e.preventDefault();
    const data = new FormData(e.target);
    const params = {
      from : account,
      to : data.get("address"),
      value : data.get("amount")
    }
    await web3.eth.sendTransaction(params);
  }*/

  async function sendERC(e) {
    e.preventDefault();
    const data = new FormData(e.target);
    var a = web3.utils.numberToHex(Number(data.get("amount_2")));

    await window.ethereum.request({
      method : "eth_sendTransaction",
      params : [{from : account, to : c_addr_2/*contract address 바꾸기*/, data : contract2.methods.transfer(data.get("address_2"), a).encodeABI()}]
    })
  }

  async function minting(e) {
    e.preventDefault();
    const data = new FormData(e.target);
    var a = web3.utils.numberToHex(Number(data.get("amount_3")));
    var b = web3.utils.numberToHex(Number(data.get("amount_3"))*10000);

    await window.ethereum.request({
      method : "eth_sendTransaction",
      params : [{
        from : account,
        to : c_addr_2, /*contract address 바꾸기*/
        value : b,
        data : contract2/*contract 1,2번 바꾸기*/.methods.MintToken(a).encodeABI()
      }]
    })
  }

  return (
    <div className="App">
      <div onClick={()=>{connect();}}>CONNECT WALLET</div>
      <li>current Block number : {blockNumber}</li>
      <li>current Address : {account}</li>
      <li>currnet balance : {balance} eth</li>
      <li>current token balance : {tbalance} </li>
      <li>current chainId : {chainId} </li>
      <form onSubmit={sendTx}>
        <input type="text" name="address" placeholder="write address"></input>
        <input type="text" name="amount" placeholder="write amount"></input>
        <button type="submit">Send TX</button>
      </form>
      <form onSubmit={sendERC}>
        <input type="text" name="address_2" placeholder="write address"></input>
        <input type="text" name="amount_2" placeholder="write amount"></input>
        <button type="submit">Send ERC</button>
      </form>
      <form onSubmit={minting}>
        <input type="text" name="amount_3" placeholder="write amount"></input>
        <button type="submit">Mint</button>
      </form>
    </div>
  );
}

export default App;
profile
개발자가 되어가는 개린이"

0개의 댓글