Skip to content

Commit

Permalink
remove bytecode for size savings
Browse files Browse the repository at this point in the history
  • Loading branch information
kyscott18 committed Sep 27, 2023
1 parent cd822c2 commit 0346116
Show file tree
Hide file tree
Showing 9 changed files with 60 additions and 24 deletions.
2 changes: 1 addition & 1 deletion packages/panoptic-sdk/src/_test/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ export const deployPool = async (): Promise<PanopticPool> => {
tokenA,
tokenB,
500,
sepoliaPanoptic.factory.uniswapFactory.address,
sepoliaPanoptic.factory.uniswapFactory,
);

const { request: initializeRequest } = await publicClient.simulateContract({
Expand Down
2 changes: 2 additions & 0 deletions packages/uniswap-v3-sdk/src/chains/mainnet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,7 @@ export const mainnetUniswapV3 = {
address: "0x1F98431c8aD98523631AE4a59f267346ea31F984",
owner: "0x1a9C8182C09F50C8318d769245beA52c32BE35BC",
blockCreated: 12369621n,
poolInitCodeHash:
"0xe34f199b19b2b4f47f68442619d555527d244f78a3297ea89325f843f87b8b54",
},
} as const satisfies { factory: UniswapV3Factory };
2 changes: 2 additions & 0 deletions packages/uniswap-v3-sdk/src/chains/sepolia.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,7 @@ export const sepoliaUniswapV3 = {
address: "0x0227628f3F023bb0B980b67D528571c95c6DaC1c",
owner: "0x1a9C8182C09F50C8318d769245beA52c32BE35BC",
blockCreated: 3518270n,
poolInitCodeHash:
"0xe34f199b19b2b4f47f68442619d555527d244f78a3297ea89325f843f87b8b54",
},
} as const satisfies { factory: UniswapV3Factory };
2 changes: 2 additions & 0 deletions packages/uniswap-v3-sdk/src/types/uniswapV3Factory.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import type { Hex } from "viem";
import type { Address } from "viem/accounts";

export type UniswapV3Factory = {
address: Address;
owner: Address;
blockCreated: bigint;
poolInitCodeHash: Hex;
};
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,12 @@ import { createUniswapV3Position } from "./createUniswapV3Position.js";
import { createUniswapV3Tick } from "./createUniswapV3Tick.js";
import { q96ToFraction } from "./q96ToFraction.js";

const uniswapV3Pool = createUniswapV3Pool(token0, token1, 100, zeroAddress);
const uniswapV3Pool = createUniswapV3Pool(token0, token1, 100, {
address: zeroAddress,
owner: zeroAddress,
blockCreated: 0n,
poolInitCodeHash: zeroAddress,
});

test("burn", () => {
const poolData: UniswapV3PoolData = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,12 @@ import { createUniswapV3Position } from "./createUniswapV3Position.js";
import { createUniswapV3Tick } from "./createUniswapV3Tick.js";
import { q96ToFraction } from "./q96ToFraction.js";

const uniswapV3Pool = createUniswapV3Pool(token0, token1, 100, zeroAddress);
const uniswapV3Pool = createUniswapV3Pool(token0, token1, 100, {
address: zeroAddress,
owner: zeroAddress,
blockCreated: 0n,
poolInitCodeHash: zeroAddress,
});

test("mint", () => {
const poolData: UniswapV3PoolData = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,12 @@ import { createUniswapV3Tick } from "./createUniswapV3Tick.js";
import { fractionToQ96 } from "./fractionToQ96.js";
import { q96ToFraction } from "./q96ToFraction.js";

const uniswapV3Pool = createUniswapV3Pool(token0, token1, 100, zeroAddress);
const uniswapV3Pool = createUniswapV3Pool(token0, token1, 100, {
address: zeroAddress,
owner: zeroAddress,
blockCreated: 0n,
poolInitCodeHash: zeroAddress,
});

test("single tick swap", async () => {
const poolData: UniswapV3PoolData = {
Expand Down
6 changes: 3 additions & 3 deletions packages/uniswap-v3-sdk/src/utils/createUniswapV3Pool.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { createERC20 } from "reverse-mirage";
import { expect, test } from "vitest";
import { mainnetUniswapV3 } from "../chains/mainnet.js";
import { mainnetUniswapV3 } from "../index.js";
import { createUniswapV3Pool } from "./createUniswapV3Pool.js";

test("create uniswap v3 pool", () => {
Expand All @@ -24,7 +24,7 @@ test("create uniswap v3 pool", () => {
tokenA,
tokenB,
500,
mainnetUniswapV3.factory.address,
mainnetUniswapV3.factory,
);

expect(pool.type).toBe("uniswapV3Pool");
Expand Down Expand Up @@ -57,7 +57,7 @@ test("create uniswap v3 pool flipped order", () => {
tokenA,
tokenB,
500,
mainnetUniswapV3.factory.address,
mainnetUniswapV3.factory,
);

expect(pool.type).toBe("uniswapV3Pool");
Expand Down
49 changes: 32 additions & 17 deletions packages/uniswap-v3-sdk/src/utils/createUniswapV3Pool.ts
Original file line number Diff line number Diff line change
@@ -1,37 +1,52 @@
import type { BaseERC20 } from "reverse-mirage";
import type { Address } from "viem/accounts";
import { encodeAbiParameters, getContractAddress, keccak256 } from "viem/utils";
import type { ByteArray } from "viem";
import {
concat,
encodeAbiParameters,
getAddress,
keccak256,
pad,
slice,
toBytes,
} from "viem/utils";
import type { UniswapV3Factory } from "../types/index.js";
import type { FeeTier, UniswapV3Pool } from "../types/uniswapV3Pool.js";
import { UniswapV3PoolInitcode } from "./bytecode.js";
import { feeAmountTickSpacing } from "./constants.js";

export const createUniswapV3Pool = (
tokenA: BaseERC20,
tokenB: BaseERC20,
feeTier: FeeTier,
factory: Address,
factory: UniswapV3Factory,
blockCreated = 0n,
): UniswapV3Pool => {
const [token0, token1] =
tokenA.address.toLowerCase() < tokenB.address.toLowerCase()
? [tokenA, tokenB]
: [tokenB, tokenA];

const address = getContractAddress({
from: factory,
opcode: "CREATE2",
bytecode: UniswapV3PoolInitcode,
salt: keccak256(
encodeAbiParameters(
[
{ name: "token0", type: "address" },
{ name: "token1", type: "address" },
{ name: "feeTier", type: "uint24" },
],
[token0.address, token1.address, feeTier],
const from = toBytes(getAddress(factory.address));
const salt = pad(
toBytes(
keccak256(
encodeAbiParameters(
[
{ name: "token0", type: "address" },
{ name: "token1", type: "address" },
{ name: "feeTier", type: "uint24" },
],
[token0.address, token1.address, feeTier],
),
),
),
});
{
size: 32,
},
) as ByteArray;
const bytecodeHash = toBytes(factory.poolInitCodeHash);
const address = getAddress(
slice(keccak256(concat([toBytes("0xff"), from, salt, bytecodeHash])), 12),
);

return {
type: "uniswapV3Pool",
Expand Down

0 comments on commit 0346116

Please sign in to comment.