public static async Task <MaticTransactionOptions> GetTransactionEstimate(WithdrawBurntTokensModel withdrawModel, MaticTransactionOptions options, Function function)
        {
            try
            {
                //Get the Account of the Sender
                Account account = GetAccount(options.SenderPrivateKey, options.ChainId);

                //Get the Gas Limit
                HexBigInteger gasLimit = await function.EstimateGasAsync(account.Address, null, null, withdrawModel.HeaderNumber, withdrawModel.HeaderProof, withdrawModel.BlockNumber, withdrawModel.BlockTimeStamp, withdrawModel.TxRoot, withdrawModel.ReceiptProof, withdrawModel.Path, withdrawModel.TxBytes, withdrawModel.TxProof, withdrawModel.ReceiptBytes, withdrawModel.ReceiptProof);

                //Get the Gas Price Estimate
                GasPriceEstimator gasPriceEstimator = new GasPriceEstimator();
                GasPriceEstimate  gasPriceEstimate  = await gasPriceEstimator.GetRecommendedGasPriceFromNetwork();

                //Fill the options
                options.GasPrice = (decimal)gasPriceEstimate.AverageGwei;
                options.GasLimit = gasLimit;
                options.From     = account.Address;
                return(options);
            }
            catch (Exception ex)
            {
                throw new Exception("Failed to Fill options because " + ex.Message);
            }
        }
Пример #2
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="headerNumber"></param>
        /// <param name="v1"></param>
        /// <param name="blockNumber"></param>
        /// <param name="blockTimeStamp"></param>
        /// <param name="txProofRoot"></param>
        /// <param name="receiptProofRoot"></param>
        /// <param name="v2"></param>
        /// <param name="txProofValue"></param>
        /// <param name="txProofParentNodes"></param>
        /// <param name="receiptProofValue"></param>
        /// <param name="receiptProofParentNodes"></param>
        /// <returns></returns>
        public async Task <string> WithdrawBurntTokens(WithdrawBurntTokensModel withdrawModel, MaticTransactionOptions options)
        {
            //Get the Contract instance by Creating a Web3 client from the Sender's Private Key
            Web3     web3Client       = Web3ClientHelper.GetWeb3Client(ProviderUrl, options.SenderPrivateKey);
            Contract contractInstance = web3Client.Eth.GetContract(ABI, ContractAddress);
            Function function         = contractInstance.GetFunction("withdrawBurntTokens");

            options = await TransactionEstimateHelper.GetTransactionEstimate(withdrawModel, options, function);

            string response = await function.SendTransactionAsync(options.From, options.GasLimit, options.GasPrice, null, withdrawModel.HeaderNumber, withdrawModel.HeaderProof, withdrawModel.BlockNumber, withdrawModel.BlockTimeStamp, withdrawModel.TxRoot, withdrawModel.ReceiptRoot, withdrawModel.Path, withdrawModel.TxBytes, withdrawModel.TxProof, withdrawModel.ReceiptBytes, withdrawModel.ReceiptProof);

            return(response);
        }
Пример #3
0
        /// <summary>
        /// Withdraw
        /// </summary>
        /// <param name="transactionId"></param>
        /// <param name="options"></param>
        /// <returns></returns>
        public async Task <string> Withdraw(string transactionId, MaticTransactionOptions options)
        {
            // fetch transaction & receipt proof
            (TransactionProofResponse txProof, TransactionReceiptResponse receiptProof)proofResponse;
            proofResponse.txProof = await GetTxProof(transactionId);

            proofResponse.receiptProof = await GetReceiptProof(transactionId);


            //fetch the header object and header proof
            Header header = null;

            try
            {
                header = await GetHeaderObject(proofResponse.txProof.BlockNumber);
            }catch (Exception ex)
            {
                //Do nothing
            }

            //Check if the Header block was found
            if (header == null)
            {
                throw new Exception($"No corresponding checkpoint/header block found for {transactionId}");
            }

            //Get the HEader Proof
            string headerProof = await GetHeaderProof(proofResponse.txProof.BlockNumber, header);

            //perform the withdrawal
            WithdrawBurntTokensModel withdrawBurntTokensModel = new WithdrawBurntTokensModel()
            {
                HeaderNumber   = header.Number,
                HeaderProof    = "",
                BlockNumber    = proofResponse.txProof.BlockNumber,
                BlockTimeStamp = proofResponse.txProof.BlockTimeStamp,
                TxRoot         = proofResponse.txProof.Root,
                ReceiptRoot    = proofResponse.receiptProof.Root,
                Path           = "",
                TxBytes        = proofResponse.txProof.Value.ToString(),
                TxProof        = proofResponse.txProof.ParentNodes,
                ReceiptBytes   = proofResponse.receiptProof.Value.ToString(),
                ReceiptProof   = proofResponse.receiptProof.ParentNodes
            };
            string response = await maticWithrawalManagerContract.WithdrawBurntTokens(withdrawBurntTokensModel, options);

            return(response);
        }
