0

I'm writing code in c# (nethereum) to pull liquidity out of pair using removeLiquidityETHWithPermitSupportingFeeOnTransferTokens function from UniswapV2Router02 contract

I encountered issue when calculating signature for permit, getting error UniswapV2: INVALID_SIGNATURE

my code:

var PERMIT_TYPEHASH = keccak256(Encoding.ASCII.GetBytes("Permit(address owner,address 
spender,uint256 value,uint256 nonce,uint256 deadline)"));

var DOMAIN_SEPARATOR = await uniswapV2Pair02Service.DOMAIN_SEPARATORQueryAsync(); //getting DOMAIN_SEPARATOR from contract
var signature = keccak256(abiEncode.GetABIEncodedPacked(
new ABIValue("bytes", StringToByteArray(@"1901")),
new ABIValue("bytes", DOMAIN_SEPARATOR),
new ABIValue("bytes", StringToByteArray(
keccak256(abiEncode.GetABIEncoded(new ABIValue("bytes", 
StringToByteArray(PERMIT_TYPEHASH)),
new ABIValue("address", account.Address),
new ABIValue("address", "0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D"),
new ABIValue("uint256", lpamount),
new ABIValue("uint256", nonce),
new ABIValue("uint256", deadline)
)))
)));


//getting V, R, S
var signer1 = new EthereumMessageSigner();
var signature1 = signer1.SignAndCalculateV(StringToByteArray(signature), new 
EthECKey(privateKey));

//transaction part
var removeLiquidityETH = new Nethereum.Uniswap.Contracts.UniswapV2Router02.ContractDefinition.RemoveLiquidityETHWithPermitSupportingFeeOnTransferTokensFunction
            {
                Token = deployedContract, //token address
                Liquidity = lpamount, //amount of lp tokens
                AmountTokenMin = 900000,

                AmountETHMin = 200000000000000000,
                To = myAddress,
                Deadline = deadline,
                ApproveMax = false,
                V = Convert.ToByte(int.Parse(signature1.V.ToHex(), System.Globalization.NumberStyles.HexNumber)),
                R = signature1.R,
                S = signature1.S


            };

      



var swapReceipt = await uniswapV2Router02Service.RemoveLiquidityETHWithPermitSupportingFeeOnTransferTokensRequestAndWaitForReceiptAsync(removeLiquidityETH);

helper functions:

 public string keccak256(byte[] data)
    {

        var digest = new KeccakDigest(256);

        digest.BlockUpdate(data, 0, data.Length);
        var calculatedHash = new byte[digest.GetByteLength()];
        digest.DoFinal(calculatedHash, 0);

        digest.Reset();
        // log(calculatedHash)
        //return(calculatedHash.ToHex());
        var transactionHash = BitConverter.ToString(calculatedHash, 0, 32).Replace("-", "").ToLower();

        return (transactionHash);

    }

    public static byte[] StringToByteArray(String hex)
    {
        int NumberChars = hex.Length;
        byte[] bytes = new byte[NumberChars / 2];
        for (int i = 0; i < NumberChars; i += 2)
            bytes[i / 2] = Convert.ToByte(hex.Substring(i, 2), 16);
        return bytes;
    }

0 Answers0