Skip to content

Commit

Permalink
testing of lock and escrow conditions with external token address
Browse files Browse the repository at this point in the history
  • Loading branch information
aaitor committed Apr 16, 2021
1 parent 4f3e49c commit c83850c
Show file tree
Hide file tree
Showing 8 changed files with 83 additions and 66 deletions.
20 changes: 12 additions & 8 deletions contracts/conditions/rewards/EscrowPaymentCondition.sol
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ contract EscrowPaymentCondition is Reward {
* @param _did asset decentralized identifier
* @param _amounts token amounts to be locked/released
* @param _receivers receiver's addresses
* @param _tokenAddress the ERC20 contract address to use during the payment
* @param _lockPaymentAddress lock payment contract address
* @param _tokenAddress the ERC20 contract address to use during the payment
* @param _lockCondition lock condition identifier
* @param _releaseCondition release condition identifier
* @return bytes32 hash of all these values
Expand All @@ -72,8 +72,8 @@ contract EscrowPaymentCondition is Reward {
bytes32 _did,
uint256[] memory _amounts,
address[] memory _receivers,
address _tokenAddress,
address _lockPaymentAddress,
address _tokenAddress,
bytes32 _lockCondition,
bytes32 _releaseCondition
)
Expand All @@ -89,15 +89,15 @@ contract EscrowPaymentCondition is Reward {
_did,
_amounts,
_receivers,
_lockPaymentAddress,
_tokenAddress,
_lockPaymentAddress,
_lockCondition,
_releaseCondition
)
);
}