Пример #4
0
        public async Task <string> WithdrawLocally(string transactionId, MaticTransactionOptions options)
        {
            EthGetTransactionByHash ethGetTransactionByHash = web3.Eth.Transactions.GetTransactionByHash;
            Transaction             withdrawTransaction     = await ethGetTransactionByHash.SendRequestAsync(transactionId);

            EthGetTransactionReceipt ethGetTransactionReceipt = web3.Eth.Transactions.GetTransactionReceipt;
            TransactionReceipt       withdrawReceipt          = await ethGetTransactionReceipt.SendRequestAsync(transactionId);

            EthGetBlockWithTransactionsByNumber ethGetBlockWithTransactionsByNumber = web3.Eth.Blocks.GetBlockWithTransactionsByNumber;
            BlockWithTransactions withdrawBlock = await ethGetBlockWithTransactionsByNumber.SendRequestAsync(withdrawReceipt.BlockNumber);


            //Draft Withdraw Object
            DraftWithdrawObject withdrawObject = new DraftWithdrawObject()
            {
                TxId    = transactionId,
                Block   = withdrawBlock,
                Tx      = withdrawTransaction,
                Receipt = withdrawReceipt
            };

            //Get Transaction Proof
            TransactionProofResponse transactionProof = await  GetTxProof(withdrawObject.Tx, withdrawObject.Block);

            //Get Receipt Proof
            TransactionProofResponse receiptProof = await GetReceiptProof(withdrawObject.Receipt, withdrawObject.Block, web3);

            //Get Current Header Block
            HexBigInteger currentHeaderBlock = await maticRootChainContract.CurrenctHeaderBlock();

            //Get the Header
            Header header = await maticRootChainContract.HeaderBlock(new HexBigInteger(currentHeaderBlock.Value - 1));

            HexBigInteger headerNumber = new HexBigInteger(currentHeaderBlock.Value - 1);
            int           start        = header.Start;
            int           end          = header.End;

            Header headers = await GetHeaders(start, end, web3);

            MerkleTree tree = new MerkleTree(headers);

            string blockHeader = GetBlockHeader(withdrawObject.Block);

            string headerProof = await tree.GetProof(blockHeader);

            WithdrawBurntTokensModel withdrawBurntTokensModel = new WithdrawBurntTokensModel()
            {
                HeaderNumber   = headerNumber,
                HeaderProof    = "",
                BlockNumber    = withdrawObject.Block.Number,
                BlockTimeStamp = withdrawObject.Block.Timestamp,
                TxRoot         = withdrawObject.Block.TransactionsRoot,
                ReceiptRoot    = withdrawObject.Block.ReceiptsRoot,
                Path           = receiptProof.Path,
                TxBytes        = withdrawObject.Tx.ToString(),
                TxProof        = transactionProof.ParentNodes,
                ReceiptBytes   = withdrawObject.Receipt.ToString(),
                ReceiptProof   = receiptProof.ParentNodes
            };

            string withdrawTxObject = await maticWithrawalManagerContract.WithdrawBurntTokens(withdrawBurntTokensModel, options);

            return(withdrawTxObject);
        }