Blockchain.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. using System;
  2. using System.Text;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Threading.Tasks;
  6. using Newtonsoft.Json;
  7. namespace HyperCube.Models
  8. {
  9. public class Blockchain
  10. {
  11. public static bool newData = false;
  12. public static List<Blockchain> loaded = new List<Blockchain>();
  13. public int port;
  14. public string url;
  15. public string address;
  16. public string name;
  17. Blockchain()
  18. {
  19. }
  20. public Blockchain(string url, int port)
  21. {
  22. this.url = url;
  23. this.port = port;
  24. }
  25. public async Task<string> GetReceipt(string transactionAddress)
  26. {
  27. Console.WriteLine("contractAddress " + transactionAddress);
  28. var req = $"{{ \"jsonrpc\":\"2.0\",\"method\":\"eth_getTransactionReceipt\",\"params\":[\"{transactionAddress}\"], \"id\":1}}";
  29. var res = await Post.PostRequestAsync(req);
  30. Console.WriteLine("result " + res);
  31. return res;
  32. }
  33. public async Task<string> Initialize()
  34. {
  35. await ListAccounts();
  36. if (!loaded.Contains(this))
  37. loaded.Add(this);
  38. Console.WriteLine("loaded blockchains " + loaded.Count);
  39. return $"Blockchain connected: {url}";
  40. }
  41. public async void GetGas(string bytecode)
  42. {
  43. }
  44. public async Task<string> AddContract(string bytecode)
  45. {
  46. Console.WriteLine("bytecode " + bytecode);
  47. var req = $"{{ \"jsonrpc\":\"2.0\",\"method\":\"eth_sendTransaction\",\"params\":[{{\"from\":\"{address}\",\"gas\":\"0x31b2e\", \"data\":\"{bytecode}\"}}], \"id\":1}}";
  48. var res = await Post.PostRequestAsync(req);
  49. Console.WriteLine("result "+ res);
  50. return res;
  51. }
  52. public async Task ListAccounts()
  53. {
  54. string answer = "no";
  55. //{ "jsonrpc":"2.0","method":"eth_getCode","params":["0x938cae6f6c21ed9d55196e96ef880f562e530553", "latest" ],"id":1}
  56. //string req = "{\"jsonrpc\":\"2.0\",\"method\":\"eth_getCode\",\"params\":[\"0x874c6a51e62680d4594cd2555ed8fa47b63ed253\",\"latest\"],\"id\":1}";
  57. string req = "{\"jsonrpc\":\"2.0\",\"method\":\"eth_accounts\", \"params\":[],\"id\":1}";
  58. answer = await Post.PostRequestAsync(req);
  59. //string json = Encoding.UTF8.GetString(bytedata, 1, bytedata.Length - 1);
  60. //try
  61. //{
  62. Console.WriteLine("Json string " + answer);
  63. dynamic jsonDe = JsonConvert.DeserializeObject(answer);
  64. address = jsonDe.result[0];
  65. Console.WriteLine("Json addr " + jsonDe.result[0]);
  66. var methodName = (string)jsonDe.name;
  67. newData = true;
  68. }
  69. public async void ExecuteContract()
  70. {
  71. string answer = "no";
  72. string req = "{\"jsonrpc\":\"2.0\",\"method\":\"eth_call\",\"params\":[{\"to\":\"0x874c6a51e62680d4594cd2555ed8fa47b63ed253\", \"data\":\"0xa87d942c\"},\"latest\"],\"id\":1}";
  73. //string req = "{\"jsonrpc\":\"2.0\",\"id\":1,\"result\":\"0x0000000000000000000000000000000000000000000000000000000000000004\"}";
  74. //Console.WriteLine($"req " + req);
  75. await Post.PostRequestAsync(req);
  76. //Console.WriteLine($"answer {answer} len {answer.Length}" );
  77. address = answer;
  78. }
  79. public string GetAddress()
  80. {
  81. //this.address = address;
  82. return address;
  83. //post запрос
  84. }
  85. }
  86. }