Skip to content

Commit

Permalink
refactor: remove broker (#1166)
Browse files Browse the repository at this point in the history
* refactor: remove broker

* refactor: update benchmarks

* docs: correct spelling

test: deploy contracts in fork tests

* test: polishes

* test: small changes

* test: increase coverage

---------

Co-authored-by: smol-ninja <shubhamy2015@gmail.com>
  • Loading branch information
andreivladbrg and smol-ninja authored Feb 6, 2025
1 parent baf9a9e commit 7725fde
Show file tree
Hide file tree
Showing 63 changed files with 587 additions and 1,271 deletions.
20 changes: 10 additions & 10 deletions benchmark/BatchLockup.Gas.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ contract BatchLockup_Gas_Test is Benchmark_Test {
//////////////////////////////////////////////////////////////////////////*/

function gasCreateWithDurationsLD(uint256 batchSize, uint256 segmentsCount) internal {
Lockup.CreateWithDurations memory createParams = defaults.createWithDurationsBrokerNull();
createParams.totalAmount = uint128(AMOUNT_PER_ITEM * segmentsCount);
Lockup.CreateWithDurations memory createParams = defaults.createWithDurations();
createParams.depositAmount = uint128(AMOUNT_PER_ITEM * segmentsCount);
LockupDynamic.SegmentWithDuration[] memory segments = _generateSegmentsWithDuration(segmentsCount);
BatchLockup.CreateWithDurationsLD[] memory params =
BatchLockupBuilder.fillBatch(createParams, segments, batchSize);
Expand All @@ -83,11 +83,11 @@ contract BatchLockup_Gas_Test is Benchmark_Test {
}

function gasCreateWithTimestampsLD(uint256 batchSize, uint256 segmentsCount) internal {
Lockup.CreateWithTimestamps memory createParams = defaults.createWithTimestampsBrokerNull();
Lockup.CreateWithTimestamps memory createParams = defaults.createWithTimestamps();
LockupDynamic.Segment[] memory segments = _generateSegments(segmentsCount);
createParams.timestamps.start = getBlockTimestamp();
createParams.timestamps.end = segments[segments.length - 1].timestamp;
createParams.totalAmount = uint128(AMOUNT_PER_ITEM * segmentsCount);
createParams.depositAmount = uint128(AMOUNT_PER_ITEM * segmentsCount);
BatchLockup.CreateWithTimestampsLD[] memory params =
BatchLockupBuilder.fillBatch(createParams, segments, batchSize);

Expand All @@ -111,7 +111,7 @@ contract BatchLockup_Gas_Test is Benchmark_Test {

function gasCreateWithDurationsLL(uint256 batchSize) internal {
BatchLockup.CreateWithDurationsLL[] memory params = BatchLockupBuilder.fillBatch({
params: defaults.createWithDurationsBrokerNull(),
params: defaults.createWithDurations(),
unlockAmounts: defaults.unlockAmounts(),
durations: defaults.durations(),
batchSize: batchSize
Expand All @@ -131,7 +131,7 @@ contract BatchLockup_Gas_Test is Benchmark_Test {

function gasCreateWithTimestampsLL(uint256 batchSize) internal {
BatchLockup.CreateWithTimestampsLL[] memory params = BatchLockupBuilder.fillBatch({
params: defaults.createWithTimestampsBrokerNull(),
params: defaults.createWithTimestamps(),
unlockAmounts: defaults.unlockAmounts(),
cliffTime: defaults.CLIFF_TIME(),
batchSize: batchSize
Expand All @@ -150,9 +150,9 @@ contract BatchLockup_Gas_Test is Benchmark_Test {
}

function gasCreateWithDurationsLT(uint256 batchSize, uint256 tranchesCount) internal {
Lockup.CreateWithDurations memory createParams = defaults.createWithDurationsBrokerNull();
Lockup.CreateWithDurations memory createParams = defaults.createWithDurations();
LockupTranched.TrancheWithDuration[] memory tranches = _generateTranchesWithDuration(tranchesCount);
createParams.totalAmount = uint128(AMOUNT_PER_ITEM * tranchesCount);
createParams.depositAmount = uint128(AMOUNT_PER_ITEM * tranchesCount);
BatchLockup.CreateWithDurationsLT[] memory params =
BatchLockupBuilder.fillBatch(createParams, tranches, batchSize);

Expand All @@ -175,11 +175,11 @@ contract BatchLockup_Gas_Test is Benchmark_Test {
}

function gasCreateWithTimestampsLT(uint256 batchSize, uint256 tranchesCount) internal {
Lockup.CreateWithTimestamps memory createParams = defaults.createWithTimestampsBrokerNull();
Lockup.CreateWithTimestamps memory createParams = defaults.createWithTimestamps();
LockupTranched.Tranche[] memory tranches = _generateTranches(tranchesCount);
createParams.timestamps.start = getBlockTimestamp();
createParams.timestamps.end = tranches[tranches.length - 1].timestamp;
createParams.totalAmount = uint128(AMOUNT_PER_ITEM * tranchesCount);
createParams.depositAmount = uint128(AMOUNT_PER_ITEM * tranchesCount);
BatchLockup.CreateWithTimestampsLT[] memory params =
BatchLockupBuilder.fillBatch(createParams, tranches, batchSize);

Expand Down
9 changes: 0 additions & 9 deletions benchmark/Benchmark.t.sol
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity >=0.8.22;

import { UD60x18, ud } from "@prb/math/src/UD60x18.sol";

import { Base_Test } from "../tests/Base.t.sol";

/// @notice Benchmark contract with common logic needed by all tests.
Expand Down Expand Up @@ -154,13 +152,6 @@ abstract contract Benchmark_Test is Base_Test {
vm.writeLine({ path: path, data: line });
}

/// @dev Calculates the total amount to be deposited in the stream, by accounting for the broker fee.
function _calculateTotalAmount(uint128 depositAmount, UD60x18 brokerFee) internal pure returns (uint128) {
UD60x18 factor = ud(1e18);
UD60x18 totalAmount = ud(depositAmount).mul(factor).div(factor.sub(brokerFee));
return totalAmount.intoUint128();
}

/// @dev Internal function to creates a few streams in each Lockup contract.
function _createFewStreams() internal {
for (uint128 i = 0; i < 100; ++i) {
Expand Down
68 changes: 11 additions & 57 deletions benchmark/LockupDynamic.Gas.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
pragma solidity >=0.8.22;

import { ud2x18 } from "@prb/math/src/UD2x18.sol";
import { UD60x18, ud } from "@prb/math/src/UD60x18.sol";

import { Lockup, LockupDynamic } from "../src/types/DataTypes.sol";

Expand Down Expand Up @@ -76,7 +75,7 @@ contract Lockup_Dynamic_Gas_Test is Benchmark_Test {
// The following function is used in the estimations of `MAX_COUNT`.
function computeGas_CreateWithDurationsLD(uint128 totalSegments) public returns (uint256 gasUsed) {
(Lockup.CreateWithDurations memory params, LockupDynamic.SegmentWithDuration[] memory segments) =
_createWithDurationParamsLD(totalSegments, defaults.BROKER_FEE());
_createWithDurationParamsLD(totalSegments);

uint256 beforeGas = gasleft();
lockup.createWithDurationsLD(params, segments);
Expand All @@ -89,36 +88,18 @@ contract Lockup_Dynamic_Gas_Test is Benchmark_Test {
resetPrank({ msgSender: users.sender });

(Lockup.CreateWithDurations memory params, LockupDynamic.SegmentWithDuration[] memory segments) =
_createWithDurationParamsLD(totalSegments, defaults.BROKER_FEE());
_createWithDurationParamsLD(totalSegments);

uint256 beforeGas = gasleft();
lockup.createWithDurationsLD(params, segments);
string memory gasUsed = vm.toString(beforeGas - gasleft());

contentToAppend = string.concat(
"| `createWithDurationsLD` (", vm.toString(totalSegments), " segments) (Broker fee set) | ", gasUsed, " |"
);
contentToAppend =
string.concat("| `createWithDurationsLD` (", vm.toString(totalSegments), " segments) | ", gasUsed, " |");

// Append the content to the file.
_appendToFile(benchmarkResultsFile, contentToAppend);

// Calculate gas usage without broker fee.
(params, segments) = _createWithDurationParamsLD(totalSegments, ud(0));

beforeGas = gasleft();
lockup.createWithDurationsLD(params, segments);
gasUsed = vm.toString(beforeGas - gasleft());

contentToAppend = string.concat(
"| `createWithDurationsLD` (",
vm.toString(totalSegments),
" segments) (Broker fee not set) | ",
gasUsed,
" |"
);

_appendToFile(benchmarkResultsFile, contentToAppend);

// Store the last 2 streams IDs for withdraw gas benchmark.
_streamIdsForWithdraw[0] = lockup.nextStreamId() - 2;
_streamIdsForWithdraw[1] = lockup.nextStreamId() - 1;
Expand All @@ -133,34 +114,15 @@ contract Lockup_Dynamic_Gas_Test is Benchmark_Test {
resetPrank({ msgSender: users.sender });

(Lockup.CreateWithTimestamps memory params, LockupDynamic.Segment[] memory segments) =
_createWithTimestampParamsLD(totalSegments, defaults.BROKER_FEE());
_createWithTimestampParamsLD(totalSegments);

uint256 beforeGas = gasleft();
lockup.createWithTimestampsLD(params, segments);

string memory gasUsed = vm.toString(beforeGas - gasleft());

contentToAppend = string.concat(
"| `createWithTimestampsLD` (", vm.toString(totalSegments), " segments) (Broker fee set) | ", gasUsed, " |"
);

// Append the data to the file
_appendToFile(benchmarkResultsFile, contentToAppend);

// Calculate gas usage without broker fee.
(params, segments) = _createWithTimestampParamsLD(totalSegments, ud(0));

beforeGas = gasleft();
lockup.createWithTimestampsLD(params, segments);
gasUsed = vm.toString(beforeGas - gasleft());

contentToAppend = string.concat(
"| `createWithTimestampsLD` (",
vm.toString(totalSegments),
" segments) (Broker fee not set) | ",
gasUsed,
" |"
);
contentToAppend =
string.concat("| `createWithTimestampsLD` (", vm.toString(totalSegments), " segments) | ", gasUsed, " |");

// Append the data to the file
_appendToFile(benchmarkResultsFile, contentToAppend);
Expand All @@ -170,10 +132,7 @@ contract Lockup_Dynamic_Gas_Test is Benchmark_Test {
HELPERS
//////////////////////////////////////////////////////////////////////////*/

function _createWithDurationParamsLD(
uint128 totalSegments,
UD60x18 brokerFee
)
function _createWithDurationParamsLD(uint128 totalSegments)
private
view
returns (Lockup.CreateWithDurations memory params, LockupDynamic.SegmentWithDuration[] memory segments_)
Expand All @@ -194,15 +153,11 @@ contract Lockup_Dynamic_Gas_Test is Benchmark_Test {
uint128 depositAmount = AMOUNT_PER_SEGMENT * totalSegments;

params = defaults.createWithDurations();
params.totalAmount = _calculateTotalAmount(depositAmount, brokerFee);
params.broker.fee = brokerFee;
params.depositAmount = depositAmount;
return (params, segments_);
}

function _createWithTimestampParamsLD(
uint128 totalSegments,
UD60x18 brokerFee
)
function _createWithTimestampParamsLD(uint128 totalSegments)
private
view
returns (Lockup.CreateWithTimestamps memory params, LockupDynamic.Segment[] memory segments_)
Expand All @@ -223,10 +178,9 @@ contract Lockup_Dynamic_Gas_Test is Benchmark_Test {
uint128 depositAmount = AMOUNT_PER_SEGMENT * totalSegments;

params = defaults.createWithTimestamps();
params.totalAmount = _calculateTotalAmount(depositAmount, brokerFee);
params.depositAmount = depositAmount;
params.timestamps.start = getBlockTimestamp();
params.timestamps.end = segments_[totalSegments - 1].timestamp;
params.broker.fee = brokerFee;
return (params, segments_);
}
}
37 changes: 5 additions & 32 deletions benchmark/LockupLinear.Gas.t.sol
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity >=0.8.22;

import { ud } from "@prb/math/src/UD60x18.sol";

import { Lockup, LockupLinear } from "../src/types/DataTypes.sol";

import { Benchmark_Test } from "./Benchmark.t.sol";
Expand Down Expand Up @@ -54,9 +52,9 @@ contract Lockup_Linear_Gas_Test is Benchmark_Test {

Lockup.CreateWithDurations memory params = defaults.createWithDurations();
LockupLinear.Durations memory durations = defaults.durations();
durations.cliff = cliffDuration;

LockupLinear.UnlockAmounts memory unlockAmounts = defaults.unlockAmounts();

durations.cliff = cliffDuration;
if (cliffDuration == 0) unlockAmounts.cliff = 0;

uint256 beforeGas = gasleft();
Expand All @@ -65,22 +63,11 @@ contract Lockup_Linear_Gas_Test is Benchmark_Test {

string memory cliffSetOrNot = cliffDuration == 0 ? " (cliff not set)" : " (cliff set)";

contentToAppend =
string.concat("| `createWithDurationsLL` (Broker fee set)", cliffSetOrNot, " | ", gasUsed, " |");

// Append the content to the file.
_appendToFile(benchmarkResultsFile, contentToAppend);

// Calculate gas usage without broker fee.
params.broker.fee = ud(0);
params.totalAmount = _calculateTotalAmount(defaults.DEPOSIT_AMOUNT(), ud(0));

beforeGas = gasleft();
lockup.createWithDurationsLL(params, unlockAmounts, durations);
gasUsed = vm.toString(beforeGas - gasleft());

contentToAppend =
string.concat("| `createWithDurationsLL` (Broker fee not set)", cliffSetOrNot, " | ", gasUsed, " |");
contentToAppend = string.concat("| `createWithDurationsLL` ", cliffSetOrNot, " | ", gasUsed, " |");

// Append the content to the file.
_appendToFile(benchmarkResultsFile, contentToAppend);
Expand All @@ -92,6 +79,7 @@ contract Lockup_Linear_Gas_Test is Benchmark_Test {

Lockup.CreateWithTimestamps memory params = defaults.createWithTimestamps();
LockupLinear.UnlockAmounts memory unlockAmounts = defaults.unlockAmounts();

if (cliffTime == 0) unlockAmounts.cliff = 0;

uint256 beforeGas = gasleft();
Expand All @@ -100,22 +88,7 @@ contract Lockup_Linear_Gas_Test is Benchmark_Test {

string memory cliffSetOrNot = cliffTime == 0 ? " (cliff not set)" : " (cliff set)";

contentToAppend =
string.concat("| `createWithTimestampsLL` (Broker fee set)", cliffSetOrNot, " | ", gasUsed, " |");

// Append the content to the file.
_appendToFile(benchmarkResultsFile, contentToAppend);

// Calculate gas usage without broker fee.
params.broker.fee = ud(0);
params.totalAmount = _calculateTotalAmount(defaults.DEPOSIT_AMOUNT(), ud(0));

beforeGas = gasleft();
lockup.createWithTimestampsLL(params, unlockAmounts, cliffTime);
gasUsed = vm.toString(beforeGas - gasleft());

contentToAppend =
string.concat("| `createWithTimestampsLL` (Broker fee not set)", cliffSetOrNot, " | ", gasUsed, " |");
contentToAppend = string.concat("| `createWithTimestampsLL` ", cliffSetOrNot, " | ", gasUsed, " |");

// Append the content to the file.
_appendToFile(benchmarkResultsFile, contentToAppend);
Expand Down
Loading

0 comments on commit 7725fde

Please sign in to comment.