ModalAssets.razor 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. @using Microsoft.AspNetCore.Components.Web
  2. <div class="modal" tabindex="-1" role="dialog" style="display:none" id="@elementid">
  3. <div class="modal-container">
  4. <div class="modal__body">
  5. <div class="modal__body__header">
  6. <div class="modal__body__header__info">
  7. <img src="img/packet.svg" alt="">
  8. <p>Цифровые активы</p>
  9. </div>
  10. @*<div class="modal__body__header__href">
  11. <a href="">Balans</a>
  12. <a href="">Swap</a>
  13. <a href="">Liquidity</a>
  14. </div>*@
  15. </div>
  16. <div class="modal__body__container">
  17. <div class="modal__body__container__left">
  18. <div class="modal__body__calc__balance__header">
  19. <p>БАЛАНСЫ [@bc_selected]</p>
  20. @*<button class="btn_orange">История</button>*@
  21. </div>
  22. <div class="modal__body__calc__balance__numbers">
  23. <div class="modal__body__calc__balance__numbers__name">
  24. <p>Система</p>
  25. </div>
  26. <div class="modal__body__calc__balance__numbers__value">
  27. <div class="modal__body__calc__balance__numbers__value__item">
  28. <p>@systemEth</p>
  29. <p>ETH</p>
  30. </div>
  31. <div class="modal__body__calc__balance__numbers__value__item">
  32. <p>@systemToken</p>
  33. <p>QNM</p>
  34. </div>
  35. </div>
  36. </div>
  37. <div><p>@_account.Name</p></div>
  38. <div class="modal__body__calc__balance__numbers">
  39. <div class="modal__body__calc__balance__numbers__name">
  40. <p>Ваш</p>
  41. </div>
  42. <div class="modal__body__calc__balance__numbers__value">
  43. <div class="modal__body__calc__balance__numbers__value__item">
  44. <p>@ethBalance</p>
  45. <p>ETH</p>
  46. </div>
  47. <div class="modal__body__calc__balance__numbers__value__item">
  48. <p>@tokenBalance</p>
  49. <p>QNM</p>
  50. </div>
  51. </div>
  52. </div>
  53. @*<div class="modal__body__calc__balance__button">
  54. <button class="btn_orange btn_center">Пополнение</button>
  55. <button class="btn_orange btn_center">Вывод</button>
  56. </div>*@
  57. </div>
  58. <div class="modal__body__container__right">
  59. <p class="modal__body__calc__change__header">Обмен</p>
  60. <p class="modal__body__calc__change__desc">Trade tokens in an istant</p>
  61. <div class="modal__input__box" place="From" balanc=@chosenBalanceFrom>
  62. <input @bind=fromInput @bind:event="oninput" type="text" class="modal__input">
  63. <select @bind="from_token_selected" type="select" class="modal__select">
  64. <option value="eth">ETH</option>
  65. <option value="qnm">QNM</option>
  66. </select>
  67. </div>
  68. <div class="modal__input__box" place="To" balanc=@chosenBalanceTo>
  69. <input @bind=toInput @bind:event="oninput" type="text" class="modal__input">
  70. <select @bind="to_token_selected" type="select" class="modal__select">
  71. <option value="qnm">QNM</option>
  72. <option value="eth">ETH</option>
  73. </select>
  74. </div>
  75. @*<div class="modal__input__total">
  76. <p>Price</p>
  77. <div class="modal_input_total_value">
  78. <p>1.000000050</p>
  79. <p>2DH</p>
  80. </div>
  81. </div>*@
  82. <div class="modal__input__button">
  83. <button @onclick="@Ok" class="btn_orange btn_center">ОК</button>
  84. </div>
  85. </div>
  86. </div>
  87. </div>
  88. <a class="modal_close" style="cursor:pointer" @onclick="@Close">&#10006;</a>
  89. </div>
  90. </div>
  91. @code {[Inject]
  92. public IJSRuntime JsRuntime { get; set; }
  93. [Parameter]
  94. public RenderFragment Title { get; set; }
  95. string ethBalance;
  96. string tokenBalance;
  97. string chosenBalanceFrom;
  98. string chosenBalanceTo;
  99. string bc_selected;
  100. string fi;
  101. string ti;
  102. long lastInput = 0;
  103. string systemEth;
  104. string systemToken;
  105. string fromInput
  106. {
  107. get { return fi;
  108. }
  109. set { fi = value;
  110. System.Threading.Thread.Sleep(1);
  111. var now = DateTimeOffset.Now.ToUnixTimeSeconds();
  112. if (now - lastInput > 0)
  113. {
  114. try
  115. {
  116. var val = float.Parse(fi, System.Globalization.CultureInfo.InvariantCulture);
  117. //var val = System.Convert.ToSingle(fi);
  118. if (to_token_selected == "qnm")
  119. val *= 1000f;
  120. else
  121. val /= 1000f;
  122. ti = val.ToString();
  123. }
  124. catch (Exception e)
  125. {
  126. toInput = "0";
  127. Console.WriteLine(e.Message);
  128. }
  129. }
  130. lastInput = DateTimeOffset.Now.ToUnixTimeSeconds();
  131. }
  132. }
  133. string toInput
  134. {
  135. get { return ti; }
  136. set { ti = value;
  137. lastInput = DateTimeOffset.Now.ToUnixTimeSeconds();
  138. System.Threading.Thread.Sleep(1);
  139. var now = DateTimeOffset.Now.ToUnixTimeSeconds();
  140. if (now - lastInput > 0)
  141. {
  142. try
  143. {
  144. var val = float.Parse(ti, System.Globalization.CultureInfo.InvariantCulture);
  145. if (to_token_selected == "eth")
  146. val *= 1000f;
  147. else
  148. val /= 1000f;
  149. fi = val.ToString();
  150. }
  151. catch (Exception e)
  152. {
  153. fromInput = "0";
  154. Console.WriteLine(e.Message);
  155. }
  156. }
  157. }
  158. }
  159. string ft;
  160. string from_token_selected
  161. {
  162. get {
  163. return ft;
  164. }
  165. set {
  166. if (value == "eth")
  167. {
  168. to_token_selected = "qnm";
  169. chosenBalanceFrom = ethBalance;
  170. chosenBalanceTo = tokenBalance;
  171. }
  172. else
  173. {
  174. to_token_selected = "eth";
  175. chosenBalanceFrom = tokenBalance;
  176. chosenBalanceTo = ethBalance;
  177. }
  178. ft = value;
  179. }
  180. }
  181. string to_token_selected = "qnm";
  182. string elementid = "modal_assets";
  183. Models.AccountModel _account = new();
  184. @using Models;Blockchain bcMain;
  185. public async Task Balances()
  186. {
  187. ethBalance = await _account.GetBalance();
  188. var bc = await _account.GetSelectedBlockChain();
  189. bc_selected = bc.name;
  190. if (bc.port == 8666)
  191. {
  192. tokenBalance = await bc.BalanceOf(Blockchain.QNMAddress, _account.GetActualAddress(bc));
  193. systemEth = await bc.GetBalance(Blockchain.MultiOwnerContractRinkeby);
  194. systemToken = await bc.GetBalanceToken(Blockchain.QNMAddress, Blockchain.MultiOwnerContractRinkeby);
  195. Console.WriteLine($"systemToken {systemToken}");
  196. }
  197. else
  198. tokenBalance = "0";
  199. }
  200. public async Task Open(Models.AccountModel account)
  201. {
  202. _account = account;
  203. await Balances();
  204. await JsRuntime.InvokeVoidAsync("OpenModal", elementid);
  205. }
  206. public async Task Ok()
  207. {
  208. bool confirmed = await JsRuntime.InvokeAsync<bool>("confirm", "Are you sure?");
  209. if (confirmed)
  210. {
  211. Console.WriteLine("true");
  212. var bc = await _account.GetSelectedBlockChain();
  213. await bc.ExchangeCurrency(from_token_selected, to_token_selected, Blockchain.QNMAddress, _account.GetActualAddress(bc), fi, ti);
  214. //run change - recalculate balances
  215. }
  216. else
  217. {
  218. Console.WriteLine("false");
  219. //cancel
  220. }
  221. await Balances();
  222. }
  223. public void Close()
  224. {
  225. JsRuntime.InvokeVoidAsync("CloseModal", elementid);
  226. }
  227. }