Skip to content

Instantly share code, notes, and snippets.

@johngrantuk
Created December 11, 2025 10:06
Show Gist options
  • Select an option

  • Save johngrantuk/4de6ad0bdf68fce6a42f671be4e3c28e to your computer and use it in GitHub Desktop.

Select an option

Save johngrantuk/4de6ad0bdf68fce6a42f671be4e3c28e to your computer and use it in GitHub Desktop.
Forge Script - Balancer V3 Swap To Native
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.22;
import "forge-std/Script.sol";
import {console} from "forge-std/console.sol";
import {IRouter} from "@balancer-v3-monorepo/interfaces/vault/IRouter.sol";
import {IERC20} from "@openzeppelin/contracts/interfaces/IERC20.sol";
import {IPermit2} from "@permit2/interfaces/IPermit2.sol";
// forge script --chain mainnet script/SwapSingleToEth.s.sol:SwapSingleToEth --rpc-url http://localhost:8545 -vvvv
// Add --broadcast to actually send the transaction
contract SwapSingleToEth is Script {
// Balancer Router
IRouter public constant router = IRouter(0x3f170631ed9821Ca51A59D996aB095162438DC10);
IPermit2 public constant permit2 = IPermit2(0x000000000022D473030F116dDEE9F6B43aC78BA3);
// Pool to swap against
address public constant pool = 0x7b4c560f33A71A9f7a500aF3C4c65b46fBBafdB7;
IERC20 usdc = IERC20(0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913);
IERC20 weth = IERC20(0x4200000000000000000000000000000000000006);
function run() public returns (uint256) {
// Add .env in root with PRIVATE_KEY
uint256 deployerPrivateKey = vm.envUint("PRIVATE_KEY");
vm.startBroadcast(deployerPrivateKey);
usdc.approve(address(permit2), 10e6);
console.log(usdc.balanceOf(address(0xdf330Ccb1d8fE97D176850BC127D0101cBe4e932)));
console.log(weth.balanceOf(address(0xdf330Ccb1d8fE97D176850BC127D0101cBe4e932)));
console.log(address(0xdf330Ccb1d8fE97D176850BC127D0101cBe4e932).balance);
permit2.approve(address(usdc), address(router), type(uint160).max, type(uint48).max);
router.swapSingleTokenExactIn(pool, usdc, weth, 1000000, 0, type(uint256).max, true, bytes(""));
console.log(usdc.balanceOf(address(0xdf330Ccb1d8fE97D176850BC127D0101cBe4e932)));
console.log(weth.balanceOf(address(0xdf330Ccb1d8fE97D176850BC127D0101cBe4e932)));
console.log(address(0xdf330Ccb1d8fE97D176850BC127D0101cBe4e932).balance);
vm.stopBroadcast();
return 0;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment