Skip to content

Commit

Permalink
feat: add hardhat ignition
Browse files Browse the repository at this point in the history
  • Loading branch information
afistfulofstraycathair committed Jul 31, 2024
1 parent 0b46430 commit fafdbf2
Show file tree
Hide file tree
Showing 23 changed files with 93,123 additions and 474 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
4 changes: 1 addition & 3 deletions contracts/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ node_modules
# Hardhat files
/cache
/artifacts
/deployments

# TypeChain files
/typechain
Expand All @@ -13,8 +14,5 @@ node_modules
/coverage
/coverage.json

# Hardhat Ignition Files
/ignition

# Hardhat Ignition default folder for deployments against a local node
/ignition/deployments/chain-31337
193 changes: 97 additions & 96 deletions contracts/contracts/SwapOracle.sol
Original file line number Diff line number Diff line change
@@ -1,99 +1,100 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

import "@openzeppelin/contracts/token/ERC20/IERC20.sol";

interface IOracle {
function oracleValues(string memory key) external view returns (string memory value, uint256 blockNumber);
}

contract TokenSwap {

IERC20 public wbtc;
IERC20 public weth;
IERC20 public usdc;
IOracle public oracle;
string public constant ORACLE_KEY_BTC = "BTC/USD";
string public constant ORACLE_KEY_ETH = "ETH/USD";
uint256 public constant DECIMALS = 18;
uint256 public constant FIXED_USDC_PRICE = 1 * 10**DECIMALS;

constructor(IERC20 _wbtc, IERC20 _weth, IERC20 _usdc, IOracle _oracle) {
wbtc = _wbtc;
weth = _weth;
usdc = _usdc;
oracle = _oracle;
}

function _getPriceFromOracle(string memory key) internal view returns (uint256) {
(string memory priceStr,) = oracle.oracleValues(key);
return _stringToUint(priceStr);
}

function _stringToUint(string memory s) internal pure returns (uint256) {
bytes memory b = bytes(s);
uint256 result = 0;
bool decimalPoint = false;
uint256 decimalPlaces = 0;

for (uint256 i = 0; i < b.length; i++) {
if (b[i] >= 0x30 && b[i] <= 0x39) {
result = result * 10 + (uint256(uint8(b[i])) - 48);
if (decimalPoint) {
decimalPlaces++;
}
} else if (b[i] == 0x2E) {
decimalPoint = true;
}
}

// Adjust the result to have 18 decimal places
if (decimalPlaces < DECIMALS) {
result = result * (10**(DECIMALS - decimalPlaces));
} else if (decimalPlaces > DECIMALS) {
result = result / (10**(decimalPlaces - DECIMALS));
}

return result;
}

function swapWBTCToUSDC(uint256 amountWBTC) external returns (uint256) {
uint256 priceWBTC = _getPriceFromOracle(ORACLE_KEY_BTC);
uint256 amountUSDC = amountWBTC.mul(priceWBTC).div(FIXED_USDC_PRICE);

require(wbtc.transferFrom(msg.sender, address(this), amountWBTC), "WBTC transfer failed");
require(usdc.transfer(msg.sender, amountUSDC), "USDC transfer failed");

return amountUSDC;
}

function swapWETHToUSDC(uint256 amountWETH) external returns (uint256) {
uint256 priceWETH = _getPriceFromOracle(ORACLE_KEY_ETH);
uint256 amountUSDC = amountWETH.mul(priceWETH).div(FIXED_USDC_PRICE);

require(weth.transferFrom(msg.sender, address(this), amountWETH), "WETH transfer failed");
require(usdc.transfer(msg.sender, amountUSDC), "USDC transfer failed");

return amountUSDC;
}

function swapUSDCToWBTC(uint256 amountUSDC) external returns (uint256) {
uint256 priceWBTC = _getPriceFromOracle(ORACLE_KEY_BTC);
uint256 amountWBTC = amountUSDC.mul(FIXED_USDC_PRICE).div(priceWBTC);

require(usdc.transferFrom(msg.sender, address(this), amountUSDC), "USDC transfer failed");
require(wbtc.transfer(msg.sender, amountWBTC), "WBTC transfer failed");

return amountWBTC;
}

function swapUSDCToWETH(uint256 amountUSDC) external returns (uint256) {
uint256 priceWETH = _getPriceFromOracle(ORACLE_KEY_ETH);
uint256 amountWETH = amountUSDC.mul(FIXED_USDC_PRICE).div(priceWETH);

require(usdc.transferFrom(msg.sender, address(this), amountUSDC), "USDC transfer failed");
require(weth.transfer(msg.sender, amountWETH), "WETH transfer failed");

return amountWETH;
}
}
// import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
// import "@openzeppelin/contracts/utils/math/Math.sol";

// interface IOracle {
// function oracleValues(string memory key) external view returns (string memory value, uint256 blockNumber);
// }

// contract TokenSwap {

// IERC20 public wbtc;
// IERC20 public weth;
// IERC20 public usdc;
// IOracle public oracle;
// string public constant ORACLE_KEY_BTC = "BTC/USD";
// string public constant ORACLE_KEY_ETH = "ETH/USD";
// uint256 public constant DECIMALS = 18;
// uint256 public constant FIXED_USDC_PRICE = 1 * 10**DECIMALS;

