@ethersproject/address#getCreate2Address TypeScript Examples

The following examples show how to use @ethersproject/address#getCreate2Address. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: pairs.ts    From limit-orders-lib with GNU General Public License v3.0 6 votes vote down vote up
getPancakeSwapPairAddress = (tokenA: Token, tokenB: Token): string => {
  const tokens = tokenA.sortsBefore(tokenB)
    ? [tokenA, tokenB]
    : [tokenB, tokenA]; // does safety checks

  return getCreate2Address(
    PANCAKESWAP_FACTORY_ADDRESS,
    keccak256(
      ["bytes"],
      [pack(["address", "address"], [tokens[0].address, tokens[1].address])]
    ),
    PANCAKESWAP_INIT_CODE_HASH
  );
}
Example #2
Source File: pair.ts    From vvs-ui with GNU General Public License v3.0 6 votes vote down vote up
public static getAddress(tokenA: Token, tokenB: Token): string {
    const tokens = tokenA.sortsBefore(tokenB) ? [tokenA, tokenB] : [tokenB, tokenA] // does safety checks
    const { chainId } = tokenA

    if (PAIR_ADDRESS_CACHE?.[tokens[0].address]?.[tokens[1].address] === undefined) {
      PAIR_ADDRESS_CACHE = {
        ...PAIR_ADDRESS_CACHE,
        [tokens[0].address]: {
          ...PAIR_ADDRESS_CACHE?.[tokens[0].address],
          [tokens[1].address]: getCreate2Address(
            FACTORY_ADDRESSES[chainId],
            keccak256(['bytes'], [pack(['address', 'address'], [tokens[0].address, tokens[1].address])]),
            INIT_CODE_HASHES[chainId]
          ),
        },
      }
    }

    return PAIR_ADDRESS_CACHE[tokens[0].address][tokens[1].address]
  }
Example #3
Source File: pair.ts    From sushiswap-exchange with GNU General Public License v3.0 6 votes vote down vote up
public static getAddress(tokenA: Token, tokenB: Token): string {
    const tokens = tokenA.sortsBefore(tokenB) ? [tokenA, tokenB] : [tokenB, tokenA] // does safety checks

    if (PAIR_ADDRESS_CACHE?.[tokens[0].address]?.[tokens[1].address] === undefined) {
      PAIR_ADDRESS_CACHE = {
        ...PAIR_ADDRESS_CACHE,
        [tokens[0].address]: {
          ...PAIR_ADDRESS_CACHE?.[tokens[0].address],
          [tokens[1].address]: getCreate2Address(
            FACTORY_ADDRESS,
            keccak256(['bytes'], [pack(['address', 'address'], [tokens[0].address, tokens[1].address])]),
            INIT_CODE_HASH
          )
        }
      }
    }

    return PAIR_ADDRESS_CACHE[tokens[0].address][tokens[1].address]
  }
Example #4
Source File: pair.ts    From sdk with MIT License 6 votes vote down vote up
public static getAddress(tokenA: Token, tokenB: Token, chainId: ChainId = ChainId.AVALANCHE): string {
    const tokens = tokenA.sortsBefore(tokenB) ? [tokenA, tokenB] : [tokenB, tokenA] // does safety checks

    if (PAIR_ADDRESS_CACHE?.[tokens[0].address]?.[tokens[1].address] === undefined) {
      PAIR_ADDRESS_CACHE = {
        ...PAIR_ADDRESS_CACHE,
        [tokens[0].address]: {
          ...PAIR_ADDRESS_CACHE?.[tokens[0].address],
          [tokens[1].address]: getCreate2Address(
            FACTORY_ADDRESS[chainId],
            keccak256(['bytes'], [pack(['address', 'address'], [tokens[0].address, tokens[1].address])]),
            INIT_CODE_HASH
          )
        }
      }
    }

    return PAIR_ADDRESS_CACHE[tokens[0].address][tokens[1].address]
  }
Example #5
Source File: pair.ts    From pancake-swap-sdk with MIT License 6 votes vote down vote up
public static getAddress(tokenA: Token, tokenB: Token): string {
    const [token0, token1] = tokenA.sortsBefore(tokenB) ? [tokenA, tokenB] : [tokenB, tokenA] // does safety checks

    const key = composeKey(token0, token1)

    if (PAIR_ADDRESS_CACHE?.[key] === undefined) {
      PAIR_ADDRESS_CACHE = {
        ...PAIR_ADDRESS_CACHE,
        [key]: getCreate2Address(
          FACTORY_ADDRESS_MAP[token0.chainId],
          keccak256(['bytes'], [pack(['address', 'address'], [token0.address, token1.address])]),
          INIT_CODE_HASH_MAP[token0.chainId]
        )
      }
    }

    return PAIR_ADDRESS_CACHE[key]
  }
