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

Fix createSwap #6

Merged
merged 5 commits into from
Oct 19, 2021
Merged
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
6 changes: 6 additions & 0 deletions packages/hardhat/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
artifacts/
cache/
node_modules/
deployments/
.env
.DS_Store
7 changes: 7 additions & 0 deletions packages/hardhat/.solhint.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"extends": "solhint:recommended",
"rules": {
"compiler-version": ["error", "^0.8.9"],
"func-visibility": ["warn", { "ignoreConstructors": true }]
}
}
11 changes: 11 additions & 0 deletions packages/hardhat/contracts/DummyERC20.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
//SPDX-License-Identifier: Apache-2.0
pragma solidity ^0.8.9;

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


contract DummyERC20 is ERC20("Dummy ERC20", "DERC20") {
constructor() {
_mint(msg.sender, 1000 * 10 ** 18);
}
}
49 changes: 47 additions & 2 deletions packages/hardhat/contracts/MoonSwapV1.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol";
import "@openzeppelin/contracts/utils/Counters.sol";
import "@openzeppelin/contracts/security/ReentrancyGuard.sol";
import "@openzeppelin/contracts/utils/structs/EnumerableSet.sol";

contract MoonSwap is ReentrancyGuard {
using SafeERC20 for IERC20;
using Counters for Counters.Counter;
using EnumerableSet for EnumerableSet.UintSet;

struct Swap {
IERC20 inToken;
Expand All @@ -21,10 +23,24 @@ contract MoonSwap is ReentrancyGuard {
}

Counters.Counter public swapId;
EnumerableSet.UintSet private activeSwaps;
mapping(uint256 => Swap) public swaps;
mapping(address => uint256) public claimableFees;

address public admin;

event NewSwap(
uint256 indexed id,
address inToken,
address outToken,
uint256 inTokens,
uint256 outTokens,
address inTokenParty,
address outTokenParty
);
event RevokeSwap(uint256 indexed id);
event CloseSwap(uint256 indexed id, address outTokenParty);

constructor() {
admin = msg.sender;
}
Expand All @@ -51,10 +67,20 @@ contract MoonSwap is ReentrancyGuard {
_outTokens, // tokensOut
msg.sender, // tokensInParty
_tokenOutParty, // tokensOutParty
false // status
true // status
);
activeSwaps.add(swapId.current());
swaps[swapId.current()] = newSwap;
swapId.increment();
emit NewSwap(
swapId.current() - 1,
address(_inToken),
address(_outToken),
_inTokens,
_outTokens,
msg.sender,
_tokenOutParty
);
return swapId.current() - 1;
}

Expand All @@ -67,8 +93,27 @@ contract MoonSwap is ReentrancyGuard {
uint256 fee = (5*_outTokens)/1000;
claimableFees[address(newSwap.outToken)] += fee;
delete swaps[_swapId]; // get those sweet gas refunds!
activeSwaps.remove(_swapId);
newSwap.outToken.safeTransfer(newSwap.inTokenParty, _outTokens - fee);
newSwap.inToken.safeTransfer(msg.sender, newSwap.tokensIn);
return newSwap.tokensIn;
emit CloseSwap(_swapId, msg.sender);
return _outTokens;
}

function revokeSwap(uint256 _swapId) external nonReentrant returns (uint256) {
Swap memory swap = swaps[_swapId];
require(swap.status, "SWAP_INACTIVE");
require(swap.inTokenParty == msg.sender, "UNAUTHORIZED");
uint256 returnFee = (5*swap.tokensIn)/1000;
claimableFees[address(swap.inToken)] -= returnFee;
delete swaps[_swapId]; // get those sweet gas refunds!
activeSwaps.remove(_swapId);
swap.inToken.safeTransfer(msg.sender, swap.tokensIn + returnFee);
emit RevokeSwap(_swapId);
return swap.tokensIn + returnFee;
}

function getActiveSwaps() external view returns(uint256[] memory) {
return activeSwaps.values();
}
}
17 changes: 17 additions & 0 deletions packages/hardhat/contracts/dETH.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
//SPDX-License-Identifier: Apache-2.0
pragma solidity ^0.8.9;

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

// todo: Do we not need any other functionality here? JR

// dummyToken Contract to test out TokenDistribution locally
contract dETH is ERC20 {
constructor() public ERC20("dummyETH", "dETH") {
_mint(msg.sender, 10000000e18);
}

function mint4Me(uint256 amount) public {
_mint(msg.sender, amount);
}
}
7 changes: 6 additions & 1 deletion packages/hardhat/deploy/00_deploy_your_contract.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ module.exports = async ({ getNamedAccounts, getChainId, deployments }) => {
await deploy("MoonSwap", {
// Learn more about args here: https://www.npmjs.com/package/hardhat-deploy#deploymentsdeploy
from: deployer,
//args: [ "Hello", ethers.utils.parseEther("1.5") ],
// args: [ "Hello", ethers.utils.parseEther("1.5") ],
log: true,
});

Expand Down Expand Up @@ -73,6 +73,11 @@ module.exports = async ({ getNamedAccounts, getChainId, deployments }) => {
from: deployer,
log: true,
});

await deploy("dETH", {
from: deployer,
log: true,
});
}
};
module.exports.tags = ["MoonSwapV1"];
1 change: 0 additions & 1 deletion packages/hardhat/deployments/localhost/.chainId

This file was deleted.

369 changes: 0 additions & 369 deletions packages/hardhat/deployments/localhost/MoonSwap.json

This file was deleted.

Loading