// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import "@openzeppelin/contracts/access/Ownable.sol";
import "@openzeppelin/contracts/utils/math/SafeMath.sol";
contract Wallet is Ownable{
using SafeMath for uint256;
modifier tokenExist(bytes32 ticker){
require(tokenMapping[ticker].tokenAddress != address(0));
_;
}
struct Token{
bytes32 ticker;
address tokenAddress;
}
mapping(bytes32 => Token)public tokenMapping;
mapping(address => mapping(bytes32 => uint256))public balances;
bytes32[]public tokenList;
function addToken(bytes32 ticker, address tokenAddress) external onlyOwner {
tokenMapping[ticker] = Token(ticker, tokenAddress);
tokenList.push(ticker);
}
function deposit(uint256 amount, bytes32 ticker )external {
IERC20(tokenMapping[ticker].tokenAddress).transferFrom(msg.sender, address(this), amount);
balances[msg.sender][ticker] = balances[msg.sender][ticker].add(amount);
}
function withdraw(bytes32 ticker, uint256 amount)external tokenExist(ticker){
require(balances[msg.sender][ticker] >= amount);
balances[msg.sender][ticker] = balances[msg.sender][ticker].sub(amount);
IERC20(tokenMapping[ticker].tokenAddress).transfer(msg.sender, amount);
}
}
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import "../node_modules/@openzeppelin/contracts/token/ERC20/ERC20.sol";
contract Link is ERC20 {
constructor() ERC20("Chainlink", "Link") {
_mint(msg.sender, 1000);
}
}
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
pragma experimental ABIEncoderV2;
import "./Wallet.sol";
contract Dex is Wallet {
enum Side {
BUY,
SELL
}
struct Order {
uint id;
address trader;
bool buyOrder;
bytes32 ticker;
uint amount;
uint price;
}
mapping(bytes32 => mapping(uint => Order[]))public orderBook;
function getOrderBook(bytes32 ticker, Side side)view public returns(Order[] memory){
return orderBook[ticker][uint(side)];
}
/*function createLimitOrder()public{
}*/
}
// SPDX-License-Identifier: MIT
pragma solidity >=0.4.22 <0.9.0;
contract Migrations {
address public owner = msg.sender;
uint public last_completed_migration;
modifier restricted() {
require(
msg.sender == owner,
"This function is restricted to the contract's owner"
);
_;
}
function setCompleted(uint completed) public restricted {
last_completed_migration = completed;
}
}
const Link = artifacts.require("Link");
const Dex = artifacts.require("Dex");
module.exports = async function (deployer, network, accounts) {
await deployer.deploy(Link);
let dex = await Dex.deployed()
let link = await Link.deployed()
await link.approve(dex.address, 500)
dex.addToken(web3.utils.fromUtf8("Link"), link.address)
/*await dex.deposit(100, web3.utils.fromUtf8("Link")) */
let balanceOfLink = await dex.balances(accounts[0], web3.utils.fromUtf8("Link"));
console.log(balanceOfLink);
};
const Link = artifacts.require("Link");
const Dex = artifacts.require("Dex");
module.exports = async function (deployer, network, accounts) {
await deployer.deploy(Link);
let dex = await Dex.deployed()
let link = await Link.deployed()
await link.approve(dex.address, 500)
dex.addToken(web3.utils.fromUtf8("Link"), link.address)
/*await dex.deposit(100, web3.utils.fromUtf8("Link")) */
let balanceOfLink = await dex.balances(accounts[0], web3.utils.fromUtf8("Link"));
console.log(balanceOfLink);
};