// constructor(IERC20 _wbtc, IERC20 _weth, IERC20 _usdc, IOracle _oracle) {
// wbtc = _wbtc;
// weth = _weth;
// usdc = _usdc;
// oracle = _oracle;
// }

// function _getPriceFromOracle(string memory key) internal view returns (uint256) {
// (string memory priceStr,) = oracle.oracleValues(key);
// return _stringToUint(priceStr);
// }

// function _stringToUint(string memory s) internal pure returns (uint256) {
// bytes memory b = bytes(s);
// uint256 result = 0;
// bool decimalPoint = false;
// uint256 decimalPlaces = 0;

// for (uint256 i = 0; i < b.length; i++) {
// if (b[i] >= 0x30 && b[i] <= 0x39) {
// result = result * 10 + (uint256(uint8(b[i])) - 48);
// if (decimalPoint) {
// decimalPlaces++;
// }
// } else if (b[i] == 0x2E) {
// decimalPoint = true;
// }
// }

// // Adjust the result to have 18 decimal places
// if (decimalPlaces < DECIMALS) {
// result = result * (10**(DECIMALS - decimalPlaces));
// } else if (decimalPlaces > DECIMALS) {
// result = result / (10**(decimalPlaces - DECIMALS));
// }

// return result;
// }

// function swapWBTCToUSDC(uint256 amountWBTC) external returns (uint256) {
// uint256 priceWBTC = _getPriceFromOracle(ORACLE_KEY_BTC);
// uint256 amountUSDC = amountWBTC.mul(priceWBTC).div(FIXED_USDC_PRICE);

// require(wbtc.transferFrom(msg.sender, address(this), amountWBTC), "WBTC transfer failed");
// require(usdc.transfer(msg.sender, amountUSDC), "USDC transfer failed");

// return amountUSDC;
// }

// function swapWETHToUSDC(uint256 amountWETH) external returns (uint256) {
// uint256 priceWETH = _getPriceFromOracle(ORACLE_KEY_ETH);
// uint256 amountUSDC = amountWETH.mul(priceWETH).div(FIXED_USDC_PRICE);

// require(weth.transferFrom(msg.sender, address(this), amountWETH), "WETH transfer failed");
// require(usdc.transfer(msg.sender, amountUSDC), "USDC transfer failed");

// return amountUSDC;
// }

// function swapUSDCToWBTC(uint256 amountUSDC) external returns (uint256) {
// uint256 priceWBTC = _getPriceFromOracle(ORACLE_KEY_BTC);
// uint256 amountWBTC = amountUSDC.mul(FIXED_USDC_PRICE).div(priceWBTC);

// require(usdc.transferFrom(msg.sender, address(this), amountUSDC), "USDC transfer failed");
// require(wbtc.transfer(msg.sender, amountWBTC), "WBTC transfer failed");

// return amountWBTC;
// }

// function swapUSDCToWETH(uint256 amountUSDC) external returns (uint256) {
// uint256 priceWETH = _getPriceFromOracle(ORACLE_KEY_ETH);
// uint256 amountWETH = amountUSDC.mul(FIXED_USDC_PRICE).div(priceWETH);

// require(usdc.transferFrom(msg.sender, address(this), amountUSDC), "USDC transfer failed");
// require(weth.transfer(msg.sender, amountWETH), "WETH transfer failed");

// return amountWETH;
// }
// }
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ interface IOracle {
function oracleValues(string memory key) external view returns (string memory value, uint256 blockNumber);
}

contract TokenSwap {
contract ThreeSwap {
enum TokenType { WBTC, WETH, USDC }

IERC20[3] public tokens;
Expand Down Expand Up @@ -80,4 +80,4 @@ contract TokenSwap {

return amountTo;
}
}
}
4 changes: 2 additions & 2 deletions contracts/contracts/Tokens/WETH.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ pragma solidity ^0.8.0;
import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
import "@openzeppelin/contracts/access/Ownable.sol";

contract USDC is ERC20, Ownable {
constructor() ERC20("USDC", "USDC") Ownable(msg.sender) {}
contract WETH is ERC20, Ownable {
constructor() ERC20("WETH", "WETH") Ownable(msg.sender) {}

function mint(address to, uint256 amount) external onlyOwner {
_mint(to, amount);
Expand Down
5 changes: 2 additions & 3 deletions contracts/hardhat.config.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { HardhatUserConfig } from "hardhat/config";
import "@nomiclabs/hardhat-ethers";
import "@nomiclabs/hardhat-waffle";
import "@nomicfoundation/hardhat-ignition-ethers";

const config: HardhatUserConfig = {
solidity: "0.8.0",
solidity: "0.8.20",
paths: {
sources: "./contracts",
tests: "./test",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"_format": "hh-sol-dbg-1",
"buildInfo": "..\\build-info\\8b024a322d9f1896d0bc14bae4af1018.json"
}
Loading

0 comments on commit fafdbf2

Please sign in to comment.