1+ //ReSharper disable All
2+
3+ using System ;
4+ using System . Collections . Generic ;
5+ using System . Linq ;
6+ using NBitcoin ;
7+ using NBitcoin . OpenAsset ;
8+
9+ namespace _4 . _2LiquidDemocracy
10+ {
11+ internal class Program
12+ {
13+ private static void Main ( )
14+ {
15+ var powerCoin = new Key ( ) ;
16+ var alice = new Key ( ) ;
17+ var bob = new Key ( ) ;
18+ var satoshi = new Key ( ) ;
19+ var init = new Transaction ( )
20+ {
21+ Outputs =
22+ {
23+ new TxOut ( Money . Coins ( 1.0m ) , powerCoin ) ,
24+ new TxOut ( Money . Coins ( 1.0m ) , alice ) ,
25+ new TxOut ( Money . Coins ( 1.0m ) , bob ) ,
26+ new TxOut ( Money . Coins ( 1.0m ) , satoshi ) ,
27+ }
28+ } ;
29+
30+ var repo = new NoSqlColoredTransactionRepository ( ) ;
31+ repo . Transactions . Put ( init ) ;
32+
33+ var issuance = GetCoins ( init , powerCoin )
34+ . Select ( c => new IssuanceCoin ( c ) )
35+ . ToArray ( ) ;
36+ var builder = new TransactionBuilder ( ) ;
37+ var toAlice =
38+ builder
39+ . AddCoins ( issuance )
40+ . AddKeys ( powerCoin )
41+ . IssueAsset ( alice , new AssetMoney ( powerCoin , 2 ) )
42+ . SetChange ( powerCoin )
43+ . Then ( )
44+ . AddCoins ( GetCoins ( init , alice ) )
45+ . AddKeys ( alice )
46+ . Send ( alice , Money . Coins ( 0.2m ) )
47+ . SetChange ( alice )
48+ . BuildTransaction ( true ) ;
49+ repo . Transactions . Put ( toAlice ) ;
50+
51+ var votingCoin = new Key ( ) ;
52+ var init2 = new Transaction ( )
53+ {
54+ Outputs =
55+ {
56+ new TxOut ( Money . Coins ( 1.0m ) , votingCoin ) ,
57+ }
58+ } ;
59+ repo . Transactions . Put ( init2 ) ;
60+
61+ issuance = GetCoins ( init2 , votingCoin ) . Select ( c => new IssuanceCoin ( c ) ) . ToArray ( ) ;
62+ builder = new TransactionBuilder ( ) ;
63+ var toVoters =
64+ builder
65+ . AddCoins ( issuance )
66+ . AddKeys ( votingCoin )
67+ . IssueAsset ( alice , new AssetMoney ( votingCoin , 1 ) )
68+ . IssueAsset ( satoshi , new AssetMoney ( votingCoin , 1 ) )
69+ . SetChange ( votingCoin )
70+ . BuildTransaction ( true ) ;
71+ repo . Transactions . Put ( toVoters ) ;
72+
73+ var aliceVotingCoin = ColoredCoin . Find ( toVoters , repo )
74+ . Where ( c => c . ScriptPubKey == alice . ScriptPubKey )
75+ . ToArray ( ) ;
76+ builder = new TransactionBuilder ( ) ;
77+ var toBob =
78+ builder
79+ . AddCoins ( aliceVotingCoin )
80+ . AddKeys ( alice )
81+ . SendAsset ( bob , new AssetMoney ( votingCoin , 1 ) )
82+ . BuildTransaction ( true ) ;
83+ repo . Transactions . Put ( toBob ) ;
84+
85+ var bobVotingCoin = ColoredCoin . Find ( toVoters , repo )
86+ . Where ( c => c . ScriptPubKey == bob . ScriptPubKey )
87+ . ToArray ( ) ;
88+
89+ builder = new TransactionBuilder ( ) ;
90+ var vote =
91+ builder
92+ . AddCoins ( bobVotingCoin )
93+ . AddKeys ( bob )
94+ . SendAsset ( BitcoinAddress . Create ( "1HZwkjkeaoZfTSaJxDw6aKkxp45agDiEzN" ) ,
95+ new AssetMoney ( votingCoin , 1 ) )
96+ . BuildTransaction ( true ) ;
97+
98+ issuance = GetCoins ( init2 , votingCoin ) . Select ( c => new IssuanceCoin ( c ) ) . ToArray ( ) ;
99+ issuance [ 0 ] . DefinitionUrl = new Uri ( "http://boss.com/vote01.json" ) ;
100+ builder = new TransactionBuilder ( ) ;
101+ toVoters =
102+ builder
103+ . AddCoins ( issuance )
104+ . AddKeys ( votingCoin )
105+ . IssueAsset ( alice , new AssetMoney ( votingCoin , 1 ) )
106+ . IssueAsset ( satoshi , new AssetMoney ( votingCoin , 1 ) )
107+ . SetChange ( votingCoin )
108+ . BuildTransaction ( true ) ;
109+ repo . Transactions . Put ( toVoters ) ;
110+
111+ Console . ReadLine ( ) ;
112+ }
113+
114+ private static IEnumerable < Coin > GetCoins ( Transaction tx , Key owner )
115+ {
116+ return tx . Outputs . AsCoins ( ) . Where ( c => c . ScriptPubKey == owner . ScriptPubKey ) ;
117+ }
118+
119+ }
120+ }
0 commit comments