Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
O
openzeppelin-contracts-upgradeable
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
俞永鹏
openzeppelin-contracts-upgradeable
Commits
e952a60b
Commit
e952a60b
authored
Oct 12, 2016
by
Manuel Aráoz
Committed by
GitHub
Oct 12, 2016
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #25 from maraoz/ppc-test
Add tests to PullPayment
parents
5ec4fbeb
25a02101
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
89 additions
and
21 deletions
+89
-21
PullPayment.sol
contracts/PullPayment.sol
+10
-5
PullPaymentExample.sol
contracts/examples/PullPaymentExample.sol
+0
-11
PullPaymentMock.sol
contracts/test-helpers/PullPaymentMock.sol
+2
-1
PullPayment.js
test/PullPayment.js
+77
-4
No files found.
contracts/PullPayment.sol
View file @
e952a60b
...
...
@@ -13,11 +13,16 @@ contract PullPayment {
}
// withdraw accumulated balance, called by payee
function withdrawPayments() external {
uint payment = payments[msg.sender];
payments[msg.sender] = 0;
if (!msg.sender.send(payment)) {
payments[msg.sender] = payment;
function withdrawPayments() {
address payee = msg.sender;
uint payment = payments[payee];
if (payment == 0) throw;
if (this.balance < payment) throw;
payments[payee] = 0;
if (!payee.send(payment)) {
throw;
}
}
}
contracts/examples/PullPaymentExample.sol
deleted
100644 → 0
View file @
5ec4fbeb
pragma solidity ^0.4.0;
import '../PullPayment.sol';
// Example class using PullPayment
contract PullPaymentExample is PullPayment {
// test helper function to call asyncSend
function callSend(address dest, uint amount) external {
asyncSend(dest, amount);
}
}
contracts/test-helpers/PullPaymentMock.sol
View file @
e952a60b
...
...
@@ -4,7 +4,8 @@ import '../PullPayment.sol';
// mock class using PullPayment
contract PullPaymentMock is PullPayment {
// test helper function to call asyncSend
function callSend(address dest, uint amount)
external
{
function callSend(address dest, uint amount) {
asyncSend(dest, amount);
}
}
test/PullPayment.js
View file @
e952a60b
contract
(
'PullPayment
Example
'
,
function
(
accounts
)
{
contract
(
'PullPayment'
,
function
(
accounts
)
{
it
(
"can't call asyncSend externally"
,
function
(
done
)
{
var
ppc
;
return
PullPaymentExample
.
new
()
return
PullPaymentMock
.
new
()
.
then
(
function
(
ppc
)
{
assert
.
isUndefined
(
ppc
.
asyncSend
);
})
...
...
@@ -12,7 +11,7 @@ contract('PullPaymentExample', function(accounts) {
it
(
"can record an async payment correctly"
,
function
(
done
)
{
var
ppce
;
var
AMOUNT
=
100
;
return
PullPayment
Example
.
new
()
return
PullPayment
Mock
.
new
()
.
then
(
function
(
_ppce
)
{
ppce
=
_ppce
;
ppce
.
callSend
(
accounts
[
0
],
AMOUNT
)
...
...
@@ -26,4 +25,78 @@ contract('PullPaymentExample', function(accounts) {
.
then
(
done
);
});
it
(
"can add multiple balances on one account"
,
function
(
done
)
{
var
ppce
;
return
PullPaymentMock
.
new
()
.
then
(
function
(
_ppce
)
{
ppce
=
_ppce
;
return
ppce
.
callSend
(
accounts
[
0
],
200
)
})
.
then
(
function
()
{
return
ppce
.
callSend
(
accounts
[
0
],
300
)
})
.
then
(
function
()
{
return
ppce
.
payments
(
accounts
[
0
]);
})
.
then
(
function
(
paymentsToAccount0
)
{
assert
.
equal
(
paymentsToAccount0
,
500
);
})
.
then
(
done
);
});
it
(
"can add balances on multiple accounts"
,
function
(
done
)
{
var
ppce
;
return
PullPaymentMock
.
new
()
.
then
(
function
(
_ppce
)
{
ppce
=
_ppce
;
return
ppce
.
callSend
(
accounts
[
0
],
200
)
})
.
then
(
function
()
{
return
ppce
.
callSend
(
accounts
[
1
],
300
)
})
.
then
(
function
()
{
return
ppce
.
payments
(
accounts
[
0
]);
})
.
then
(
function
(
paymentsToAccount0
)
{
assert
.
equal
(
paymentsToAccount0
,
200
);
})
.
then
(
function
()
{
return
ppce
.
payments
(
accounts
[
1
]);
})
.
then
(
function
(
paymentsToAccount0
)
{
assert
.
equal
(
paymentsToAccount0
,
300
);
})
.
then
(
done
);
});
it
(
"can withdraw payment"
,
function
(
done
)
{
var
ppce
;
var
AMOUNT
=
17
*
1
e18
;
var
payee
=
accounts
[
1
];
var
initialBalance
=
web3
.
eth
.
getBalance
(
payee
);
return
PullPaymentMock
.
new
({
value
:
AMOUNT
})
.
then
(
function
(
_ppce
)
{
ppce
=
_ppce
;
return
ppce
.
callSend
(
payee
,
AMOUNT
);
})
.
then
(
function
()
{
return
ppce
.
payments
(
payee
);
})
.
then
(
function
(
paymentsToAccount0
)
{
assert
.
equal
(
paymentsToAccount0
,
AMOUNT
);
})
.
then
(
function
()
{
return
ppce
.
withdrawPayments
({
from
:
payee
});
})
.
then
(
function
()
{
return
ppce
.
payments
(
payee
);
})
.
then
(
function
(
paymentsToAccount0
)
{
assert
.
equal
(
paymentsToAccount0
,
0
);
var
balance
=
web3
.
eth
.
getBalance
(
payee
);
assert
(
Math
.
abs
(
balance
-
initialBalance
-
AMOUNT
)
<
1
e16
);
})
.
then
(
done
);
});
});
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment