Skip to content
This repository has been archived by the owner on Aug 6, 2021. It is now read-only.

Commit

Permalink
Merge pull request #43 from atpar/feature/ap-585-dvp-beneficiary
Browse files Browse the repository at this point in the history
adding creatorBeneficiary to DvP contract
  • Loading branch information
jo-es committed Aug 3, 2020
2 parents cb1980f + 01f396b commit ea14122
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 5 deletions.
9 changes: 8 additions & 1 deletion packages/ap-contracts/contracts/DvPSettlement.sol
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ contract DvPSettlement {
address creator;
address creatorToken;
uint256 creatorAmount;
address creatorBeneficiary;
address counterparty;
address counterpartyToken;
uint256 counterpartyAmount;
Expand All @@ -45,6 +46,7 @@ contract DvPSettlement {
function createSettlement(
address creatorToken,
uint256 creatorAmount,
address creatorBeneficiary,
address counterparty,
address counterpartyToken,
uint256 counterpartyAmount,
Expand All @@ -62,6 +64,7 @@ contract DvPSettlement {
settlements[id].creator = msg.sender;
settlements[id].creatorToken = creatorToken;
settlements[id].creatorAmount = creatorAmount;
settlements[id].creatorBeneficiary = creatorBeneficiary;
settlements[id].counterparty = counterparty;
settlements[id].counterpartyToken = counterpartyToken;
settlements[id].counterpartyAmount = counterpartyAmount;
Expand Down Expand Up @@ -105,12 +108,16 @@ contract DvPSettlement {
"DvPSettlement.executeSettlement - sender not allowed to execute settlement"
);

// if empty (0x0) creatorBeneficiary address, send funds to creator
address creatorReveiver = (settlements[id].creatorBeneficiary == address(0)) ?
settlements[id].creator : settlements[id].creatorBeneficiary;

// transfer both tokens
require(
(IERC20(settlements[id].counterpartyToken)
.transferFrom(
msg.sender,
settlements[id].creator,
creatorReveiver,
settlements[id].counterpartyAmount
)),
"DvPSettlement.executeSettlement - transferFrom sender failed"
Expand Down
68 changes: 64 additions & 4 deletions packages/ap-contracts/test/DvP/TestDvPSettlement.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ function timeNow() {
}

contract('DvPSettlement', function (accounts) {
const [creator, counterparty, someone, anyone] = accounts;
const [creator, counterparty, creatorBeneficiary, someone, anyone] = accounts;
const creatorAmount = new BN(20000);
const counterpartyAmount = new BN(1000);

Expand All @@ -38,6 +38,7 @@ contract('DvPSettlement', function (accounts) {
const receipt = await this.dvpSettlementContract.createSettlement(
this.creatorToken.address,
creatorAmount,
creatorBeneficiary,
counterparty,
this.counterpartyToken.address,
counterpartyAmount,
Expand Down Expand Up @@ -72,8 +73,8 @@ contract('DvPSettlement', function (accounts) {
const settlement2 = await this.dvpSettlementContract.settlements(id)
assert.equal(settlement2.status, '2');

// Check the balance of both creator and counterparty after execution to ensure that tokens were swapped
const creatorBalanceOfcpToken = await this.counterpartyToken.balanceOf(creator);
// Check the balance of both creatorBeneficiary and counterparty after execution to ensure that tokens were swapped
const creatorBalanceOfcpToken = await this.counterpartyToken.balanceOf(creatorBeneficiary);
const cpBalanceOfCreatorToken = await this.creatorToken.balanceOf(counterparty);
creatorBalanceOfcpToken.should.be.bignumber.equal(counterpartyAmount);
cpBalanceOfCreatorToken.should.be.bignumber.equal(creatorAmount);
Expand All @@ -87,6 +88,7 @@ contract('DvPSettlement', function (accounts) {
const receipt = await this.dvpSettlementContract.createSettlement(
this.creatorToken.address,
creatorAmount,
creatorBeneficiary,
ZERO_ADDRESS,
this.counterpartyToken.address,
counterpartyAmount,
Expand All @@ -113,13 +115,68 @@ contract('DvPSettlement', function (accounts) {
const settlement2 = await this.dvpSettlementContract.settlements(id)
assert.equal(settlement2.status, '2');

const creatorBalanceOfcpToken = await this.counterpartyToken.balanceOf(creator);
const creatorBalanceOfcpToken = await this.counterpartyToken.balanceOf(creatorBeneficiary);
const cpBalanceOfCreatorToken = await this.creatorToken.balanceOf(counterparty);

creatorBalanceOfcpToken.should.be.bignumber.equal(counterpartyAmount);
cpBalanceOfCreatorToken.should.be.bignumber.equal(creatorAmount);

});

it('should pass empty beneficiary end to end test', async function () {

// creator must first approve at least `creatorAmount` of tokens before creating a new settlement
await this.creatorToken.approve(this.dvpSettlementContract.address, creatorAmount, { from: creator });

// some time in the future
const tomorrow = timeNow() + (60 * 60 * 24)

// create new Settlement with a specified `counterparty` address
const receipt = await this.dvpSettlementContract.createSettlement(
this.creatorToken.address,
creatorAmount,
ZERO_ADDRESS,
counterparty,
this.counterpartyToken.address,
counterpartyAmount,
tomorrow,
{ from: creator }
);

// check that the contract has received the `creatorAmount` of `creatorToken`
(await this.creatorToken.balanceOf(this.dvpSettlementContract.address)).should.be.bignumber.equal(creatorAmount);

// get Settlement ID from the logs
const id = receipt.logs[0].args[0];

// Read settlement from blockchain and check that it is initialized correctly
const settlement = await this.dvpSettlementContract.settlements(id);
(settlement.expirationDate).should.be.bignumber.equal(new BN(tomorrow));
assert.equal(settlement.status, '1');


// counterparty must approve at least `counterpartyAmount` of `counterpartyToken` before calling executeSettlement
await this.counterpartyToken.approve(this.dvpSettlementContract.address, counterpartyAmount, { from: counterparty });

// counterparty calls executeSettlement
const tx = await this.dvpSettlementContract.executeSettlement(id, { from: counterparty })

// check that SettlementExecuted event exists in transaction logs
await expectEvent.inTransaction(
tx.tx, DvPSettlement, 'SettlementExecuted'
);

// Read the settlement from the blockchain again and check that status is now EXECUTED
const settlement2 = await this.dvpSettlementContract.settlements(id)
assert.equal(settlement2.status, '2');

// Check the balance of both creator and counterparty after execution to ensure that tokens were swapped
const creatorBalanceOfcpToken = await this.counterpartyToken.balanceOf(creator);
const cpBalanceOfCreatorToken = await this.creatorToken.balanceOf(counterparty);
creatorBalanceOfcpToken.should.be.bignumber.equal(counterpartyAmount);
cpBalanceOfCreatorToken.should.be.bignumber.equal(creatorAmount);

});
});

describe('expiration date tests', function () {
Expand All @@ -130,6 +187,7 @@ contract('DvPSettlement', function (accounts) {
this.dvpSettlementContract.createSettlement(
this.creatorToken.address,
creatorAmount,
creatorBeneficiary,
counterparty,
this.counterpartyToken.address,
counterpartyAmount,
Expand All @@ -145,6 +203,7 @@ contract('DvPSettlement', function (accounts) {
const tx = await this.dvpSettlementContract.createSettlement(
this.creatorToken.address,
creatorAmount,
creatorBeneficiary,
counterparty,
this.counterpartyToken.address,
counterpartyAmount,
Expand All @@ -169,6 +228,7 @@ contract('DvPSettlement', function (accounts) {
const tx = await this.dvpSettlementContract.createSettlement(
this.creatorToken.address,
creatorAmount,
creatorBeneficiary,
counterparty,
this.counterpartyToken.address,
counterpartyAmount,
Expand Down

0 comments on commit ea14122

Please sign in to comment.