Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update extend with price in StateChannel #417

Merged
merged 4 commits into from
Jul 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions contracts/StateChannel.sol
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ contract StateChannel is Initializable, OwnableUpgradeable, SQParameter {
bytes callback
);
/// @notice Emitted when extend the channel
event ChannelExtend(uint256 indexed channelId, uint256 expiredAt);
event ChannelExtend(uint256 indexed channelId, uint256 expiredAt, uint256 price);
/// @notice Emitted when deposit more amount to the channel
event ChannelFund(uint256 indexed channelId, uint256 realTotal, uint256 total);
/// @notice Emitted when indexer send a checkpoint to claim the part-amount
Expand Down Expand Up @@ -245,21 +245,23 @@ contract StateChannel is Initializable, OwnableUpgradeable, SQParameter {
* @param expiration Extend tiem in seconds
* @param indexerSign indexer's signature
* @param consumerSign consumer's signature
* @param price new price
*/
function extend(
uint256 channelId,
uint256 preExpirationAt,
uint256 expiration,
bytes memory indexerSign,
bytes memory consumerSign
bytes memory consumerSign,
uint256 price
) external {
address indexer = channels[channelId].indexer;
address consumer = channels[channelId].consumer;
require(channels[channelId].expiredAt == preExpirationAt, 'SC002');

// check sign
bytes32 payload = keccak256(
abi.encode(channelId, indexer, consumer, preExpirationAt, expiration)
abi.encode(channelId, indexer, consumer, price, preExpirationAt, expiration)
);
if (_isContract(consumer)) {
require(IConsumer(consumer).checkSign(channelId, payload, consumerSign), 'C006');
Expand All @@ -269,7 +271,9 @@ contract StateChannel is Initializable, OwnableUpgradeable, SQParameter {
_checkSign(payload, indexerSign, indexer, true);

channels[channelId].expiredAt += expiration;
emit ChannelExtend(channelId, channels[channelId].expiredAt);
channelPrice[channelId] = price;

emit ChannelExtend(channelId, channels[channelId].expiredAt, price);
}

/**
Expand Down
22 changes: 18 additions & 4 deletions test/StateChannel.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -544,16 +544,30 @@ describe('StateChannel Contract', () => {
const preExpirationAt = state.expiredAt;
const nextExpiration = 5;
const msg2 = abi.encode(
['uint256', 'address', 'address', 'uint256', 'uint256'],
[channelId, runner.address, consumer.address, preExpirationAt, nextExpiration]
['uint256', 'address', 'address', 'uint256', 'uint256', 'uint256'],
[channelId, runner.address, consumer.address, etherParse('0.2'), preExpirationAt, nextExpiration]
);
const payload2 = ethers.utils.keccak256(msg2);
const indexerSign = await runner.signMessage(ethers.utils.arrayify(payload2));
const consumerSign = await consumer.signMessage(ethers.utils.arrayify(payload2));

await stateChannel.extend(channelId, preExpirationAt, nextExpiration, indexerSign, consumerSign);
await stateChannel.extend(
channelId,
preExpirationAt,
nextExpiration,
indexerSign,
consumerSign,
etherParse('0.2')
);
await expect(
stateChannel.extend(channelId, preExpirationAt, nextExpiration, indexerSign, consumerSign)
stateChannel.extend(
channelId,
preExpirationAt,
nextExpiration,
indexerSign,
consumerSign,
etherParse('0.2')
)
).to.be.revertedWith('SC002');
});

Expand Down
Loading