Skip to content

Instantly share code, notes, and snippets.

View johngrantuk's full-sized avatar

John Grant johngrantuk

View GitHub Profile
@johngrantuk
johngrantuk / SwapSingleToEth.s.sol
Created December 11, 2025 10:06
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
@johngrantuk
johngrantuk / swap.integration.test.ts
Created December 10, 2025 16:18
V3 Swap, Native Out, Local Fork
// pnpm test ./test/entities/swaps/v3/swap.integration.test.ts
import { config } from 'dotenv';
config();
import {
Address,
createTestClient,
http,
publicActions,
walletActions,
TestActions,
@johngrantuk
johngrantuk / zeKrakenExample.ts
Created June 26, 2025 11:55
Shows how to fetch list of pool addresses from API, filter out specific address then query API for SOR paths.
/**
*
* Run with:
* pnpm example ./examples/swaps/zeKrakenExample.ts
*/
import { Address } from 'viem';
import {
BalancerApi,
API_ENDPOINT,
ChainId,
@johngrantuk
johngrantuk / balanceTracking-28-04-25.ts
Created April 29, 2025 08:47
Confirm balance tracking works via events
// pnpm example ./examples/balanceTracking-28-04-25.ts
// Checks events for pool: https://balancer.fi/pools/base/v3/0xaf5b7999f491c42c05b5a2ca80f1d200d617cc8c
import { Address, createPublicClient, http, parseAbiItem, Log } from 'viem';
import { base } from 'viem/chains';
import fs from 'fs';
import path from 'path';
// Define event signatures
const swapEvent = parseAbiItem(
'event Swap(address indexed pool, address indexed tokenIn, address indexed tokenOut, uint256 amountIn, uint256 amountOut, uint256 swapFeePercentage, uint256 swapFeeAmount)',
@johngrantuk
johngrantuk / balanceTracking.ts
Created April 11, 2025 14:14
balanceTracking
// pnpm example ./examples/balanceTracking.ts
import { Address, createPublicClient, http, parseAbiItem, Log } from 'viem';
import { base } from 'viem/chains';
import fs from 'fs';
import path from 'path';
// Define event signatures
const swapEvent = parseAbiItem(
'event Swap(address indexed pool, address indexed tokenIn, address indexed tokenOut, uint256 amountIn, uint256 amountOut, uint256 swapFeePercentage, uint256 swapFeeAmount)',
);
/**
* Example showing how to find a swap for a pair using SOR directly
* - Path only uses swaps: use queryBatchSwap on Vault to see result
* - Path use join/exit: Use SDK functions to build calls to submit tx via Relayer
*/
import dotenv from 'dotenv';
import { BigNumber, parseFixed } from '@ethersproject/bignumber';
import { Wallet } from '@ethersproject/wallet';
import { AddressZero } from '@ethersproject/constants';
import {
// Example using Etherscan API to analyse contract gas usage over time period.
// And .env file with ETHERSCAN API key. Free key from: https://etherscan.io/myapikey.
require('dotenv').config();
const moment = require('moment');
const axios = require('axios').default;
const Decimal = require('decimal.js');
export const BONE = new Decimal(10).pow(18);
// Returns all transactions for block range
@johngrantuk
johngrantuk / transactionDetails.ts
Created November 17, 2020 11:42
Example showing how to get tx info from tx hash using ethers.js
// Example showing how to get tx info from tx hash using ethers.js
// Add .env file with INFURA key. Free key from: https://infura.io/
require('dotenv').config();
import { JsonRpcProvider } from '@ethersproject/providers';
import { Interface } from '@ethersproject/abi';
async function getTransactionDetails(){
let txHash = `0x4b55c0b756dbfbcd997e94b98698241299d16d5297cd17aa463d280d215a1f52`; // Example tx hash from Etherscan
// npm add @ethersproject/abi
import { Interface } from '@ethersproject/abi';
function transactionParsing(){
console.log(`transactionParsing`);
const proxyArtifact = require('./abi/ExchangeProxy.json'); // See below
const iface = new Interface(proxyArtifact.abi);
require('dotenv').config();
var moment = require('moment');
const axios = require('axios').default;
const Decimal = require('decimal.js');
export const BONE = new Decimal(10).pow(18);
// Returns all transactions for block range
export async function fetchTransactions(startBlock, endBlock) {
const proxyAddr = `0x6317C5e82A06E1d8bf200d21F4510Ac2c038AC81`;