Example #6
Source File: pair.ts    From pancake-swap-sdk-testnet with MIT License 6 votes vote down vote up
public static getAddress(tokenA: Token, tokenB: Token): string {
    const tokens = tokenA.sortsBefore(tokenB) ? [tokenA, tokenB] : [tokenB, tokenA] // does safety checks

    if (PAIR_ADDRESS_CACHE?.[tokens[0].address]?.[tokens[1].address] === undefined) {
      PAIR_ADDRESS_CACHE = {
        ...PAIR_ADDRESS_CACHE,
        [tokens[0].address]: {
          ...PAIR_ADDRESS_CACHE?.[tokens[0].address],
          [tokens[1].address]: getCreate2Address(
            FACTORY_ADDRESS,
            keccak256(['bytes'], [pack(['address', 'address'], [tokens[0].address, tokens[1].address])]),
            INIT_CODE_HASH
          ),
        },
      }
    }

    return PAIR_ADDRESS_CACHE[tokens[0].address][tokens[1].address]
  }
Example #7
Source File: pairs.ts    From limit-orders-lib with GNU General Public License v3.0 6 votes vote down vote up
getDefySwapPairAddress = (tokenA: Token, tokenB: Token): string => {
  const tokens = tokenA.sortsBefore(tokenB)
    ? [tokenA, tokenB]
    : [tokenB, tokenA]; // does safety checks

  return getCreate2Address(
    DEFYSWAP_FACTORY_ADDRESS,
    keccak256(
      ["bytes"],
      [pack(["address", "address"], [tokens[0].address, tokens[1].address])]
    ),
    DEFYSWAP_INIT_CODE_HASH
  );
}
Example #8
Source File: pairs.ts    From limit-orders-lib with GNU General Public License v3.0 6 votes vote down vote up
getTraderJoePairAddress = (tokenA: Token, tokenB: Token): string => {
  const tokens = tokenA.sortsBefore(tokenB)
    ? [tokenA, tokenB]
    : [tokenB, tokenA]; // does safety checks

  return getCreate2Address(
    TRADERJOE_FACTORY_ADDRESS,
    keccak256(
      ["bytes"],
      [pack(["address", "address"], [tokens[0].address, tokens[1].address])]
    ),
    TRADERJOE_INIT_CODE_HASH
  );
}
Example #9
Source File: pair.ts    From QuickSwap-sdk with MIT License 6 votes vote down vote up
public static getAddress(tokenA: Token, tokenB: Token): string {
    const tokens = tokenA.sortsBefore(tokenB) ? [tokenA, tokenB] : [tokenB, tokenA] // does safety checks

    if (PAIR_ADDRESS_CACHE?.[tokens[0].address]?.[tokens[1].address] === undefined) {
      PAIR_ADDRESS_CACHE = {
        ...PAIR_ADDRESS_CACHE,
        [tokens[0].address]: {
          ...PAIR_ADDRESS_CACHE?.[tokens[0].address],
          [tokens[1].address]: getCreate2Address(
            FACTORY_ADDRESS,
            keccak256(['bytes'], [pack(['address', 'address'], [tokens[0].address, tokens[1].address])]),
            INIT_CODE_HASH
          )
        }
      }
    }

    return PAIR_ADDRESS_CACHE[tokens[0].address][tokens[1].address]
  }
