Skip to content

Commit

Permalink
Merge branch 'master' into amie-update-fireblocks-signer
Browse files Browse the repository at this point in the history
  • Loading branch information
amiecorso authored Sep 29, 2023
2 parents 967417d + d9a135d commit 1378d7a
Show file tree
Hide file tree
Showing 6 changed files with 168 additions and 67 deletions.
113 changes: 59 additions & 54 deletions .gas-snapshot
Original file line number Diff line number Diff line change
Expand Up @@ -20,34 +20,34 @@ Certificate_supportsInterface:test() (gas: 5159)
Certificate_transferFrom:test() (gas: 48618)
Certificate_transferFrom:test_reverts_when_paused() (gas: 38088)
Certificate_transferFrom_reverts_ForbiddenTransferAfterMinting:test() (gas: 18960)
Checkout_buyingFromOneRemoval:test() (gas: 651416)
Checkout_buyingFromOneRemoval_byApproval:test() (gas: 587321)
Checkout_buyingFromTenRemovals:test() (gas: 1976657)
Checkout_buyingFromTenRemovals_singleSupplier:test() (gas: 1714379)
Checkout_buyingFromTenRemovals_singleSupplier_withoutFee:test() (gas: 1709579)
Checkout_buyingFromTenRemovals_withoutFee:test() (gas: 1722284)
Checkout_buyingFromTenSuppliers:test() (gas: 2854944)
Checkout_buyingWithAlternateERC20:test() (gas: 545362)
Checkout_buyingWithAlternateERC20_floatingPointPriceMultiple:test() (gas: 511416)
Checkout_swapWithDifferentPermitSignerAndMsgSender:test() (gas: 653410)
Checkout_swapWithoutFeeSpecialOrder:test() (gas: 569751)
Checkout_swapWithoutFeeSpecialOrder_specificSupplier:test() (gas: 564450)
Checkout_swapWithoutFeeSpecialOrder_specificSupplier:test_revertsWhenSupplierDoesNotExistInMarket() (gas: 57664)
Checkout_swapWithoutFeeSpecialOrder_specificVintages:test_basicFulfillment() (gas: 966448)
Checkout_swapWithoutFeeSpecialOrder_specificVintages:test_revertsWhenNoRemovalsFromSpecifiedVintages() (gas: 90933)
Checkout_swapWithoutFeeSpecialOrder_specificVintagesSpecificSupplier:test_basicFulfillment() (gas: 702596)
Checkout_buyingFromOneRemoval:test() (gas: 650755)
Checkout_buyingFromOneRemoval_byApproval:test() (gas: 586647)
Checkout_buyingFromTenRemovals:test() (gas: 1974107)
Checkout_buyingFromTenRemovals_singleSupplier:test() (gas: 1711817)
Checkout_buyingFromTenRemovals_singleSupplier_withoutFee:test() (gas: 1707017)
Checkout_buyingFromTenRemovals_withoutFee:test() (gas: 1719723)
Checkout_buyingFromTenSuppliers:test() (gas: 2852393)
Checkout_buyingWithAlternateERC20:test() (gas: 544701)
Checkout_buyingWithAlternateERC20_floatingPointPriceMultiple:test() (gas: 510755)
Checkout_swapWithDifferentPermitSignerAndMsgSender:test() (gas: 652749)
Checkout_swapWithoutFeeSpecialOrder:test() (gas: 569077)
Checkout_swapWithoutFeeSpecialOrder_specificSupplier:test() (gas: 563776)
Checkout_swapWithoutFeeSpecialOrder_specificSupplier:test_revertsWhenSupplierDoesNotExistInMarket() (gas: 57679)
Checkout_swapWithoutFeeSpecialOrder_specificVintages:test_basicFulfillment() (gas: 965356)
Checkout_swapWithoutFeeSpecialOrder_specificVintages:test_revertsWhenNoRemovalsFromSpecifiedVintages() (gas: 90946)
Checkout_swapWithoutFeeSpecialOrder_specificVintagesSpecificSupplier:test_basicFulfillment() (gas: 701713)
LockedNORILib_availableAmount:test() (gas: 12371)
LockedNORITest:testBatchGrantCreation() (gas: 705179)
LockedNORITest:testNormalWithdrawal() (gas: 1102743)
LockedNORITest:testReentryTokensReceived() (gas: 1102887)
LockedNORITest:testReentryTokensToSend() (gas: 1104432)
LockedNORITest:testTokensReceivedReverts() (gas: 69026)
MarketInvariantTest:invariant_callSummary() (runs: 400, calls: 6000, reverts: 4297)
MarketInvariantTest:invariant_sumOfPurchaseAmounts() (runs: 400, calls: 6000, reverts: 4257)
MarketSupplierSelectionNotUsingUpSuppliersLastRemoval:test() (gas: 922136)
MarketInvariantTest:invariant_callSummary() (runs: 400, calls: 6000, reverts: 4258)
MarketInvariantTest:invariant_sumOfPurchaseAmounts() (runs: 400, calls: 6000, reverts: 4265)
MarketSupplierSelectionNotUsingUpSuppliersLastRemoval:test() (gas: 921266)
Market_ALLOWLIST_ROLE:test() (gas: 12821)
Market_USDC_swap_respects_decimal_mismatch:test() (gas: 716679)
Market_USDC_swap_withholds_restricted_nori:test() (gas: 891754)
Market_USDC_swap_respects_decimal_mismatch:test() (gas: 716031)
Market_USDC_swap_withholds_restricted_nori:test() (gas: 891106)
Market__addActiveRemoval:test() (gas: 183344)
Market__addActiveRemoval:test__lis2VintagesFor1SupplierFor2SubIdentifiers() (gas: 242879)
Market__addActiveRemoval:test__list1VintageFor1Supplier() (gas: 188331)
Expand Down Expand Up @@ -84,21 +84,21 @@ Market_onERC1155BatchReceived_reverts_SenderNotRemovalContract:test() (gas: 3552
Market_onERC1155Received:test() (gas: 206036)
Market_onERC1155Received_reverts_SenderNotRemovalContract:test() (gas: 159000)
Market_purchasingTokenAddress:test() (gas: 17080)
Market_replace:test() (gas: 345632)
Market_replace:test() (gas: 344957)
Market_replace_reverts_CertificateNotYetMinted:test() (gas: 49494)
Market_replace_reverts_ReplacementAmountExceedsNrtDeficit:test() (gas: 52525)
Market_replace_reverts_ReplacementAmountMismatch:test() (gas: 86960)
Market_replace_reverts_ReplacementAmountMismatch:test() (gas: 86288)
Market_setNoriFeePercentage_revertsInvalidPercentage:test() (gas: 20298)
Market_setPriorityRestrictedThreshold:test() (gas: 157447)
Market_setPriorityRestrictedThreshold:test_zeroAvailable() (gas: 152422)
Market_setPurchasingTokenAndPriceMultiple:test() (gas: 1026654)
Market_setPurchasingTokenAndPriceMultiple_revertsIfNotAdmin:test() (gas: 50857)
Market_supplierSelectionUsingUpSuppliersLastRemoval:test() (gas: 918844)
Market_swapWithoutFeeSpecialOrder_emits_and_skips_transfer_when_transferring_wrong_erc20_to_rNori:test() (gas: 421711)
Market_swap_emits_and_skips_transfer_when_transferring_wrong_erc20_to_rNori:test() (gas: 520160)
Market_swap_emits_event_and_skips_mint_when_minting_rNori_to_nonERC1155Receiver:test() (gas: 597382)
Market_swap_revertsWhenUnsafeERC20TransferFails:test() (gas: 217232)
Market_validates_certificate_amount:test() (gas: 595657)
Market_supplierSelectionUsingUpSuppliersLastRemoval:test() (gas: 917974)
Market_swapWithoutFeeSpecialOrder_emits_and_skips_transfer_when_transferring_wrong_erc20_to_rNori:test() (gas: 421037)
Market_swap_emits_and_skips_transfer_when_transferring_wrong_erc20_to_rNori:test() (gas: 519499)
Market_swap_emits_event_and_skips_mint_when_minting_rNori_to_nonERC1155Receiver:test() (gas: 596721)
Market_swap_revertsWhenUnsafeERC20TransferFails:test() (gas: 216571)
Market_validates_certificate_amount:test() (gas: 595965)
Market_withdraw_1x3_center:test() (gas: 340924)
Market_withdraw_2x1_back:test() (gas: 345584)
Market_withdraw_2x1_front:test() (gas: 333941)
Expand All @@ -121,23 +121,23 @@ Removal__createRemovalData:test() (gas: 22593)
Removal__createRemovalData:test_reverts_InvalidData() (gas: 25711)
Removal__createRemovalDataBatch:test() (gas: 29572)
Removal__createRemovalDataBatch:test_reverts_InvalidData2() (gas: 36714)
Removal__isValidTransferAmount:testFuzz_ReturnFalse_NonMultiplesOf1e14(uint256) (runs: 256, μ: 13891, ~: 13847)
Removal__isValidTransferAmount:testFuzz_ReturnTrue_MultiplesOf1e14(uint256) (runs: 256, μ: 14380, ~: 14497)
Removal__isValidTransferAmount:testFuzz_ReturnTrue_SmallestGranularity() (gas: 6832)
Removal__isValidTransferAmount:test_ReturnFalse_AmountIsTooGranular() (gas: 6898)
Removal__isValidTransferAmount:test_ReturnFalse_AmountIsTooGranularAndToIsTheCertificate() (gas: 4745)
Removal__isValidTransferAmount:test_ReturnFalse_AmountIsTooGranularAndToIsTheMarket() (gas: 2608)
Removal__isValidTransferAmount:test_ReturnFalse_AmountIsZeroAndToIsTheCertificate() (gas: 4705)
Removal__isValidTransferAmount:test_ReturnFalse_AmountIsZeroAndToIsTheMarket() (gas: 2521)
Removal__isValidTransferAmount:test_ReturnTrue_AmountIsZeroAndToIsNeitherTheMarketNorCertificate() (gas: 6852)
Removal__isValidTransfer:testFuzz_ReturnFalse_NonMultiplesOf1e14(uint256) (runs: 256, μ: 13892, ~: 13847)
Removal__isValidTransfer:testFuzz_ReturnTrue_MultiplesOf1e14(uint256) (runs: 256, μ: 14383, ~: 14497)
Removal__isValidTransfer:testFuzz_ReturnTrue_SmallestGranularity() (gas: 6832)
Removal__isValidTransfer:test_ReturnFalse_AmountIsTooGranular() (gas: 6898)
Removal__isValidTransfer:test_ReturnFalse_AmountIsTooGranularAndToIsTheCertificate() (gas: 4745)
Removal__isValidTransfer:test_ReturnFalse_AmountIsTooGranularAndToIsTheMarket() (gas: 2608)
Removal__isValidTransfer:test_ReturnFalse_AmountIsZeroAndToIsTheCertificate() (gas: 4705)
Removal__isValidTransfer:test_ReturnFalse_AmountIsZeroAndToIsTheMarket() (gas: 2521)
Removal__isValidTransfer:test_ReturnTrue_AmountIsZeroAndToIsNeitherTheMarketNorCertificate() (gas: 6852)
Removal__validateRemoval:test() (gas: 2491)
Removal__validateRemoval:test_reverts_InvalidData() (gas: 5373)
Removal_addBalance:test() (gas: 60280)
Removal_addBalance_reverts_RemovalNotYetMinted:test() (gas: 31115)
Removal_batchGetHoldbackPercentages_multipleIds:test() (gas: 11098)
Removal_batchGetHoldbackPercentages_singleId:test() (gas: 10346)
Removal_consign_revertsForSoldRemovals:test() (gas: 1091252)
Removal_getMarketBalance:test() (gas: 1103785)
Removal_consign_revertsForSoldRemovals:test() (gas: 1090733)
Removal_getMarketBalance:test() (gas: 1103137)
Removal_getOwnedTokenIds:test_multiple_tokens_with_transfer() (gas: 1078578)
Removal_getOwnedTokenIds:test_no_tokens() (gas: 18683)
Removal_getProjectId:test() (gas: 19307)
Expand Down Expand Up @@ -188,18 +188,23 @@ RestrictedNORI_scheduleExists:test() (gas: 15177)
RestrictedNORI_scheduleExists:test_doesntExist() (gas: 15156)
RestrictedNORI_transfers_revert:testSafeBatchTransferFromReverts() (gas: 29158)
RestrictedNORI_transfers_revert:testSafeTransferFromReverts() (gas: 23292)
UInt256ArrayLib_fill:test() (gas: 120280)
UInt256ArrayLib_fill:test_gas() (gas: 101624)
UInt256ArrayLib_fill:test_library() (gas: 11843)
UInt256ArrayLib_fill:test_reference() (gas: 12347)
UInt256ArrayLib_slice:test() (gas: 105299)
UInt256ArrayLib_slice:test_gas() (gas: 95750)
UInt256ArrayLib_slice:test_library() (gas: 59625)
UInt256ArrayLib_slice:test_reference() (gas: 69247)
UInt256ArrayLib_fill:test() (gas: 120236)
UInt256ArrayLib_fill:test_gas() (gas: 101580)
UInt256ArrayLib_fill:test_library() (gas: 11821)
UInt256ArrayLib_fill:test_reference() (gas: 12325)
UInt256ArrayLib_shrink:test() (gas: 99746)
UInt256ArrayLib_shrink:test_gas() (gas: 90191)
UInt256ArrayLib_shrink:test_library() (gas: 54108)
UInt256ArrayLib_shrink:test_reference() (gas: 69278)
UInt256ArrayLib_shrink:test_revertsWhenArgumentsAreInvalid() (gas: 3295)
UInt256ArrayLib_slice:test() (gas: 105300)
UInt256ArrayLib_slice:test_gas() (gas: 95751)
UInt256ArrayLib_slice:test_library() (gas: 59581)
UInt256ArrayLib_slice:test_reference() (gas: 69292)
UInt256ArrayLib_slice:test_revertsWhenArgumentsAreInvalid() (gas: 5677)
UInt256ArrayLib_slice:test_shouldNotChangeOriginalArrayValuesOrSize() (gas: 80733)
UInt256ArrayLib_sum:test() (gas: 91109)
UInt256ArrayLib_sum:test_gas() (gas: 91271)
UInt256ArrayLib_sum:test_library() (gas: 40505)
UInt256ArrayLib_sum:test_library_overflow() (gas: 9495)
UInt256ArrayLib_sum:test_reference() (gas: 55490)
UInt256ArrayLib_slice:test_shouldNotChangeOriginalArrayValuesOrSize() (gas: 80293)
UInt256ArrayLib_sum:test() (gas: 91155)
UInt256ArrayLib_sum:test_gas() (gas: 91317)
UInt256ArrayLib_sum:test_library() (gas: 40528)
UInt256ArrayLib_sum:test_library_overflow() (gas: 9518)
UInt256ArrayLib_sum:test_reference() (gas: 55513)
28 changes: 28 additions & 0 deletions contracts/ArrayLib.sol
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,34 @@ library UInt256ArrayLib {
}
}
}

