From 38be892bcd6caaabe671573f336bafb91ad36a61 Mon Sep 17 00:00:00 2001 From: xiaodao Date: Sat, 17 Sep 2022 18:34:51 +0800 Subject: [PATCH 1/2] Add otc.sol --- src/pages/defi/otc/index.md | 14 ++++++++++++ src/pages/defi/otc/otc.sol | 44 +++++++++++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+) create mode 100644 src/pages/defi/otc/index.md create mode 100644 src/pages/defi/otc/otc.sol diff --git a/src/pages/defi/otc/index.md b/src/pages/defi/otc/index.md new file mode 100644 index 000000000..30e9efc37 --- /dev/null +++ b/src/pages/defi/otc/index.md @@ -0,0 +1,14 @@ +--- +title: OTC Exchange Examples +version: 0.8.13 +description: OTC Exchange examples +--- + +### OTC Exchange Examples + +```solidity +{{{otc}}} +``` + +### Links + diff --git a/src/pages/defi/otc/otc.sol b/src/pages/defi/otc/otc.sol new file mode 100644 index 000000000..a7d3a0bf5 --- /dev/null +++ b/src/pages/defi/otc/otc.sol @@ -0,0 +1,44 @@ +// SPDX-License-Identifier: GPL-3.0 + +pragma solidity >=0.7.0 <0.9.0; + +contract TokenExchange { + address fromAddress; + address fromToken; + uint256 fromAmount; + address toToken; + uint256 toAmount; + + function CreateExchange(address _fromToken, address _toToken, uint256 _fromAmount, uint256 _toAmount) public { + IERC20(_fromToken).transferFrom(msg.sender, address(this), _fromAmount); + fromAddress = msg.sender; + fromToken = _fromToken; + fromAmount = _fromAmount; + toToken = _toToken; + toAmount = _toAmount; + } + + function DoChange() public { + IERC20(toToken).transferFrom(msg.sender, address(this), toAmount); + IERC20(fromToken).transfer(msg.sender, fromAmount); + IERC20(toToken).transfer(fromAddress, toAmount); + } +} + +interface IERC20 { + function totalSupply() external view returns (uint256); + + function balanceOf(address who) external view returns (uint256); + + function allowance(address owner, address spender) external view returns (uint256); + + function transfer(address to, uint256 value) external returns (bool); + + function approve(address spender, uint256 value) external returns (bool); + + function transferFrom(address from, address to, uint256 value) external returns (bool); + + event Transfer(address indexed from, address indexed to, uint256 value); + + event Approval(address indexed owner, address indexed spender, uint256 value); +} \ No newline at end of file From c4639db566319cb9f5e08150ea93e1244e7a4860 Mon Sep 17 00:00:00 2001 From: xiaodao Date: Sun, 18 Sep 2022 09:01:15 +0800 Subject: [PATCH 2/2] Cont. --- src/pages/defi/otc/index.md | 9 ++++- src/pages/defi/otc/otc.sol | 53 +++++++++++++++++--------- src/pages/defi/otc/single_desk_otc.sol | 44 +++++++++++++++++++++ 3 files changed, 85 insertions(+), 21 deletions(-) create mode 100644 src/pages/defi/otc/single_desk_otc.sol diff --git a/src/pages/defi/otc/index.md b/src/pages/defi/otc/index.md index 30e9efc37..e9a65258b 100644 --- a/src/pages/defi/otc/index.md +++ b/src/pages/defi/otc/index.md @@ -4,11 +4,16 @@ version: 0.8.13 description: OTC Exchange examples --- -### OTC Exchange Examples +### Single Desk OTC Exchange Examples ```solidity -{{{otc}}} +{{{single_desk_otc}}} ``` +### Multi-Desk OTC Exchange Examples + +```solidity +{{{otc}}} +``` ### Links diff --git a/src/pages/defi/otc/otc.sol b/src/pages/defi/otc/otc.sol index a7d3a0bf5..025d6e6d0 100644 --- a/src/pages/defi/otc/otc.sol +++ b/src/pages/defi/otc/otc.sol @@ -1,27 +1,42 @@ // SPDX-License-Identifier: GPL-3.0 - pragma solidity >=0.7.0 <0.9.0; -contract TokenExchange { - address fromAddress; - address fromToken; - uint256 fromAmount; - address toToken; - uint256 toAmount; - - function CreateExchange(address _fromToken, address _toToken, uint256 _fromAmount, uint256 _toAmount) public { - IERC20(_fromToken).transferFrom(msg.sender, address(this), _fromAmount); - fromAddress = msg.sender; - fromToken = _fromToken; - fromAmount = _fromAmount; - toToken = _toToken; - toAmount = _toAmount; +contract OTC { + uint256 public counter; + + struct Order { + address owner; + address bid_token; + address ask_token; + uint bid_amount; + uint ask_amount; + } + + Order[] public orders; + + function give(address bid_token, address ask_token, uint bid_amount, uint ask_amount) public { + IERC20(bid_token).transferFrom(msg.sender, address(this), bid_amount); + orders.push(Order({ + owner: msg.sender, + bid_token: bid_token, + ask_token: ask_token, + bid_amount: bid_amount, + ask_amount: ask_amount + })); + } + + function take(uint id) public { + IERC20(orders[id].ask_token).transferFrom(msg.sender, orders[id].owner, orders[id].ask_amount); + IERC20(orders[id].bid_token).transfer(msg.sender, orders[id].bid_amount); + orders[id].bid_amount = 0; + orders[id].ask_amount = 0; } - function DoChange() public { - IERC20(toToken).transferFrom(msg.sender, address(this), toAmount); - IERC20(fromToken).transfer(msg.sender, fromAmount); - IERC20(toToken).transfer(fromAddress, toAmount); + function withdraw(uint id) public { + require(msg.sender == orders[id].owner, "Owner mismatch"); + IERC20(orders[id].bid_token).transfer(msg.sender, orders[id].bid_amount); + orders[id].bid_amount = 0; + orders[id].ask_amount = 0; } } diff --git a/src/pages/defi/otc/single_desk_otc.sol b/src/pages/defi/otc/single_desk_otc.sol new file mode 100644 index 000000000..a7d3a0bf5 --- /dev/null +++ b/src/pages/defi/otc/single_desk_otc.sol @@ -0,0 +1,44 @@ +// SPDX-License-Identifier: GPL-3.0 + +pragma solidity >=0.7.0 <0.9.0; + +contract TokenExchange { + address fromAddress; + address fromToken; + uint256 fromAmount; + address toToken; + uint256 toAmount; + + function CreateExchange(address _fromToken, address _toToken, uint256 _fromAmount, uint256 _toAmount) public { + IERC20(_fromToken).transferFrom(msg.sender, address(this), _fromAmount); + fromAddress = msg.sender; + fromToken = _fromToken; + fromAmount = _fromAmount; + toToken = _toToken; + toAmount = _toAmount; + } + + function DoChange() public { + IERC20(toToken).transferFrom(msg.sender, address(this), toAmount); + IERC20(fromToken).transfer(msg.sender, fromAmount); + IERC20(toToken).transfer(fromAddress, toAmount); + } +} + +interface IERC20 { + function totalSupply() external view returns (uint256); + + function balanceOf(address who) external view returns (uint256); + + function allowance(address owner, address spender) external view returns (uint256); + + function transfer(address to, uint256 value) external returns (bool); + + function approve(address spender, uint256 value) external returns (bool); + + function transferFrom(address from, address to, uint256 value) external returns (bool); + + event Transfer(address indexed from, address indexed to, uint256 value); + + event Approval(address indexed owner, address indexed spender, uint256 value); +} \ No newline at end of file