@page "/blockchains"
@using System.Text.RegularExpressions;
@using Console = HyperCube.Utils.AdvConsole;
@namespace HyperCube.Models
@attribute [Authorize]
Blockchain
@code
{
string hidecontr = "visible";
string hidereceipt = "hidden";
string result = "";
string newcode = "";
string newname = "";
string balance = "";
string sha3 = "";
string function1 = "";
string function_processed = "";
string contractAddDisabled = "visible";
SmartContract ctrSelected = new SmartContract();
int ct;
//ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff fff7
string accountSelected;
int contrtest
{
get
{
return ct;
}
set
{
ct = value;
Console.WriteLine("ch contrtest val " + value);
var ctr = GetContract(value);
if (ctr != null)
{
Console.WriteLine("ctr found " + ctr.Name);
ctrSelected = ctr;
}
}
}
}
@if (Blockchain.newData)
{
@foreach (var bc in Blockchain.loaded.Values)
{
@bc.url:@bc.port
, address: @bc.address
}
}
Ethereum dev network
Add smart contract:
@**@
@*
0x60806040526000805534801561001457600080fd5b50610101806100246000396000f3006080604052600436106053576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff1680635b34b966146058578063a87d942c14606c578063f5c5ad83146094575b600080fd5b348015606357600080fd5b50606a60a8565b005b348015607757600080fd5b50607e60ba565b6040518082815260200191505060405180910390f35b348015609f57600080fd5b5060a660c3565b005b60016000808282540192505081905550565b60008054905090565b600160008082825403925050819055505600a165627a7a723058207815571f5c1d82bff3c0512a4cbc7a1f1c2063fa9a0de61913b76925f041e9ed0029
*@
@*
Get smart contract address:
*@
@ctrSelected.Code
@ctrSelected.Name
@**@
Balance: @balance
Result: @result
@code
{
private Random r = new Random();
private string bcURL = "127.0.0.1";
private int bcport = 8545;
private string bytecode = "";
private string error = "";
string lastTransAddr = "";
string contractAddress = "";
int focus = 0;
//Verify
//function verify( address payable verifier, address payable initiator, uint8 article_value, uint8 verify_complexity)
//0x608060405234801561001057600080fd5b50610190806100206000396000f3fe60806040526004361061001e5760003560e01c8063619837de14610023575b600080fd5b61009f6004803603608081101561003957600080fd5b81019080803573ffffffffffffffffffffffffffffffffffffffff169060200190929190803573ffffffffffffffffffffffffffffffffffffffff169060200190929190803560ff169060200190929190803560ff1690602001909291905050506100a1565b005b600060058302820160ff1690508063ffffffff164710610153578473ffffffffffffffffffffffffffffffffffffffff166108fc8360ff169081150290604051600060405180830381858888f19350505050158015610104573d6000803e3d6000fd5b508373ffffffffffffffffffffffffffffffffffffffff166108fc6005850260ff169081150290604051600060405180830381858888f19350505050158015610151573d6000803e3d6000fd5b505b505050505056fea2646970667358221220847188fccb79ed4087ece85f7a8d9c3eaf187342166d7a66131b00a13bb0577b64736f6c63430007040033
async Task deleteContract()
{
var main = Blockchain.GetMain();
if (main != null)
{
main.contracts.Remove(ctrSelected.ID);
main.contractNames.Remove(ctrSelected.Name);
MySQLConnector.Instance().SQLInsert($"delete from smart_contracts where id ={ctrSelected.ID}");
MySQLConnector.Instance().SQLInsert($"delete from functions where contract_id ={ctrSelected.ID}");
ctrSelected = null;
}
}
async Task checkCode()
{
//string pattern = @"function\s+(\S+)\s*\((.*)\)\s*{.*}";
string pattern = @"function\s+(\S+)\s*\((.*)\).*";
string replacement_name = "$1";
string replacement_params = "$2";
string input = function1;
string resultname = Regex.Replace(input, pattern, replacement_name).Trim();
string param_pattern = @"(uint8|address)";
var final = resultname + "(";
if (input != resultname)
{
//function_processed += "name " + resultname + " ";
string resultparams = Regex.Replace(input, pattern, replacement_params).Trim();
if (input != resultparams)
{
//function_processed += "params " + resultparams+" matches: ";
int i = 0;
foreach (Match match in Regex.Matches(resultparams, param_pattern))
{
if (i > 0)
final += "," + match.Value;
else
final += match.Value;
i++;
}
}
}
function_processed = final + ")";
sha3 = await GetSHA3(function_processed);
if (function1 != "()" && function1 != "")
contractAddDisabled = "";
}
async Task GetSHA3(string code)
{
var res = await Blockchain.GetSHA3(code);
Console.WriteLine($"GetSHA3 {code} {res}");
var ret = res.Substring(2, 8);
return ret;
}
async Task GetSHA3_2()
{
var res = await Blockchain.GetSHA3(sha3);
Console.WriteLine($"GetSHA3 {res}");
var ret = res.Substring(2, 8);
return ret;
}
protected async Task TransferContract()
{
var ttt = "123";
Console.WriteLine($"len {ttt.Length}");
try
{
var myacc = AccountModel.Current;
Console.WriteLine($"TransferContract run");
Console.WriteLine($"TransferContract {myacc.Name}");
var recipient = AccountModel.Find(accountSelected);
if (recipient != null)
{
//var b2h = Blockchain.bin2hex("getCount()");
//Console.WriteLine("bin2hex " + b2h);
var part1 = await Blockchain.GetSHA3("myTransfer(address)");
//var part2 = await Blockchain.GetSHA3(recipient.eth_address);
var zerofill = Blockchain.zerofill(part1.Substring(0, 10), 32, true);
//var zerofill2 = Blockchain.zerofill(part2, 32, true);
var paramstring = "0x" + zerofill + recipient.eth_address.Remove(0, 2);
Console.WriteLine($"paramstring {paramstring}");
//todo pack myTransfer(address)
//todo pack contr.Address
if (Blockchain.GetMain().contractNames.ContainsKey("Transfer"))
{
var contr = Blockchain.GetMain().contractNames["Transfer"];
var res = await Blockchain.GetMain().RunContractWrite(contr.Address, $"{paramstring}");
Console.WriteLine("TransferContract result " + res);
var receipt = await Blockchain.GetMain().GetReceipt(res);
Console.WriteLine("TransferContract receipt " + receipt);
}
}
//TODO run smart contract with verifier wallet
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
}
private async Task Transfer50()
{
var acc = AccountModel.Find(accountSelected);
var res = await Blockchain.GetMain().SendTransaction(Blockchain.GetMain().address, acc.eth_address, "0x32");
}
private async Task GetBalance()
{
if (accountSelected != "")
{
var acc = AccountModel.Find(accountSelected);
if (acc != null)
{
var res = await acc.GetBalance();
var balanceInt = AccountModel.ConvertBalance(res);
balance = balanceInt.ToString();
}
}
}
private async Task CreateBlockchainAccount()
{
AccountModel account = AccountModel.Find(accountSelected);
if (account != null)
{
var res = await Blockchain.GetMain().CreateBlockchainAccount(account);
}
}
private async Task ChangeContract()
{
Console.WriteLine("ChangeContract " + contrtest);
//TODO result as transaction addr
//result = await Blockchain.loaded[0].GetReceipt(result);
}
private async Task RunContract()
{
//TODO result as transaction addr
}
private async Task AddContract()
{
if (Blockchain.loaded.Count > 0)
{
var taddr = await Blockchain.GetMain().AddContract(newname, newcode, bytecode);
if (taddr.Length > 0 && taddr[0] != "")
{
result = taddr[0].ToString();
//hidereceipt = "visible";
var caddr = await Blockchain.GetMain().GetReceipt(result, true);
if (caddr != "")
{
result = "contractAddress: " + caddr;
contractAddress = caddr;
//var id = (int) taddr[1];
var smc = taddr[1] as SmartContract;//Blockchain.GetMain().contracts[id];
smc.Address = contractAddress;
MySQLConnector.Instance().SQLInsert($"update smart_contracts set address='{contractAddress}' where id= {smc.ID}");
//var run = await Blockchain.GetMain().RunContractWrite(contractAddress, "0x11111111");
//result = "run: " + run;
smc.AddFunction(function_processed, sha3);
}
}
}
//private Post post = new Post();
}
SmartContract GetContract(int id)
{
if (Blockchain.GetMain().contracts.ContainsKey(id))
return Blockchain.GetMain().contracts[id];
else
return null;
}
private async Task GetBlockChain()
{
//if (Blockchain.loaded.Count == 0)
//{
Blockchain bc = new HyperCube.Models.Blockchain(bcURL, bcport);
result = await bc.Initialize();
if (result != "")
hidecontr = "visible";
//bcURL = bc.GetAddress();
//bc.ExecuteContract();
//bcURL = bc.address;
//hidecontr = "hidden";
//}
//private Post post = new Post();
}
}