Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add How to calculate pair address #5

Open
juanfranblanco opened this issue Jan 5, 2022 · 2 comments
Open

Add How to calculate pair address #5

juanfranblanco opened this issue Jan 5, 2022 · 2 comments

Comments

@juanfranblanco
Copy link
Member

juanfranblanco commented Jan 5, 2022

Thanks to @subnet this is the code on how to calculate a pair address
For reference https://docs.uniswap.org/protocol/V2/guides/smart-contract-integration/getting-pair-addresses

string factoryAddress = "0xcA143Ce32Fe78f1f7019d7d551a6402fC5350c73";
string factoryInitCodeHash = "0x00fb7f630766e6a796048ea87d01acd3068e8ff67d078148a3fa3f4a84f69bd5";
string tokenB = "0x8ac76a51cc950d9822d68b83fe1ad97b32cd580d";
string tokenA = "0xbb4CdB9CBd36B01bD1cBaEBF2De08d9173bc095c";
BigInteger tokenAInt = new HexBigInteger(tokenA).Value;
BigInteger tokenBInt = new HexBigInteger(tokenB).Value;
string token0 = tokenAInt < tokenBInt ? tokenA : tokenB;
string token1 = token0 == tokenB ? tokenA : tokenB;
ABIEncode abiEncode = new ABIEncode();
byte[] salt = abiEncode.GetSha3ABIEncodedPacked(
        new ABIValue("address", token0), 
        new ABIValue("address", token1)
);
byte[] abiEncoded = abiEncode.GetABIEncodedPacked(
        new ABIValue("address", factoryAddress), 
        new ABIValue("bytes32", salt)
);
string pairAddress = string.Concat("0x", abiEncode.GetSha3ABIEncodedPacked(
        new ABIValue("bytes", (string.Concat("ff", abiEncoded.ToHex())).HexToByteArray()), 
        new ABIValue("bytes", factoryInitCodeHash.HexToByteArray())
).ToHex().Substring(24));
//tokenAddress returns 0xd99c7F6C65857AC913a8f880A4cb84032AB2FC5b (edited)
@Ajes1337
Copy link

Ajes1337 commented Mar 1, 2023

Have anyone figured out how to do this with V3 yet, in Nethereum? I found a example in GoLang, but I struggle to convert it to C#

poolinithash v3 eth: e34f199b19b2b4f47f68442619d555527d244f78a3297ea89325f843f87b8b54
factory v3 eth: 0x1F98431c8aD98523631AE4a59f267346ea31F984

https://github.com/ackermanx/ethclient/blob/707b0bbc4a0cdf5dbf3110b69bab2486efd7d962/uniswap/address.go

// CalculatePoolAddressV3 calculate uniswapV3 pool address offline from pool tokens and fee
func CalculatePoolAddressV3(tokenA, tokenB string, fee *big.Int) (poolAddress common.Address, err error) {
	tkn0, tkn1 := sortAddressess(common.HexToAddress(tokenA), common.HexToAddress(tokenB))
	paramsPacked, err := saltAbiArguments.Pack(tkn0, tkn1, fee)
	if err != nil {
		err = errors.Wrap(err, "pack arguments")
		return
	}

	salt := crypto.Keccak256(paramsPacked)
	// "0xff"
	msg := []byte{255}
	msg = append(msg, common.HexToAddress(FactoryAddrV3).Bytes()...)
	msg = append(msg, salt...)
	msg = append(msg, PoolInitCodeV3...)

	hash := crypto.Keccak256(msg)
	return common.BytesToAddress(hash[12:]), nil
}

@juanfranblanco
Copy link
Member Author

@Ajes1337 If you check the first part on the OP, it includes the sorting of addresses, then it does the salt packing but now it needs to add also the fee.

The message is is an array of bytes with the Address, the salt you have just created and PoolInitCodeV3.

PoolInitCodeV3, _ = hex.DecodeString("e34f199b19b2b4f47f68442619d555527d244f78a3297ea89325f843f87b8b54")

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants