SmartContract.cs 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Threading.Tasks;
  5. using System.Numerics;
  6. using Console = HyperCube.Utils.AdvConsole;
  7. namespace HyperCube.Models
  8. {
  9. public class SmartContract
  10. {
  11. public int ID;
  12. public int blockchainId;
  13. public string Code;
  14. public string ByteCode;
  15. public string Name = "";
  16. public string Gas = "";
  17. public string Address = "";
  18. public int functionId = 0;
  19. public string functionName = "";
  20. public string functionCompiledHeader = "";
  21. public static SmartContract Find(string name, Blockchain bc)
  22. {
  23. if (bc != null)
  24. {
  25. var contracts = bc.contractNames;
  26. if (contracts.ContainsKey(name))
  27. {
  28. return contracts[name];
  29. }
  30. }
  31. return null;
  32. }
  33. public static async Task<string> Verify(AccountModel verifier, ArticleModel articleModel, Blockchain bc)
  34. {
  35. if (articleModel == null)
  36. return "Verify failed";
  37. Console.WriteLine($"Verify starting");
  38. try
  39. {
  40. VerifyContract verifyContract = Find("Verify", bc) as VerifyContract;
  41. if (verifyContract != null)
  42. {
  43. Console.WriteLine($"VerifyContract found");
  44. return await verifyContract.Run(verifier, articleModel);
  45. }
  46. else
  47. Console.WriteLine($"VerifyContract null");
  48. }
  49. catch (Exception e)
  50. {
  51. Console.WriteLine(e.Message + "stack trace" + e.StackTrace);
  52. }
  53. return "Verify failed";
  54. }
  55. public SmartContract()
  56. { }
  57. public async Task<string> Run(AccountModel initiator, string weiValue = "0x0")
  58. {
  59. var bc = Blockchain.Find(blockchainId);
  60. if (bc != null)
  61. {
  62. var initiator_address = await initiator.GetOrCreateActualAddress(bc);
  63. initiator_address = Blockchain.zerofill(initiator_address, 64, true);
  64. //Console.WriteLine($"VerifyContract params article_value {article_value} edits {edits} verifier {AccountModel.Current.Name} verifier_address: {verifier_address} initiator {initiator.Name} initiator_address: {initiator_address}");
  65. //function verify( address payable verifier, address payable initiator, int article_value, int verify_complexity) public returns (uint vReward, uint iReward)
  66. var paramstring = "0x" + functionCompiledHeader;
  67. //Console.WriteLine($"VerifyContract Run {initiator.Name} params {paramstring}");
  68. var res = await bc.RunContractWrite(Address, $"{paramstring}", Gas, "0x100");
  69. //Console.WriteLine($"VerifyContract transactionId res {res}");
  70. var receipt = await bc.GetReceipt(res);
  71. //Console.WriteLine("VerifyContract receipt " + receipt);
  72. return res;
  73. }
  74. return "none";
  75. }
  76. public SmartContract(string name, string code, string bytecode, int blockchain_id, string gas)
  77. {
  78. Name = name;
  79. Code = code;
  80. ByteCode = bytecode;
  81. blockchainId = blockchain_id;
  82. Gas = gas;
  83. }
  84. public async Task LoadFunction()
  85. {
  86. if (functionId == 0)
  87. {
  88. var ret = await MySQLConnector.Instance().SQLSelectComplex($"select * from functions where contract_id={ID} limit 1");
  89. if (ret.Count > 0)
  90. {
  91. functionId = Convert.ToInt32(ret[0]["id"]);
  92. functionName = ret[0]["name_with_args"].ToString();
  93. functionCompiledHeader = ret[0]["compiled_header"].ToString();
  94. Console.WriteLine($"{ID} LoadFunction {functionId} name {functionName} header {functionCompiledHeader}");
  95. }
  96. }
  97. }
  98. public async Task<long> AddFunction(string name_with_args, string compiled_header)
  99. {
  100. return await MySQLConnector.Instance().SQLInsert($"insert into functions (name_with_args, contract_id, compiled_header) values ('{name_with_args}',{ID},'{compiled_header}')");
  101. }
  102. }
  103. public class VerifyContract : SmartContract
  104. {
  105. public VerifyContract() : base() { }
  106. public VerifyContract(string name, string code, string bytecode, int blockchain_id, string gas) : base(name, code, bytecode, blockchain_id, gas) { }
  107. /// <summary>
  108. ///
  109. /// </summary>
  110. /// <param name="newArticle"></param>
  111. /// <param name="weiValue"></param>
  112. /// <returns>ID транзакции</returns>
  113. public async Task<string> Run(AccountModel verifier, ArticleModel newArticle, string weiValue = "0x0")
  114. {
  115. var uuidInitiator = await newArticle.GetInitiatorUUID(ArticleStatus.AwatingVerify);
  116. var bc = Blockchain.Find(blockchainId);
  117. var initiator = AccountModel.Find(uuidInitiator);
  118. Console.WriteLine($"verify verifierid {verifier.UUID} initiator {uuidInitiator} verifier {verifier.GetActualAddress(bc)} initiator_address {initiator.GetActualAddress(bc)}");
  119. if (bc != null)
  120. {
  121. var verifier_address = await verifier.GetOrCreateActualAddress(bc);
  122. initiator = AccountModel.Find(uuidInitiator);
  123. var initiator_address = await initiator.GetOrCreateActualAddress(bc);
  124. Console.WriteLine($"verify verifier {verifier.UUID} initiator found " + initiator + " bc port " + bc.port + $" initiator_address {initiator.GetActualAddress(bc)} verifier_address {verifier_address}");
  125. //var article_value = (int)newArticle.Rating;
  126. var article_value = newArticle.CalcPValue();
  127. //calc comission
  128. //init add transaction 2 contract
  129. //add transaction 2db
  130. var article_value_hex = article_value.ToString("X64");
  131. //Console.WriteLine($"article_value {article_value} hex {article_value_hex}");
  132. var edits = await newArticle.GetEditsCount();
  133. //GetRatingExpert - ???
  134. //GetRatingProspector - ???
  135. long expert_value = article_value * verifier.GetRatingExpert() * edits*1000000000000000;
  136. long initiator_value = article_value * initiator.GetRatingProspector() * 1000000000000000;
  137. Console.WriteLine($"article_value {article_value} expert_value {expert_value} initiator_value {initiator_value} CharCount {newArticle.CharCount} Keywords count {newArticle.Keywords.Split(",").Length} ");
  138. var tid1 = await bc.SubmitTransaction("0xD3ae749f1253b320ea2b90dc235ed72c80447AFF", expert_value, verifier_address);
  139. var tid2 = await bc.SubmitTransaction("0xD3ae749f1253b320ea2b90dc235ed72c80447AFF", initiator_value, initiator_address);
  140. Console.WriteLine($"Verify tid1 {tid1} tid2 {tid2}");
  141. var t1hash = await bc.GetTByHash("0x"+tid1);
  142. var t2hash = await bc.GetTByHash("0x" + tid2);
  143. Console.WriteLine($"t1hash {t1hash} t2hash {t2hash}");
  144. var receiver = AccountModel.FindByMail("lord_rimmon@mail.ru");
  145. receiver.SendTestNotification(verifier, "0x" + tid1);
  146. receiver.SendTestNotification(initiator, "0x" + tid2);
  147. //init notifications 2DEVS
  148. //SendTestNotification(string header, string body, AccountModel receiver, AccountModel sender);
  149. return "none";
  150. var edits_hex = edits.ToString("X64");
  151. verifier_address = Blockchain.zerofill(verifier_address, 64, true);
  152. initiator_address = Blockchain.zerofill(initiator_address, 64, true);
  153. Console.WriteLine($"VerifyContract params article_value {article_value} edits {edits} verifier {verifier.Name} verifier_address: {verifier_address} initiator {initiator.Name} initiator_address: {initiator_address}");
  154. //function verify( address payable verifier, address payable initiator, int article_value, int verify_complexity) public returns (uint vReward, uint iReward)
  155. var paramstring = "0x" + functionCompiledHeader + verifier_address + initiator_address + article_value_hex + edits_hex;
  156. Console.WriteLine($"VerifyContract Run {initiator.Name} params {paramstring}");
  157. var res = await bc.RunContractWrite(Address, $"{paramstring}", Gas, "0x100");
  158. Console.WriteLine($"VerifyContract transactionId res {res}");
  159. var receipt = await bc.GetReceipt(res);
  160. Console.WriteLine("VerifyContract receipt " + receipt);
  161. return res;
  162. }
  163. return "none";
  164. }
  165. }
  166. }