Nethereum.Uniswap-V2-and-V3 icon indicating copy to clipboard operation
Nethereum.Uniswap-V2-and-V3 copied to clipboard

Add How to calculate pair address

Open juanfranblanco opened this issue 4 years ago • 4 comments

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)

juanfranblanco avatar Jan 05 '22 13:01 juanfranblanco

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
}

Ajes1337 avatar Mar 01 '23 13:03 Ajes1337

@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")

juanfranblanco avatar Mar 01 '23 13:03 juanfranblanco