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
25a02101
Unverified
Commit
25a02101
authored
Oct 11, 2016
by
Manuel Araoz
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
improve PullPaymentCapable
parent
5ec4fbeb
Show 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 @
25a02101
...
@@ -13,11 +13,16 @@ contract PullPayment {
...
@@ -13,11 +13,16 @@ contract PullPayment {
}
}
// withdraw accumulated balance, called by payee
// withdraw accumulated balance, called by payee
function withdrawPayments() external {
function withdrawPayments() {
uint payment = payments[msg.sender];
address payee = msg.sender;
payments[msg.sender] = 0;
uint payment = payments[payee];
if (!msg.sender.send(payment)) {
payments[msg.sender] = payment;
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 @
25a02101
...
@@ -4,7 +4,8 @@ import '../PullPayment.sol';
...
@@ -4,7 +4,8 @@ import '../PullPayment.sol';
// mock class using PullPayment
// mock class using PullPayment
contract PullPaymentMock is PullPayment {
contract PullPaymentMock is PullPayment {
// test helper function to call asyncSend
// test helper function to call asyncSend
function callSend(address dest, uint amount)
external
{
function callSend(address dest, uint amount) {
asyncSend(dest, amount);
asyncSend(dest, amount);
}
}
}
}
test/PullPayment.js
View file @
25a02101
contract
(
'PullPayment
Example
'
,
function
(
accounts
)
{
contract
(
'PullPayment'
,
function
(
accounts
)
{
it
(
"can't call asyncSend externally"
,
function
(
done
)
{
it
(
"can't call asyncSend externally"
,
function
(
done
)
{
var
ppc
;
return
PullPaymentMock
.
new
()
return
PullPaymentExample
.
new
()
.
then
(
function
(
ppc
)
{
.
then
(
function
(
ppc
)
{
assert
.
isUndefined
(
ppc
.
asyncSend
);
assert
.
isUndefined
(
ppc
.
asyncSend
);
})
})
...
@@ -12,7 +11,7 @@ contract('PullPaymentExample', function(accounts) {
...
@@ -12,7 +11,7 @@ contract('PullPaymentExample', function(accounts) {
it
(
"can record an async payment correctly"
,
function
(
done
)
{
it
(
"can record an async payment correctly"
,
function
(
done
)
{
var
ppce
;
var
ppce
;
var
AMOUNT
=
100
;
var
AMOUNT
=
100
;
return
PullPayment
Example
.
new
()
return
PullPayment
Mock
.
new
()
.
then
(
function
(
_ppce
)
{
.
then
(
function
(
_ppce
)
{
ppce
=
_ppce
;
ppce
=
_ppce
;
ppce
.
callSend
(
accounts
[
0
],
AMOUNT
)
ppce
.
callSend
(
accounts
[
0
],
AMOUNT
)
...
@@ -26,4 +25,78 @@ contract('PullPaymentExample', function(accounts) {
...
@@ -26,4 +25,78 @@ contract('PullPaymentExample', function(accounts) {
.
then
(
done
);
.
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