|
@@ -8,6 +8,9 @@ using Console = HyperCube.Utils.AdvConsole;
|
|
|
using System.Text.RegularExpressions;
|
|
|
|
|
|
//0xe5D682717955d6C35d465A3485625C64655a04f4 - HCB in rinkeby
|
|
|
+//0xb504ba124b74333d8536db534f7fcdc174d6ee3d - system address rinkeby
|
|
|
+//0x413D9500A675d9b645034aC5f4325BF12ddeb7c1 bitcoin
|
|
|
+
|
|
|
|
|
|
namespace HyperCube.Models
|
|
|
{
|
|
@@ -28,6 +31,11 @@ namespace HyperCube.Models
|
|
|
}
|
|
|
set { }
|
|
|
}
|
|
|
+ public static string tokenName;
|
|
|
+ public static string decimals;
|
|
|
+ public static string symbol;
|
|
|
+ public static string tokenBalance;
|
|
|
+
|
|
|
public static Dictionary<int, Blockchain> supported;
|
|
|
public static string Connected
|
|
|
{
|
|
@@ -52,6 +60,7 @@ namespace HyperCube.Models
|
|
|
public string url;
|
|
|
public string address = "";
|
|
|
public string name;
|
|
|
+ public string balance;
|
|
|
static Blockchain instance;
|
|
|
|
|
|
public async static Task RegisterNetworks()
|
|
@@ -96,6 +105,11 @@ namespace HyperCube.Models
|
|
|
return hexValue;
|
|
|
}
|
|
|
|
|
|
+ public static int hex2dec(string hexValue)
|
|
|
+ {
|
|
|
+ return Convert.ToInt32(hexValue, 16);
|
|
|
+ }
|
|
|
+
|
|
|
//public static Blockchain GetInstance()
|
|
|
//{
|
|
|
// if (instance == null)
|
|
@@ -204,13 +218,15 @@ namespace HyperCube.Models
|
|
|
Console.WriteLine("Json string " + answer);
|
|
|
dynamic jsonDe = JsonConvert.DeserializeObject(answer);
|
|
|
var res = (Newtonsoft.Json.Linq.JArray)jsonDe.result;
|
|
|
- if (res.Count > 0)
|
|
|
+ if (res.Count > 0 && id == 0) //Dev
|
|
|
{
|
|
|
address = jsonDe.result[0];
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
- address = await CreateEthAddress("test_Password_212");
|
|
|
+ address = await GetAddress();
|
|
|
+ if (address == "" || address == null)
|
|
|
+ address = await CreateEthAddress("test_Password_212");
|
|
|
}
|
|
|
var query = $"update blockchains set address_main='{address}' where id={id}";
|
|
|
Console.WriteLine("query " + query);
|
|
@@ -249,22 +265,25 @@ namespace HyperCube.Models
|
|
|
public async Task<string> GetBalance(AccountModel account)
|
|
|
{
|
|
|
var addr = await account.GetOrCreateActualAddress(this);
|
|
|
- var ret = await RunFunction("eth_getBalance", $"\"{addr}\",\"latest\"");
|
|
|
- return ret;
|
|
|
+ //var ret = await RunFunction("eth_getBalance", $"\"{addr}\",\"latest\"");
|
|
|
+ Console.WriteLine($"GetBalance AccountModel addr {addr}");
|
|
|
+ return await GetBalance(addr);
|
|
|
}
|
|
|
|
|
|
- public async Task<string> GetBalance(string address)
|
|
|
+ public async Task<string> GetBalance()
|
|
|
{
|
|
|
- var ret = await RunFunction("eth_getBalance", $"\"{address}\",\"latest\"");
|
|
|
- return ret;
|
|
|
+ return await GetBalance(address);
|
|
|
}
|
|
|
//eth.getBalance('0xb504ba124b74333D8536DB534F7fcdC174d6Ee3d')
|
|
|
|
|
|
- public async Task<string> GetBalance()
|
|
|
+ public async Task<string> GetBalance(string address)
|
|
|
{
|
|
|
var ret = await RunFunction("eth_getBalance", $"\"{address}\",\"latest\"");
|
|
|
- var balanceInt = AccountModel.ConvertBalance(ret);
|
|
|
- return balanceInt.ToString();
|
|
|
+ var balanceWei = AccountModel.ConvertBalance(ret);
|
|
|
+ var balanceToken = (double)balanceWei / 1000000000000000000.0;
|
|
|
+ Console.WriteLine($"GetBalance balanceToken {balanceToken}");
|
|
|
+ balance = balanceToken.ToString();
|
|
|
+ return balance;
|
|
|
}
|
|
|
|
|
|
public async Task<string> RunFunction(string name, string parms)
|
|
@@ -280,7 +299,7 @@ namespace HyperCube.Models
|
|
|
//return "test answer";
|
|
|
}
|
|
|
|
|
|
- public async Task<string> RunFunction2(string name, TransactionObject to)
|
|
|
+ public async Task<string> RunFunction2(string name, TransactionObject to, string address = "")
|
|
|
{
|
|
|
//транзакция добавления контракта 0x00bdbf1571aa69a2cf8c8c7913a01eaca3a4b7e5e7e49fc787cb81c0df2c2682
|
|
|
Dictionary<string, string> paramDict = new Dictionary<string, string>();
|
|
@@ -294,6 +313,8 @@ namespace HyperCube.Models
|
|
|
if (to.data != null)
|
|
|
{
|
|
|
paramDict["data"] = "0x"+to.data;
|
|
|
+ if (address != "")
|
|
|
+ paramDict["data"] += zerofill(address, 64, true);
|
|
|
}
|
|
|
int count = 0;
|
|
|
foreach (var p in paramDict)
|
|
@@ -357,6 +378,13 @@ namespace HyperCube.Models
|
|
|
return sha3;
|
|
|
}
|
|
|
|
|
|
+ //public double UInt256Conv(string input)
|
|
|
+ //{
|
|
|
+ // System.Numerics.BigInteger bigint1 = System.Numerics.BigInteger.Parse("329974300448100608374211110737048701521");
|
|
|
+ // System.Numerics.BigInteger bigint2 = new System.Numerics.BigInteger(18);
|
|
|
+ // return bigint1 / 18.0;
|
|
|
+ //}
|
|
|
+
|
|
|
public static string HextoString(string InputText)
|
|
|
{
|
|
|
|
|
@@ -371,17 +399,26 @@ namespace HyperCube.Models
|
|
|
// or etc.
|
|
|
}
|
|
|
|
|
|
- public string ParseStringAnswer(string answer)
|
|
|
+ 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 ret = new string[] { };
|
|
|
+ if (answer.Length > 2)
|
|
|
{
|
|
|
- var word = answer.Substring(i * 64, 64);
|
|
|
- Console.WriteLine($"word {i} {word}");
|
|
|
- Console.WriteLine($"str {i} {HextoString(word)}");
|
|
|
+ answer = answer.Substring(2, answer.Length - 2);
|
|
|
+ int words = answer.Length / 64;
|
|
|
+ ret = new string[words];
|
|
|
+ for (int i = 0; i < words; i++)
|
|
|
+ {
|
|
|
+ var word = answer.Substring(i * 64, 64);
|
|
|
+ ret[i] = word;
|
|
|
+ Console.WriteLine($"word {i} {word}");
|
|
|
+ Console.WriteLine($"str {i} {HextoString(word)}");
|
|
|
+ var testbalance1 = (double)(AccountModel.ConvertBalance(word)/ 1000000000000000000);
|
|
|
+ var testbalance2 = ((double)AccountModel.ConvertBalance(word) / 1000000000000000000.0);
|
|
|
+ Console.WriteLine($"testbalance1 {testbalance1} testbalance2 {testbalance2} ");
|
|
|
+ }
|
|
|
}
|
|
|
- return answer;
|
|
|
+ return ret;
|
|
|
}
|
|
|
|
|
|
public async Task ImportERC20(string contractAddress)
|
|
@@ -398,15 +435,25 @@ namespace HyperCube.Models
|
|
|
to.data = await compileFunction("name");
|
|
|
var answer = await RunFunction2("eth_call", to);
|
|
|
//Console.WriteLine("ImportERC20 len "+ answer.Length + " answer " + answer);
|
|
|
- answer = ParseStringAnswer(answer);
|
|
|
+ var parsed = ParseStringAnswer(answer);
|
|
|
+ int tokenlen = hex2dec(parsed[1]);
|
|
|
+ tokenName = HextoString(parsed[2]).Substring(0,tokenlen);
|
|
|
//to.data = await compileFunction("balanceOf(address)");
|
|
|
to.data = await compileFunction("symbol");
|
|
|
answer = await RunFunction2("eth_call", to);
|
|
|
- answer = ParseStringAnswer(answer);
|
|
|
+ parsed = ParseStringAnswer(answer);
|
|
|
+ int symlen = hex2dec(parsed[1]);
|
|
|
+ symbol = HextoString(parsed[2]).Substring(0, symlen);
|
|
|
//Console.WriteLine("ImportERC20 len " + answer.Length + " answer " + answer);
|
|
|
to.data = await compileFunction("decimals");
|
|
|
answer = await RunFunction2("eth_call", to);
|
|
|
- answer = ParseStringAnswer(answer);
|
|
|
+ decimals = hex2dec(ParseStringAnswer(answer)[0]).ToString();
|
|
|
+
|
|
|
+ to.data = await compileFunction($"function balanceOf(address account)");
|
|
|
+ //answer = await RunFunction2("eth_call", to, AccountModel.Current.GetActualAddress(this));
|
|
|
+ answer = await RunFunction2("eth_call", to, "0xb504ba124b74333D8536DB534F7fcdC174d6Ee3d");
|
|
|
+ tokenBalance = ((double)AccountModel.ConvertBalance(ParseStringAnswer(answer)[0]) / 1000000000000000000.0).ToString();
|
|
|
+
|
|
|
}
|
|
|
|
|
|
public async void RunContractRead()
|
|
@@ -520,9 +567,12 @@ namespace HyperCube.Models
|
|
|
return ret;
|
|
|
}
|
|
|
|
|
|
- public string GetAddress()
|
|
|
+ public async Task<string> GetAddress()
|
|
|
{
|
|
|
- //this.address = address;
|
|
|
+ if (address == "")
|
|
|
+ {
|
|
|
+ address = await MySQLConnector.Instance().SQLSelectUUID("select address_main from blockchains where id="+id);
|
|
|
+ }
|
|
|
return address;
|
|
|
//post запрос
|
|
|
}
|