diff --git a/src/pages/defi/otc/index.md b/src/pages/defi/otc/index.md new file mode 100644 index 000000000..e9a65258b --- /dev/null +++ b/src/pages/defi/otc/index.md @@ -0,0 +1,19 @@ +--- +title: OTC Exchange Examples +version: 0.8.13 +description: OTC Exchange examples +--- + +### Single Desk OTC Exchange Examples + +```solidity +{{{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 new file mode 100644 index 000000000..025d6e6d0 --- /dev/null +++ b/src/pages/defi/otc/otc.sol @@ -0,0 +1,59 @@ +// SPDX-License-Identifier: GPL-3.0 +pragma solidity >=0.7.0 <0.9.0; + +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 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; + } +} + +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 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