123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101 |
- using System;
- using System.Text;
- using System.Collections.Generic;
- using System.Linq;
- using System.Threading.Tasks;
- using Newtonsoft.Json;
- namespace HyperCube.Models
- {
- public class Blockchain
- {
- public static bool newData = false;
- public static List<Blockchain> loaded = new List<Blockchain>();
- public int port;
- public string url;
- public string address;
- public string name;
- Blockchain()
- {
- }
- public Blockchain(string url, int port)
- {
- this.url = url;
- this.port = port;
- }
- public async Task<string> GetReceipt(string transactionAddress)
- {
- Console.WriteLine("contractAddress " + transactionAddress);
- var req = $"{{ \"jsonrpc\":\"2.0\",\"method\":\"eth_getTransactionReceipt\",\"params\":[\"{transactionAddress}\"], \"id\":1}}";
- var res = await Post.PostRequestAsync(req);
- Console.WriteLine("result " + res);
- return res;
- }
- public async Task<string> Initialize()
- {
- await ListAccounts();
- if (!loaded.Contains(this))
- loaded.Add(this);
- Console.WriteLine("loaded blockchains " + loaded.Count);
- return $"Blockchain connected: {url}";
- }
- public async void GetGas(string bytecode)
- {
- }
- public async Task<string> AddContract(string bytecode)
- {
- Console.WriteLine("bytecode " + bytecode);
- var req = $"{{ \"jsonrpc\":\"2.0\",\"method\":\"eth_sendTransaction\",\"params\":[{{\"from\":\"{address}\",\"gas\":\"0x31b2e\", \"data\":\"{bytecode}\"}}], \"id\":1}}";
- var res = await Post.PostRequestAsync(req);
- Console.WriteLine("result "+ res);
- return res;
- }
- public async Task ListAccounts()
- {
- string answer = "no";
- //{ "jsonrpc":"2.0","method":"eth_getCode","params":["0x938cae6f6c21ed9d55196e96ef880f562e530553", "latest" ],"id":1}
- //string req = "{\"jsonrpc\":\"2.0\",\"method\":\"eth_getCode\",\"params\":[\"0x874c6a51e62680d4594cd2555ed8fa47b63ed253\",\"latest\"],\"id\":1}";
- string req = "{\"jsonrpc\":\"2.0\",\"method\":\"eth_accounts\", \"params\":[],\"id\":1}";
- answer = await Post.PostRequestAsync(req);
- //string json = Encoding.UTF8.GetString(bytedata, 1, bytedata.Length - 1);
- //try
- //{
- Console.WriteLine("Json string " + answer);
- dynamic jsonDe = JsonConvert.DeserializeObject(answer);
- address = jsonDe.result[0];
- Console.WriteLine("Json addr " + jsonDe.result[0]);
- var methodName = (string)jsonDe.name;
- newData = true;
- }
- public async void ExecuteContract()
- {
- string answer = "no";
- string req = "{\"jsonrpc\":\"2.0\",\"method\":\"eth_call\",\"params\":[{\"to\":\"0x874c6a51e62680d4594cd2555ed8fa47b63ed253\", \"data\":\"0xa87d942c\"},\"latest\"],\"id\":1}";
- //string req = "{\"jsonrpc\":\"2.0\",\"id\":1,\"result\":\"0x0000000000000000000000000000000000000000000000000000000000000004\"}";
- //Console.WriteLine($"req " + req);
- await Post.PostRequestAsync(req);
- //Console.WriteLine($"answer {answer} len {answer.Length}" );
- address = answer;
- }
- public string GetAddress()
- {
- //this.address = address;
- return address;
- //post запрос
- }
- }
- }
|