|
@@ -57,6 +57,13 @@ namespace HyperCube.Models
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ public static Blockchain Find(int id)
|
|
|
+ {
|
|
|
+ if (loaded.ContainsKey(id))
|
|
|
+ return loaded[id];
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
public async Task<string> Initialize()
|
|
|
{
|
|
|
if (!loaded.ContainsKey(id))
|
|
@@ -70,6 +77,14 @@ namespace HyperCube.Models
|
|
|
return $"{name} {url}:{port}";
|
|
|
}
|
|
|
|
|
|
+ public static string dec2hex(int decValue, bool prefix = true)
|
|
|
+ {
|
|
|
+ string hexValue = decValue.ToString("X");
|
|
|
+ if (prefix)
|
|
|
+ hexValue = "0x"+hexValue;
|
|
|
+ return hexValue;
|
|
|
+ }
|
|
|
+
|
|
|
//public static Blockchain GetInstance()
|
|
|
//{
|
|
|
// if (instance == null)
|
|
@@ -97,8 +112,12 @@ namespace HyperCube.Models
|
|
|
{
|
|
|
Console.WriteLine($"CreateBlockchainAccount {account}: {res}");
|
|
|
//query = $"update aspnetusers set eth_address='{res}' where UserName='{account.Name}'";
|
|
|
- query = $"update account_wallets set uuid='{res}' where account_uuid='{account.UUID}'";
|
|
|
- account.eth_address = res;
|
|
|
+ var addr = account.GetActualAddress(this);
|
|
|
+ if (addr == null)
|
|
|
+ query = $"insert into account_wallets (uuid, blockchain_id, account_uuid, password_plain) values ('{res}',{id},'{account.UUID}','{account.PWDHash}')";
|
|
|
+ else
|
|
|
+ query = $"update account_wallets set uuid='{res}', password_plain='{account.PWDHash}' where account_uuid='{account.UUID}' and blockchain_id={id}";
|
|
|
+ account.SetActualAddress(res, this);
|
|
|
Console.WriteLine($"CreateBlockchainAccount query {query}");
|
|
|
MySQLConnector.Instance().SQLInsert(query);
|
|
|
}
|
|
@@ -220,7 +239,7 @@ namespace HyperCube.Models
|
|
|
|
|
|
public async Task<string> GetBalance(AccountModel account)
|
|
|
{
|
|
|
- var addr = id == 0 ? account.eth_address : account.eth_address1;
|
|
|
+ var addr = await account.GetOrCreateActualAddress(this);
|
|
|
var ret = await RunFunction("eth_getBalance", $"\"{addr}\",\"latest\"");
|
|
|
return ret;
|
|
|
}
|
|
@@ -249,18 +268,20 @@ namespace HyperCube.Models
|
|
|
address = answer;
|
|
|
}
|
|
|
|
|
|
- public async Task<string> SendTransaction(string fromAddress, string toAddress, string sum)
|
|
|
+ public async Task<string> SendTransaction(string fromAddress, string toAddress, int wei)
|
|
|
{
|
|
|
+ var sum = dec2hex(wei);
|
|
|
+ var gas = await GetEstimatedGasTransaction(wei);
|
|
|
Console.WriteLine($"SendTransaction from {fromAddress} to {toAddress} sum {sum}");
|
|
|
- var answer = await RunFunction("eth_sendTransaction", $"{{\"from\":\"{address}\",\"to\":\"{toAddress}\",\"gas\":\"0x31b2ef\", \"data\":\"\", \"value\":\"{sum}\"}}");
|
|
|
+ var answer = await RunFunction("eth_sendTransaction", $"{{\"from\":\"{address}\",\"to\":\"{toAddress}\",\"gas\":\"{gas}\", \"data\":\"\", \"value\":\"{sum}\"}}");
|
|
|
return answer;
|
|
|
}
|
|
|
|
|
|
//{"jsonrpc":"2.0","method":"eth_sendTransaction","params":[{ "from":"0xD81eeE6b39d9556c3067A3551A3FB2882b92F327", "to":"0x119b58faddcdbc09cafcd272530aa079cec10004", "gas":"0x31b2ef", "data":"0x11111111"}], "id":1}
|
|
|
- public async Task<string> RunContractWrite(string contractAddress, string data)
|
|
|
+ public async Task<string> RunContractWrite(string contractAddress, string data, string gas, string value)
|
|
|
{
|
|
|
Console.WriteLine("RunContract contractAddress " + contractAddress);
|
|
|
- var answer = await RunFunction("eth_sendTransaction", $"{{\"from\":\"{address}\",\"to\":\"{contractAddress}\",\"gas\":\"0x31b2ef\", \"data\":\"{data}\", \"value\":\"0x32555\"}}");
|
|
|
+ var answer = await RunFunction("eth_sendTransaction", $"{{\"from\":\"{address}\",\"to\":\"{contractAddress}\",\"gas\":\"{gas}\", \"data\":\"{data}\", \"value\":\"{value}\"}}");
|
|
|
//$"{{ \"jsonrpc\":\"2.0\",\"method\":\"eth_sendTransaction\",\"params\":[{{\"from\":\"{address}\",\"to\":\"{contractAddress}\",\"gas\":\"0x31b2ef\", \"data\":\"{data}\"}}], \"id\":1}}";
|
|
|
//var answer = await Post.PostRequestAsync(req);
|
|
|
//dynamic jsonDe = JsonConvert.DeserializeObject(answer);
|
|
@@ -277,22 +298,31 @@ namespace HyperCube.Models
|
|
|
var answer = await Post.PostRequestAsync(this, req);
|
|
|
|
|
|
dynamic jsonDe = JsonConvert.DeserializeObject(answer);
|
|
|
- var blockHash = jsonDe.result.blockHash;
|
|
|
- var blockNumber = jsonDe.result.blockNumber;
|
|
|
- var contractAddress = jsonDe.result.contractAddress;
|
|
|
- MySQLConnector.Instance().SQLInsert($"insert into transactions (result, name) values ('{Convert.ToString(jsonDe.result)}', 'eth_getTransactionReceipt')");
|
|
|
- Console.WriteLine("result " + answer);
|
|
|
- if (returnAddress)
|
|
|
- return contractAddress;
|
|
|
+ if (jsonDe.result != null)
|
|
|
+ {
|
|
|
+ var blockHash = jsonDe.result.blockHash;
|
|
|
+ var blockNumber = jsonDe.result.blockNumber;
|
|
|
+ var contractAddress = jsonDe.result.contractAddress;
|
|
|
+ MySQLConnector.Instance().SQLInsert($"insert into transactions (result, name) values ('{Convert.ToString(jsonDe.result)}', 'eth_getTransactionReceipt')");
|
|
|
+ Console.WriteLine("result " + answer);
|
|
|
+ if (returnAddress)
|
|
|
+ return contractAddress;
|
|
|
+ else
|
|
|
+ return Convert.ToString(jsonDe.result);
|
|
|
+ }
|
|
|
else
|
|
|
- return Convert.ToString(jsonDe.result);
|
|
|
+ {
|
|
|
+ Console.WriteLine("eth_getTransactionReceipt reuslt NULL " + transactionAddress);
|
|
|
+ return null;
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
//TODO add GAS!!!
|
|
|
public async Task<object[]> AddContract(string name, string code, string bytecode)
|
|
|
{
|
|
|
Console.WriteLine("bytecode " + bytecode);
|
|
|
- var req = $"{{ \"jsonrpc\":\"2.0\",\"method\":\"eth_sendTransaction\",\"params\":[{{\"from\":\"{address}\",\"gas\":\"0x31b2e\", \"data\":\"{bytecode}\"}}], \"id\":1}}";
|
|
|
+ var gas = await GetEstimatedGasContractAdd(bytecode);
|
|
|
+ var req = $"{{ \"jsonrpc\":\"2.0\",\"method\":\"eth_sendTransaction\",\"params\":[{{\"from\":\"{address}\",\"gas\":\"{gas}\", \"data\":\"{bytecode}\"}}], \"id\":1}}";
|
|
|
|
|
|
var answer = await Post.PostRequestAsync(this, req);
|
|
|
dynamic jsonDe = JsonConvert.DeserializeObject(answer);
|
|
@@ -300,8 +330,8 @@ namespace HyperCube.Models
|
|
|
Console.WriteLine("result AddContract transactionAddress: " + res);
|
|
|
if (res != null)
|
|
|
{
|
|
|
- SmartContract newctr = new SmartContract(name, code, bytecode);
|
|
|
- var gas = await GetEstimatedGas(newctr);
|
|
|
+ SmartContract newctr = new SmartContract(name, code, bytecode, this.id, gas);
|
|
|
+
|
|
|
if (gas == null)
|
|
|
gas = "invalid";
|
|
|
int id = (int)MySQLConnector.Instance().SQLInsert($"insert into smart_contracts (code, bytecode, name, date_add, gas_cost, blockchain_id) values ('{code}','{bytecode}','{name}',NOW(), '{gas}', {this.id})");
|
|
@@ -313,17 +343,24 @@ namespace HyperCube.Models
|
|
|
return null;
|
|
|
}
|
|
|
|
|
|
- public async Task<string> GetEstimatedGas(SmartContract contract)
|
|
|
+ public async Task<string> GetEstimatedGasTransaction(int weiAmount)
|
|
|
{
|
|
|
- if (contract != null)
|
|
|
+ if (weiAmount > 0)
|
|
|
{
|
|
|
- var ret = await RunFunction("eth_estimateGas", $"{{\"from\":\"{address}\",\"data\":\"{contract.ByteCode}\"}}");
|
|
|
+ var ret = await RunFunction("eth_estimateGas", $"{{\"from\":\"{address}\",\"value\":\"{dec2hex(weiAmount)}\"}}");
|
|
|
Console.WriteLine("GetEstimatedGas " + ret);
|
|
|
return ret;
|
|
|
}
|
|
|
return null;
|
|
|
}
|
|
|
|
|
|
+ public async Task<string> GetEstimatedGasContractAdd(string bytecode)
|
|
|
+ {
|
|
|
+ var ret = await RunFunction("eth_estimateGas", $"{{\"from\":\"{address}\",\"data\":\"{bytecode}\"}}");
|
|
|
+ Console.WriteLine("GetEstimatedGasContractAdd " + ret);
|
|
|
+ return ret;
|
|
|
+ }
|
|
|
+
|
|
|
public string GetAddress()
|
|
|
{
|
|
|
//this.address = address;
|