Example #10
Source File: pairs.ts    From limit-orders-lib with GNU General Public License v3.0 6 votes vote down vote up
getCafeSwapPairAddress = (tokenA: Token, tokenB: Token): string => {
  const tokens = tokenA.sortsBefore(tokenB)
    ? [tokenA, tokenB]
    : [tokenB, tokenA]; // does safety checks

  return getCreate2Address(
    CAFESWAP_FACTORY_ADDRESS,
    keccak256(
      ["bytes"],
      [pack(["address", "address"], [tokens[0].address, tokens[1].address])]
    ),
    CAFESWAP_INIT_CODE_HASH
  );
}
Example #11
Source File: pairs.ts    From limit-orders-lib with GNU General Public License v3.0 6 votes vote down vote up
getPolydexPairAddress = (tokenA: Token, tokenB: Token): string => {
  const tokens = tokenA.sortsBefore(tokenB)
    ? [tokenA, tokenB]
    : [tokenB, tokenA]; // does safety checks

  return getCreate2Address(
    POLYDEX_FACTORY_ADDRESS,
    keccak256(
      ["bytes"],
      [pack(["address", "address"], [tokens[0].address, tokens[1].address])]
    ),
    POLYDEX_INIT_CODE_HASH
  );
}
Example #12
Source File: pairs.ts    From limit-orders-lib with GNU General Public License v3.0 6 votes vote down vote up
getUniswapPairAddress = (tokenA: Token, tokenB: Token): string => {
  const tokens = tokenA.sortsBefore(tokenB)
    ? [tokenA, tokenB]
    : [tokenB, tokenA]; // does safety checks

  return getCreate2Address(
    UNISWAP_FACTORY_ADDRESS,
    keccak256(
      ["bytes"],
      [pack(["address", "address"], [tokens[0].address, tokens[1].address])]
    ),
    UNISWAP_INIT_CODE_HASH
  );
}
Example #13
Source File: pairs.ts    From limit-orders-lib with GNU General Public License v3.0 6 votes vote down vote up
getSpookySwapPairAddress = (tokenA: Token, tokenB: Token): string => {
  const tokens = tokenA.sortsBefore(tokenB)
    ? [tokenA, tokenB]
    : [tokenB, tokenA]; // does safety checks

  return getCreate2Address(
    SPOOKY_SWAP_FACTORY_ADDRESS,
    keccak256(
      ["bytes"],
      [pack(["address", "address"], [tokens[0].address, tokens[1].address])]
    ),
    SPOOKY_SWAP_INIT_CODE_HASH
  );
}
Example #14
Source File: pairs.ts    From limit-orders-lib with GNU General Public License v3.0 6 votes vote down vote up
getQuickSwapPairAddress = (tokenA: Token, tokenB: Token): string => {
  const tokens = tokenA.sortsBefore(tokenB)
    ? [tokenA, tokenB]
    : [tokenB, tokenA]; // does safety checks

  return getCreate2Address(
    QUICK_SWAP_FACTORY_ADDRESS,
    keccak256(
      ["bytes"],
      [pack(["address", "address"], [tokens[0].address, tokens[1].address])]
    ),
    QUICK_SWAP_INIT_CODE_HASH
  );
}
Example #15
Source File: pairs.ts    From limit-orders-lib with GNU General Public License v3.0 6 votes vote down vote up
getSpiritSwapPairAddress = (tokenA: Token, tokenB: Token): string => {
  const tokens = tokenA.sortsBefore(tokenB)
    ? [tokenA, tokenB]
    : [tokenB, tokenA]; // does safety checks

  return getCreate2Address(
    SPIRIT_SWAP_FACTORY_ADDRESS,
    keccak256(
      ["bytes"],
      [pack(["address", "address"], [tokens[0].address, tokens[1].address])]
    ),
    SPIRIT_SWAP_INIT_CODE_HASH
  );
}
Example #16
Source File: pair.ts    From Elastos.Essentials.App with MIT License 6 votes vote down vote up
computePairAddress = ({
  initCodeHash,
  factoryAddress,
  tokenA,
  tokenB
}: {
  factoryAddress: string
  initCodeHash: string
  tokenA: Token
  tokenB: Token
}): string => {
  const [token0, token1] = tokenA.sortsBefore(tokenB) ? [tokenA, tokenB] : [tokenB, tokenA] // does safety checks
  return getCreate2Address(
    factoryAddress,
    keccak256(['bytes'], [pack(['address', 'address'], [token0.address, token1.address])]),
    initCodeHash
  )
}
Example #17
Source File: pair.ts    From spookyswap-sdk with MIT License 6 votes vote down vote up
public static getAddress(tokenA: Token, tokenB: Token): string {
    const tokens = tokenA.sortsBefore(tokenB) ? [tokenA, tokenB] : [tokenB, tokenA] // does safety checks

    if (PAIR_ADDRESS_CACHE?.[tokens[0].address]?.[tokens[1].address] === undefined) {
      PAIR_ADDRESS_CACHE = {
        ...PAIR_ADDRESS_CACHE,
        [tokens[0].address]: {
          ...PAIR_ADDRESS_CACHE?.[tokens[0].address],
          [tokens[1].address]: getCreate2Address(
            FACTORY_ADDRESS,
            keccak256(['bytes'], [pack(['address', 'address'], [tokens[0].address, tokens[1].address])]),
            INIT_CODE_HASH
          )
        }
      }
    }

    return PAIR_ADDRESS_CACHE[tokens[0].address][tokens[1].address]
  }
