Skip to content

Commit

Permalink
fix: more bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
kupermind committed Dec 26, 2024
1 parent 0a3e4b7 commit cb283c8
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 23 deletions.
43 changes: 33 additions & 10 deletions contracts/BalanceTrackerFixedPriceBase.sol
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ abstract contract BalanceTrackerFixedPriceBase {
event Withdraw(address indexed account, address indexed token, uint256 amount);
event Drained(address indexed token, uint256 collectedFees);

// Max marketplace fee
uint256 public constant MAX_FEE = 10_000;
// Max marketplace fee factor (100%)
uint256 public constant MAX_FEE_FACTOR = 10_000;

// Mech marketplace address
address public immutable mechMarketplace;
Expand Down Expand Up @@ -189,18 +189,41 @@ abstract contract BalanceTrackerFixedPriceBase {
_locked = 1;
}

function _withdraw(uint256 balance) internal virtual;
function _withdraw(address mech, uint256 balance) internal virtual;

/// @dev Processes mech payment by withdrawing funds.
function processPayment() external returns (uint256 mechPayment, uint256 marketplaceFee) {
/// @dev Processes mech payment by mech service multisig.
/// @param mech Mech address.
/// @return Mech payment.
/// @return Marketplace fee.
function processPaymentByMultisig(address mech) external returns (uint256, uint256) {
// Check for mech service multisig address
if (!IMech(mech).isOperator(msg.sender)) {
revert UnauthorizedAccount(msg.sender);
}

return _processPayment(mech);
}

/// @dev Processes mech payment.
/// @return Mech payment.
/// @return Marketplace fee.
function processPayment() external returns (uint256, uint256) {
return _processPayment(msg.sender);
}

/// @dev Process mech payment.
/// @param mech Mech address.
/// @return mechPayment Mech payment.
/// @return marketplaceFee Marketplace fee.
function _processPayment(address mech) internal returns (uint256 mechPayment, uint256 marketplaceFee) {
// Reentrancy guard
if (_locked > 1) {
revert ReentrancyGuard();
}
_locked = 2;

// Get mech balance
uint256 balance = mapMechBalances[msg.sender];
uint256 balance = mapMechBalances[mech];
if (balance == 0) {
revert ZeroValue();
}
Expand All @@ -211,7 +234,7 @@ abstract contract BalanceTrackerFixedPriceBase {
// If requested balance is too small, charge the minimal fee
// ceil(a, b) = (a + b - 1) / b
// This formula will always get at least a fee of 1
marketplaceFee = (balance * fee + (MAX_FEE - 1)) / MAX_FEE;
marketplaceFee = (balance * fee + (MAX_FEE_FACTOR - 1)) / MAX_FEE_FACTOR;

// Calculate mech payment
mechPayment = balance - marketplaceFee;
Expand All @@ -225,10 +248,10 @@ abstract contract BalanceTrackerFixedPriceBase {
collectedFees += marketplaceFee;

// Clear balances
mapMechBalances[msg.sender] = 0;
mapMechBalances[mech] = 0;

// Process withdraw
_withdraw(balance);
_withdraw(mech, mechPayment);

_locked = 1;
}
Expand All @@ -251,7 +274,7 @@ abstract contract BalanceTrackerFixedPriceBase {
mapRequesterBalances[msg.sender] = 0;

// Process withdraw
_withdraw(balance);
_withdraw(msg.sender, balance);

_locked = 1;
}
Expand Down
8 changes: 4 additions & 4 deletions contracts/BalanceTrackerFixedPriceNative.sol
Original file line number Diff line number Diff line change
Expand Up @@ -61,16 +61,16 @@ contract BalanceTrackerFixedPriceNative is BalanceTrackerFixedPriceBase {
emit Drained(wrappedNativeToken, amount);
}

function _withdraw(uint256 balance) internal virtual override {
function _withdraw(address account, uint256 amount) internal virtual override {
// solhint-disable-next-line avoid-low-level-calls
(bool success, ) = msg.sender.call{value: balance}("");
(bool success, ) = account.call{value: amount}("");

// Check transfer
if (!success) {
revert TransferFailed(address(0), address(this), msg.sender, balance);
revert TransferFailed(address(0), address(this), account, amount);
}

emit Withdraw(msg.sender, address(0), balance);
emit Withdraw(msg.sender, address(0), amount);
}

// Deposits funds for requester.
Expand Down
8 changes: 4 additions & 4 deletions contracts/BalanceTrackerFixedPriceToken.sol
Original file line number Diff line number Diff line change
Expand Up @@ -76,15 +76,15 @@ contract BalanceTrackerFixedPriceToken is BalanceTrackerFixedPriceBase {
emit Drained(olas, amount);
}

function _withdraw(uint256 balance) internal virtual override {
bool success = IToken(olas).transfer(msg.sender, balance);
function _withdraw(address account, uint256 amount) internal virtual override {
bool success = IToken(olas).transfer(account, amount);

// Check transfer
if (!success) {
revert TransferFailed(olas, address(this), msg.sender, balance);
revert TransferFailed(olas, address(this), account, amount);
}

emit Withdraw(msg.sender, olas, balance);
emit Withdraw(msg.sender, olas, amount);
}

// Deposits token funds for requester.
Expand Down
8 changes: 4 additions & 4 deletions contracts/MechMarketplace.sol
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ contract MechMarketplace is IErrorsMarketplace {
// Domain separator type hash
bytes32 public constant DOMAIN_SEPARATOR_TYPE_HASH =
keccak256("EIP712Domain(string name,string version,uint256 chainId,address verifyingContract)");
// Max marketplace fee
uint256 public constant MAX_FEE = 10_000;
// Max marketplace fee factor (100%)
uint256 public constant MAX_FEE_FACTOR = 10_000;

// Original domain separator value
bytes32 public immutable domainSeparator;
Expand Down Expand Up @@ -160,8 +160,8 @@ contract MechMarketplace is IErrorsMarketplace {
}

// Check for fee value
if (newFee > MAX_FEE) {
revert Overflow(newFee, MAX_FEE);
if (newFee > MAX_FEE_FACTOR) {
revert Overflow(newFee, MAX_FEE_FACTOR);
}

// Check for sanity values
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,8 @@ contract BalanceTrackerNvmSubscription {
_locked = 1;
}

/// @dev Processes payment.
/// @dev Processes requester credits.
/// @param requester Requester address.
function processPayment(address requester) external {
// Reentrancy guard
if (_locked > 1) {
Expand Down
27 changes: 27 additions & 0 deletions test/MechFixedPriceNative.js
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,33 @@ describe("MechFixedPriceNative", function () {
// Check requester mech karma
mechKarma = await karma.mapRequesterMechKarma(deployer.address, priorityMech.address);
expect(mechKarma).to.equal(1);

// Check priority mech balance now
let mechBalance = await balanceTrackerFixedPriceNative.mapMechBalances(priorityMech.address);
expect(mechBalance).to.equal(maxDeliveryRate);

const balanceBefore = await ethers.provider.getBalance(priorityMech.address);
// Process payment for mech
await balanceTrackerFixedPriceNative.processPaymentByMultisig(priorityMech.address);
const balanceAfter = await ethers.provider.getBalance(priorityMech.address);

// Check charged fee
const collectedFees = await balanceTrackerFixedPriceNative.collectedFees();
// Since the delivery rate is smaller than MAX_FEE_FACTOR, the minimal fee was charged
expect(collectedFees).to.equal(1);

// Check mech payout: payment - fee
const balanceDiff = balanceAfter.sub(balanceBefore);
expect(balanceDiff).to.equal(maxDeliveryRate - 1);

// Check requester leftover balance
let requesterBalance = await balanceTrackerFixedPriceNative.mapRequesterBalances(deployer.address);
expect(requesterBalance).to.equal(maxDeliveryRate - 1);

// Withdraw requester balances
await balanceTrackerFixedPriceNative.withdraw();
requesterBalance = await balanceTrackerFixedPriceNative.mapRequesterBalances(deployer.address);
expect(requesterBalance).to.equal(0);
});

it("Delivering a request by a different mech", async function () {
Expand Down

0 comments on commit cb283c8

Please sign in to comment.