Skip to content

Commit

Permalink
ln
Browse files Browse the repository at this point in the history
  • Loading branch information
pysel committed Nov 18, 2024
1 parent b0abef5 commit 481d7ac
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 36 deletions.
1 change: 1 addition & 0 deletions foundry.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ remappings = [
"@openzeppelin/contracts/=lib/openzeppelin-contracts/contracts/",
"@uniswap/v3-periphery/=lib/v3-periphery/",
"@uniswap/v3-core/=lib/v3-core/",
"@prb-math/=lib/prb-math/src/",
]
14 changes: 10 additions & 4 deletions src/ClvrExecutor.sol
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,22 @@ contract ClvrExecutor {
swapRouter = ISwapRouter(swapRouter_);
}

function executeBatch(ClvrLibrary.CLVRIntent[] memory intents) public {
/**
* @notice Execute a batch of intents
* @param statusQuoPrice The starting price TODO: get this in contract
* @param poolKey The identifier for the pool through which the intents are being executed
* @param intents The intents to execute
*/
function executeBatch(uint256 statusQuoPrice, bytes32 poolKey, ClvrLibrary.CLVRIntent[] memory intents) public {
// uint256[] memory volumes = new uint256[](intents.length);
for (uint256 i = 0; i < intents.length; i++) {
bytes32 intentHash = keccak256(abi.encode(intents[i]));
bytes4 verification = intentPool.isValidSignature(intentHash, "");

bytes4 verification = intentPool.intentExists(poolKey, intentHash);

if (verification != ClvrLibrary.MAGICVALUE) {
revert("Invalid Intent Passed to Executor");
}


}
}

Expand Down
22 changes: 12 additions & 10 deletions src/ClvrIntentPool.sol
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ import "@openzeppelin/contracts/utils/Address.sol";
import { ClvrLibrary } from "./ClvrLibrary.sol";


contract ClvrIntentPool is IERC1271 {
// Mapping of intents from bytes32 hash to intent itself
mapping(bytes32 => ClvrLibrary.CLVRIntent) public intents;
contract ClvrIntentPool {
// Mapping from PoolKey to Intents for a pool
mapping(bytes32 => mapping(bytes32 => ClvrLibrary.CLVRIntent)) public intents;

constructor() {}

Expand All @@ -29,17 +29,19 @@ contract ClvrIntentPool is IERC1271 {
revert("Invalid signature");
}

bytes32 poolKey = ClvrLibrary.getPoolKey(intent.tokenIn, intent.tokenOut, intent.fee);

// Store the intent
// Note: if the intent exists in the mapping, it has been signed already
intents[digest] = intent;
intents[poolKey][digest] = intent;
}

function isValidSignature(
bytes32 intentHash,
bytes memory
) external pure override returns (bytes4 magicValue) {
bytes32 actualHash = keccak256(abi.encode(intentHash));
if (actualHash != intentHash) {
function intentExists(
bytes32 poolKey,
bytes32 intentHash
) external view returns (bytes4 magicValue) {
ClvrLibrary.CLVRIntent memory intent = intents[poolKey][intentHash];
if (intent.creator == ClvrLibrary.EMPTY_ADDRESS) {
return ClvrLibrary.INVALID_SIGNATURE;
}

Expand Down
23 changes: 3 additions & 20 deletions src/ClvrLibrary.sol
Original file line number Diff line number Diff line change
Expand Up @@ -18,25 +18,8 @@ library ClvrLibrary {
bytes4 internal constant MAGICVALUE = 0x1626ba7e;
bytes4 internal constant INVALID_SIGNATURE = ~MAGICVALUE;

function ln(uint256 value) internal pure returns (uint256) {
uint256 x = value;
uint256 LOG = 0;
while (x >= 1500000) {
LOG = LOG + 405465;
x = x * 2 / 3;
}
x = x - 1000000;
uint256 y = x;
uint256 i = 1;
while (i < 10) {
LOG = LOG + (y / i);
i = i + 1;
y = y * x / 1000000;
LOG = LOG - (y / i);
i = i + 1;
y = y * x / 1000000;
}

return LOG;
function getPoolKey(address tokenIn, address tokenOut, uint24 fee) external pure returns (bytes32) {
return keccak256(abi.encode(tokenIn, tokenOut, fee));
}

}
8 changes: 6 additions & 2 deletions test/ClvrLibrary.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,18 @@
pragma solidity ^0.8.24;

import {Test} from "forge-std/Test.sol";
import { console } from "forge-std/console.sol";
import { ln } from "@prb-math/ud60x18/Math.sol";
import { UD60x18, ud } from "@prb-math/UD60x18.sol";
import "../src/ClvrLibrary.sol";

contract ClvrLibraryTest is Test {
function setUp() public {
//
}

function testLn() public {
assertEq(ClvrLibrary.ln(1000000), 1381551);
function testLn() public pure {
// ln(ud(1e18 + 1e17)).unwrap();
assertEq(ln(ud(1e18 + 1e17)).unwrap(), 95310179804324849);
}
}

0 comments on commit 481d7ac

Please sign in to comment.