Skip to content

Commit

Permalink
Added Deployment code
Browse files Browse the repository at this point in the history
  • Loading branch information
chuacw committed Sep 12, 2024
1 parent b8a02d9 commit bef11ee
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 5 deletions.
1 change: 1 addition & 0 deletions script/HookDonation.s.sol
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,5 @@ contract AfterSwapDonationHookScript is Script, Deployers {

vm.stopBroadcast();
}

}
67 changes: 67 additions & 0 deletions script/V4Deployer.s.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.26;

import {Script} from "forge-std/Script.sol";
import {PoolManager} from "v4-core/PoolManager.sol";
import {PoolSwapTest} from "v4-core/test/PoolSwapTest.sol";
import {PoolModifyLiquidityTest} from "v4-core/test/PoolModifyLiquidityTest.sol";
import {PoolDonateTest} from "v4-core/test/PoolDonateTest.sol";
import {PoolTakeTest} from "v4-core/test/PoolTakeTest.sol";
import {PoolClaimsTest} from "v4-core/test/PoolClaimsTest.sol";
import {Hooks} from "lib/v4-periphery/lib/v4-core/src/libraries/Hooks.sol";
import {HookMiner} from "../test/utils/HookMiner.sol";
import {AfterSwapDonationHook} from "../src/HookDonation.sol";
import {PoolKey} from "lib/v4-core/src/types/PoolKey.sol";
import {IHooks} from "lib/v4-core/src/interfaces/IHooks.sol";
import {CurrencyLibrary, Currency} from "lib/v4-core/src/types/Currency.sol";
import {IPoolManager} from "@uniswap/v4-core/src/interfaces/IPoolManager.sol";
import {console} from "lib/v4-periphery/lib/v4-core/lib/forge-std/src/console.sol";


contract V4Deployer is Script {
address constant CREATE2_DEPLOYER = address(0x4e59b44847b379578588920cA78FbF26c0B4956C);
AfterSwapDonationHook public donationHook;

function run() public {
vm.startBroadcast();

PoolManager manager = new PoolManager();
PoolSwapTest swapRouter = new PoolSwapTest(manager);

// Anything else you need to do like minting mock ERC20s or initializing a pool
// you need to do directly here as well without using Deployers
// Define tokens and pool parameters
address tokenA = 0x7b79995e5f793A07Bc00c21412e50Ecae098E7f9; // WETH
address tokenB = 0xaA8E23Fb1079EA71e0a56F48a2aA51851D8433D0; // USDT
uint24 fee = 3000; // Fee in basis points, ie, 0.30%

uint160 flags = uint160(Hooks.AFTER_SWAP_FLAG);
uint256 tempValue = 0;
uint256 seed = tempValue;
address hookAddress = address(0);
bytes32 salt;
while (hookAddress == address(0)) {
(hookAddress, salt) = HookMiner.find(
CREATE2_DEPLOYER, flags, seed, type(AfterSwapDonationHook).creationCode, abi.encode(address(manager))
);
if (hookAddress == address(0)) {
seed = uint256(salt)+1;
}
}
console.log("Hook Address: %s", hookAddress);
donationHook = new AfterSwapDonationHook{salt: salt}(IPoolManager(address(manager)));

// Create PoolKey
PoolKey memory key = PoolKey({
currency0: Currency.wrap(tokenA),
currency1: Currency.wrap(tokenB),
fee: fee,
tickSpacing: 60,
hooks: IHooks(hookAddress)
});



vm.stopBroadcast();
}
}
6 changes: 3 additions & 3 deletions test/HookDonation.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -122,17 +122,17 @@ contract DonationTest is Test, Deployers {
vm.startPrank(tx.origin);

MockERC20 t0 = MockERC20(Currency.unwrap(token0));
MockERC20 t1 = MockERC20(Currency.unwrap(token1));

mint(tx.origin, 1000, 1000);

uint256 percent = 10;
// Workflow: The user will need to call enableDonation on the AfterSwapDonationHook contract
donationHook.enableDonation(RECIPIENT, percent);

// Approve the donationHook contract to spend on behalf of tx.origin, which is the user / EOA
// This is essential, otherwise, in afterSwap, token.transferFrom will fail
t0.approve(address(donationHook), t0.balanceOf(tx.origin));
t1.approve(address(donationHook), t1.balanceOf(tx.origin));
// Workflow: The user will need to call approve for token0 on their own.
t0.approve(address(donationHook), type(uint256).max);

vm.stopPrank();

Expand Down
5 changes: 3 additions & 2 deletions test/utils/HookMiner.sol
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ library HookMiner {
bytes memory creationCodeWithArgs = abi.encodePacked(creationCode, constructorArgs);

uint256 salt = seed;
for (salt; salt < MAX_LOOP;) {
for (salt; salt < MAX_LOOP+seed;) {
hookAddress = computeAddress(deployer, salt, creationCodeWithArgs);
if (uint160(hookAddress) & FLAG_MASK == flags) {
return (hookAddress, bytes32(salt));
Expand All @@ -43,7 +43,8 @@ library HookMiner {
}
}

revert("HookMiner: could not find salt");
// revert("HookMiner: could not find salt");
return (address(0), bytes32(salt));
}

/// @notice Precompute a contract address deployed via CREATE2
Expand Down

0 comments on commit bef11ee

Please sign in to comment.