Skip to content

Use Max rate to charge for Notice Period #95

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

Open
wants to merge 9 commits into
base: arbitrum
Choose a base branch
from

Conversation

prateekreddy
Copy link
Member

This pull request introduces several updates to the MarketV1 smart contract, the Credit token contract, and associated deployment scripts and tests. The changes focus on improving functionality, enhancing security, and aligning the implementation with updated requirements. Key updates include modifications to job settlement logic, the addition of new features in the Credit contract, and updates to tests to reflect these changes.

Updates to MarketV1 Contract:

  • Added a maxRate field to the Job struct to track the highest rate for a job. This field is updated when a new higher rate is set, and additional logic ensures sufficient balance to cover the notice period's extra cost. (contracts/enclaves/MarketV1.sol, [1] [2]
  • Updated event definitions for JobOpened and JobClosed to include a timestamp field, improving traceability. (contracts/enclaves/MarketV1.sol, [1] [2]
  • Simplified the _jobSettle function by removing the settleTill parameter and using block.timestamp directly. This change also ensures the job balance and rate are validated before settlement. (contracts/enclaves/MarketV1.sol, contracts/enclaves/MarketV1.solL287-R311)
  • Modified _emergencyWithdrawCredit to directly update the job balance and emit a JobWithdrawn event, ensuring better transparency during emergency withdrawals. (contracts/enclaves/MarketV1.sol, contracts/enclaves/MarketV1.solL253-R263)

Updates to Credit Contract:

  • Introduced pause and unpause functions with a new PAUSER_ROLE, allowing the contract to be paused for security or administrative reasons. (contracts/token/Credit.sol, contracts/token/Credit.solR133-R169)
  • Added detailed documentation for key functions like mint, burn, and redeemAndBurn, improving code clarity. (contracts/token/Credit.sol, [1] [2]
  • Made the USDC address public for better visibility. (contracts/token/Credit.sol, contracts/token/Credit.solL78-R83)

Deployment and Testing Updates:

  • Commented out the reinitialization logic in the UpgradeMarketV1.ts deployment script, as it is no longer required. (scripts/deploy/enclaves/UpgradeMarketV1.ts, scripts/deploy/enclaves/UpgradeMarketV1.tsL39-R42)
  • Updated tests for MarketV1 to validate the new maxRate field and ensure lastSettled timestamps match the block timestamp when a job is opened. (test/enclaves/MarketV1.ts, [1] [2]

These changes collectively enhance the robustness, clarity, and maintainability of the contracts and associated codebase.

@prateekreddy prateekreddy requested a review from Copilot May 22, 2025 08:40
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR enhances smart contract functionality and its associated tests by introducing a new field (maxRate) in the MarketV1 contract to accurately track rate changes, updates event definitions to include timestamp data, and improves deposit/withdrawal as well as emergency procedures. Key changes include:

  • Updates in MarketV1.sol to remove the extra settleTill parameter and compute settlement using block.timestamp, while integrating maxRate for rate revisions.
  • Enhancements in Credit.sol with pause/unpause capabilities and refined redeemAndBurn functionality.
  • Adjustments in tests and deployment scripts to align with the new contract logic and behavior.

Reviewed Changes

Copilot reviewed 6 out of 6 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
test/token/Credit.ts Added comprehensive tests for access control, mint/burn, redeemAndBurn, and pause/unpause behavior.
test/enclaves/MarketV1.ts Updated test assertions for job settlement and deposit/withdraw logic using block.timestamp values.
scripts/deploy/enclaves/UpgradeMarketV1.ts Comments out deprecated reinitialization logic post-upgrade.
contracts/token/Credit.sol Introduced PAUSER_ROLE and updated function documentation; adjusted redeemAndBurn sequence.
contracts/enclaves/MarketV1.sol Redesigned job settlement and rate revision logic using block.timestamp and introduced maxRate.
addresses/421614.json Updated MarketV1 implementation address.
Comments suppressed due to low confidence (1)

contracts/token/Credit.sol:141

  • Verify that burning tokens before transferring USDC is the intended sequence; ensure that if the USDC transfer fails after burning, the contract state remains consistent or appropriate error handling is in place.
function redeemAndBurn(address _to, uint256 _amount) external whenNotPaused onlyRole(REDEEMER_ROLE) {

@@ -274,8 +277,8 @@ contract MarketV1 is
bytes32 jobId = bytes32(_jobIndex);

// create job with initial balance 0
jobs[jobId] = Job(_metadata, _owner, _provider, 0, 0, block.timestamp);
emit JobOpened(jobId, _metadata, _owner, _provider);
jobs[jobId] = Job(_metadata, _owner, _provider, 0, 0, block.timestamp, 0);
Copy link
Preview

Copilot AI May 22, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider initializing the maxRate field with the initial rate value (e.g. _rate) during job creation instead of 0. This would immediately reflect the actual job rate and simplify subsequent rate revision logic.

Suggested change
jobs[jobId] = Job(_metadata, _owner, _provider, 0, 0, block.timestamp, 0);
jobs[jobId] = Job(_metadata, _owner, _provider, 0, _rate, block.timestamp, 0);

Copilot uses AI. Check for mistakes.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is intentionally separated so that events are consistent across actions.

const jobInfo = await marketv1.jobs(INITIAL_JOB_INDEX);
expect(jobInfo.metadata).to.equal("some metadata");
expect(jobInfo.owner).to.equal(await user.getAddress());
expect(jobInfo.provider).to.equal(await provider.getAddress());
expect(jobInfo.rate).to.equal(JOB_RATE_1);
expect(jobInfo.balance).to.equal(initialBalance.sub(noticePeriodCost));
expect(jobInfo.lastSettled).to.be.within(INITIAL_TIMESTAMP + FIVE_MINUTES, INITIAL_TIMESTAMP + FIVE_MINUTES + 1);
expect(jobInfo.lastSettled).to.equal(jobOpenTs);
Copy link
Preview

Copilot AI May 22, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using strict equality for block timestamps might lead to brittle tests; consider using a tolerance window (e.g. to.be.closeTo) to account for minor timing variations.

Suggested change
expect(jobInfo.lastSettled).to.equal(jobOpenTs);
expect(jobInfo.lastSettled).to.be.closeTo(jobOpenTs, 1);

Copilot uses AI. Check for mistakes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants