# KrypoCamp 第三週實體 ### 打造自動做市商交易 - 部署至 Remix - 建立 ERC20 Token 作為交易用代幣 - 添加 ETH 與 ERC20 Token 的流動性 - 透過 remix 其他錢包地址交換代幣 > 1. 部署Exchange時,需傳入ERC20部署的地址 > 2. 部署Exchange完,ERC20需Approve Exchange的地址和數量 > 3. addLiquidity : 增加流動性 > 4. ethToTokenTransferInput : eth換ERC20 Token #### UniswapV1Exchange ``` solidity= //SPDX-License-Identifier: Unlicense pragma solidity ^0.8.0; import "@openzeppelin/contracts/token/ERC20/IERC20.sol"; import "@openzeppelin/contracts/token/ERC20/ERC20.sol"; import "hardhat/console.sol"; contract Exchange is ERC20 { IERC20 public token; constructor(IERC20 _token) ERC20("Exchange", "EX") { token = _token; } function addLiquidity( uint256 minLiquidity, uint256 maxTokens, uint256 deadline ) public payable returns (uint256) { uint256 liquidity = totalSupply(); if (liquidity > 0) { uint256 ethReserve = address(this).balance - msg.value; uint256 tokenReserve = token.balanceOf(address(this)); uint256 tokenAmount = (msg.value * tokenReserve) / ethReserve + 1; uint256 liquidityMinted = (msg.value * liquidity) / ethReserve; require( maxTokens >= tokenAmount && liquidityMinted >= minLiquidity, "LMT" ); _mint(msg.sender, liquidityMinted); token.transferFrom(msg.sender, address(this), tokenAmount); return totalSupply(); } else { uint256 initialLiquidity = address(this).balance; _mint(msg.sender, initialLiquidity); token.transferFrom(msg.sender, address(this), maxTokens); return totalSupply(); } } function getInputPrice( uint256 inputAmount, uint256 inputReserve, uint256 outputReserve ) internal pure returns (uint256) { require(inputReserve > 0 && outputReserve > 0, "LIO"); uint256 inputAmountWithFee = inputAmount * 997; uint256 numerator = inputAmountWithFee * outputReserve; uint256 denominator = (inputReserve * 1000) + inputAmountWithFee; return numerator / denominator; } function ethToTokenTransferInput(uint256 minTokens) public payable returns (uint256) { uint256 ethSold = msg.value; uint256 tokenReserve = token.balanceOf(address(this)); uint256 inputReserve = address(this).balance - ethSold; uint256 tokensBought = getInputPrice( ethSold, inputReserve, tokenReserve ); token.transfer(msg.sender, tokensBought); require(tokensBought >= minTokens, "LMT"); return tokensBought; } } ``` --- ### 打造多簽錢包 - 部署至 Rinkeby 並且在 remix UI 上直接互相進行操作 - 分工負責建立多簽錢包(將多簽錢包地址丟到 Discord 上) - 分工負責存入 ETH - 分工負責提案轉出 ETH(錢包任意) - 至少 3 個人以上同意轉移 - 驗證轉移成功 延伸讀物 : [多重簽名錢包](https://academy.binance.com/zt/articles/what-is-a-multisig-wallet) ``` solidity= // SPDX-License-Identifier: MIT pragma solidity ^0.8.13; contract MultiSigWallet { event Deposit(address indexed sender, uint amount, uint balance); event SubmitTransaction( address indexed owner, uint indexed txIndex, address indexed to, uint value, bytes data ); event ConfirmTransaction(address indexed owner, uint indexed txIndex); event RevokeConfirmation(address indexed owner, uint indexed txIndex); event ExecuteTransaction(address indexed owner, uint indexed txIndex); address[] public owners; mapping(address => bool) public isOwner; uint public numConfirmationsRequired; struct Transaction { address to; uint value; bytes data; bool executed; uint numConfirmations; } // mapping from tx index => owner => bool mapping(uint => mapping(address => bool)) public isConfirmed; Transaction[] public transactions; modifier onlyOwner() { require(isOwner[msg.sender], "not owner"); _; } modifier txExists(uint _txIndex) { require(_txIndex < transactions.length, "tx does not exist"); _; } modifier notExecuted(uint _txIndex) { require(!transactions[_txIndex].executed, "tx already executed"); _; } modifier notConfirmed(uint _txIndex) { require(!isConfirmed[_txIndex][msg.sender], "tx already confirmed"); _; } constructor(address[] memory _owners, uint _numConfirmationsRequired) { require(_owners.length > 0, "owners required"); require( _numConfirmationsRequired > 0 && _numConfirmationsRequired <= _owners.length, "invalid number of required confirmations" ); for (uint i = 0; i < _owners.length; i++) { address owner = _owners[i]; require(owner != address(0), "invalid owner"); require(!isOwner[owner], "owner not unique"); isOwner[owner] = true; owners.push(owner); } numConfirmationsRequired = _numConfirmationsRequired; } receive() external payable { emit Deposit(msg.sender, msg.value, address(this).balance); } function submitTransaction( address _to, uint _value, bytes memory _data ) public onlyOwner { uint txIndex = transactions.length; transactions.push( Transaction({ to: _to, value: _value, data: _data, executed: false, numConfirmations: 0 }) ); emit SubmitTransaction(msg.sender, txIndex, _to, _value, _data); } function confirmTransaction(uint _txIndex) public onlyOwner txExists(_txIndex) notExecuted(_txIndex) notConfirmed(_txIndex) { Transaction storage transaction = transactions[_txIndex]; transaction.numConfirmations += 1; isConfirmed[_txIndex][msg.sender] = true; emit ConfirmTransaction(msg.sender, _txIndex); } function executeTransaction(uint _txIndex) public onlyOwner txExists(_txIndex) notExecuted(_txIndex) { Transaction storage transaction = transactions[_txIndex]; require( transaction.numConfirmations >= numConfirmationsRequired, "cannot execute tx" ); transaction.executed = true; (bool success, ) = transaction.to.call{value: transaction.value}( transaction.data ); require(success, "tx failed"); emit ExecuteTransaction(msg.sender, _txIndex); } function revokeConfirmation(uint _txIndex) public onlyOwner txExists(_txIndex) notExecuted(_txIndex) { Transaction storage transaction = transactions[_txIndex]; require(isConfirmed[_txIndex][msg.sender], "tx not confirmed"); transaction.numConfirmations -= 1; isConfirmed[_txIndex][msg.sender] = false; emit RevokeConfirmation(msg.sender, _txIndex); } function getOwners() public view returns (address[] memory) { return owners; } function getTransactionCount() public view returns (uint) { return transactions.length; } function getTransaction(uint _txIndex) public view returns ( address to, uint value, bytes memory data, bool executed, uint numConfirmations ) { Transaction storage transaction = transactions[_txIndex]; return ( transaction.to, transaction.value, transaction.data, transaction.executed, transaction.numConfirmations ); } } ```