/**
* @notice fulfill escrow reward condition
* @dev fulfill method checks whether the lock and
Expand All @@ -108,8 +108,8 @@ contract EscrowPaymentCondition is Reward {
* @param _did asset decentralized identifier
* @param _amounts token amounts to be locked/released
* @param _receivers receiver's address
* @param _tokenAddress the ERC20 contract address to use during the payment
* @param _lockPaymentAddress lock payment contract address
* @param _tokenAddress the ERC20 contract address to use during the payment
* @param _lockCondition lock condition identifier
* @param _lockPaymentAddress release condition identifier
* @return condition state (Fulfilled/Aborted)
Expand All @@ -119,8 +119,8 @@ contract EscrowPaymentCondition is Reward {
bytes32 _did,
uint256[] memory _amounts,
address[] memory _receivers,
address _tokenAddress,
address _lockPaymentAddress,
address _tokenAddress,
bytes32 _lockCondition,
bytes32 _releaseCondition
)
Expand Down Expand Up @@ -155,8 +155,8 @@ contract EscrowPaymentCondition is Reward {
_did,
_amounts,
_receivers,
_tokenAddress,
_lockPaymentAddress,
_tokenAddress,
_lockCondition,
_releaseCondition
)
Expand Down Expand Up @@ -217,6 +217,10 @@ contract EscrowPaymentCondition is Reward {
IERC20Upgradeable token = ERC20Upgradeable(_tokenAddress);

for(uint i = 0; i < _receivers.length; i++) {
require(
_receivers[i] != address(this),
'Escrow contract can not be a receiver'
);
require(
token.transfer(_receivers[i], _amounts[i]),
'Could not transfer token'
Expand Down
26 changes: 14 additions & 12 deletions test/int/agreement/AccessAgreement.Test.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,11 +90,11 @@ contract('Access Template integration test', (accounts) => {
} = {}) {
// generate IDs from attributes
const conditionIdLock = await lockPaymentCondition.generateId(agreementId,
await lockPaymentCondition.hashValues(did, escrowPaymentCondition.address, escrowAmounts, receivers))
await lockPaymentCondition.hashValues(did, escrowPaymentCondition.address, token.address, escrowAmounts, receivers))
const conditionIdAccess = await accessCondition.generateId(agreementId,
await accessCondition.hashValues(did, receivers[0]))
const conditionIdEscrow = await escrowPaymentCondition.generateId(agreementId,
await escrowPaymentCondition.hashValues(did, escrowAmounts, receivers, escrowPaymentCondition.address, conditionIdLock, conditionIdAccess))
await escrowPaymentCondition.hashValues(did, escrowAmounts, receivers, escrowPaymentCondition.address, token.address, conditionIdLock, conditionIdAccess))

// construct agreement
const agreement = {
Expand Down Expand Up @@ -158,7 +158,7 @@ contract('Access Template integration test', (accounts) => {

// fulfill lock reward
await token.approve(lockPaymentCondition.address, totalAmount, { from: sender })
await lockPaymentCondition.fulfill(agreementId, did, escrowPaymentCondition.address, escrowAmounts, receivers, { from: sender })
await lockPaymentCondition.fulfill(agreementId, did, escrowPaymentCondition.address, token.address, escrowAmounts, receivers, { from: sender })

assert.strictEqual(await getBalance(token, sender), 0)
assert.strictEqual(await getBalance(token, lockPaymentCondition.address), 0)
Expand All @@ -177,7 +177,7 @@ contract('Access Template integration test', (accounts) => {
constants.condition.state.fulfilled)

// get reward
await escrowPaymentCondition.fulfill(agreementId, did, escrowAmounts, receivers, escrowPaymentCondition.address, agreement.conditionIds[1], agreement.conditionIds[0], { from: receiver })
await escrowPaymentCondition.fulfill(agreementId, did, escrowAmounts, receivers, escrowPaymentCondition.address, token.address, agreement.conditionIds[1], agreement.conditionIds[0], { from: receiver })

assert.strictEqual(
(await conditionStoreManager.getConditionState(agreement.conditionIds[2])).toNumber(),
Expand Down Expand Up @@ -210,14 +210,14 @@ contract('Access Template integration test', (accounts) => {

// fulfill lock reward
await token.approve(lockPaymentCondition.address, totalAmount, { from: sender })
await lockPaymentCondition.fulfill(agreementId, did, escrowPaymentCondition.address, escrowAmounts, receivers, { from: sender })
await lockPaymentCondition.fulfill(agreementId, did, escrowPaymentCondition.address, token.address, escrowAmounts, receivers, { from: sender })
assert.strictEqual(
(await conditionStoreManager.getConditionState(agreement.conditionIds[1])).toNumber(),
constants.condition.state.fulfilled)

// No update since access is not fulfilled yet
// refund
const result = await escrowPaymentCondition.fulfill(agreementId, did, escrowAmounts, receivers, escrowPaymentCondition.address, agreement.conditionIds[1], agreement.conditionIds[0], { from: receiver })
const result = await escrowPaymentCondition.fulfill(agreementId, did, escrowAmounts, receivers, escrowPaymentCondition.address, token.address, agreement.conditionIds[1], agreement.conditionIds[0], { from: receiver })
assert.strictEqual(
(await conditionStoreManager.getConditionState(agreement.conditionIds[2])).toNumber(),
constants.condition.state.unfulfilled
Expand All @@ -234,7 +234,7 @@ contract('Access Template integration test', (accounts) => {
constants.condition.state.aborted)

// refund
await escrowPaymentCondition.fulfill(agreementId, did, escrowAmounts, receivers, escrowPaymentCondition.address, agreement.conditionIds[1], agreement.conditionIds[0], { from: sender })
await escrowPaymentCondition.fulfill(agreementId, did, escrowAmounts, receivers, escrowPaymentCondition.address, token.address, agreement.conditionIds[1], agreement.conditionIds[0], { from: sender })
assert.strictEqual(
(await conditionStoreManager.getConditionState(agreement.conditionIds[2])).toNumber(),
constants.condition.state.fulfilled
Expand Down Expand Up @@ -265,7 +265,7 @@ contract('Access Template integration test', (accounts) => {

// fulfill lock reward
await token.approve(lockPaymentCondition.address, totalAmount, { from: sender })
await lockPaymentCondition.fulfill(agreementId, did, escrowPaymentCondition.address, escrowAmounts, receivers, { from: sender })
await lockPaymentCondition.fulfill(agreementId, did, escrowPaymentCondition.address, token.address, escrowAmounts, receivers, { from: sender })
assert.strictEqual(
(await conditionStoreManager.getConditionState(agreement.conditionIds[1])).toNumber(),
constants.condition.state.fulfilled)
Expand Down Expand Up @@ -296,6 +296,7 @@ contract('Access Template integration test', (accounts) => {
escrowAmounts,
receivers,
escrowPaymentCondition.address,
token.address,
agreement.conditionIds[1],
agreement.conditionIds[0],
{ from: receiver }
Expand Down Expand Up @@ -334,6 +335,7 @@ contract('Access Template integration test', (accounts) => {
agreement2Amounts,
receivers,
escrowPaymentCondition.address,
token.address,
agreement2.conditionIds[1],
agreement2.conditionIds[0]))

Expand All @@ -345,25 +347,25 @@ contract('Access Template integration test', (accounts) => {

// fulfill lock reward
await token.approve(lockPaymentCondition.address, totalAmount, { from: sender })
await lockPaymentCondition.fulfill(agreementId, did, escrowPaymentCondition.address, escrowAmounts, receivers, { from: sender })
await lockPaymentCondition.fulfill(agreementId, did, escrowPaymentCondition.address, token.address, escrowAmounts, receivers, { from: sender })

await token.approve(lockPaymentCondition.address, totalAmount * 2, { from: sender })
await lockPaymentCondition.fulfill(agreementId2, did, escrowPaymentCondition.address, escrowAmounts, receivers, { from: sender })
await lockPaymentCondition.fulfill(agreementId2, did, escrowPaymentCondition.address, token.address, escrowAmounts, receivers, { from: sender })
// fulfill access
await accessCondition.fulfill(agreementId, agreement.did, receiver, { from: receiver })
await accessCondition.fulfill(agreementId2, agreement2.did, receiver, { from: receiver })

// get reward
await assert.isRejected(
escrowPaymentCondition.fulfill(agreementId2, agreement2.did, agreement2Amounts, receivers, agreement2.conditionIds[1], agreement2.conditionIds[0], { from: receiver })
escrowPaymentCondition.fulfill(agreementId2, agreement2.did, agreement2Amounts, receivers, token.address, agreement2.conditionIds[1], agreement2.conditionIds[0], { from: receiver })
)

assert.strictEqual(
(await conditionStoreManager.getConditionState(agreement.conditionIds[2])).toNumber(),
constants.condition.state.unfulfilled
)

await escrowPaymentCondition.fulfill(agreementId, agreement.did, escrowAmounts, receivers, escrowPaymentCondition.address, agreement.conditionIds[1], agreement.conditionIds[0], { from: receiver })
await escrowPaymentCondition.fulfill(agreementId, agreement.did, escrowAmounts, receivers, escrowPaymentCondition.address, token.address, agreement.conditionIds[1], agreement.conditionIds[0], { from: receiver })
assert.strictEqual(
(await conditionStoreManager.getConditionState(agreement.conditionIds[2])).toNumber(),
constants.condition.state.fulfilled
Expand Down

0 comments on commit c83850c

Please sign in to comment.