|
@@ -10,6 +10,13 @@ namespace HyperCube.Models
|
|
|
{
|
|
|
public class Blockchain
|
|
|
{
|
|
|
+ public static Blockchain Dev
|
|
|
+ {
|
|
|
+ get {
|
|
|
+ return loaded[0];
|
|
|
+ }
|
|
|
+ set { }
|
|
|
+ }
|
|
|
public static Dictionary<int, Blockchain> supported;
|
|
|
public static string Connected
|
|
|
{
|
|
@@ -82,15 +89,24 @@ namespace HyperCube.Models
|
|
|
this.name = name;
|
|
|
}
|
|
|
|
|
|
- public async Task<string> CreateBlockchainAccount(AccountModel account)
|
|
|
+ public async Task<string> CreateBlockchainAccount(AccountModel account, string pass)
|
|
|
{
|
|
|
- var res = await GetMain().RunFunction("personal_newAccount", "\"password\"");
|
|
|
-
|
|
|
- Console.WriteLine($"CreateBlockchainAccount {account}: {res}");
|
|
|
- var query = $"update aspnetusers set eth_address='{res}' where UserName='{account.Name}'";
|
|
|
- account.eth_address = res;
|
|
|
- Console.WriteLine($"CreateBlockchainAccount query {query}");
|
|
|
- MySQLConnector.Instance().SQLInsert(query);
|
|
|
+ var res = await RunFunction("personal_newAccount", $"\"{pass}\"");
|
|
|
+ string query;
|
|
|
+ if (account != null)
|
|
|
+ {
|
|
|
+ Console.WriteLine($"CreateBlockchainAccount {account}: {res}");
|
|
|
+ query = $"update aspnetusers set eth_address='{res}' where UserName='{account.Name}'";
|
|
|
+ account.eth_address = res;
|
|
|
+ Console.WriteLine($"CreateBlockchainAccount query {query}");
|
|
|
+ MySQLConnector.Instance().SQLInsert(query);
|
|
|
+ }
|
|
|
+ else
|
|
|
+ Console.WriteLine($"CreateBlockchainAccount {id}: {res}");
|
|
|
+ query = $"update blockchains set address_main='{res}' where id={id}";
|
|
|
+ address = res;
|
|
|
+ Console.WriteLine($"CreateBlockchainAccount query {query}");
|
|
|
+ MySQLConnector.Instance().SQLInsert(query);
|
|
|
return res;
|
|
|
}
|
|
|
|
|
@@ -138,22 +154,35 @@ namespace HyperCube.Models
|
|
|
//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);
|
|
|
+ answer = await Post.PostRequestAsync(this, 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];
|
|
|
- var query = $"update blockchains set address_main='{address}' where id=1";
|
|
|
+ var res = (Newtonsoft.Json.Linq.JArray) jsonDe.result;
|
|
|
+ if (res.Count > 0)
|
|
|
+ {
|
|
|
+ address = jsonDe.result[0];
|
|
|
+
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ address = await CreateEthAddress("test_Password_212");
|
|
|
+ }
|
|
|
+ var query = $"update blockchains set address_main='{address}' where id={id}";
|
|
|
Console.WriteLine("query " + query);
|
|
|
MySQLConnector.Instance().SQLInsert(query);
|
|
|
Console.WriteLine("Json addr " + jsonDe.result[0]);
|
|
|
- var methodName = (string)jsonDe.name;
|
|
|
return address;
|
|
|
}
|
|
|
|
|
|
+ public async Task<string> CreateEthAddress(string pass)
|
|
|
+ {
|
|
|
+ return await CreateBlockchainAccount(null, pass);
|
|
|
+ }
|
|
|
+
|
|
|
public async Task LoadContracts()
|
|
|
{
|
|
|
MySQLConnector dbCon = MySQLConnector.Instance();
|
|
@@ -169,7 +198,7 @@ namespace HyperCube.Models
|
|
|
var hex = bin2hex(code);
|
|
|
var req = $"{{ \"jsonrpc\":\"2.0\",\"method\":\"web3_sha3\",\"params\":[\"{hex}\"], \"id\":1}}";
|
|
|
|
|
|
- var answer = await Post.PostRequestAsync(req);
|
|
|
+ var answer = await Post.PostRequestAsync(Blockchain.Dev, req);
|
|
|
dynamic jsonDe = JsonConvert.DeserializeObject(answer);
|
|
|
|
|
|
Console.WriteLine("result " + jsonDe.result);
|
|
@@ -181,7 +210,7 @@ namespace HyperCube.Models
|
|
|
Console.WriteLine($"RunFunction {name} params {parms}");
|
|
|
var req = $"{{ \"jsonrpc\":\"2.0\",\"method\":\"{name}\",\"params\":[{parms}], \"id\":1}}";
|
|
|
|
|
|
- var answer = await Post.PostRequestAsync(req);
|
|
|
+ var answer = await Post.PostRequestAsync(this, req);
|
|
|
dynamic jsonDe = JsonConvert.DeserializeObject(answer);
|
|
|
|
|
|
Console.WriteLine("RunFunction result " + jsonDe.result);
|
|
@@ -195,13 +224,20 @@ namespace HyperCube.Models
|
|
|
return ret;
|
|
|
}
|
|
|
|
|
|
+ public async Task<string> GetBalance()
|
|
|
+ {
|
|
|
+ var ret = await RunFunction("eth_getBalance", $"\"{address}\",\"latest\"");
|
|
|
+ var balanceInt = AccountModel.ConvertBalance(ret);
|
|
|
+ return balanceInt.ToString();
|
|
|
+ }
|
|
|
+
|
|
|
public async void RunContractRead()
|
|
|
{
|
|
|
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);
|
|
|
+ await Post.PostRequestAsync(this, req);
|
|
|
//Console.WriteLine($"answer {answer} len {answer.Length}" );
|
|
|
address = answer;
|
|
|
}
|
|
@@ -231,7 +267,7 @@ namespace HyperCube.Models
|
|
|
Console.WriteLine("transactionAddress " + transactionAddress);
|
|
|
var req = $"{{ \"jsonrpc\":\"2.0\",\"method\":\"eth_getTransactionReceipt\",\"params\":[\"{transactionAddress}\"], \"id\":1}}";
|
|
|
|
|
|
- var answer = await Post.PostRequestAsync(req);
|
|
|
+ var answer = await Post.PostRequestAsync(this, req);
|
|
|
|
|
|
dynamic jsonDe = JsonConvert.DeserializeObject(answer);
|
|
|
var blockHash = jsonDe.result.blockHash;
|
|
@@ -251,7 +287,7 @@ namespace HyperCube.Models
|
|
|
Console.WriteLine("bytecode " + bytecode);
|
|
|
var req = $"{{ \"jsonrpc\":\"2.0\",\"method\":\"eth_sendTransaction\",\"params\":[{{\"from\":\"{address}\",\"gas\":\"0x31b2e\", \"data\":\"{bytecode}\"}}], \"id\":1}}";
|
|
|
|
|
|
- var answer = await Post.PostRequestAsync(req);
|
|
|
+ var answer = await Post.PostRequestAsync(this, req);
|
|
|
dynamic jsonDe = JsonConvert.DeserializeObject(answer);
|
|
|
var res = jsonDe.result;
|
|
|
Console.WriteLine("result AddContract transactionAddress: " + res);
|