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

test: more tests #67

Merged
merged 1 commit into from
Dec 27, 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
3 changes: 2 additions & 1 deletion contracts/BalanceTrackerFixedPriceBase.sol
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,9 @@
uint256 public constant MAX_FEE_FACTOR = 10_000;

// Mech marketplace address
address public immutable mechMarketplace;

Check warning on line 59 in contracts/BalanceTrackerFixedPriceBase.sol

View workflow job for this annotation

GitHub Actions / build

Immutable variables name are set to be in capitalized SNAKE_CASE
// Buy back burner address
address public immutable buyBackBurner;

Check warning on line 61 in contracts/BalanceTrackerFixedPriceBase.sol

View workflow job for this annotation

GitHub Actions / build

Immutable variables name are set to be in capitalized SNAKE_CASE
// Collected fees
uint256 public collectedFees;
// Reentrancy lock
Expand Down Expand Up @@ -224,7 +224,8 @@

// Get mech balance
uint256 balance = mapMechBalances[mech];
if (balance == 0) {
// If balance is 1, the marketplace fee is still 1, and thus mech payment will be zero
if (balance < 2) {
Comment on lines +227 to +228
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

A small logical refactor here

revert ZeroValue();
}

Expand Down
4 changes: 2 additions & 2 deletions contracts/OlasMech.sol
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ abstract contract OlasMech is Mech, IErrorsMech, ImmutableStorage {
function requestFromMarketplace(address requester, bytes memory data, uint256 requestId) external {
// Check for marketplace access
if (msg.sender != mechMarketplace) {
revert MarketplaceNotAuthorized(msg.sender);
revert MarketplaceOnly(msg.sender, mechMarketplace);
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Just to be consistent with the rest

}

// Perform a request
Expand All @@ -236,7 +236,7 @@ abstract contract OlasMech is Mech, IErrorsMech, ImmutableStorage {
function revokeRequest(uint256 requestId) external {
// Check for marketplace access
if (msg.sender != mechMarketplace) {
revert MarketplaceNotAuthorized(msg.sender);
revert MarketplaceOnly(msg.sender, mechMarketplace);
}

address requester = mapRequestAddresses[requestId];
Expand Down
7 changes: 4 additions & 3 deletions contracts/interfaces/IErrorsMech.sol
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@ interface IErrorsMech {
/// @dev The contract is already initialized.
error AlreadyInitialized();

/// @dev Mech marketplace is not authorized.
/// @param mechMarketplace Mech marketplace address.
error MarketplaceNotAuthorized(address mechMarketplace);
/// @dev Only `marketplace` has a privilege, but the `sender` was provided.
/// @param sender Sender address.
/// @param marketplace Required marketplace address.
error MarketplaceOnly(address sender, address marketplace);

/// @dev Request Id not found.
/// @param requestId Request Id.
Expand Down
23 changes: 22 additions & 1 deletion test/MechFixedPriceNative.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ describe("MechFixedPriceNative", function () {
// Try to post a request directly to the mech
await expect(
priorityMech.requestFromMarketplace(deployer.address, data, 0)
).to.be.revertedWithCustomError(priorityMech, "MarketplaceNotAuthorized");
).to.be.revertedWithCustomError(priorityMech, "MarketplaceOnly");

// Response time is out of bounds
await expect(
Expand Down Expand Up @@ -224,6 +224,11 @@ describe("MechFixedPriceNative", function () {
priorityMech.deliverToMarketplace(requestId, data)
).to.be.revertedWithCustomError(priorityMech, "RequestIdNotFound");

// Try to check and record delivery rate not by marketplace
await expect(
balanceTrackerFixedPriceNative.checkAndRecordDeliveryRate(deployer.address, 0, "0x")
).to.be.revertedWithCustomError(balanceTrackerFixedPriceNative, "MarketplaceOnly");

// Create a request
await mechMarketplace.request(data, mechServiceId, requesterServiceId, minResponseTimeout, "0x", {value: maxDeliveryRate});

Expand All @@ -232,6 +237,11 @@ describe("MechFixedPriceNative", function () {
priorityMech.connect(signers[1]).deliverToMarketplace(requestId, data)
).to.be.reverted;

// Try to finalize delivery rate not by marketplace
await expect(
balanceTrackerFixedPriceNative.finalizeDeliveryRate(priorityMech.address, deployer.address, 0, 0)
).to.be.revertedWithCustomError(balanceTrackerFixedPriceNative, "MarketplaceOnly");

// Get the request status (requested priority)
status = await mechMarketplace.getRequestStatus(requestId);
expect(status).to.equal(1);
Expand Down Expand Up @@ -302,6 +312,17 @@ describe("MechFixedPriceNative", function () {
let mechBalance = await balanceTrackerFixedPriceNative.mapMechBalances(priorityMech.address);
expect(mechBalance).to.equal(maxDeliveryRate);

// Try to collect fees before any payment processing
await expect(
balanceTrackerFixedPriceNative.drain()
).to.be.revertedWithCustomError(balanceTrackerFixedPriceNative, "ZeroValue");

// Try to process payment for mech not by its service multisig
await expect(
balanceTrackerFixedPriceNative.connect(signers[1]).processPaymentByMultisig(priorityMech.address)
).to.be.revertedWithCustomError(balanceTrackerFixedPriceNative, "UnauthorizedAccount");


const balanceBefore = await ethers.provider.getBalance(priorityMech.address);
// Process payment for mech
await balanceTrackerFixedPriceNative.processPaymentByMultisig(priorityMech.address);
Expand Down
9 changes: 8 additions & 1 deletion test/MechMarketplace.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ describe("MechMarketplace", function () {
const mechMarketplaceProxy = await MechMarketplaceProxy.deploy(mechMarketplace.address, proxyData);
await mechMarketplaceProxy.deployed();

// Get implementation
const implementation = await mechMarketplaceProxy.getImplementation();
expect(implementation).to.equal(mechMarketplace.address);

mechMarketplace = await ethers.getContractAt("MechMarketplace", mechMarketplaceProxy.address);

// Deploy mech factory
Expand Down Expand Up @@ -148,7 +152,7 @@ describe("MechMarketplace", function () {
).to.be.revertedWithCustomError(mechMarketplace, "ZeroAddress");

// Changing the implementation
await mechMarketplace.connect(deployer).changeOwner(mechMarketplace.address);
await mechMarketplace.connect(deployer).changeImplementation(mechMarketplace.address);
});

it("Change marketplace params", async function () {
Expand Down Expand Up @@ -183,6 +187,9 @@ describe("MechMarketplace", function () {
await expect(
mechMarketplace.changeMarketplaceParams(10, 10, maxUint96)
).to.be.revertedWithCustomError(mechMarketplace, "Overflow");

// Change params
await mechMarketplace.changeMarketplaceParams(fee, minResponseTimeout, maxResponseTimeout);
});

it("Factories and balance trackers", async function () {
Expand Down
Loading