コード例 #1
0
        public void FillStateFrom(Offer?offer)
        {
            if (offer is null)
            {
                return;
            }
            s.OffererInputs = offer.FundingInputs;
            s.OffererChange = offer.ChangeAddress?.ScriptPubKey;
            s.OracleInfo    = offer.OracleInfo;
            s.Timeouts      = offer.Timeouts;
            s.ContractId    = offer.GetTemporaryContractId();
            s.Offerer ??= new Party();

            if (offer.ContractInfo is ContractInfo[] c &&
                c.Length > 0 &&
                offer.TotalCollateral is Money)
            {
                s.OffererPayoffs = DiscretePayoffs.CreateFromContractInfo(offer.ContractInfo, offer.TotalCollateral);
            }
            s.Offerer.VSizes            = new VSizeCalculator(offer).Calculate();
            s.Offerer.Collateral        = offer.TotalCollateral ?? s.OffererPayoffs?.CalculateMinimumCollateral();
            s.Offerer.FundPubKey        = offer.PubKeys?.FundingKey;
            s.Offerer.PayoutDestination = offer.PubKeys?.PayoutAddress?.ScriptPubKey;
            s.FeeRate = offer.FeeRate;
        }
コード例 #2
0
ファイル: BitcoinSTests.cs プロジェクト: dgarage/NDLC
        public void CanConvertContractInfoToPayoff()
        {
            var payoffs = new DiscretePayoffs();

            payoffs.Add(new DiscreteOutcome("a"), Money.Coins(5.0m));
            payoffs.Add(new DiscreteOutcome("b"), Money.Coins(-5.0m));
            payoffs.Add(new DiscreteOutcome("c"), Money.Coins(-2.0m));
            Assert.Equal(Money.Coins(5.0m), payoffs.CalculateMinimumCollateral());
            var ci = payoffs.ToContractInfo(payoffs.CalculateMinimumCollateral());

            Assert.Equal(Money.Coins(10.0m), ci[0].Payout);
            Assert.Equal(Money.Coins(0m), ci[1].Payout);
            Assert.Equal(Money.Coins(3.0m), ci[2].Payout);

            payoffs = DiscretePayoffs.CreateFromContractInfo(ci, Money.Coins(5.0m));
            Assert.Equal(Money.Coins(5.0m), payoffs[0].Reward);
            Assert.Equal(Money.Coins(-5.0m), payoffs[1].Reward);
            Assert.Equal(Money.Coins(-2.0m), payoffs[2].Reward);
        }
コード例 #3
0
ファイル: BitcoinSTests.cs プロジェクト: theotherside/NDLC
        public void FullExchange()
        {
            var offerExample      = Parse <Messages.Offer>("Data/Offer2.json");
            var offerKey          = new Key();
            var acceptKey         = new Key();
            var initiatorInputKey = new Key();
            var acceptorInputKey  = new Key();
            var initiator         = new DLCTransactionBuilder(true, null, null, null, Network.RegTest);
            var requiredFund      = initiator.Offer(offerExample.OracleInfo.PubKey,
                                                    offerExample.OracleInfo.RValue,
                                                    DiscretePayoffs.CreateFromContractInfo(offerExample.ContractInfo, offerExample.TotalCollateral,
                                                                                           new[] { new DiscreteOutcome("Republicans_win"), new DiscreteOutcome("Democrats_win"), new DiscreteOutcome("other") }),
                                                    offerExample.Timeouts);
            var fund1          = GetFundingPSBT(initiatorInputKey, requiredFund);
            var offer          = initiator.FundOffer(offerKey, fund1);
            var acceptor       = new DLCTransactionBuilder(false, null, null, null, Network.RegTest);
            var acceptorPayoff = acceptor.Accept(offer);
            var fund2          = GetFundingPSBT(acceptorInputKey, acceptorPayoff.CalculateMinimumCollateral());
            var accept         = acceptor.FundAccept(acceptKey, fund2);

            initiator.Sign1(accept);
            var fundPSBT = initiator.GetFundingPSBT();

            fundPSBT.SignWithKeys(initiatorInputKey);
            var sign = initiator.Sign2(offerKey, fundPSBT);

            acceptor.Finalize1(sign);
            fundPSBT = acceptor.GetFundingPSBT();
            fundPSBT.SignWithKeys(acceptorInputKey);
            var fullyVerified = acceptor.Finalize(fundPSBT);

            foreach (var i in fullyVerified.Inputs)
            {
                Assert.NotNull(i.WitScript);
            }
            fundPSBT = acceptor.GetFundingPSBT();
            if (fundPSBT.TryGetEstimatedFeeRate(out var estimated))
            {
                Assert.True(estimated > new FeeRate(1.0m), "Fee Rate of the funding PSBT are too low");
            }
        }