123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Threading.Tasks;
- using System.Numerics;
- using Console = HyperCube.Utils.AdvConsole;
- namespace HyperCube.Models
- {
- public class SmartContract
- {
- public int ID;
- public string Code;
- public string ByteCode;
- public string Name = "";
- public string Address = "";
- public int functionId = 0;
- public string functionName = "";
- public string functionCompiledHeader = "";
- public static SmartContract Find(string name)
- {
- var bc = Blockchain.GetMain();
- if (bc != null)
- {
- var contracts = bc.contractNames;
- if (contracts.ContainsKey(name))
- {
- return contracts[name];
- }
- }
- return null;
- }
- public SmartContract()
- { }
- public Task Run(string weiValue = "0x0")
- {
- return null;
- }
- public SmartContract(int id, string name, string code, string bytecode)
- {
- ID = id;
- Name = name;
- Code = code;
- ByteCode = bytecode;
- }
- public async Task LoadFunction()
- {
- if (functionId == 0)
- {
- var ret = await MySQLConnector.Instance().SQLSelectComplex($"select * from functions where contract_id={ID} limit 1");
- if (ret.Count > 0)
- {
- functionId = Convert.ToInt32(ret[0]["id"]);
- functionName = ret[0]["name_with_args"].ToString();
- functionCompiledHeader = ret[0]["compiled_header"].ToString();
- Console.WriteLine($"{ID} LoadFunction {functionId} name {functionName} header {functionCompiledHeader}");
- }
- }
- }
- public async Task<long> AddFunction(string name_with_args, string compiled_header)
- {
- return MySQLConnector.Instance().SQLInsert($"insert into functions (name_with_args, contract_id, compiled_header) values ('{name_with_args}',{ID},'{compiled_header}')");
- }
- }
- public class VerifyContract : SmartContract
- {
- public VerifyContract() : base() { }
- public VerifyContract(int id, string name, string code, string bytecode) : base(id, name, code, bytecode) { }
- /// <summary>
- ///
- /// </summary>
- /// <param name="newArticle"></param>
- /// <param name="weiValue"></param>
- /// <returns>ID транзакции</returns>
- public async Task<string> Run(ArticleModel newArticle, string weiValue = "0x0")
- {
- var uuid = await newArticle.GetInitiatorUUID();
- var verifier_address = AccountModel.Current.eth_address;
- var initiator = AccountModel.Find(uuid);
- var initiator_address = initiator.eth_address;
- var article_value = (int)newArticle.Rating;
-
- var article_value_hex = article_value.ToString("X64");
- //Console.WriteLine($"article_value {article_value} hex {article_value_hex}");
- var edits = await newArticle.GetEditsCount();
- var edits_hex = edits.ToString("X64");
- verifier_address = Blockchain.zerofill(verifier_address, 64, true);
- initiator_address = Blockchain.zerofill(initiator_address, 64, true);
- Console.WriteLine($"VerifyContract params article_value {article_value} edits {edits} verifier {AccountModel.Current.Name} verifier_address: {verifier_address} initiator {initiator.Name} initiator_address: {initiator_address}");
- //function verify( address payable verifier, address payable initiator, int article_value, int verify_complexity) public returns (uint vReward, uint iReward)
- var paramstring = "0x" + functionCompiledHeader + verifier_address + initiator_address + article_value_hex + edits_hex;
- Console.WriteLine($"VerifyContract Run {initiator.Name} params {paramstring}");
- var res = await Blockchain.GetMain().RunContractWrite(Address, $"{paramstring}");
- Console.WriteLine($"VerifyContract transactionId res {res}");
- var receipt = await Blockchain.GetMain().GetReceipt(res);
- Console.WriteLine("VerifyContract receipt " + receipt);
- return res;
- }
- }
- }
|