Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add otc.sol #234

Open
wants to merge 2 commits into
base: gh-pages
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions src/pages/defi/otc/index.md
Original file line number Diff line number Diff line change
@@ -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

59 changes: 59 additions & 0 deletions src/pages/defi/otc/otc.sol
Original file line number Diff line number Diff line change
@@ -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);
}
44 changes: 44 additions & 0 deletions src/pages/defi/otc/single_desk_otc.sol
Original file line number Diff line number Diff line change
@@ -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);
}