Created
July 17, 2018 09:45
-
-
Save caffeinum/ba65a5fc04ab3c5ec454d4011383464d to your computer and use it in GitHub Desktop.
Created using remix-ide: Realtime Ethereum Contract Compiler and Runtime. Load this file by pasting this gists URL or ID at https://remix.ethereum.org/#version=soljson-v0.4.24+commit.e67f0147.js&optimize=true&gist=
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pragma solidity ^0.4.23; | |
contract AtomicSwap { | |
uint256 SafeTime = 3 hours; // atomic swap timeOut | |
struct Swap { | |
bytes32 secret; | |
bytes32 secretHash; | |
uint256 createdAt; | |
uint256 balance; | |
} | |
// ETH Owner => BTC Owner => Swap | |
mapping(address => mapping(address => Swap)) public swaps; | |
constructor () public { | |
// owner = msg.sender; | |
} | |
function deposit (address recipient, bytes32 _secretHash) public payable { | |
require(swaps[msg.sender][recipient].balance == 0); | |
swaps[msg.sender][recipient] = Swap( | |
bytes32(0), | |
_secretHash, | |
now, | |
msg.value | |
); | |
} | |
function refund(address recipient) public { | |
Swap memory swap = swaps[msg.sender][recipient]; | |
require(swap.balance > 0); | |
require(swap.createdAt + SafeTime < now); | |
msg.sender.transfer(swap.balance); | |
} | |
function getBalance (address creator) public view returns (uint256) { | |
return swaps[creator][msg.sender].balance; | |
} | |
function getHash (address creator) public view returns (bytes32) { | |
return swaps[creator][msg.sender].secretHash; | |
} | |
function withdraw (address creator, bytes32 _secret) public { | |
Swap memory swap = swaps[creator][msg.sender]; | |
require(sha256(abi.encodePacked(_secret)) == swap.secretHash); | |
require(swap.secret == bytes32(0)); | |
swap.secret = _secret; | |
msg.sender.transfer(swap.balance); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pragma solidity ^0.4.23; | |
import './SafeMath.sol'; | |
contract ERC20 { | |
function transfer(address _to, uint256 _value) public; | |
function transferFrom(address _from, address _to, uint256 _value) public returns(bool success); | |
} | |
contract EthTokenToSmthSwaps { | |
using SafeMath for uint; | |
address public owner; | |
uint256 SafeTime = 3 hours; // atomic swap timeOut | |
struct Swap { | |
address token; | |
bytes32 secret; | |
bytes20 secretHash; | |
uint256 createdAt; | |
uint256 balance; | |
} | |
// ETH Owner => BTC Owner => Swap | |
mapping(address => mapping(address => Swap)) public swaps; | |
// ETH Owner => BTC Owner => secretHash => Swap | |
// mapping(address => mapping(address => mapping(bytes20 => Swap))) public swaps; | |
constructor () public { | |
owner = msg.sender; | |
} | |
event CreateSwap(uint256 createdAt); | |
// ETH Owner creates Swap with secretHash | |
// ETH Owner make token deposit | |
function createSwap(bytes20 _secretHash, address _participantAddress, uint256 _value, address _token) public { | |
require(_value > 0); | |
require(swaps[msg.sender][_participantAddress].balance == uint256(0)); | |
require(ERC20(_token).transferFrom(msg.sender, this, _value)); | |
swaps[msg.sender][_participantAddress] = Swap( | |
_token, | |
bytes32(0), | |
_secretHash, | |
now, | |
_value | |
); | |
CreateSwap(now); | |
} | |
function getBalance(address _ownerAddress) public view returns (uint256) { | |
return swaps[_ownerAddress][msg.sender].balance; | |
} | |
event Withdraw(); | |
// BTC Owner withdraw money and adds secret key to swap | |
// BTC Owner receive +1 reputation | |
function withdraw(bytes32 _secret, address _ownerAddress) public { | |
Swap memory swap = swaps[_ownerAddress][msg.sender]; | |
require(swap.secretHash == ripemd160(_secret)); | |
require(swap.balance > uint256(0)); | |
require(swap.createdAt.add(SafeTime) > now); | |
ERC20(swap.token).transfer(msg.sender, swap.balance); | |
swaps[_ownerAddress][msg.sender].balance = 0; | |
swaps[_ownerAddress][msg.sender].secret = _secret; | |
Withdraw(); | |
} | |
// ETH Owner receive secret | |
function getSecret(address _participantAddress) public view returns (bytes32) { | |
return swaps[msg.sender][_participantAddress].secret; | |
} | |
event Refund(); | |
// ETH Owner refund money | |
// BTC Owner gets -1 reputation | |
function refund(address _participantAddress) public { | |
Swap memory swap = swaps[msg.sender][_participantAddress]; | |
require(swap.balance > uint256(0)); | |
require(swap.createdAt.add(SafeTime) < now); | |
ERC20(swap.token).transfer(msg.sender, swap.balance); | |
clean(msg.sender, _participantAddress); | |
Refund(); | |
} | |
function clean(address _ownerAddress, address _participantAddress) internal { | |
delete swaps[_ownerAddress][_participantAddress]; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pragma solidity ^0.4.23; | |
import './SafeMath.sol'; | |
contract EthToSmthSwaps { | |
using SafeMath for uint; | |
address public owner; | |
address public ratingContractAddress; | |
uint256 SafeTime = 3 hours; // atomic swap timeOut | |
struct Swap { | |
bytes32 secret; | |
bytes20 secretHash; | |
uint256 createdAt; | |
uint256 balance; | |
} | |
// ETH Owner => BTC Owner => Swap | |
mapping(address => mapping(address => Swap)) public swaps; | |
constructor () public { | |
owner = msg.sender; | |
} | |
function setReputationAddress(address _ratingContractAddress) public { | |
require(owner == msg.sender); | |
ratingContractAddress = _ratingContractAddress; | |
} | |
event Sign(); | |
// ETH Owner signs swap | |
// initializing time for correct work of close() method | |
function sign(address _participantAddress) public { | |
require(swaps[msg.sender][_participantAddress].balance == 0); | |
emit Sign(); | |
} | |
// BTC Owner checks if ETH Owner signed swap | |
function checkSign() public view returns (uint) { | |
return now; | |
} | |
event CreateSwap(uint256 createdAt); | |
// ETH Owner creates Swap with secretHash | |
// ETH Owner make token deposit | |
function createSwap(bytes20 _secretHash, address _participantAddress) public payable { | |
require(msg.value > 0); | |
require(swaps[msg.sender][_participantAddress].balance == uint256(0)); | |
swaps[msg.sender][_participantAddress] = Swap( | |
bytes32(0), | |
_secretHash, | |
now, | |
msg.value | |
); | |
emit CreateSwap(now); | |
} | |
// BTC Owner receive balance | |
function getBalance(address _ownerAddress) public view returns (uint256) { | |
return swaps[_ownerAddress][msg.sender].balance; | |
} | |
event Withdraw(); | |
// BTC Owner withdraw money and adds secret key to swap | |
// BTC Owner receive +1 reputation | |
function withdraw(bytes32 _secret, address _ownerAddress) public { | |
Swap memory swap = swaps[_ownerAddress][msg.sender]; | |
require(swap.secretHash == ripemd160(_secret)); | |
require(swap.balance > uint256(0)); | |
require(swap.createdAt.add(SafeTime) > now); | |
msg.sender.transfer(swap.balance); | |
swaps[_ownerAddress][msg.sender].balance = 0; | |
swaps[_ownerAddress][msg.sender].secret = _secret; | |
emit Withdraw(); | |
} | |
// ETH Owner receive secret | |
function getSecret(address _participantAddress) public view returns (bytes32) { | |
return swaps[msg.sender][_participantAddress].secret; | |
} | |
event Close(); | |
// ETH Owner closes swap | |
// ETH Owner receive +1 reputation | |
function close(address _participantAddress) public { | |
require(swaps[msg.sender][_participantAddress].balance == 0); | |
clean(msg.sender, _participantAddress); | |
emit Close(); | |
} | |
event Refund(); | |
// ETH Owner refund money | |
// BTC Owner gets -1 reputation | |
function refund(address _participantAddress) public { | |
Swap memory swap = swaps[msg.sender][_participantAddress]; | |
require(swap.balance > uint256(0)); | |
require(swap.createdAt.add(SafeTime) < now); | |
msg.sender.transfer(swap.balance); | |
// TODO it looks like ETH Owner can create as many swaps as possible and refund them to decrease someone reputation | |
clean(msg.sender, _participantAddress); | |
emit Refund(); | |
} | |
event Abort(); | |
// BTC Owner closes Swap | |
// If ETH Owner don't create swap after init in in safeTime | |
// ETH Owner -1 reputation | |
function abort(address _ownerAddress) public { | |
require(swaps[_ownerAddress][msg.sender].balance == uint256(0)); | |
clean(_ownerAddress, msg.sender); | |
emit Abort(); | |
} | |
function clean(address _ownerAddress, address _participantAddress) internal { | |
delete swaps[_ownerAddress][_participantAddress]; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pragma solidity ^0.4.23; | |
// ---------------------------------------------------------------------------- | |
// Safe maths from OpenZeppelin | |
// ---------------------------------------------------------------------------- | |
library SafeMath { | |
function mul(uint256 a, uint256 b) internal pure returns(uint256) { | |
uint256 c = a * b; | |
assert(a == 0 || c / a == b); | |
return c; | |
} | |
function div(uint256 a, uint256 b) internal pure returns(uint256) { | |
// assert(b > 0); // Solidity automatically throws when dividing by 0 | |
uint256 c = a / b; | |
// assert(a == b * c + a % b); // There is no case in which this doesn't hold | |
return c; | |
} | |
function sub(uint256 a, uint256 b) internal pure returns(uint256) { | |
assert(b <= a); | |
return a - b; | |
} | |
function add(uint256 a, uint256 b) internal pure returns(uint256) { | |
uint256 c = a + b; | |
assert(c >= a); | |
return c; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment