SmartContract.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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 string Code;
  13. public string ByteCode;
  14. public string Name = "";
  15. public string Address = "";
  16. public int functionId = 0;
  17. public string functionName = "";
  18. public string functionCompiledHeader = "";
  19. public static SmartContract Find(string name)
  20. {
  21. var bc = Blockchain.GetMain();
  22. if (bc != null)
  23. {
  24. var contracts = bc.contractNames;
  25. if (contracts.ContainsKey(name))
  26. {
  27. return contracts[name];
  28. }
  29. }
  30. return null;
  31. }
  32. public SmartContract()
  33. { }
  34. public Task Run(string weiValue = "0x0")
  35. {
  36. return null;
  37. }
  38. public SmartContract(int id, string name, string code, string bytecode)
  39. {
  40. ID = id;
  41. Name = name;
  42. Code = code;
  43. ByteCode = bytecode;
  44. }
  45. public async Task LoadFunction()
  46. {
  47. if (functionId == 0)
  48. {
  49. var ret = await MySQLConnector.Instance().SQLSelectComplex($"select * from functions where contract_id={ID} limit 1");
  50. if (ret.Count > 0)
  51. {
  52. functionId = Convert.ToInt32(ret[0]["id"]);
  53. functionName = ret[0]["name_with_args"].ToString();
  54. functionCompiledHeader = ret[0]["compiled_header"].ToString();
  55. Console.WriteLine($"{ID} LoadFunction {functionId} name {functionName} header {functionCompiledHeader}");
  56. }
  57. }
  58. }
  59. public async Task<long> AddFunction(string name_with_args, string compiled_header)
  60. {
  61. return MySQLConnector.Instance().SQLInsert($"insert into functions (name_with_args, contract_id, compiled_header) values ('{name_with_args}',{ID},'{compiled_header}')");
  62. }
  63. }
  64. public class VerifyContract : SmartContract
  65. {
  66. public VerifyContract() : base() { }
  67. public VerifyContract(int id, string name, string code, string bytecode) : base(id, name, code, bytecode) { }
  68. /// <summary>
  69. ///
  70. /// </summary>
  71. /// <param name="newArticle"></param>
  72. /// <param name="weiValue"></param>
  73. /// <returns>ID транзакции</returns>
  74. public async Task<string> Run(ArticleModel newArticle, string weiValue = "0x0")
  75. {
  76. var uuid = await newArticle.GetInitiatorUUID();
  77. var verifier_address = AccountModel.Current.eth_address;
  78. var initiator = AccountModel.Find(uuid);
  79. var initiator_address = initiator.eth_address;
  80. var article_value = (int)newArticle.Rating;
  81. var article_value_hex = article_value.ToString("X64");
  82. //Console.WriteLine($"article_value {article_value} hex {article_value_hex}");
  83. var edits = await newArticle.GetEditsCount();
  84. var edits_hex = edits.ToString("X64");
  85. verifier_address = Blockchain.zerofill(verifier_address, 64, true);
  86. initiator_address = Blockchain.zerofill(initiator_address, 64, true);
  87. 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}");
  88. //function verify( address payable verifier, address payable initiator, int article_value, int verify_complexity) public returns (uint vReward, uint iReward)
  89. var paramstring = "0x" + functionCompiledHeader + verifier_address + initiator_address + article_value_hex + edits_hex;
  90. Console.WriteLine($"VerifyContract Run {initiator.Name} params {paramstring}");
  91. var res = await Blockchain.GetMain().RunContractWrite(Address, $"{paramstring}");
  92. Console.WriteLine($"VerifyContract transactionId res {res}");
  93. var receipt = await Blockchain.GetMain().GetReceipt(res);
  94. Console.WriteLine("VerifyContract receipt " + receipt);
  95. return res;
  96. }
  97. }
  98. }