ソースを参照

промежуточная версия

Rimmon 2 年 前
コミット
3d84a6ff0d
2 ファイル変更99 行追加30 行削除
  1. 62 0
      Models/Blockchain.cs
  2. 37 30
      Pages/ModalAssets.razor

+ 62 - 0
Models/Blockchain.cs

@@ -155,6 +155,68 @@ namespace HyperCube.Models
             var answer = await RunFunction2("eth_sendTransaction", transObj);
         }
 
+        public async Task Approve(string spender, string amount, string tokenAddress)
+        {
+            TransactionObject transObj = new TransactionObject();
+            transObj.from = spender;
+            transObj.to = tokenAddress;// "0xe5D682717955d6C35d465A3485625C64655a04f4";
+            transObj.gas = "9000";
+            transObj.value = "0";
+            transObj.data = await compileFunction($"function approve(address spender, uint256 amount)");
+            if (tokenAddress != null)
+            {
+                transObj.data += zerofill(tokenAddress, 64, true);
+                transObj.data += zerofill(spender, 64, true);
+                transObj.data += zerofill(dec2hex(Convert.ToInt32(amount)), 64, true);                
+            }
+            var answer = await RunFunction2("eth_sendTransaction", transObj);
+        }
+
+        public async Task SubmitTransaction(string owner, string tokenvalue, string receiver, string tokenAddress = "0xe5D682717955d6C35d465A3485625C64655a04f4")
+        {
+            //"submitTransaction(address destination, uint value, address tokenAddress)"
+            var tokenAmount = float.Parse(tokenvalue, System.Globalization.CultureInfo.InvariantCulture);// * 1000000000000000000;
+            
+            //ETHtoERC20(address tokenAddress)
+            //ERC20toETH(address tokenAddress, address sender, uint256 tokenAmount, uint256 ethAmount) public payable returns(bool)
+            TransactionObject transObj = new TransactionObject();
+            transObj.from = owner;
+            transObj.to = "0x862A2Ac8D1dB37085c786981A8F5bd54594f7162";// "0xe5D682717955d6C35d465A3485625C64655a04f4";
+            transObj.gas = "9000";
+            transObj.value = "0";
+            transObj.data = await compileFunction($"submitTransaction(address destination, uint value, address tokenAddress)");
+            if (tokenvalue != null)
+            {
+                transObj.data += zerofill(receiver, 64, true);
+                transObj.data += zerofill(dec2hex(Convert.ToInt32(tokenAmount)), 64, true);
+                transObj.data += zerofill(tokenAddress, 64, true);
+            }
+            var answer = await RunFunction2("eth_sendTransaction", transObj);
+        }
+
+        public async Task ChangeCurrency(string from_token_selected, string to_token_selected, string tokenAddress, string sender, string from, string to, string fi, string ti)
+        {
+            var val = float.Parse(fi, System.Globalization.CultureInfo.InvariantCulture);
+            var tokenAmount = val;// * 1000000000000000000;
+            val = float.Parse(ti, System.Globalization.CultureInfo.InvariantCulture);
+            var ethAmount = val / 5;   // /5
+            //ETHtoERC20(address tokenAddress)
+            //ERC20toETH(address tokenAddress, address sender, uint256 tokenAmount, uint256 ethAmount) public payable returns(bool)
+            TransactionObject transObj = new TransactionObject();
+            transObj.from = sender;
+            transObj.to = "0x862A2Ac8D1dB37085c786981A8F5bd54594f7162";// "0xe5D682717955d6C35d465A3485625C64655a04f4";
+            transObj.gas = "9000";
+            transObj.value = "0";
+            transObj.data = await compileFunction($"function ERC20toETH(address tokenAddress, address sender, uint256 tokenAmount, uint256 ethAmount)");
+            if (tokenAddress != null && tokenAmount != null)
+            {
+                transObj.data += zerofill(tokenAddress, 64, true);
+                transObj.data += zerofill(sender, 64, true);
+                transObj.data += zerofill(dec2hex(Convert.ToInt32(tokenAmount)), 64, true);
+                transObj.data += zerofill(dec2hex(Convert.ToInt32(ethAmount)), 64, true);
+            }
+            var answer = await RunFunction2("eth_sendTransaction", transObj);
+        }
 
         public async Task<string> Verify(AccountModel verifier, ArticleModel article)
         {

+ 37 - 30
Pages/ModalAssets.razor

@@ -186,41 +186,48 @@
 
         @using Models;Blockchain bcMain;
 
-        public async Task Balances()
+    public async Task Balances()
+    {
+        ethBalance = await _account.GetBalance();
+        var bc = await _account.GetSelectedBlockChain();
+        bc_selected = bc.name;
+        if (bc.port == 8666)
+            tokenBalance = await bc.BalanceOf("0xe5D682717955d6C35d465A3485625C64655a04f4", _account.GetActualAddress(bc));
+        else
+            tokenBalance = "0";
+    }
+
+    public async Task Open(Models.AccountModel account)
+    {
+        _account = account;
+        await Balances();
+        await JsRuntime.InvokeVoidAsync("OpenModal", elementid);
+    }
+
+    public async Task Ok()
+    {
+        bool confirmed = await JsRuntime.InvokeAsync<bool>("confirm", "Are you sure?");
+        if (confirmed)
         {
-            ethBalance = await _account.GetBalance();
+            Console.WriteLine("true");
             var bc = await _account.GetSelectedBlockChain();
-            bc_selected = bc.name;
-            if (bc.port == 8666)
-                tokenBalance = await bc.BalanceOf("0xe5D682717955d6C35d465A3485625C64655a04f4", _account.GetActualAddress(bc));
-            else
-                tokenBalance = "0";
-        }
+            //сначала нужен аппрув перевода!
+            //from=erc20 to=eth
+            var amount = float.Parse(fi, System.Globalization.CultureInfo.InvariantCulture);
+            await bc.Approve(_account.GetActualAddress(bc), fi, "0xe5D682717955d6C35d465A3485625C64655a04f4");
 
-        public async Task Open(Models.AccountModel account)
-        {
-            _account = account;
-            await Balances();
-            await JsRuntime.InvokeVoidAsync("OpenModal", elementid);
+            await bc.ChangeCurrency(from_token_selected, to_token_selected, "0xe5D682717955d6C35d465A3485625C64655a04f4", _account.GetActualAddress(bc), chosenBalanceFrom, chosenBalanceTo, fi, ti);
+            //run change - recalculate balances
         }
-
-        public async Task Ok()
+        else
         {
-            bool confirmed = await JsRuntime.InvokeAsync<bool>("confirm", "Are you sure?");
-            if (confirmed)
-            {
-                Console.WriteLine("true");
-                //run change - recalculate balances
-            }
-            else
-            {
-                Console.WriteLine("false");
-                //cancel
-            }
+            Console.WriteLine("false");
+            //cancel
         }
+    }
 
-        public void Close()
-        {
-            JsRuntime.InvokeVoidAsync("CloseModal", elementid);
-        }
+    public void Close()
+    {
+        JsRuntime.InvokeVoidAsync("CloseModal", elementid);
+    }
     }