Skip to content

Commit 1db06c9

Browse files
authored
Merge pull request #1323 from JoinColony/feat/streaming-payments-duration
StreamingPayments starttime=0 behaviour change
2 parents 6554f07 + 588d9a4 commit 1db06c9

File tree

3 files changed

+41
-15
lines changed

3 files changed

+41
-15
lines changed

contracts/extensions/StreamingPayments.sol

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ contract StreamingPayments is ColonyExtensionMeta {
116116
/// @notice Returns the version of the extension
117117
/// @return _version The extension's version number
118118
function version() public pure override returns (uint256 _version) {
119-
return 6;
119+
return 7;
120120
}
121121

122122
/// @notice Called when upgrading the extension
@@ -137,7 +137,8 @@ contract StreamingPayments is ColonyExtensionMeta {
137137
/// @param _adminChildSkillIndex The index linking the adminPermissionDomainId to the domainId
138138
/// @param _domainId The domain out of which the streaming payment will be paid
139139
/// @param _startTime The time at which the payment begins paying out
140-
/// @param _endTime The time at which the payment ends paying out
140+
/// @param _endTimeOrDuration The time at which the payment ends paying out. If _startTime is 0, then this is
141+
/// the duration of the payment from the current time
141142
/// @param _interval The period of time over which _amounts are paid out
142143
/// @param _recipient The recipient of the streaming payment
143144
/// @param _token The token to be paid out
@@ -149,7 +150,7 @@ contract StreamingPayments is ColonyExtensionMeta {
149150
uint256 _adminChildSkillIndex,
150151
uint256 _domainId,
151152
uint256 _startTime,
152-
uint256 _endTime,
153+
uint256 _endTimeOrDuration,
153154
uint256 _interval,
154155
address payable _recipient,
155156
address _token,
@@ -160,23 +161,37 @@ contract StreamingPayments is ColonyExtensionMeta {
160161
validateFundingPermission(_fundingPermissionDomainId, _fundingChildSkillIndex, _domainId)
161162
validateAdministrationPermission(_adminPermissionDomainId, _adminChildSkillIndex, _domainId)
162163
{
163-
uint256 startTime = (_startTime == 0) ? block.timestamp : _startTime;
164-
165-
require(startTime <= _endTime, "streaming-payments-bad-end-time");
166-
require(_interval > 0, "streaming-payments-bad-interval");
167-
168164
numStreamingPayments++;
169165
streamingPayments[numStreamingPayments] = StreamingPayment(
170166
_recipient,
171167
_domainId,
172-
startTime,
173-
_endTime,
168+
_startTime,
169+
_endTimeOrDuration,
174170
_interval,
175171
_token,
176172
_amount,
177173
0
178174
);
179175

176+
StreamingPayment storage streamingPayment = streamingPayments[numStreamingPayments];
177+
178+
require(
179+
streamingPayment.startTime <= streamingPayment.endTime,
180+
"streaming-payments-bad-end-time"
181+
);
182+
require(streamingPayment.interval > 0, "streaming-payments-bad-interval");
183+
184+
if (_startTime == 0) {
185+
// If _startTime was 0, then we set the startTime to now
186+
streamingPayments[numStreamingPayments].startTime = block.timestamp;
187+
// If _startTime was 0, then we interpret _endTime as a duration from now, not an absolute time, if possible
188+
if (streamingPayments[numStreamingPayments].endTime < 2 ** 256 - 1 - block.timestamp) {
189+
streamingPayments[numStreamingPayments].endTime += block.timestamp;
190+
} else {
191+
streamingPayments[numStreamingPayments].endTime = 2 ** 256 - 1;
192+
}
193+
}
194+
180195
if (getAmountClaimableLifetime(numStreamingPayments) > 0) {
181196
nUnresolvedStreamingPayments += 1;
182197
}

docs/interfaces/extensions/streamingpayments.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ Claim a streaming payment
6565
|_id|uint256|The id of the streaming payment
6666

6767

68-
### `create(uint256 _fundingPermissionDomainId, uint256 _fundingChildSkillIndex, uint256 _adminPermissionDomainId, uint256 _adminChildSkillIndex, uint256 _domainId, uint256 _startTime, uint256 _endTime, uint256 _interval, address _recipient, address _token, uint256 _amount)`
68+
### `create(uint256 _fundingPermissionDomainId, uint256 _fundingChildSkillIndex, uint256 _adminPermissionDomainId, uint256 _adminChildSkillIndex, uint256 _domainId, uint256 _startTime, uint256 _endTimeOrDuration, uint256 _interval, address _recipient, address _token, uint256 _amount)`
6969

7070
Creates a new streaming payment
7171

@@ -80,7 +80,7 @@ Creates a new streaming payment
8080
|_adminChildSkillIndex|uint256|The index linking the adminPermissionDomainId to the domainId
8181
|_domainId|uint256|The domain out of which the streaming payment will be paid
8282
|_startTime|uint256|The time at which the payment begins paying out
83-
|_endTime|uint256|The time at which the payment ends paying out
83+
|_endTimeOrDuration|uint256|The time at which the payment ends paying out. If _startTime is 0, then this is the duration of the payment from the current time
8484
|_interval|uint256|The period of time over which _amounts are paid out
8585
|_recipient|address|The recipient of the streaming payment
8686
|_token|address|The token to be paid out

test/extensions/streaming-payments.js

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,16 @@ contract("Streaming Payments", (accounts) => {
118118
expect(streamingPaymentCount).to.eq.BN(1);
119119
});
120120

121+
it("when creating a streaming payment, if start time is 0, endTime is treated as a duration", async () => {
122+
const tx = await streamingPayments.create(1, UINT256_MAX, 1, UINT256_MAX, 1, 0, SECONDS_PER_DAY, SECONDS_PER_DAY, USER1, token.address, WAD);
123+
const streamingPaymentId = await streamingPayments.getNumStreamingPayments();
124+
const streamingPayment = await streamingPayments.getStreamingPayment(streamingPaymentId);
125+
126+
const blockTime = await getBlockTime(tx.receipt.blockNumber);
127+
expect(streamingPayment.startTime).to.eq.BN(blockTime);
128+
expect(streamingPayment.endTime).to.eq.BN(blockTime + SECONDS_PER_DAY);
129+
});
130+
121131
it("cannot create a streaming payment without relevant permissions", async () => {
122132
await checkErrorRevert(
123133
streamingPayments.create(1, UINT256_MAX, 1, UINT256_MAX, 1, 0, UINT256_MAX, SECONDS_PER_DAY, USER1, token.address, WAD, { from: USER1 }),
@@ -218,18 +228,19 @@ contract("Streaming Payments", (accounts) => {
218228
});
219229

220230
it("can update the end time", async () => {
221-
const blockTime = await getBlockTime();
231+
let tx = await streamingPayments.create(1, UINT256_MAX, 1, UINT256_MAX, 1, 0, SECONDS_PER_DAY, SECONDS_PER_DAY, USER1, token.address, WAD);
232+
233+
const blockTime = await getBlockTime(tx.receipt.blockNumber);
222234
const endTime = blockTime + SECONDS_PER_DAY;
223235
const newEndTime = endTime + SECONDS_PER_DAY;
224236

225-
await streamingPayments.create(1, UINT256_MAX, 1, UINT256_MAX, 1, 0, endTime, SECONDS_PER_DAY, USER1, token.address, WAD);
226237
const streamingPaymentId = await streamingPayments.getNumStreamingPayments();
227238

228239
let streamingPayment;
229240
streamingPayment = await streamingPayments.getStreamingPayment(streamingPaymentId);
230241
expect(streamingPayment.endTime).to.eq.BN(endTime);
231242

232-
const tx = await streamingPayments.setEndTime(1, UINT256_MAX, streamingPaymentId, newEndTime);
243+
tx = await streamingPayments.setEndTime(1, UINT256_MAX, streamingPaymentId, newEndTime);
233244
streamingPayment = await streamingPayments.getStreamingPayment(streamingPaymentId);
234245
expect(streamingPayment.endTime).to.eq.BN(newEndTime);
235246

0 commit comments

Comments
 (0)