Skip to content

Commit

Permalink
thanks shark0der
Browse files Browse the repository at this point in the history
  • Loading branch information
yakuhito committed Sep 10, 2021
1 parent 0d4405d commit 1490b08
Show file tree
Hide file tree
Showing 6 changed files with 323 additions and 247 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Expand Up @@ -4,3 +4,5 @@ package-lock.json
#Hardhat files
cache
artifacts

.env
140 changes: 140 additions & 0 deletions contracts/YakuSwap.sol
@@ -0,0 +1,140 @@
//SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

import "@openzeppelin/contracts/access/Ownable.sol";

contract YakuSwap is Ownable {

// Uninitialized - Default status (if swaps[index] doesn't exist, status will get this value)
// Created - the swap was created, but the mone is still in the contract
// Completed - the money has been sent to 'toAddress' (swap successful)
// Cancelled - the money has been sent to 'fromAddress' (maxBlockHeight was reached)
enum SwapStatus {Uninitialized, Created, Completed, Cancelled}

mapping(bytes32 => SwapStatus) public swaps;

uint public totalFees = 0;

uint constant public MAX_BLOCK_HEIGHT = 256;

event SwapCreated(
bytes32 indexed swapHash,
address indexed fromAddress,
address indexed toAddress,
uint value,
bytes32 secretHash,
uint blockNumber
);

function _getSwapHash(
address fromAddress,
address toAddress,
uint value,
bytes32 secretHash,
uint blockNumber
) internal view returns (bytes32) {
return keccak256(
abi.encode(
fromAddress,
toAddress,
value,
secretHash,
blockNumber,
block.chainid
)
);
}

function getSwapHash(
address fromAddress,
address toAddress,
uint value,
bytes32 secretHash,
uint blockNumber
) external view returns (bytes32) {
return _getSwapHash(
fromAddress, toAddress, value, secretHash, blockNumber
);
}

function createSwap(address toAddress, bytes32 secretHash) payable external {
require(toAddress != address(0), "Destination address cannot be zero");

bytes32 swapHash = _getSwapHash(
msg.sender,
toAddress,
msg.value,
secretHash,
block.number
);

require(swaps[swapHash] == SwapStatus.Uninitialized);
swaps[swapHash] = SwapStatus.Created;

emit SwapCreated(
swapHash,
msg.sender,
toAddress,
msg.value,
secretHash,
block.number
);
}

function completeSwap(
address fromAddress,
address toAddress,
uint value,
uint blockNumber,
string memory secret
) external {
bytes32 secretHash = sha256(abi.encodePacked(secret));
bytes32 swapHash = _getSwapHash(
fromAddress,
toAddress,
value,
secretHash,
blockNumber
);

require(swaps[swapHash] == SwapStatus.Created, "Invalid swap data or swap already completed");
require(block.number < blockNumber + MAX_BLOCK_HEIGHT, "Deadline exceeded");
swaps[swapHash] = SwapStatus.Completed;

uint swapAmount = value * 993 / 1000;
totalFees += value - swapAmount;

(bool success,) = toAddress.call{value : swapAmount}("");
require(success, "Transfer failed");
}

function cancelSwap(
address toAddress,
uint value,
bytes32 secretHash,
uint blockNumber
) public {
bytes32 swapHash = _getSwapHash(
msg.sender,
toAddress,
value,
secretHash,
blockNumber
);

require(swaps[swapHash] == SwapStatus.Created, "Invalid swap status");
require(block.number >= blockNumber + MAX_BLOCK_HEIGHT, "MAX_BLOCK_HEIGHT not exceeded");

swaps[swapHash] = SwapStatus.Cancelled;
(bool success,) = msg.sender.call{value : value}("");
require(success, "Transfer failed");
}

function withdrawFees() external onlyOwner {
uint feesToWithdraw = totalFees;
totalFees = 0;

(bool success,) = owner().call{value : feesToWithdraw}("");
require(success, "Transfer failed");
}
}
91 changes: 0 additions & 91 deletions contracts/yakuSwap.sol

This file was deleted.

23 changes: 17 additions & 6 deletions hardhat.config.js
@@ -1,7 +1,7 @@
require("@nomiclabs/hardhat-waffle");
require("@nomiclabs/hardhat-etherscan");
require("hardhat-gas-reporter");

// This is a sample Hardhat task. To learn how to create your own go to
// https://hardhat.org/guides/create-task.html
task("accounts", "Prints the list of accounts", async (taskArgs, hre) => {
const accounts = await hre.ethers.getSigners();

Expand All @@ -10,10 +10,21 @@ task("accounts", "Prints the list of accounts", async (taskArgs, hre) => {
}
});

// You need to export an object to set up your config
// Go to https://hardhat.org/config/ to learn more

require("./tasks/faucet");
module.exports = {
solidity: "0.8.4",
solidity: {
version: "0.8.7",
settings: {
optimizer: {
enabled: true,
runs: 200,
}
}
},
gasReporter: {
enabled: true,
currency: "USD",
coinmarketcap: require('dotenv').config().parsed.COINMARKETCAP_API_KEY,
gasPrice: 100
}
};
5 changes: 4 additions & 1 deletion package.json
@@ -1,11 +1,14 @@
{
"devDependencies": {
"@nomiclabs/hardhat-ethers": "^2.0.2",
"@nomiclabs/hardhat-etherscan": "^2.1.6",
"@nomiclabs/hardhat-waffle": "^2.0.1",
"chai": "^4.3.4",
"dotenv": "^10.0.0",
"ethereum-waffle": "^3.4.0",
"ethers": "^5.4.4",
"hardhat": "^2.6.1"
"hardhat": "^2.6.1",
"hardhat-gas-reporter": "^1.0.4"
},
"dependencies": {
"@openzeppelin/contracts": "^4.3.0"
Expand Down

0 comments on commit 1490b08

Please sign in to comment.