Exemplo n.º 1
0
        public void CanGetColoredBalance()
        {
            using(var tester = CreateTester())
            {
                var chainBuilder = tester.CreateChainBuilder();
                tester.Client.ColoredBalance = true;

                //Colored coin Payment
                //GoldGuy emits gold to Nico
                var txBuilder = new TransactionBuilder();

                var issuanceCoinsTransaction
                    = new Transaction()
                    {
                        Outputs =
                        {
                            new TxOut("1.0", goldGuy.PrivateKey.PubKey),
                            new TxOut("1.0", silverGuy.PrivateKey.PubKey),
                            new TxOut("1.0", nico.GetAddress()),
                            new TxOut("1.0", alice.GetAddress()),
                        }
                    };

                IssuanceCoin[] issuanceCoins = issuanceCoinsTransaction
                                        .Outputs
                                        .Take(2)
                                        .Select((o, i) => new Coin(new OutPoint(issuanceCoinsTransaction.GetHash(), i), o))
                                        .Select(c => new IssuanceCoin(c))
                                        .ToArray();
                var goldIssuanceCoin = issuanceCoins[0];
                var silverIssuanceCoin = issuanceCoins[1];
                var nicoCoin = new Coin(new OutPoint(issuanceCoinsTransaction, 2), issuanceCoinsTransaction.Outputs[2]);
                var aliceCoin = new Coin(new OutPoint(issuanceCoinsTransaction, 3), issuanceCoinsTransaction.Outputs[3]);

                var goldId = goldIssuanceCoin.AssetId;
                var silverId = silverIssuanceCoin.AssetId;

                chainBuilder.Emit(issuanceCoinsTransaction);
                var b = chainBuilder.SubmitBlock();
                chainBuilder.SyncIndexer();

                var balance = tester.Client.GetOrderedBalance(nico).ToArray();
                var entry = balance[0];
                Assert.NotNull(entry.ColoredTransaction);
                Assert.Equal(Money.Parse("1.0"), entry.Amount);

                txBuilder = new TransactionBuilder();
                txBuilder.StandardTransactionPolicy.MinRelayTxFee = new FeeRate(Money.Satoshis(1000));
                var tx = txBuilder
                    .AddKeys(goldGuy)
                    .AddCoins(goldIssuanceCoin)
                    .IssueAsset(nico.GetAddress(), new AssetMoney(goldId, 30))
                    .SetChange(goldGuy.PrivateKey.PubKey)
                    .BuildTransaction(true);

                chainBuilder.Emit(tx);
                b = chainBuilder.SubmitBlock();
                chainBuilder.SyncIndexer();

                var ctx = new IndexerColoredTransactionRepository(tester.Indexer.Configuration);

                balance = tester.Client.GetOrderedBalance(nico.GetAddress()).ToArray();
                var coloredEntry = balance[0];
                Assert.Equal(Money.Parse("0.0"), coloredEntry.Amount);
                Assert.True(coloredEntry.GetAssetAmount(goldId).CompareTo(30L) == 0);

                var coloredCoins = ColoredCoin.Find(tx, ctx).ToArray();
                var nicoGold = coloredCoins[0];

                txBuilder = new TransactionBuilder(1);
                txBuilder.StandardTransactionPolicy.MinRelayTxFee = new FeeRate(Money.Satoshis(1000));
                //GoldGuy sends 20 gold to alice against 0.6 BTC. Nico sends 10 gold to alice + 0.02 BTC.
                tx = txBuilder
                    .AddKeys(goldGuy)
                    .AddCoins(goldIssuanceCoin)
                    .IssueAsset(alice.GetAddress(), new AssetMoney(goldId, 20))
                    .SetChange(goldGuy.PrivateKey.PubKey)
                    .Then()
                    .AddKeys(nico.PrivateKey)
                    .AddCoins(nicoCoin)
                    .AddCoins(nicoGold)
                    .SendAsset(alice.GetAddress(), new AssetMoney(goldId, 10))
                    .Send(alice.GetAddress(), Money.Parse("0.02"))
                    .SetChange(nico.GetAddress())
                    .Then()
                    .AddKeys(alice)
                    .AddCoins(aliceCoin)
                    .Send(goldGuy.GetAddress(), Money.Parse("0.6"))
                    .SetChange(alice.GetAddress())
                    .Shuffle()
                    .BuildTransaction(true);

                chainBuilder.Emit(tx);
                b = chainBuilder.SubmitBlock();
                chainBuilder.SyncIndexer();

                //Nico, should have lost 0.02 BTC and 10 gold
                balance = tester.Client.GetOrderedBalance(nico.GetAddress()).ToArray();
                balance = tester.Client.GetOrderedBalance(nico.GetAddress()).ToArray();
                coloredEntry = balance[0];
                Assert.Equal(Money.Parse("-0.02") - Money.Satoshis(546), coloredEntry.Amount);
                Assert.True(coloredEntry.GetAssetAmount(goldId).CompareTo(-10L) == 0);

                //Alice, should have lost 0.58 BTC, but win 10 + 20 gold (one is a transfer, the other issuance)
                balance = tester.Client.GetOrderedBalance(alice.GetAddress()).ToArray();
                coloredEntry = balance[0];
                Assert.Equal(Money.Parse("-0.58"), coloredEntry.Amount);
                Assert.True(coloredEntry.GetAssetAmount(goldId).CompareTo(30L) == 0);
            }
        }
Exemplo n.º 2
0
        public void CanGetColoredTransaction()
        {
            using(var tester = CreateTester())
            {
                var node = tester.CreateLocalNode();
                var ccTester = new ColoredCoinTester("CanColorizeTransferTransaction");
                node.ChainBuilder.Emit(ccTester.Transactions);
                node.ChainBuilder.SubmitBlock();
                tester.Indexer.IndexBlocks();
                tester.Indexer.IndexTransactions();
                var txRepo = new IndexerTransactionRepository(tester.Indexer.Configuration);
                var indexedTx = txRepo.Get(ccTester.TestedTxId);
                Assert.NotNull(indexedTx);
                Assert.Null(txRepo.Get(tester.UnknownTransactionId));

                var ccTxRepo = new IndexerColoredTransactionRepository(tester.Indexer.Configuration);
                var colored = ccTxRepo.Get(ccTester.TestedTxId);
                Assert.Null(colored);

                colored = ColoredTransaction.FetchColors(ccTester.TestedTxId, ccTxRepo);
                Assert.NotNull(colored);

                colored = ccTxRepo.Get(ccTester.TestedTxId);
                Assert.NotNull(colored);
            }
        }