/**
* @notice Shorten an array to specified length.
* @dev Shortens the specified array to the specified length by directly overwriting
* the length of the original array in storage.
*
* ##### Example usage:
*
* ```solidity
* new uint256[](100).fill(1).shrink(50); // resizes the original array to length 50
* ```
* -
* @param values The array to shorten.
* @param length The desired length of the array.
* @return values The shortened array.
*/
function shrink(
uint256[] memory values,
uint256 length
) internal pure returns (uint256[] memory) {
assert(length <= values.length);
uint256 location;
assembly {
location := values
mstore(location, length)
}
return values;
}
}

/**
Expand Down
20 changes: 8 additions & 12 deletions contracts/Market.sol
Original file line number Diff line number Diff line change
Expand Up @@ -448,13 +448,11 @@ contract Market is
amount: totalAmountToReplace
});
uint256 countOfRemovalsAllocated = allocationData.countOfRemovalsAllocated;
uint256[] memory removalIds = allocationData.ids.slice({
from: 0,
to: countOfRemovalsAllocated
uint256[] memory removalIds = allocationData.ids.shrink({
length: countOfRemovalsAllocated
});
uint256[] memory removalAmounts = allocationData.amounts.slice({
from: 0,
to: countOfRemovalsAllocated
uint256[] memory removalAmounts = allocationData.amounts.shrink({
length: countOfRemovalsAllocated
});
_validateReplacementAmounts({
totalAmountToReplace: totalAmountToReplace,
Expand Down Expand Up @@ -1228,13 +1226,11 @@ contract Market is
* @param orderData The order fulfillment data.
*/
function _fulfillOrder(FulfillOrderData memory orderData) internal {
uint256[] memory removalIds = orderData.allocationData.ids.slice({
from: 0,
to: orderData.allocationData.countOfRemovalsAllocated
uint256[] memory removalIds = orderData.allocationData.ids.shrink({
length: orderData.allocationData.countOfRemovalsAllocated
});
uint256[] memory removalAmounts = orderData.allocationData.amounts.slice({
from: 0,
to: orderData.allocationData.countOfRemovalsAllocated
uint256[] memory removalAmounts = orderData.allocationData.amounts.shrink({
length: orderData.allocationData.countOfRemovalsAllocated
});
_transferFunds({
chargeFee: orderData.chargeFee,
Expand Down
2 changes: 1 addition & 1 deletion slither.db.json

Large diffs are not rendered by default.

57 changes: 57 additions & 0 deletions test/ArrayLib.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,63 @@ contract UInt256ArrayLib_slice is Global {
}
}

contract UInt256ArrayLib_shrink is Global {
using UInt256ArrayLib for uint256[];

UInt256ArrayLibHarness private _harness;

function setUp() external {
_harness = new UInt256ArrayLibHarness();
}

function test() external {
assertEq(
_harness.shrinkUsingStandardImplementation({
values: new uint256[](100).fill(1),
length: 50
}),
_harness.shrink({values: new uint256[](100).fill(1), length: 50})
);
}

function test_gas() external {
uint256 gasLeft = gasleft();
_harness.shrinkUsingStandardImplementation({
values: new uint256[](100).fill(1),
length: 50
});
uint256 standardGasUsed = gasLeft - gasleft();
gasLeft = gasleft();
_harness.shrink({values: new uint256[](100).fill(1), length: 50});
uint256 libraryGasUsed = gasLeft - gasleft();
assertLt({a: libraryGasUsed, b: standardGasUsed});
}

/** @dev Calling shrink() with invalid params (e.g., length longer than original array) should revert */
function test_revertsWhenArgumentsAreInvalid() external {
uint256[] memory values = new uint256[](10);
vm.expectRevert();
values.shrink({length: 11});
}

function test_reference() external {
assertEq(
_harness.shrinkUsingStandardImplementation({
values: new uint256[](100).fill(1),
length: 50
}),
new uint256[](50).fill(1)
);
}

function test_library() external {
assertEq(
_harness.shrink({values: new uint256[](100).fill(1), length: 50}),
new uint256[](50).fill(1)
);
}
}

contract UInt256ArrayLib_fill is Global {
using UInt256ArrayLib for uint256[];

Expand Down
15 changes: 15 additions & 0 deletions test/helpers/array-lib.sol
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,21 @@ contract UInt256ArrayLibHarness {
return UInt256ArrayLib.slice({values: values, from: from, to: to});
}

function shrinkUsingStandardImplementation(
uint256[] memory values,
uint256 length
) external pure returns (uint256[] memory cut) {
cut = new uint256[](length);
for (uint256 i = 0; i < length; ++i) cut[i] = values[0 + i];
}

function shrink(
uint256[] memory values,
uint256 length
) external pure returns (uint256[] memory) {
return UInt256ArrayLib.shrink({values: values, length: length});
}

function sumUsingStandardImplementation(
uint256[] memory values
) external pure returns (uint256 total) {
Expand Down

0 comments on commit 1378d7a

Please sign in to comment.