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

[SC-935] Feature/asm #135

Closed
wants to merge 9 commits into from
Closed
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
138 changes: 87 additions & 51 deletions contracts/SettlementExtension.sol
Original file line number Diff line number Diff line change
Expand Up @@ -77,34 +77,63 @@ contract SettlementExtension is IPostInteraction, IAmountGetter, FeeBankCharger
/// (bytes3,bytes2)[N] pointsAndTimeDeltas;
/// }

function _getRateBump(bytes calldata auctionDetails) private view returns (uint256) {
unchecked {
uint256 auctionStartTime = uint32(bytes4(auctionDetails[0:4]));
uint256 auctionFinishTime = auctionStartTime + uint24(bytes3(auctionDetails[4:7]));
uint256 initialRateBump = uint24(bytes3(auctionDetails[7:10]));

if (block.timestamp <= auctionStartTime) {
return initialRateBump;
} else if (block.timestamp >= auctionFinishTime) {
return 0; // Means 0% bump
function _getRateBump(bytes calldata auctionDetails) private view returns (uint256 result) {
// solhint-disable-next-line no-inline-assembly
assembly ("memory-safe") {
function timeWeightedAvg(t1, v1, t2, v2) -> avg {
avg := div(add(mul(sub(timestamp(), t1), v2), mul(sub(t2, timestamp()), v1)), sub(t2, t1))
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
avg := div(add(mul(sub(timestamp(), t1), v2), mul(sub(t2, timestamp()), v1)), sub(t2, t1))
avg := div(add(mul(sub(timestamp(), t1), v2), mul(sub(t2, timestamp()), v1)), sub(t2, t1)) // ((block.timestamp - t1) * v2 + (t2 - block.timestamp) * v1) / (t2 - t1)

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 not used in fact. I forgot to remove the timeWeightedAvg function 😩

}

auctionDetails = auctionDetails[10:];
uint256 pointsSize = auctionDetails.length / 5;
uint256 currentPointTime = auctionStartTime;
uint256 currentRateBump = initialRateBump;
let firstWord := calldataload(auctionDetails.offset)
let auctionStartTime := shr(224, firstWord)
firstWord := shl(32, firstWord)
let auctionFinishTime := add(auctionStartTime, shr(232, firstWord))
firstWord := shl(24, firstWord)
let initialRateBump := shr(232, firstWord)

for (uint256 i = 0; i < pointsSize; i++) {
uint256 nextRateBump = uint24(bytes3(auctionDetails[:3]));
uint256 nextPointTime = currentPointTime + uint16(bytes2(auctionDetails[3:5]));
if (block.timestamp <= nextPointTime) {
return ((block.timestamp - currentPointTime) * nextRateBump + (nextPointTime - block.timestamp) * currentRateBump) / (nextPointTime - currentPointTime);
switch gt(timestamp(), auctionStartTime)
case 0 {
result := initialRateBump
}
default {
switch lt(timestamp(), auctionFinishTime)
case 0 {
result := 0
}
default {
let cdPtr := add(auctionDetails.offset, 10)
let cdEnd := add(auctionDetails.offset, auctionDetails.length)
let currentPointTime := auctionStartTime
let currentRateBump := initialRateBump
for { } lt(cdPtr, cdEnd) { cdPtr := add(cdPtr, 5) } {
let data := calldataload(cdPtr)
let nextRateBump := shr(232, data)
data := shl(24, data)
let nextPointTime := add(currentPointTime, shr(240, data))
switch gt(timestamp(), nextPointTime)
case 0 {
result := div(
add(
mul(sub(timestamp(), currentPointTime), nextRateBump),
mul(sub(nextPointTime, timestamp()), currentRateBump)
),
sub(nextPointTime, currentPointTime)
)
break
}
default {
currentPointTime := nextPointTime
currentRateBump := nextRateBump
}
}
if eq(cdPtr, cdEnd) {
result := div(
mul(sub(auctionFinishTime, timestamp()), currentRateBump),
sub(auctionFinishTime, currentPointTime)
)
}
}
currentRateBump = nextRateBump;
currentPointTime = nextPointTime;
auctionDetails = auctionDetails[5:];
}
return (auctionFinishTime - block.timestamp) * currentRateBump / (auctionFinishTime - currentPointTime);
}
}

Expand Down Expand Up @@ -140,44 +169,51 @@ contract SettlementExtension is IPostInteraction, IAmountGetter, FeeBankCharger
uint256 actualMakingAmount,
uint256 actualTakingAmount
) private pure returns (uint256 resolverFee, address integrator, uint256 integrationFee, bytes calldata whitelist) {
bytes1 feeType = extraData[0];
extraData = extraData[1:];
if (feeType & 0x01 == 0x01) {
// resolverFee enabled
resolverFee = uint256(uint32(bytes4(extraData[:4]))) * _ORDER_FEE_BASE_POINTS * actualMakingAmount / orderMakingAmount;
extraData = extraData[4:];
}
if (feeType & 0x02 == 0x02) {
// integratorFee enabled
integrator = address(bytes20(extraData[:20]));
integrationFee = actualTakingAmount * uint256(uint32(bytes4(extraData[20:24]))) / _TAKING_FEE_BASE;
extraData = extraData[24:];
// solhint-disable-next-line no-inline-assembly
assembly ("memory-safe") {
let firstWord := calldataload(extraData.offset)
let feeType := shr(248, firstWord)
firstWord := shl(8, firstWord)
// let extraDataEnd := add(extraData.offset, extraData.length)
// extraData.offset := add(extraData.offset, 1)
whitelist.offset := add(extraData.offset, 1)
if and(feeType, 0x01) {
// resolverFee enabled
resolverFee := div(mul(mul(shr(224, firstWord), _ORDER_FEE_BASE_POINTS), actualMakingAmount), orderMakingAmount)
firstWord := shl(32, firstWord)
whitelist.offset := add(whitelist.offset, 4)
}
if and(feeType, 0x02) {
// integratorFee enabled
integrator := shr(96, firstWord)
firstWord := shl(160, firstWord)
integrationFee := div(mul(actualTakingAmount, shr(224, firstWord)), _TAKING_FEE_BASE)
whitelist.offset := add(whitelist.offset, 24)
}
whitelist.length := sub(extraData.length, sub(whitelist.offset, extraData.offset))
}
whitelist = extraData;
}

/// struct WhitelistDetails {
/// bytes4 auctionStartTime;
/// (bytes10,bytes2)[N] resolversAddressesAndTimeDeltas;
/// }

function _isWhitelisted(bytes calldata whitelist, address resolver) private view returns (bool) {
unchecked {
uint256 allowedTime = uint32(bytes4(whitelist[0:4])); // initially set to auction start time
whitelist = whitelist[4:];
uint256 whitelistSize = whitelist.length / 12;
uint80 maskedResolverAddress = uint80(uint160(resolver) & _RESOLVER_ADDRESS_MASK);
for (uint256 i = 0; i < whitelistSize; i++) {
uint80 whitelistedAddress = uint80(bytes10(whitelist[:10]));
allowedTime += uint16(bytes2(whitelist[10:12])); // add next time delta
if (maskedResolverAddress == whitelistedAddress) {
return allowedTime <= block.timestamp;
} else if (allowedTime > block.timestamp) {
return false;
function _isWhitelisted(bytes calldata whitelist, address resolver) private view returns (bool result) {
// solhint-disable-next-line no-inline-assembly
assembly ("memory-safe") {
let allowedTime := shr(224, calldataload(whitelist.offset))
let maskedResolverAddress := and(resolver, _RESOLVER_ADDRESS_MASK)
let cdEnd := add(whitelist.offset, whitelist.length)
for { let cdPtr := add(whitelist.offset, 4) } lt(cdPtr, cdEnd) { cdPtr := add(cdPtr, 12) } {
let data := calldataload(cdPtr)
let whitelistedAddress := shr(176, data)
allowedTime := add(allowedTime, and(shr(160, data), 0xffff))
if eq(maskedResolverAddress, whitelistedAddress) {
result := sub(1, gt(allowedTime, timestamp()))
break
}
whitelist = whitelist[12:];
}
return false;
}
}
}
Loading