Skip to content

Commit

Permalink
Merge pull request #221 from nevermined-io/feat/nft-dtp-agreements
Browse files Browse the repository at this point in the history
NFT dtp agreements
  • Loading branch information
aaitor authored Feb 8, 2022
2 parents ccdc347 + 32c1f61 commit 5908789
Show file tree
Hide file tree
Showing 23 changed files with 3,557 additions and 1,114 deletions.
23 changes: 23 additions & 0 deletions contracts/conditions/NFTs/INFTLock.sol
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ interface INFTLock {
address indexed _lockAddress,
bytes32 _conditionId,
uint256 _amount,
address _receiver,
address _nftContractAddress
);

Expand All @@ -36,6 +37,17 @@ interface INFTLock {
pure
returns (bytes32);

function hashValuesMarked(
bytes32 _did,
address _lockAddress,
uint256 _amount,
address _receiver,
address _nftContractAddress
)
external
pure
returns (bytes32);

/**
* @notice fulfill the transfer NFT condition
* @dev Fulfill method transfer a certain amount of NFTs
Expand All @@ -58,4 +70,15 @@ interface INFTLock {
external
returns (ConditionStoreLibrary.ConditionState);

function fulfillMarked(
bytes32 _agreementId,
bytes32 _did,
address _lockAddress,
uint256 _amount,
address _receiver,
address _nftContractAddress
)
external
returns (ConditionStoreLibrary.ConditionState);

}
43 changes: 37 additions & 6 deletions contracts/conditions/NFTs/NFT721LockCondition.sol
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ contract NFT721LockCondition is Condition, INFTLock, ReentrancyGuardUpgradeable,
* @param _lockAddress the contract address where the NFT will be locked
* @param _amount is the amount of the locked tokens
* @param _nftContractAddress Is the address of the NFT (ERC-721) contract to use
* @return bytes32 hash of all these values
* @return bytes32 hash of all these values
*/
function hashValues(
bytes32 _did,
Expand All @@ -61,11 +61,26 @@ contract NFT721LockCondition is Condition, INFTLock, ReentrancyGuardUpgradeable,
address _nftContractAddress
)
public
override
pure
returns (bytes32)
{
return hashValuesMarked(_did, _lockAddress, _amount, address(0), _nftContractAddress);
}

function hashValuesMarked(
bytes32 _did,
address _lockAddress,
uint256 _amount,
address _receiver,
address _nftContractAddress
)
public
override
pure
returns (bytes32)
{
return keccak256(abi.encode(_did, _lockAddress, _amount, _nftContractAddress));
return keccak256(abi.encode(_did, _lockAddress, _amount, _receiver, _nftContractAddress));
}

/**
Expand All @@ -78,14 +93,15 @@ contract NFT721LockCondition is Condition, INFTLock, ReentrancyGuardUpgradeable,
* @param _nftContractAddress Is the address of the NFT (ERC-721) contract to use
* @return condition state (Fulfilled/Aborted)
*/
function fulfill(
function fulfillMarked(
bytes32 _agreementId,
bytes32 _did,
address _lockAddress,
uint256 _amount,
address _receiver,
address _nftContractAddress
)
external
public
override
nonReentrant
returns (ConditionStoreLibrary.ConditionState)
Expand All @@ -100,10 +116,10 @@ contract NFT721LockCondition is Condition, INFTLock, ReentrancyGuardUpgradeable,
if (_amount == 1) {
erc721.safeTransferFrom(msg.sender, _lockAddress, uint256(_did));
}

bytes32 _id = generateId(
_agreementId,
hashValues(_did, _lockAddress, _amount, _nftContractAddress)
hashValuesMarked(_did, _lockAddress, _amount, _receiver, _nftContractAddress)
);
ConditionStoreLibrary.ConditionState state = super.fulfill(
_id,
Expand All @@ -116,11 +132,26 @@ contract NFT721LockCondition is Condition, INFTLock, ReentrancyGuardUpgradeable,
_lockAddress,
_id,
_amount,
_receiver,
_nftContractAddress
);
return state;
}

function fulfill(
bytes32 _agreementId,
bytes32 _did,
address _lockAddress,
uint256 _amount,
address _nftContractAddress
)
external
override
returns (ConditionStoreLibrary.ConditionState)
{
return fulfillMarked(_agreementId, _did, _lockAddress, _amount, address(0), _nftContractAddress);
}

/**
* Always returns `IERC721Receiver.onERC721Received.selector`.
*/
Expand Down
74 changes: 45 additions & 29 deletions contracts/conditions/NFTs/NFTLockCondition.sol
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,6 @@ contract NFTLockCondition is Condition, INFTLock, ReentrancyGuardUpgradeable, IE
bytes4 constant internal ERC1155_ACCEPTED = 0xf23a6e61; // bytes4(keccak256("onERC1155Received(address,address,uint256,uint256,bytes)"))
bytes4 constant internal ERC1155_BATCH_ACCEPTED = 0xbc197c81; // bytes4(keccak256("onERC1155BatchReceived(address,address,uint256[],uint256[],bytes)"))

bytes public lastData;
address public lastOperator;
address public lastFrom;
uint256 public lastId;
uint256 public lastValue;

/**
* @notice initialize init the contract with the following parameters
* @dev this function is called only once during the contract
Expand Down Expand Up @@ -100,13 +94,29 @@ contract NFTLockCondition is Condition, INFTLock, ReentrancyGuardUpgradeable, IE
pure
override
returns (bytes32)
{
return hashValuesMarked(_did, _lockAddress, _amount, address(0), _nftContractAddress);
}

function hashValuesMarked(
bytes32 _did,
address _lockAddress,
uint256 _amount,
address _receiver,
address _nftContractAddress
)
public
pure
override
returns (bytes32)
{
return keccak256(
abi.encode(
CONDITION_TYPE,
_did,
_lockAddress,
_amount,
_receiver,
_nftContractAddress
)
);
Expand Down Expand Up @@ -135,6 +145,20 @@ contract NFTLockCondition is Condition, INFTLock, ReentrancyGuardUpgradeable, IE
return fulfill(_agreementId, _did, _lockAddress, _amount, address(erc1155));
}

function fulfill(
bytes32 _agreementId,
bytes32 _did,
address _lockAddress,
uint256 _amount,
address _nft
)
public
override
returns (ConditionStoreLibrary.ConditionState)
{
return fulfillMarked(_agreementId, _did, _lockAddress, _amount, address(0), _nft);
}

/**
* @notice fulfill the transfer NFT condition
* @dev Fulfill method transfer a certain amount of NFTs
Expand All @@ -147,23 +171,24 @@ contract NFTLockCondition is Condition, INFTLock, ReentrancyGuardUpgradeable, IE
* @param _nftContractAddress Is the address of the NFT (ERC-1155) contract to use
* @return condition state (Fulfilled/Aborted)
*/
function fulfill(
function fulfillMarked(
bytes32 _agreementId,
bytes32 _did,
address _lockAddress,
uint256 _amount,
address _receiver,
address _nftContractAddress
)
public
override
nonReentrant
returns (ConditionStoreLibrary.ConditionState)
{
erc1155.safeTransferFrom(msg.sender, _lockAddress, uint256(_did), _amount, '');
IERC1155Upgradeable(_nftContractAddress).safeTransferFrom(msg.sender, _lockAddress, uint256(_did), _amount, '');

bytes32 _id = generateId(
_agreementId,
hashValues(_did, _lockAddress, _amount, _nftContractAddress)
hashValuesMarked(_did, _lockAddress, _amount, _receiver, _nftContractAddress)
);
ConditionStoreLibrary.ConditionState state = super.fulfill(
_id,
Expand All @@ -176,47 +201,38 @@ contract NFTLockCondition is Condition, INFTLock, ReentrancyGuardUpgradeable, IE
_lockAddress,
_id,
_amount,
_receiver,
_nftContractAddress
);
return state;
}

// solhint-disable-next-line
function onERC1155Received(
address _operator,
address _from,
uint256 _id,
uint256 _value,
bytes calldata _data
address,
address,
uint256,
uint256,
bytes calldata
)
external
override
returns(bytes4)
{
lastOperator = _operator;
lastFrom = _from;
lastId = _id;
lastValue = _value;
lastData = _data;
return ERC1155_ACCEPTED;
}

function onERC1155BatchReceived(
address _operator,
address _from,
uint256[] calldata _ids,
uint256[] calldata _values,
bytes calldata _data
address,
address,
uint256[] calldata,
uint256[] calldata,
bytes calldata
)
external
override
returns(bytes4)
{
lastOperator = _operator;
lastFrom = _from;
lastId = _ids[0];
lastValue = _values[0];
lastData = _data;
return ERC1155_BATCH_ACCEPTED;
}

Expand Down
Loading

0 comments on commit 5908789

Please sign in to comment.