Created
January 10, 2019 17:01
-
-
Save diminator/552da28b4ecf0b66925b4a2249aa62aa 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=false&gist=
This file contains 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.22 <0.6.0; | |
import 'browser/Condition.sol'; | |
contract AgreementStore { | |
struct Agreement { | |
bool exists; | |
bytes32 templateId; | |
bool state; | |
bytes32 did; | |
} | |
mapping(bytes32 => Agreement) _agreements; | |
} | |
contract TemplateStore { | |
struct Template { | |
bool exists; | |
bytes32[] conditionTypes; | |
bytes32 rewardType; | |
} | |
mapping(bytes32 => Template) _templates; | |
} |
This file contains 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.22 <0.6.0; | |
import 'github.com/OpenZeppelin/zeppelin-solidity/contracts/cryptography/ECDSA.sol'; | |
import 'browser/OceanToken.sol'; | |
contract ConditionStore { | |
enum ConditionState { Uninitialized, Unfufilled, Fulfilled, Aborted } | |
struct Condition { | |
ConditionState state; | |
bytes32 dependency; | |
} | |
mapping(bytes32 => Condition) internal _conditions; | |
function exists(bytes32 id) public view returns (bool) { | |
return _conditions[id].state != ConditionState.Uninitialized; | |
} | |
function getState(bytes32 id) public view returns (ConditionState) { | |
return _conditions[id].state; | |
} | |
function getDependency(bytes32 id) public view returns (bytes32) { | |
return _conditions[id].dependency; | |
} | |
function create(bytes32 id) public returns (bool) { | |
if (!exists(id)) _conditions[id].state = ConditionState.Unfufilled; | |
} | |
function updateState(bytes32 id, ConditionState state) public returns (bool) { | |
// can only be executed by Condition contract | |
if (getDependency(id) > 0x0) { | |
if (!(getState(getDependency(id)) == ConditionState.Fulfilled)) return false; | |
} | |
_conditions[id].state = state; | |
return true; | |
} | |
function updateDependency(bytes32 id, bytes32 dependentId) public returns (bool) { | |
// can only be executed by Condition owner | |
_conditions[id].dependency = dependentId; | |
return true; | |
} | |
} | |
contract Condition { | |
ConditionStore internal conditionStore; | |
event ConditionFulfilled(bytes32 indexed agreementId, address indexed _type, bytes32 id); | |
function generateId(bytes32 agreementId, bytes32 valueHash) public pure returns (bytes32){ | |
return keccak256(abi.encodePacked(agreementId, valueHash)); | |
} | |
function create(bytes32 id) internal returns (bool) { | |
return conditionStore.create(id); | |
} | |
function update(bytes32 id, ConditionStore.ConditionState state) internal returns (bool) { | |
return conditionStore.updateState(id, state); | |
} | |
} | |
contract HashCondition is Condition { | |
constructor(address conditionStoreAddress) public { | |
conditionStore = ConditionStore(conditionStoreAddress); | |
} | |
function hashValues(uint256 preimage) public pure returns (bytes32) { | |
return hashValues(abi.encodePacked(preimage)); | |
} | |
function hashValues(string memory preimage) public pure returns (bytes32) { | |
return hashValues(abi.encodePacked(preimage)); | |
} | |
function hashValues(bytes32 preimage) public pure returns (bytes32) { | |
return hashValues(abi.encodePacked(preimage)); | |
} | |
function hashValues(bytes memory preimage) public pure returns (bytes32) { | |
return keccak256(preimage); | |
} | |
function create(bytes32 agreementId, bytes32 hash) public returns (bool){ | |
bytes32 id = generateId(agreementId, hash); | |
return super.create(id); | |
} | |
function fulfill(bytes32 agreementId, uint256 preimage) public returns (bool) { | |
bytes32 id = generateId(agreementId, hashValues(preimage)); | |
return _fulfill(agreementId, id); | |
} | |
function fulfill(bytes32 agreementId, string memory preimage) public returns (bool) { | |
bytes32 id = generateId(agreementId, hashValues(preimage)); | |
return _fulfill(agreementId, id); | |
} | |
function fulfill(bytes32 agreementId, bytes32 preimage) public returns (bool) { | |
bytes32 id = generateId(agreementId, hashValues(preimage)); | |
return _fulfill(agreementId, id); | |
} | |
function _fulfill(bytes32 agreementId, bytes32 id) private returns (bool) { | |
bool result = update(id, ConditionStore.ConditionState.Fulfilled); | |
emit ConditionFulfilled(agreementId, address(this), id); | |
return result; | |
} | |
} | |
contract SignCondition is Condition { | |
constructor(address conditionStoreAddress) public { | |
conditionStore = ConditionStore(conditionStoreAddress); | |
} | |
function hashValues(bytes32 message, address publicKey) public pure returns (bytes32) { | |
return keccak256(abi.encodePacked(message, publicKey)); | |
} | |
function hashValues(string memory message, address publicKey) public pure returns (bytes32) { | |
return keccak256(abi.encodePacked(message, publicKey)); | |
} | |
function hashValues(bytes memory message, address publicKey) public pure returns (bytes32) { | |
return keccak256(abi.encodePacked(message, publicKey)); | |
} | |
function create(bytes32 agreementId, bytes32 message, address publicKey) public returns (bool){ | |
bytes32 id = generateId(agreementId, hashValues(message, publicKey)); | |
return super.create(id); | |
} | |
function fulfill(bytes32 agreementId, bytes32 message, address publicKey, bytes memory signature) public returns (bool) { | |
bytes32 id = generateId(agreementId, hashValues(message, publicKey)); | |
bool fulfills = ECDSA.recover(message, signature) == publicKey; | |
if (fulfills) { | |
bool result = update(id, ConditionStore.ConditionState.Fulfilled); | |
if (result) emit ConditionFulfilled(agreementId, address(this), id); | |
} | |
return false; | |
} | |
} | |
contract LockPaymentCondition is Condition { | |
ERC20 private token; | |
constructor(address conditionStoreAddress, address tokenAddress) public { | |
conditionStore = ConditionStore(conditionStoreAddress); | |
token = OceanToken(tokenAddress); | |
} | |
function hashValues(address rewardContract, uint256 amount) public pure returns (bytes32) { | |
return keccak256(abi.encodePacked(rewardContract, amount)); | |
} | |
function create(bytes32 agreementId, address rewardContract, uint256 amount) public returns (bool){ | |
bytes32 id = generateId(agreementId, hashValues(rewardContract, amount)); | |
return super.create(id); | |
} | |
function fulfill(bytes32 agreementId, address rewardContract, uint256 amount) public returns (bool) { | |
if (token.transferFrom(msg.sender, rewardContract, amount)) { | |
bytes32 id = generateId(agreementId, hashValues(rewardContract, amount)); | |
bool result = update(id, ConditionStore.ConditionState.Fulfilled); | |
if (result) emit ConditionFulfilled(agreementId, address(this), id); | |
} | |
} | |
} | |
This file contains 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.22 <0.6.0; | |
import 'github.com/OpenZeppelin/zeppelin-solidity/contracts/token/ERC20/ERC20.sol'; | |
import 'github.com/OpenZeppelin/zeppelin-solidity/contracts/token/ERC20/IERC20.sol'; | |
import 'github.com/OpenZeppelin/zeppelin-solidity/contracts/math/SafeMath.sol'; | |
/** | |
@title Ocean Protocol ERC20 Token Contract | |
@author Team: Fang Gong | |
*/ | |
contract OceanToken is ERC20 { | |
using SafeMath for uint256; | |
// ============ | |
// DATA STRUCTURES: | |
// ============ | |
string public constant name = 'OceanToken'; // Set the token name for display | |
string public constant symbol = 'OCN'; // Set the token symbol for display | |
// SUPPLY | |
uint8 public constant decimals = 18; // Set the number of decimals for display | |
uint256 public constant TOTAL_SUPPLY = 1400000000 * 10 ** 18; // OceanToken total supply | |
// EMIT TOKENS | |
address public receiver = address(0); // address to receive TOKENS | |
uint256 public _totalSupply; // total supply of Ocean tokens including | |
// initial tokens plus block rewards | |
/** | |
* @dev OceanToken Constructor | |
* Runs only on initial contract creation. | |
*/ | |
constructor() public { | |
_totalSupply = TOTAL_SUPPLY; | |
} | |
/** | |
* @dev setReceiver set the address to receive the emitted tokens | |
* @param to The address to send tokens | |
* @return success setting is successful. | |
*/ | |
function setReceiver(address to) public returns (bool success){ | |
// make sure receiver is not set already | |
require(receiver == address(0), 'Receiver address already set.'); | |
// Creator address is assigned initial available tokens | |
super._mint(to, TOTAL_SUPPLY); | |
// set receiver | |
receiver = to; | |
return true; | |
} | |
/** | |
* @dev Transfer token for a specified address when not paused | |
* @param to The address to transfer to. | |
* @param value The amount to be transferred. | |
*/ | |
function transfer( | |
address to, | |
uint256 value | |
) | |
public | |
returns (bool) | |
{ | |
require( | |
to != address(0), | |
'To address is 0x0.' | |
); | |
return super.transfer(to, value); | |
} | |
/** | |
* @dev Transfer tokens from one address to another when not paused | |
* @param _from address The address which you want to send tokens from | |
* @param to address The address which you want to transfer to | |
* @param value uint256 the amount of tokens to be transferred | |
*/ | |
function transferFrom( | |
address _from, | |
address to, | |
uint256 value | |
) | |
public | |
returns (bool) | |
{ | |
require( | |
to != address(0), | |
'To address is 0x0.' | |
); | |
return super.transferFrom(_from, to, value); | |
} | |
} | |
This file contains 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.22 <0.6.0; | |
import "browser/Condition.sol"; | |
contract EscrowRewardStore { | |
struct EscrowReward { | |
uint256 amount; | |
address receiver; | |
address sender; | |
bytes32 lockCondition; // 0xLOCK | |
bytes32 rewardCondition; // 0xACCESS | |
} | |
mapping(bytes32 => EscrowReward) _rewards; | |
ConditionStore internal conditionStore; | |
constructor(address conditionStoreAddress) public { | |
conditionStore = ConditionStore(conditionStoreAddress); | |
} | |
function reward(bytes32 agreementId) public returns (bool) { | |
if (conditionStore.getState(_rewards[agreementId].lockCondition) == ConditionStore.ConditionState.Fulfilled) { | |
if (conditionStore.getState(_rewards[agreementId].rewardCondition) == ConditionStore.ConditionState.Fulfilled) { | |
return true; // transfer amount to receiver | |
} else if (conditionStore.getState(_rewards[agreementId].rewardCondition) == ConditionStore.ConditionState.Aborted) { | |
return true; // transfer amount to sender | |
} | |
} | |
return false; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment