Uniswap / v2-core

🦄 🦄 Core smart contracts of Uniswap V2
https://uniswap.org/docs
GNU General Public License v3.0
2.93k stars 3.15k forks source link

How to generate init code hash in the method of pairFor? #102

Closed zjsunzone closed 3 years ago

zjsunzone commented 3 years ago
 function pairFor(address factory, address tokenA, address tokenB) internal pure returns (address pair) {
        (address token0, address token1) = sortTokens(tokenA, tokenB);
        pair = address(uint(keccak256(abi.encodePacked(
                hex'ff',
                factory,
                keccak256(abi.encodePacked(token0, token1)),
                hex'96e8ac4277198ff8b6f785478aa9a39f403cb768dd02cbee326c3e7da348845f' // init code hash
            ))));
    }

How to generate this hash, what are the rules for 96e8ac4277198ff8b6f785478aa9a39f403cb768dd02cbee326c3e7da348845f?

chrisfranko commented 3 years ago
import { INIT_CODE_HASH } from '../src/constants'

import { bytecode } from '@uniswap/v2-core/build/UniswapV2Pair.json'
import { keccak256 } from '@ethersproject/solidity'

const COMPUTED_INIT_CODE_HASH = keccak256(['bytes'], [`0x${bytecode}`])

In short, its the keccak256 hash of the uniswapv2pair contract's bytecode.

dgczhh commented 3 years ago

When I use a smart contract to calculate this hash, I get a different value.Here is the smart contract:

pragma solidity =0.5.16;
import './UniswapV2Pair.sol';

contract CalHash {
    function getInitHash() public pure returns(bytes32){
        bytes memory bytecode = type(UniswapV2Pair).creationCode;
        return keccak256(abi.encodePacked(bytecode));
    }
}

The value I calculated is ae3553e7d4b4190ae1b8d77953c1bd697a5452e482587fe71f1424afe4dc849f. This contract is deployed on the Ropsten testnet and this value can pass the test. Does anyone know the cause of this problem?

dgczhh commented 3 years ago

When I use a smart contract to calculate this hash, I get a different value.Here is the smart contract:

pragma solidity =0.5.16;
import './UniswapV2Pair.sol';

contract CalHash {
    function getInitHash() public pure returns(bytes32){
        bytes memory bytecode = type(UniswapV2Pair).creationCode;
        return keccak256(abi.encodePacked(bytecode));
    }
}

The value I calculated is ae3553e7d4b4190ae1b8d77953c1bd697a5452e482587fe71f1424afe4dc849f. This contract is deployed on the Ropsten testnet and this value can pass the test. Does anyone know the cause of this problem?

I found the cause of the problem.Because I did not use pre-compiled smart contracts.Instead I used truffle to compile smart contracts.When I compiled the CalHash contract with yarn compile,I got the matching value 96e8ac4277198ff8b6f785478aa9a39f403cb768dd02cbee326c3e7da348845f.

Here is the documentation:https://uniswap.org/docs/v2/smart-contract-integration/quick-start/#writing-tests

Thanks moodysalem .

burgossrodrigo commented 2 years ago

The easiest way that i've found to solve this:

1 - Deploy the factory contract and call the function createPair. 2 - Check in scan you factory painel, you'will perceve that a contract where being created. 3 - Go to that file, copy the initial deployment code! 4 - Go here: https://emn178.github.io/online-tools/keccak_256.html 5 - Paste the initial deployment code here, the input type should be "hex" insteed of "text"

Voi la, you have your init_code_pair_hash. If you paste it in your sdk fork along with the factory address it will problably work.

banshee commented 2 years ago

Here is the documentation:https://uniswap.org/docs/v2/smart-contract-integration/quick-start/#writing-tests

It's moved to here: https://docs.uniswap.org/protocol/V2/guides/smart-contract-integration/quick-start#writing-tests

davedavinci commented 5 months ago

The easiest way that i've found to solve this:

1 - Deploy the factory contract and call the function createPair. 2 - Check in scan you factory painel, you'will perceve that a contract where being created. 3 - Go to that file, copy the initial deployment code! 4 - Go here: https://emn178.github.io/online-tools/keccak_256.html 5 - Paste the initial deployment code here, the input type should be "hex" insteed of "text"

Voi la, you have your init_code_pair_hash. If you paste it in your sdk fork along with the factory address it will problably work.

Can you explain point 2 and 3 please? You mean Etherscan? What if we are working locally? Thanks

burgossrodrigo commented 5 months ago

Exactly, on etherscan.


De: davedavinci @.> Enviado: terça-feira, 9 de abril de 2024 08:59 Para: Uniswap/v2-core @.> Cc: Rodrigo Burgos @.>; Comment @.> Assunto: Re: [Uniswap/v2-core] How to generate init code hash in the method of pairFor? (#102)

The easiest way that i've found to solve this:

1 - Deploy the factory contract and call the function createPair. 2 - Check in scan you factory painel, you'will perceve that a contract where being created. 3 - Go to that file, copy the initial deployment code! 4 - Go here: https://emn178.github.io/online-tools/keccak_256.html 5 - Paste the initial deployment code here, the input type should be "hex" insteed of "text"

Voi la, you have your init_code_pair_hash. If you paste it in your sdk fork along with the factory address it will problably work.

Can you explain point 2 and 3 please? You mean Etherscan? What if we are working locally? Thanks

— Reply to this email directly, view it on GitHubhttps://github.com/Uniswap/v2-core/issues/102#issuecomment-2044715456, or unsubscribehttps://github.com/notifications/unsubscribe-auth/ASI7VXTQPHSQPASGYMYYDNLY4PCZNAVCNFSM4RTALKC2U5DIOJSWCZC7NNSXTN2JONZXKZKDN5WW2ZLOOQ5TEMBUGQ3TCNJUGU3A. You are receiving this because you commented.Message ID: @.***>