|
@@ -142,6 +142,13 @@ namespace HyperCube.Models
|
|
return res;
|
|
return res;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ public static string hex2bin(string hexstring)
|
|
|
|
+ {
|
|
|
|
+ string binarystring = String.Join(String.Empty, hexstring.Select( c => Convert.ToString(Convert.ToInt32(c.ToString(), 16), 2).PadLeft(4, '0') ));
|
|
|
|
+ Console.WriteLine("bin " + binarystring);
|
|
|
|
+ return binarystring;
|
|
|
|
+ }
|
|
|
|
+
|
|
public static string bin2hex(string code)
|
|
public static string bin2hex(string code)
|
|
{
|
|
{
|
|
//code = "0x".bin2hex('getCount()');
|
|
//code = "0x".bin2hex('getCount()');
|
|
@@ -162,12 +169,15 @@ namespace HyperCube.Models
|
|
}
|
|
}
|
|
|
|
|
|
public static Blockchain GetMain()
|
|
public static Blockchain GetMain()
|
|
- {
|
|
|
|
- Console.WriteLine("GetMain blockchain_selected " + AccountModel.GetCurrent().blockchain_selected);
|
|
|
|
|
|
+ {
|
|
try
|
|
try
|
|
{
|
|
{
|
|
if (loaded.Count > AccountModel.GetCurrent().blockchain_selected)
|
|
if (loaded.Count > AccountModel.GetCurrent().blockchain_selected)
|
|
- return loaded[AccountModel.GetCurrent().blockchain_selected];
|
|
|
|
|
|
+ {
|
|
|
|
+ var bc = loaded[AccountModel.GetCurrent().blockchain_selected];
|
|
|
|
+ Console.WriteLine($"GetMain blockchain_selected {AccountModel.GetCurrent().blockchain_selected} " + bc.address);
|
|
|
|
+ return bc;
|
|
|
|
+ }
|
|
else
|
|
else
|
|
Console.WriteLine($"Error: blockchains loaded {loaded.Count}");
|
|
Console.WriteLine($"Error: blockchains loaded {loaded.Count}");
|
|
}
|
|
}
|
|
@@ -224,12 +234,12 @@ namespace HyperCube.Models
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
- public static async Task<string> GetSHA3(string code)
|
|
|
|
|
|
+ public async Task<string> GetSHA3(string code)
|
|
{
|
|
{
|
|
var hex = bin2hex(code);
|
|
var hex = bin2hex(code);
|
|
var req = $"{{ \"jsonrpc\":\"2.0\",\"method\":\"web3_sha3\",\"params\":[\"{hex}\"], \"id\":1}}";
|
|
var req = $"{{ \"jsonrpc\":\"2.0\",\"method\":\"web3_sha3\",\"params\":[\"{hex}\"], \"id\":1}}";
|
|
|
|
|
|
- var answer = await Post.PostRequestAsync(Blockchain.Dev, req);
|
|
|
|
|
|
+ var answer = await Post.PostRequestAsync(this, req);
|
|
dynamic jsonDe = JsonConvert.DeserializeObject(answer);
|
|
dynamic jsonDe = JsonConvert.DeserializeObject(answer);
|
|
|
|
|
|
Console.WriteLine("result " + jsonDe.result);
|
|
Console.WriteLine("result " + jsonDe.result);
|
|
@@ -248,6 +258,7 @@ namespace HyperCube.Models
|
|
var ret = await RunFunction("eth_getBalance", $"\"{address}\",\"latest\"");
|
|
var ret = await RunFunction("eth_getBalance", $"\"{address}\",\"latest\"");
|
|
return ret;
|
|
return ret;
|
|
}
|
|
}
|
|
|
|
+ //eth.getBalance('0xb504ba124b74333D8536DB534F7fcdC174d6Ee3d')
|
|
|
|
|
|
public async Task<string> GetBalance()
|
|
public async Task<string> GetBalance()
|
|
{
|
|
{
|
|
@@ -270,16 +281,20 @@ namespace HyperCube.Models
|
|
}
|
|
}
|
|
|
|
|
|
public async Task<string> RunFunction2(string name, TransactionObject to)
|
|
public async Task<string> RunFunction2(string name, TransactionObject to)
|
|
- {
|
|
|
|
|
|
+ {
|
|
|
|
+ //транзакция добавления контракта 0x00bdbf1571aa69a2cf8c8c7913a01eaca3a4b7e5e7e49fc787cb81c0df2c2682
|
|
Dictionary<string, string> paramDict = new Dictionary<string, string>();
|
|
Dictionary<string, string> paramDict = new Dictionary<string, string>();
|
|
string parms = "{";
|
|
string parms = "{";
|
|
- if (to.from != "")
|
|
|
|
|
|
+ if (to.from != null)
|
|
paramDict["from"] = to.from;
|
|
paramDict["from"] = to.from;
|
|
//parms += "\"from\":" + to.from;
|
|
//parms += "\"from\":" + to.from;
|
|
- if (to.to != "")
|
|
|
|
|
|
+ if (to.to != null)
|
|
paramDict["to"] = to.to;
|
|
paramDict["to"] = to.to;
|
|
//parms += "\"to\":"+to.to;
|
|
//parms += "\"to\":"+to.to;
|
|
-
|
|
|
|
|
|
+ if (to.data != null)
|
|
|
|
+ {
|
|
|
|
+ paramDict["data"] = "0x"+to.data;
|
|
|
|
+ }
|
|
int count = 0;
|
|
int count = 0;
|
|
foreach (var p in paramDict)
|
|
foreach (var p in paramDict)
|
|
{
|
|
{
|
|
@@ -290,7 +305,9 @@ namespace HyperCube.Models
|
|
parms += ",";
|
|
parms += ",";
|
|
}
|
|
}
|
|
}
|
|
}
|
|
- Console.WriteLine($"RunFunction {name} params {parms}");
|
|
|
|
|
|
+ parms += "}";
|
|
|
|
+ parms += ", \"latest\"";
|
|
|
|
+ Console.WriteLine($"RunFunction {name} params {parms} active bc: {this.address}");
|
|
var req = $"{{ \"jsonrpc\":\"2.0\",\"method\":\"{name}\",\"params\":[{parms}], \"id\":1}}";
|
|
var req = $"{{ \"jsonrpc\":\"2.0\",\"method\":\"{name}\",\"params\":[{parms}], \"id\":1}}";
|
|
|
|
|
|
var answer = await Post.PostRequestAsync(this, req);
|
|
var answer = await Post.PostRequestAsync(this, req);
|
|
@@ -303,7 +320,7 @@ namespace HyperCube.Models
|
|
|
|
|
|
async Task<string> mySHA3(string code)
|
|
async Task<string> mySHA3(string code)
|
|
{
|
|
{
|
|
- var res = await Blockchain.GetSHA3(code);
|
|
|
|
|
|
+ var res = await GetSHA3(code);
|
|
Console.WriteLine($"GetSHA3 {code} {res}");
|
|
Console.WriteLine($"GetSHA3 {code} {res}");
|
|
var ret = res.Substring(2, 8);
|
|
var ret = res.Substring(2, 8);
|
|
return ret;
|
|
return ret;
|
|
@@ -334,24 +351,62 @@ namespace HyperCube.Models
|
|
}
|
|
}
|
|
}
|
|
}
|
|
string function_processed = final + ")";
|
|
string function_processed = final + ")";
|
|
|
|
+ Console.WriteLine($"function_processed {function_processed}");
|
|
string sha3 = await mySHA3(function_processed);
|
|
string sha3 = await mySHA3(function_processed);
|
|
|
|
|
|
return sha3;
|
|
return sha3;
|
|
}
|
|
}
|
|
|
|
|
|
- public async Task<string> ImportERC20(string contractAddress)
|
|
|
|
|
|
+ public static string HextoString(string InputText)
|
|
{
|
|
{
|
|
|
|
+
|
|
|
|
+ byte[] bb = Enumerable.Range(0, InputText.Length)
|
|
|
|
+ .Where(x => x % 2 == 0)
|
|
|
|
+ .Select(x => Convert.ToByte(InputText.Substring(x, 2), 16))
|
|
|
|
+ .ToArray();
|
|
|
|
+ //return System.Text.Encoding.ASCII.GetString(bb);
|
|
|
|
+ // or System.Text.Encoding.UTF7.GetString
|
|
|
|
+ return System.Text.Encoding.UTF8.GetString(bb);
|
|
|
|
+ // or System.Text.Encoding.Unicode.GetString
|
|
|
|
+ // or etc.
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public string ParseStringAnswer(string answer)
|
|
|
|
+ {
|
|
|
|
+ answer = answer.Substring(2, answer.Length - 2);
|
|
|
|
+ int words = answer.Length / 64;
|
|
|
|
+ for (int i = 0; i < words; i++)
|
|
|
|
+ {
|
|
|
|
+ var word = answer.Substring(i * 64, 64);
|
|
|
|
+ Console.WriteLine($"word {i} {word}");
|
|
|
|
+ Console.WriteLine($"str {i} {HextoString(word)}");
|
|
|
|
+ }
|
|
|
|
+ return answer;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public async Task ImportERC20(string contractAddress)
|
|
|
|
+ {
|
|
|
|
+ //var gbh = await GetTByHash("0x00bdbf1571aa69a2cf8c8c7913a01eaca3a4b7e5e7e49fc787cb81c0df2c2682");
|
|
|
|
+ //Console.WriteLine("GetTByHash " + gbh );
|
|
//runfunction balanceOf totalSupply symbol name
|
|
//runfunction balanceOf totalSupply symbol name
|
|
//RunContractRead
|
|
//RunContractRead
|
|
TransactionObject to = new TransactionObject();
|
|
TransactionObject to = new TransactionObject();
|
|
to.to = contractAddress;
|
|
to.to = contractAddress;
|
|
|
|
|
|
//compile data
|
|
//compile data
|
|
- to.data = await compileFunction("name()");
|
|
|
|
|
|
+ //to.data = await compileFunction("decimals");
|
|
|
|
+ to.data = await compileFunction("name");
|
|
var answer = await RunFunction2("eth_call", to);
|
|
var answer = await RunFunction2("eth_call", to);
|
|
- //var answer = await RunFunction("eth_sendTransaction", $"{{\"from\":\"{address}\",\"to\":\"{contractAddress}\",\"gas\":\"{gas}\", \"data\":\"{data}\", \"value\":\"{value}\"}}");
|
|
|
|
- Console.WriteLine("ImportERC20 "+ answer);
|
|
|
|
- return answer;
|
|
|
|
|
|
+ //Console.WriteLine("ImportERC20 len "+ answer.Length + " answer " + answer);
|
|
|
|
+ answer = ParseStringAnswer(answer);
|
|
|
|
+ //to.data = await compileFunction("balanceOf(address)");
|
|
|
|
+ to.data = await compileFunction("symbol");
|
|
|
|
+ answer = await RunFunction2("eth_call", to);
|
|
|
|
+ answer = ParseStringAnswer(answer);
|
|
|
|
+ //Console.WriteLine("ImportERC20 len " + answer.Length + " answer " + answer);
|
|
|
|
+ to.data = await compileFunction("decimals");
|
|
|
|
+ answer = await RunFunction2("eth_call", to);
|
|
|
|
+ answer = ParseStringAnswer(answer);0
|
|
}
|
|
}
|
|
|
|
|
|
public async void RunContractRead()
|
|
public async void RunContractRead()
|
|
@@ -387,6 +442,13 @@ namespace HyperCube.Models
|
|
return answer;
|
|
return answer;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ public async Task<string> GetTByHash(string transactionAddress)
|
|
|
|
+ {
|
|
|
|
+ string req = $"{{\"jsonrpc\":\"2.0\",\"method\":\"eth_getTransactionByHash\",\"params\":[\"{transactionAddress}\"],\"id\":1}}";
|
|
|
|
+ Console.WriteLine("GetTByHash " + req);
|
|
|
|
+ return await Post.PostRequestAsync(this, req);
|
|
|
|
+ }
|
|
|
|
+
|
|
public async Task<string> GetReceipt(string transactionAddress, bool returnAddress = false)
|
|
public async Task<string> GetReceipt(string transactionAddress, bool returnAddress = false)
|
|
{
|
|
{
|
|
Console.WriteLine("transactionAddress " + transactionAddress);
|
|
Console.WriteLine("transactionAddress " + transactionAddress);
|