Skip to content

Commit

Permalink
Refactoring Access Template
Browse files Browse the repository at this point in the history
  • Loading branch information
aaitor committed Mar 26, 2021
1 parent 906f80e commit 5bbb516
Show file tree
Hide file tree
Showing 17 changed files with 295 additions and 264 deletions.
2 changes: 2 additions & 0 deletions contracts/conditions/ConditionStoreLibrary.sol
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ library ConditionStoreLibrary {
struct Condition {
address typeRef;
ConditionState state;
address createdBy;
address lastUpdatedBy;
uint256 blockNumberUpdated;
}
Expand Down Expand Up @@ -59,6 +60,7 @@ library ConditionStoreLibrary {
_self.conditions[_id] = Condition({
typeRef: _typeRef,
state: ConditionState.Unfulfilled,
createdBy: tx.origin, // solhint-disable avoid-tx-origin
lastUpdatedBy: msg.sender,
blockNumberUpdated: block.number
});
Expand Down
19 changes: 18 additions & 1 deletion contracts/conditions/ConditionStoreManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,8 @@ contract ConditionStoreManager is OwnableUpgradeable, Common {
* @return timeLock the time lock
* @return timeOut time out
* @return blockNumber block number
* @return lastUpdatedBy block number
* @return createdBy address
* @return lastUpdatedBy address
* @return blockNumberUpdated block number updated
*/
function getCondition(bytes32 _id)
Expand All @@ -292,6 +293,7 @@ contract ConditionStoreManager is OwnableUpgradeable, Common {
uint timeLock,
uint timeOut,
uint blockNumber,
address createdBy,
address lastUpdatedBy,
uint blockNumberUpdated
)
Expand All @@ -301,6 +303,7 @@ contract ConditionStoreManager is OwnableUpgradeable, Common {
timeLock = epochList.epochs[_id].timeLock;
timeOut = epochList.epochs[_id].timeOut;
blockNumber = epochList.epochs[_id].blockNumber;
createdBy = conditionList.conditions[_id].createdBy;
lastUpdatedBy = conditionList.conditions[_id].lastUpdatedBy;
blockNumberUpdated = conditionList.conditions[_id].blockNumberUpdated;
}
Expand All @@ -318,6 +321,20 @@ contract ConditionStoreManager is OwnableUpgradeable, Common {
return conditionList.conditions[_id].state;
}

/**
* @dev getConditionCreatedBy
* @return condition createdBy address
*/
function getConditionCreatedBy(bytes32 _id)
external
view
virtual
returns (address)
{
return conditionList.conditions[_id].createdBy;
}


/**
* @dev isConditionTimeLocked
* @return whether the condition is timedLock ended
Expand Down
7 changes: 5 additions & 2 deletions contracts/conditions/NFTs/TransferNFTCondition.sol
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,9 @@ contract TransferNFTCondition is Condition {
* @param _did refers to the DID in which secret store will issue the decryption keys
* @param _nftReceiver is the address of the account to receive the NFT
* @param _nftAmount amount of NFTs to transfer
* @param _rewardAddress is the lock payment contract address
* @param _amounts token amounts to be locked/released
* @param _receivers receiver's addresses
* @param _lockPaymentCondition lock payment condition identifier
* @return condition state (Fulfilled/Aborted)
*/
Expand All @@ -126,7 +129,7 @@ contract TransferNFTCondition is Condition {

address lockConditionTypeRef;
ConditionStoreLibrary.ConditionState lockConditionState;
(lockConditionTypeRef,lockConditionState,,,,,) = conditionStoreManager
(lockConditionTypeRef,lockConditionState,,,,,,) = conditionStoreManager
.getCondition(_lockPaymentCondition);

require(
Expand Down Expand Up @@ -206,7 +209,7 @@ contract TransferNFTCondition is Condition {

address lockConditionTypeRef;
ConditionStoreLibrary.ConditionState lockConditionState;
(lockConditionTypeRef,lockConditionState,,,,,) = conditionStoreManager
(lockConditionTypeRef,lockConditionState,,,,,,) = conditionStoreManager
.getCondition(_nftLockCondition);

require(
Expand Down
2 changes: 1 addition & 1 deletion contracts/conditions/ThresholdCondition.sol
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ contract ThresholdCondition is Condition {

for(uint i = 0; i < _inputConditions.length; i++)
{
(,inputConditionState,,,,,) = conditionStoreManager.
(,inputConditionState,,,,,,) = conditionStoreManager.
getCondition(_inputConditions[i]);

if(inputConditionState == Fulfilled)
Expand Down
17 changes: 7 additions & 10 deletions contracts/conditions/rewards/EscrowPaymentCondition.sol
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ contract EscrowPaymentCondition is Reward {
bytes32 _conditionId,
uint256[] _amounts
);

/**
* @notice initialize init the
* contract with the following parameters
Expand Down Expand Up @@ -139,11 +139,11 @@ contract EscrowPaymentCondition is Reward {
);
address lockConditionTypeRef;
ConditionStoreLibrary.ConditionState lockConditionState;
(lockConditionTypeRef,lockConditionState,,,,,) = conditionStoreManager
(lockConditionTypeRef,lockConditionState,,,,,,) = conditionStoreManager
.getCondition(_lockCondition);

uint256 _totalAmount = 0;
for(uint i = 0; i < _amounts.length; i++)
uint256 _totalAmount;
for(uint i; i < _amounts.length; i++)
_totalAmount = _totalAmount + _amounts[i];

bytes32 generatedLockConditionId = keccak256(
Expand Down Expand Up @@ -171,19 +171,19 @@ contract EscrowPaymentCondition is Reward {
);

ConditionStoreLibrary.ConditionState state = conditionStoreManager
.getConditionState(_releaseCondition);
.getConditionState(_releaseCondition);

if (state == ConditionStoreLibrary.ConditionState.Fulfilled)
{
state = _transferAndFulfill(id, _receivers, _amounts);
emit Fulfilled(_agreementId, _receivers, id, _amounts);

} else if (state == ConditionStoreLibrary.ConditionState.Aborted)
{
uint256[] memory _totalAmounts = new uint256[](1);
_totalAmounts[0] = _totalAmount;
address[] memory _originalSender = new address[](1);
_originalSender[0] = lockConditionTypeRef;
_originalSender[0] = conditionStoreManager.getConditionCreatedBy(_lockCondition);
state = _transferAndFulfill(id, _originalSender, _totalAmounts);
emit Fulfilled(_agreementId, _originalSender, id, _totalAmounts);
} else
Expand Down Expand Up @@ -232,6 +232,3 @@ contract EscrowPaymentCondition is Reward {
}

}



2 changes: 1 addition & 1 deletion contracts/conditions/rewards/EscrowReward.sol
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ contract EscrowReward is Reward {
);
address lockConditionTypeRef;
ConditionStoreLibrary.ConditionState lockConditionState;
(lockConditionTypeRef,lockConditionState,,,,,) = conditionStoreManager
(lockConditionTypeRef,lockConditionState,,,,,,) = conditionStoreManager
.getCondition(_lockCondition);

uint256 _totalAmount = 0;
Expand Down
3 changes: 0 additions & 3 deletions contracts/conditions/rewards/Reward.sol
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,6 @@ import '@openzeppelin/contracts-upgradeable/token/ERC20/ERC20Upgradeable.sol';
* @dev Implementation of the Reward.
*
* Generic reward condition
* For more information, please refer the following link:
* https://github.com/oceanprotocol/OEPs/issues/133
* TODO: update the OEP link
*/
contract Reward is Condition {
IERC20Upgradeable internal token;
Expand Down
106 changes: 106 additions & 0 deletions contracts/templates/AccessTemplate.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
pragma solidity 0.6.12;
// Copyright 2020 Keyko GmbH.
// SPDX-License-Identifier: (Apache-2.0 AND CC-BY-4.0)
// Code is Apache-2.0 and docs are CC-BY-4.0


import './BaseEscrowTemplate.sol';
import '../conditions/AccessCondition.sol';
import '../conditions/LockPaymentCondition.sol';
import '../conditions/rewards/EscrowPaymentCondition.sol';
import '../registry/DIDRegistry.sol';

/**
* @title Agreement Template
* @author Keyko
*
* @dev Implementation of Access Agreement Template
*
* Access template is use case specific template.
* Anyone (consumer/provider/publisher) can use this template in order
* to setup an on-chain SEA. The template is a composite of three basic
* conditions. Once the agreement is created, the consumer will lock an amount
* of tokens (as listed in the DID document - off-chain metadata) to the
* the lock reward contract which in turn will fire an event. ON the other hand
* the provider is listening to the to all the emitted events, the provider
* will catch the event and grant permissions to the consumer through
* secret store contract, the consumer now is able to download the data set
* by asking the off-chain component of secret store to decrypt the DID and
* encrypt it using the consumer's public key. Then the secret store will
* provide an on-chain proof that the consumer had access to the data set.
* Finally, the provider can call the escrow reward condition in order
* to release the payment. Every condition has a time window (time lock and
* time out). This implies that if the provider didn't grant the access to
* the consumer through secret store within this time window, the consumer
* can ask for refund.
*/
contract AccessTemplate is BaseEscrowTemplate {

DIDRegistry internal didRegistry;
AccessCondition internal accessCondition;
LockPaymentCondition internal lockCondition;
EscrowPaymentCondition internal escrowReward;

/**
* @notice initialize init the
* contract with the following parameters.
* @dev this function is called only once during the contract
* initialization. It initializes the ownable feature, and
* set push the required condition types including
* access secret store, lock reward and escrow reward conditions.
* @param _owner contract's owner account address
* @param _agreementStoreManagerAddress agreement store manager contract address
* @param _didRegistryAddress DID registry contract address
* @param _accessConditionAddress access condition address
* @param _lockConditionAddress lock reward condition contract address
* @param _escrowConditionAddress escrow reward contract address
*/
function initialize(
address _owner,
address _agreementStoreManagerAddress,
address _didRegistryAddress,
address _accessConditionAddress,
address _lockConditionAddress,
address _escrowConditionAddress
)
external
initializer()
{
require(
_owner != address(0) &&
_agreementStoreManagerAddress != address(0) &&
_didRegistryAddress != address(0) &&
_accessConditionAddress != address(0) &&
_lockConditionAddress != address(0) &&
_escrowConditionAddress != address(0),
'Invalid address'
);

OwnableUpgradeable.__Ownable_init();
transferOwnership(_owner);

agreementStoreManager = AgreementStoreManager(
_agreementStoreManagerAddress
);

didRegistry = DIDRegistry(
_didRegistryAddress
);

accessCondition = AccessCondition(
_accessConditionAddress
);

lockCondition = LockPaymentCondition(
_lockConditionAddress
);

escrowReward = EscrowPaymentCondition(
_escrowConditionAddress
);

conditionTypes.push(address(accessCondition));
conditionTypes.push(address(lockCondition));
conditionTypes.push(address(escrowReward));
}
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@nevermined-io/contracts",
"version": "1.0.0-rc0",
"version": "1.0.0-rc1",
"description": "Nevermined implementation of Ocean Protocol in Solidity",
"bugs": {
"url": "https://github.com/nevermined-io/contracts/issues"
Expand Down
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
<groupId>io.keyko.nevermined</groupId>
<artifactId>contracts</artifactId>
<packaging>jar</packaging>
<version>1.0.0-rc0</version>
<version>1.0.0-rc1</version>
<name>Nevermined Contracts</name>
<description>Nevermined Data Plarform Smart Contracts in Solidity</description>
<description>Nevermined Data Platform Smart Contracts in Solidity</description>
<url>https://github.com/nevermined-io/contracts</url>
<licenses>
<license>
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,6 @@
test_suite='tests',
tests_require=test_requirements,
url='https://github.com/nevermined-io/contracts',
version='1.0.0-rc0',
version='1.0.0-rc1',
zip_safe=False,
)
27 changes: 24 additions & 3 deletions test/helpers/deployConditions.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
/* global artifacts */
const AccessCondition = artifacts.require('AccessCondition')
const EscrowReward = artifacts.require('EscrowReward')
const EscrowPaymentCondition = artifacts.require('EscrowPaymentCondition')
const LockRewardCondition = artifacts.require('LockRewardCondition')
const LockPaymentCondition = artifacts.require('LockPaymentCondition')
const ComputeExecutionCondition = artifacts.require('ComputeExecutionCondition')

const deployConditions = async function(
Expand All @@ -20,8 +22,17 @@ const deployConditions = async function(
{ from: deployer }
)

const accessSecretStoreCondition = await AccessCondition.new({ from: deployer })
await accessSecretStoreCondition.methods['initialize(address,address,address)'](
const lockPaymentCondition = await LockPaymentCondition.new({ from: deployer })
await lockPaymentCondition.initialize(
owner,
conditionStoreManager.address,
token.address,
didRegistry.address,
{ from: deployer }
)

const accessCondition = await AccessCondition.new({ from: deployer })
await accessCondition.methods['initialize(address,address,address)'](
owner,
conditionStoreManager.address,
agreementStoreManager.address,
Expand All @@ -36,6 +47,14 @@ const deployConditions = async function(
{ from: deployer }
)

const escrowPaymentCondition = await EscrowPaymentCondition.new({ from: deployer })
await escrowPaymentCondition.initialize(
owner,
conditionStoreManager.address,
token.address,
{ from: deployer }
)

const computeExecutionCondition = await ComputeExecutionCondition.new({ from: deployer })
await computeExecutionCondition.methods['initialize(address,address,address)'](
owner,
Expand All @@ -45,9 +64,11 @@ const deployConditions = async function(
)

return {
accessSecretStoreCondition,
accessCondition,
escrowReward,
escrowPaymentCondition,
lockRewardCondition,
lockPaymentCondition,
computeExecutionCondition
}
}
Expand Down

0 comments on commit 5bbb516

Please sign in to comment.