Skip to content
This repository has been archived by the owner on Aug 1, 2023. It is now read-only.

Commit

Permalink
fix Pair.getAddress
Browse files Browse the repository at this point in the history
  • Loading branch information
Max-3-7 committed May 11, 2023
1 parent c9050a5 commit d5e3be1
Showing 1 changed file with 13 additions and 10 deletions.
23 changes: 13 additions & 10 deletions src/entities/pair.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Price } from './fractions/price'
import { TokenAmount } from './fractions/tokenAmount'
import invariant from 'tiny-invariant'
import JSBI from 'jsbi'
import { getAddress, keccak256, encodePacked, getContractAddress } from 'viem'
import { getAddress, keccak256, encodePacked, toBytes, pad, ByteArray, slice, concat } from 'viem'

import {
BigintIsh,
Expand Down Expand Up @@ -30,19 +30,22 @@ export class Pair {
const tokens = tokenA.sortsBefore(tokenB) ? [tokenA, tokenB] : [tokenB, tokenA] // does safety checks

if (PAIR_ADDRESS_CACHE?.[tokens[0].address]?.[tokens[1].address] === undefined) {
const from = toBytes(getAddress(FACTORY_ADDRESS[chainId]))
const salt = pad(
keccak256(
encodePacked(['address', 'address'], [getAddress(tokens[0].address), getAddress(tokens[1].address)]),
'bytes'
),
{
size: 32
}
) as ByteArray
const bytecodeHash = toBytes(INIT_CODE_HASH[chainId])
PAIR_ADDRESS_CACHE = {
...PAIR_ADDRESS_CACHE,
[tokens[0].address]: {
...PAIR_ADDRESS_CACHE?.[tokens[0].address],
[tokens[1].address]: getContractAddress({
from: FACTORY_ADDRESS[chainId],
salt: keccak256(
encodePacked(['address', 'address'], [getAddress(tokens[0].address), getAddress(tokens[1].address)]),
'bytes'
),
opcode: 'CREATE2',
bytecode: INIT_CODE_HASH[chainId]
})
[tokens[1].address]: getAddress(slice(keccak256(concat([toBytes('0xff'), from, salt, bytecodeHash])), 12))
}
}
}
Expand Down

0 comments on commit d5e3be1

Please sign in to comment.