UFRMSaleAccounts.pas 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. unit UFRMSaleAccounts;
  2. interface
  3. uses
  4. Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  5. Dialogs, StdCtrls, UAccounts, Buttons, ActnList;
  6. type
  7. TFRMSaleAccounts = class(TForm)
  8. lblAccountCaption: TLabel;
  9. ebSenderAccount: TEdit;
  10. memoAccounts: TMemo;
  11. Label1: TLabel;
  12. ebSalePrice: TEdit;
  13. Label2: TLabel;
  14. ebLockedUntilBlock: TEdit;
  15. gbSellOptions: TGroupBox;
  16. rbEveryoneCanBuy: TRadioButton;
  17. rbReservedForAPublickKey: TRadioButton;
  18. lblNewOwnerPublicKey: TLabel;
  19. ebNewOwnerPublicKey: TEdit;
  20. Label3: TLabel;
  21. ebSellerAccount: TEdit;
  22. bbExecute: TBitBtn;
  23. bbCancel: TBitBtn;
  24. lblAccountBalance: TLabel;
  25. lblAccountsCount: TLabel;
  26. lblBalanceCaption: TLabel;
  27. ActionList: TActionList;
  28. actExecute: TAction;
  29. procedure FormCreate(Sender: TObject);
  30. procedure actExecuteExecute(Sender: TObject);
  31. procedure FormDestroy(Sender: TObject);
  32. private
  33. FDisabled : Boolean;
  34. FSenderAccounts: TOrderedCardinalList;
  35. FWalletKeys: TWalletKeys;
  36. FOldOnChanged : TNotifyEvent;
  37. { Private declarations }
  38. Procedure UpdateAccountsInfo;
  39. procedure SetWalletKeys(const Value: TWalletKeys);
  40. public
  41. { Public declarations }
  42. Property SenderAccounts : TOrderedCardinalList read FSenderAccounts;
  43. Property WalletKeys : TWalletKeys read FWalletKeys write SetWalletKeys;
  44. end;
  45. implementation
  46. uses UNode;
  47. {$R *.dfm}
  48. procedure TFRMSaleAccounts.actExecuteExecute(Sender: TObject);
  49. Var errors : AnsiString;
  50. P : PAccount;
  51. i,iAcc : Integer;
  52. wk : TWalletKey;
  53. ops : TOperationsHashTree;
  54. op : TPCOperation;
  55. account : TAccount;
  56. operation_to_string, operationstxt, auxs : String;
  57. _amount,_fee, _totalamount, _totalfee : Int64;
  58. dooperation : Boolean;
  59. begin
  60. if Not Assigned(WalletKeys) then raise Exception.Create('No wallet keys');
  61. If Not UpdateOperationOptions(errors) then raise Exception.Create(errors);
  62. ops := TOperationsHashTree.Create;
  63. Try
  64. _totalamount := 0;
  65. _totalfee := 0;
  66. operationstxt := '';
  67. operation_to_string := '';
  68. for iAcc := 0 to FSenderAccounts.Count - 1 do begin
  69. op := Nil;
  70. account := FNode.Operations.SafeBoxTransaction.Account(FSenderAccounts.Get(iAcc));
  71. If Not UpdatePayload(account, errors) then
  72. raise Exception.Create('Error encoding payload of sender account '+TAccountComp.AccountNumberToAccountTxtNumber(account.account)+': '+errors);
  73. i := WalletKeys.IndexOfAccountKey(account.accountInfo.accountKey);
  74. if i<0 then begin
  75. Raise Exception.Create('Sender account private key not found in Wallet');
  76. end;
  77. wk := WalletKeys.Key[i];
  78. dooperation := true;
  79. //
  80. if rbTransaction.Checked then begin
  81. // Amount
  82. _amount := 0;
  83. _fee := 0;
  84. if FSenderAccounts.Count>1 then begin
  85. if account.balance>0 then begin
  86. if account.balance>fee then begin
  87. _amount := account.balance - fee;
  88. _fee := fee;
  89. end else begin
  90. _amount := account.balance;
  91. _fee := 0;
  92. end;
  93. end else dooperation := false;
  94. end else begin
  95. _amount := FTxAmount;
  96. _fee := fee;
  97. end;
  98. if dooperation then begin
  99. op := TOpTransaction.CreateTransaction(account.account,account.n_operation+1,FTxDestAccount,wk.PrivateKey,_amount,_fee,FEncodedPayload);
  100. inc(_totalamount,_amount);
  101. inc(_totalfee,_fee);
  102. end;
  103. operationstxt := 'Transaction to '+TAccountComp.AccountNumberToAccountTxtNumber(FTxDestAccount);
  104. end else if rbChangeKey.Checked then begin
  105. i := PtrInt(cbNewPrivateKey.Items.Objects[cbNewPrivateKey.ItemIndex]);
  106. if (i<0) Or (i>=WalletKeys.Count) then raise Exception.Create('Invalid selected key');
  107. FNewAccountPublicKey := WalletKeys.Key[i].AccountKey;
  108. if account.balance>fee then _fee := fee
  109. else _fee := 0;
  110. op := TOpChangeKey.Create(account.account,account.n_operation+1,wk.PrivateKey,FNewAccountPublicKey,_fee,FEncodedPayload);
  111. inc(_totalfee,_fee);
  112. operationstxt := 'Change private key to '+wk.Name;
  113. end else if rbTransferToANewOwner.Checked then begin
  114. if account.balance>fee then _fee := fee
  115. else _fee := 0;
  116. op := TOpChangeKey.Create(account.account,account.n_operation+1,wk.PrivateKey,FNewAccountPublicKey,_fee,FEncodedPayload);
  117. operationstxt := 'Transfer to a new owner with key type '+TAccountComp.GetECInfoTxt(FNewAccountPublicKey.EC_OpenSSL_NID);
  118. inc(_totalfee,_fee);
  119. end else begin
  120. raise Exception.Create('No operation selected');
  121. end;
  122. if Assigned(op) And (dooperation) then begin
  123. ops.AddOperationToHashTree(op);
  124. if operation_to_string<>'' then operation_to_string := operation_to_string + #10;
  125. operation_to_string := operation_to_string + op.ToString;
  126. end;
  127. FreeAndNil(op);
  128. end;
  129. if (ops.OperationsCount=0) then raise Exception.Create('No valid operation to execute');
  130. if (FSenderAccounts.Count>1) then begin
  131. if rbTransaction.Checked then auxs := 'Total amount that dest will receive: '+TAccountComp.FormatMoney(_totalamount)+#10
  132. else auxs:='';
  133. if Application.MessageBox(PChar('Execute '+Inttostr(FSenderAccounts.Count)+' operations?'+#10+
  134. 'Operation: '+operationstxt+#10+
  135. auxs+
  136. 'Total fee: '+TAccountComp.FormatMoney(_totalfee)+#10+#10+'Note: This operation will be transmitted to the network!'),
  137. PChar(Application.Title),MB_YESNO+MB_ICONINFORMATION+MB_DEFBUTTON2)<>IdYes then exit;
  138. end else begin
  139. if Application.MessageBox(PChar('Execute this operation:'+#10+#10+operation_to_string+#10+#10+'Note: This operation will be transmitted to the network!'),
  140. PChar(Application.Title),MB_YESNO+MB_ICONINFORMATION+MB_DEFBUTTON2)<>IdYes then exit;
  141. end;
  142. i := FNode.AddOperations(nil,ops,Nil,errors);
  143. if (i=ops.OperationsCount) then begin
  144. Application.MessageBox(PChar('Successfully executed '+inttostr(i)+' operations!'+#10+#10+operation_to_string),PChar(Application.Title),MB_OK+MB_ICONINFORMATION);
  145. ModalResult := MrOk;
  146. end else if (i>0) then begin
  147. Application.MessageBox(PChar('One or more of your operations has not been executed:'+#10+
  148. 'Errors:'+#10+
  149. errors+#10+#10+
  150. 'Total successfully executed operations: '+inttostr(i)),PChar(Application.Title),MB_OK+MB_ICONWARNING);
  151. ModalResult := MrOk;
  152. end else begin
  153. raise Exception.Create(errors);
  154. end;
  155. Finally
  156. ops.Free;
  157. End;
  158. end;
  159. procedure TFRMSaleAccounts.FormCreate(Sender: TObject);
  160. begin
  161. FSenderAccounts := TOrderedCardinalList.Create;
  162. ebSenderAccount.Text := '';
  163. ebSalePrice.Text := '';
  164. ebLockedUntilBlock.Text := '';
  165. ebSellerAccount.Text := '';
  166. ebNewOwnerPublicKey.Text := '';
  167. memoAccounts.lines.Clear;
  168. FDisabled := false;
  169. end;
  170. procedure TFRMSaleAccounts.FormDestroy(Sender: TObject);
  171. begin
  172. FSenderAccounts.Free;
  173. end;
  174. procedure TFRMSaleAccounts.SetWalletKeys(const Value: TWalletKeys);
  175. begin
  176. if FWalletKeys=Value then exit;
  177. if Assigned(FWalletKeys) then FWalletKeys.OnChanged := FOldOnChanged;
  178. FWalletKeys := Value;
  179. if Assigned(FWalletKeys) then begin
  180. FOldOnChanged := FWalletKeys.OnChanged;
  181. FWalletKeys.OnChanged := OnWalletKeysChanged;
  182. end;
  183. UpdateWalletKeys;
  184. end;
  185. procedure TFRMSaleAccounts.UpdateAccountsInfo;
  186. Var ld : Boolean;
  187. i : Integer;
  188. balance : int64;
  189. acc : TAccount;
  190. accountstext : String;
  191. begin
  192. ld := FDisabled;
  193. FDisabled := true;
  194. Try
  195. lblAccountCaption.Caption := 'Account';
  196. lblAccountsCount.Visible := false;
  197. lblAccountsCount.caption := inttostr(senderAccounts.Count)+' accounts';
  198. balance := 0;
  199. if SenderAccounts.Count<=0 then begin
  200. ebSenderAccount.Text := '';
  201. memoAccounts.Visible := false;
  202. ebSenderAccount.Visible := true;
  203. end else if SenderAccounts.Count=1 then begin
  204. ebSenderAccount.Text := TAccountComp.AccountNumberToAccountTxtNumber(SenderAccounts.Get(0));
  205. memoAccounts.Visible := false;
  206. ebSenderAccount.Visible := true;
  207. balance := TNode.Node.Operations.SafeBoxTransaction.Account(SenderAccounts.Get(0)).balance;
  208. end else begin
  209. // Multiple sender accounts
  210. lblAccountCaption.Caption := 'Accounts';
  211. lblAccountsCount.Visible := true;
  212. ebSenderAccount.Visible := false;
  213. accountstext := '';
  214. for i := 0 to SenderAccounts.Count - 1 do begin
  215. acc := TNode.Node.Operations.SafeBoxTransaction.Account(SenderAccounts.Get(i));
  216. balance := balance + acc.balance;
  217. if (accountstext<>'') then accountstext:=accountstext+'; ';
  218. accountstext := accountstext+TAccountComp.AccountNumberToAccountTxtNumber(acc.account)+' ('+TAccountComp.FormatMoney(acc.balance)+')';
  219. end;
  220. memoAccounts.Lines.Text := accountstext;
  221. memoAccounts.Visible := true;
  222. end;
  223. ebSenderAccount.Enabled := ebSenderAccount.Visible;
  224. lblAccountBalance.Caption := TAccountComp.FormatMoney(balance);
  225. Finally
  226. FDisabled := ld;
  227. End;
  228. end;
  229. end.