Skip to content

Commit

Permalink
Finance: include payment repeat number in transactions (#257)
Browse files Browse the repository at this point in the history
  • Loading branch information
sohkai committed Apr 25, 2018
1 parent 4f2d9a7 commit 7b30e2c
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 9 deletions.
16 changes: 13 additions & 3 deletions apps/finance/contracts/Finance.sol
Expand Up @@ -51,6 +51,7 @@ contract Finance is AragonApp {
uint256 amount;
uint256 paymentId;
string reference;
uint256 paymentRepeatNumber;
}

struct TokenStatement {
Expand Down Expand Up @@ -202,7 +203,8 @@ contract Finance is AragonApp {
_token,
_receiver,
_amount,
0, // unrelated to any payment id, it isn't created
0, // unrelated to any payment id; it isn't created
0, // also unrelated to any payment repeats
_reference
);
return;
Expand Down Expand Up @@ -368,7 +370,7 @@ contract Finance is AragonApp {
createdBy = payment.createdBy;
}

function getTransaction(uint256 _transactionId) public view returns (uint256 periodId, uint256 amount, uint256 paymentId, address token, address entity, bool isIncoming, uint64 date, string reference) {
function getTransaction(uint256 _transactionId) public view returns (uint256 periodId, uint256 amount, uint256 paymentId, uint256 paymentRepeatNumber, address token, address entity, bool isIncoming, uint64 date, string reference) {
Transaction storage transaction = transactions[_transactionId];

token = transaction.token;
Expand All @@ -378,6 +380,7 @@ contract Finance is AragonApp {
periodId = transaction.periodId;
amount = transaction.amount;
paymentId = transaction.paymentId;
paymentRepeatNumber = transaction.paymentRepeatNumber;
reference = transaction.reference;
}

Expand Down Expand Up @@ -478,6 +481,7 @@ contract Finance is AragonApp {
payment.receiver,
payment.amount,
_paymentId,
payment.repeats,
"" // since paymentId is saved, the payment reference can be fetched
);
}
Expand All @@ -488,6 +492,7 @@ contract Finance is AragonApp {
address _receiver,
uint256 _amount,
uint256 _paymentId,
uint256 _paymentRepeatNumber,
string _reference
) isInitialized internal
{
Expand All @@ -498,6 +503,7 @@ contract Finance is AragonApp {
_receiver,
_amount,
_paymentId,
_paymentRepeatNumber,
_reference
);

Expand All @@ -517,6 +523,7 @@ contract Finance is AragonApp {
_sender,
_amount,
0, // unrelated to any existing payment
0, // and no payment repeats
_reference
);
}
Expand All @@ -527,6 +534,7 @@ contract Finance is AragonApp {
address _entity,
uint256 _amount,
uint256 _paymentId,
uint256 _paymentRepeatNumber,
string _reference
) internal
{
Expand All @@ -543,15 +551,17 @@ contract Finance is AragonApp {
transaction.periodId = periodId;
transaction.amount = _amount;
transaction.paymentId = _paymentId;
transaction.paymentRepeatNumber = _paymentRepeatNumber;
transaction.isIncoming = _incoming;
transaction.token = _token;
transaction.entity = _entity;
transaction.date = uint64(getTimestamp());
transaction.reference = _reference;

Period storage period = periods[periodId];
if (period.firstTransactionId == 0)
if (period.firstTransactionId == 0) {
period.firstTransactionId = transactionId;
}

NewTransaction(transactionId, _incoming, _entity, _amount);
}
Expand Down
27 changes: 21 additions & 6 deletions apps/finance/test/finance.js
Expand Up @@ -74,13 +74,14 @@ contract('Finance App', accounts => {
await token1.approve(app.address, 5)
await app.deposit(token1.address, 5, 'ref')

const [periodId, amount, paymentId, token, entity, incoming, date, ref] = await app.getTransaction(1)
const [periodId, amount, paymentId, paymentRepeatNumber, token, entity, incoming, date, ref] = await app.getTransaction(1)

// vault has 100 token1 initially
assert.equal((await token1.balanceOf(vault.address)).toString(), 100 + 5, 'deposited tokens must be in vault')
assert.equal(periodId, 0, 'period id should be correct')
assert.equal(amount, 5, 'amount should be correct')
assert.equal(paymentId, 0, 'payment id should be 0')
assert.equal(paymentRepeatNumber, 0, 'payment repeat number should be 0')
assert.equal(token, token1.address, 'token should be correct')
assert.equal(entity, accounts[0], 'entity should be correct')
assert.isTrue(incoming, 'tx should be incoming')
Expand All @@ -97,13 +98,14 @@ contract('Finance App', accounts => {
it('records ETH deposits', async () => {
await app.send(10, { gas: 3e5 })

const [periodId, amount, paymentId, token, entity, incoming, date, ref] = await app.getTransaction(1)
const [periodId, amount, paymentId, paymentRepeatNumber, token, entity, incoming, date, ref] = await app.getTransaction(1)

// vault has 400 wei initially
assert.equal(await ETHConnector.at(vault.address).balance(ETH), 400 + 10, 'deposited ETH must be in vault')
assert.equal(periodId, 0, 'period id should be correct')
assert.equal(amount, 10, 'amount should be correct')
assert.equal(paymentId, 0, 'payment id should be 0')
assert.equal(paymentRepeatNumber, 0, 'payment repeat number should be 0')
assert.equal(token, ETH, 'token should be ETH token')
assert.equal(entity, accounts[0], 'entity should be correct')
assert.isTrue(incoming, 'tx should be incoming')
Expand All @@ -120,14 +122,15 @@ contract('Finance App', accounts => {

await app.depositToVault(token1.address)

const [periodId, amount, paymentId, token, entity, incoming, date, ref] = await app.getTransaction(1)
const [periodId, amount, paymentId, paymentRepeatNumber, token, entity, incoming, date, ref] = await app.getTransaction(1)

let finalBalance = await token1.balanceOf(vault.address)
assert.equal(finalBalance.toString(), initialBalance.plus(5).toString(), 'deposited tokens must be in vault')
assert.equal(await token1.balanceOf(app.address), 0, 'finance shouldn\'t have tokens')
assert.equal(periodId, 0, 'period id should be correct')
assert.equal(amount, 5, 'amount should be correct')
assert.equal(paymentId, 0, 'payment id should be 0')
assert.equal(paymentRepeatNumber, 0, 'payment repeat number should be 0')
assert.equal(token, token1.address, 'token should be correct')
assert.equal(entity, app.address, 'entity should be correct')
assert.isTrue(incoming, 'tx should be incoming')
Expand Down Expand Up @@ -212,10 +215,11 @@ contract('Finance App', accounts => {

assert.equal(await token1.balanceOf(recipient), amount, 'recipient should have received tokens')

const [periodId, am, paymentId, token, entity, isIncoming, date, ref] = await app.getTransaction(1)
const [periodId, am, paymentId, paymentRepeatNumber, token, entity, isIncoming, date, ref] = await app.getTransaction(1)
assert.equal(periodId, 0, 'period id should be correct')
assert.equal(am, amount, 'amount should match')
assert.equal(paymentId, 0, 'payment id should be 0 for single payment')
assert.equal(paymentRepeatNumber, 0, 'payment repeat number should be 0')
assert.equal(token, token1.address, 'token address should match')
assert.equal(entity, recipient, 'receiver should match')
assert.isFalse(isIncoming, 'single payment should be outgoing')
Expand Down Expand Up @@ -251,12 +255,23 @@ contract('Finance App', accounts => {
const amount = 10

// repeats up to 10 times every 2 seconds
await app.newPayment(token1.address, recipient, amount, time, 2, 10, '')
const firstReceipt = await app.newPayment(token1.address, recipient, amount, time, 2, 10, '')
await app.mock_setTimestamp(time + 4)
await app.executePayment(1)
const secondReceipt = await app.executePayment(1)

assert.equal(await token1.balanceOf(recipient), amount * 3, 'recipient should have received tokens')
assert.equal(await app.nextPaymentTime(1), time + 4 + 2, 'payment should be repeated again in 2')

return Promise.all([firstReceipt, secondReceipt].map(async (receipt, index) => {
const repeatNum = index + 1

const transactionId = receipt.logs.filter(log => log.event == 'NewTransaction')[0].args.transactionId
const [periodId, txAmount, paymentId, paymentRepeatNumber, token, entity, incoming, date, ref] = await app.getTransaction(transactionId)

assert.equal(txAmount, amount, 'amount should be correct')
assert.equal(paymentId, 1, 'payment id should be 1')
assert.equal(paymentRepeatNumber.toNumber(), repeatNum, `payment repeat number should be ${repeatNum}`)
}))
})

it('can create recurring ether payment', async () => {
Expand Down

0 comments on commit 7b30e2c

Please sign in to comment.