Example #18
Source File: MasterDeployer.test.ts    From trident with GNU General Public License v3.0 4 votes vote down vote up
describe("MasterDeployer", function () {
  before(async function () {
    this.feeTo = await ethers.getNamedSigner("feeTo");
    this.MasterDeployer = await ethers.getContractFactory("MasterDeployer");
    this.ConstantProductPoolFactory = await ethers.getContractFactory("ConstantProductPoolFactory");
    this.BentoBox = await ethers.getContractFactory("BentoBoxV1");
    this.ERC20 = await ethers.getContractFactory("ERC20Mock");
    this.sushi = await this.ERC20.deploy("SushiToken", "SUSHI", ethers.constants.MaxUint256);
    await this.sushi.deployed();
    this.WETH9 = await ethers.getContractFactory("WETH9");
    this.weth = await this.WETH9.deploy();
    await this.weth.deployed();
    this.bentoBox = await this.BentoBox.deploy(this.weth.address);
    await this.bentoBox.deployed();
  });

  it("Reverts on invalid fee", async function () {
    await expect(
      this.MasterDeployer.deploy(MAX_FEE.add(1), this.feeTo.address, this.bentoBox.address)
    ).to.be.revertedWith("InvalidBarFee");
  });

  it("Reverts on fee to zero address", async function () {
    await expect(
      this.MasterDeployer.deploy(MAX_FEE, ethers.constants.AddressZero, this.bentoBox.address)
    ).to.be.revertedWith("ZeroAddress");
  });

  it("Reverts on bento zero address", async function () {
    await expect(
      this.MasterDeployer.deploy(MAX_FEE, this.feeTo.address, ethers.constants.AddressZero)
    ).to.be.revertedWith("ZeroAddress");
  });

  beforeEach(async function () {
    this.masterDeployer = await this.MasterDeployer.deploy(MAX_FEE, this.feeTo.address, this.bentoBox.address);
    await this.masterDeployer.deployed();
    this.constantProductPoolFactory = await this.ConstantProductPoolFactory.deploy(this.masterDeployer.address);
  });

  describe("#deployPool", async function () {
    it("Reverts on non-whitelisted factory", async function () {
      const deployData = defaultAbiCoder.encode(
        ["address", "address", "uint256", "bool"],
        [...[this.weth.address, this.sushi.address].sort(), 30, true]
      );

      await expect(
        this.masterDeployer.deployPool(this.constantProductPoolFactory.address, deployData)
      ).to.be.revertedWith("NotWhitelisted");
    });

    it("Adds address to pools array", async function () {
      await this.masterDeployer.addToWhitelist(this.constantProductPoolFactory.address);

      const deployData = defaultAbiCoder.encode(
        ["address", "address", "uint256", "bool"],
        [...[this.weth.address, this.sushi.address].sort(), 30, true]
      );

      await this.masterDeployer.deployPool(this.constantProductPoolFactory.address, deployData);
    });

    it("Reverts on direct deployment via factory", async function () {
      const deployData = defaultAbiCoder.encode(
        ["address", "address", "uint256", "bool"],
        [...[this.weth.address, this.sushi.address].sort(), 30, true]
      );

      await expect(this.constantProductPoolFactory.deployPool(deployData)).to.be.revertedWith(
        customError("UnauthorisedDeployer")
      );
    });

    // TODO: Fix this
    it("Emits event on successful deployment", async function () {
      await this.masterDeployer.addToWhitelist(this.constantProductPoolFactory.address);

      const deployData = defaultAbiCoder.encode(
        ["address", "address", "uint256", "bool"],
        [...[this.weth.address, this.sushi.address].sort(), 30, true]
      );

      const INIT_CODE_HASH = keccak256(["bytes"], [constantProductPoolBytecode]);

      const computedConstantProductPoolAddress = getCreate2Address(
        this.constantProductPoolFactory.address,
        keccak256(["bytes"], [deployData]),
        INIT_CODE_HASH
      );

      await expect(this.masterDeployer.deployPool(this.constantProductPoolFactory.address, deployData))
        .to.emit(this.masterDeployer, "DeployPool")
        .withArgs(this.constantProductPoolFactory.address, computedConstantProductPoolAddress, deployData);
    });
  });

  describe("#addToWhiteList", async function () {
    it("Adds factory to whitelist", async function () {
      await this.masterDeployer.addToWhitelist(this.constantProductPoolFactory.address);
      expect(await this.masterDeployer.whitelistedFactories(this.constantProductPoolFactory.address)).to.be.true;
    });
  });

  describe("#removeFromWhitelist", async function () {
    it("Removes factory from whitelist", async function () {
      await this.masterDeployer.addToWhitelist(this.constantProductPoolFactory.address);
      await this.masterDeployer.removeFromWhitelist(this.constantProductPoolFactory.address);
      expect(await this.masterDeployer.whitelistedFactories(this.constantProductPoolFactory.address)).to.be.false;
    });
  });

  describe("#setBarFee", async function () {
    it("Reverts on invalid fee", async function () {
      await expect(this.masterDeployer.setBarFee(MAX_FEE.add(1))).to.be.revertedWith("InvalidBarFee");
    });
    it("Mutates on valid fee", async function () {
      this.masterDeployer.setBarFee(0);
      expect(await this.masterDeployer.barFee()).to.equal(0);
    });
  });
});