diff --git a/src/Neo.CLI/CLI/MainService.Vote.cs b/src/Neo.CLI/CLI/MainService.Vote.cs index 0edbb6fd81..23e18b411b 100644 --- a/src/Neo.CLI/CLI/MainService.Vote.cs +++ b/src/Neo.CLI/CLI/MainService.Vote.cs @@ -24,6 +24,17 @@ namespace Neo.CLI { + public static class VoteMethods + { + public const string Register = "registerCandidate"; + public const string Unregister = "unregisterCandidate"; + public const string Vote = "vote"; + public const string GetAccountState = "getAccountState"; + public const string GetCandidates = "getCandidates"; + public const string GetCommittee = "getCommittee"; + public const string GetNextBlockValidators = "getNextBlockValidators"; + } + partial class MainService { /// @@ -35,30 +46,12 @@ private void OnRegisterCandidateCommand(UInt160 account) { var testGas = NativeContract.NEO.GetRegisterPrice(NeoSystem.StoreView) + (BigInteger)Math.Pow(10, NativeContract.GAS.Decimals) * 10; if (NoWallet()) return; - WalletAccount currentAccount = CurrentWallet!.GetAccount(account); - - if (currentAccount == null) - { - ConsoleHelper.Warning("This address isn't in your wallet!"); - return; - } - else - { - if (currentAccount.Lock || currentAccount.WatchOnly) - { - ConsoleHelper.Warning("Locked or WatchOnly address."); - return; - } - } - ECPoint? publicKey = currentAccount.GetKey()?.PublicKey; - byte[] script; - using (ScriptBuilder scriptBuilder = new()) - { - scriptBuilder.EmitDynamicCall(NativeContract.NEO.Hash, "registerCandidate", publicKey); - script = scriptBuilder.ToArray(); - } + var currentAccount = GetValidAccountOrWarn(account); + if (currentAccount == null) return; + var publicKey = currentAccount.GetKey()?.PublicKey; + var script = BuildNeoScript(VoteMethods.Register, publicKey); SendTransaction(script, account, (long)testGas); } @@ -70,30 +63,12 @@ private void OnRegisterCandidateCommand(UInt160 account) private void OnUnregisterCandidateCommand(UInt160 account) { if (NoWallet()) return; - WalletAccount currentAccount = CurrentWallet!.GetAccount(account); - if (currentAccount == null) - { - ConsoleHelper.Warning("This address isn't in your wallet!"); - return; - } - else - { - if (currentAccount.Lock || currentAccount.WatchOnly) - { - ConsoleHelper.Warning("Locked or WatchOnly address."); - return; - } - } - - ECPoint? publicKey = currentAccount?.GetKey()?.PublicKey; - byte[] script; - using (ScriptBuilder scriptBuilder = new()) - { - scriptBuilder.EmitDynamicCall(NativeContract.NEO.Hash, "unregisterCandidate", publicKey); - script = scriptBuilder.ToArray(); - } + var currentAccount = GetValidAccountOrWarn(account); + if (currentAccount == null) return; + var publicKey = currentAccount?.GetKey()?.PublicKey; + var script = BuildNeoScript(VoteMethods.Unregister, publicKey); SendTransaction(script, account); } @@ -106,13 +81,8 @@ private void OnUnregisterCandidateCommand(UInt160 account) private void OnVoteCommand(UInt160 senderAccount, ECPoint publicKey) { if (NoWallet()) return; - byte[] script; - using (ScriptBuilder scriptBuilder = new()) - { - scriptBuilder.EmitDynamicCall(NativeContract.NEO.Hash, "vote", senderAccount, publicKey); - script = scriptBuilder.ToArray(); - } + var script = BuildNeoScript(VoteMethods.Vote, senderAccount, publicKey); SendTransaction(script, senderAccount); } @@ -124,13 +94,8 @@ private void OnVoteCommand(UInt160 senderAccount, ECPoint publicKey) private void OnUnvoteCommand(UInt160 senderAccount) { if (NoWallet()) return; - byte[] script; - using (ScriptBuilder scriptBuilder = new()) - { - scriptBuilder.EmitDynamicCall(NativeContract.NEO.Hash, "vote", senderAccount, null); - script = scriptBuilder.ToArray(); - } + var script = BuildNeoScript(VoteMethods.Vote, senderAccount, null); SendTransaction(script, senderAccount); } @@ -140,7 +105,7 @@ private void OnUnvoteCommand(UInt160 senderAccount) [ConsoleCommand("get candidates", Category = "Vote Commands")] private void OnGetCandidatesCommand() { - if (!OnInvokeWithResult(NativeContract.NEO.Hash, "getCandidates", out StackItem result, null, null, false)) return; + if (!OnInvokeWithResult(NativeContract.NEO.Hash, VoteMethods.GetCandidates, out var result, null, null, false)) return; var resJArray = (Array)result; @@ -166,7 +131,7 @@ private void OnGetCandidatesCommand() [ConsoleCommand("get committee", Category = "Vote Commands")] private void OnGetCommitteeCommand() { - if (!OnInvokeWithResult(NativeContract.NEO.Hash, "getCommittee", out StackItem result, null, null, false)) return; + if (!OnInvokeWithResult(NativeContract.NEO.Hash, VoteMethods.GetCommittee, out StackItem result, null, null, false)) return; var resJArray = (Array)result; @@ -188,7 +153,7 @@ private void OnGetCommitteeCommand() [ConsoleCommand("get next validators", Category = "Vote Commands")] private void OnGetNextBlockValidatorsCommand() { - if (!OnInvokeWithResult(NativeContract.NEO.Hash, "getNextBlockValidators", out StackItem result, null, null, false)) return; + if (!OnInvokeWithResult(NativeContract.NEO.Hash, VoteMethods.GetNextBlockValidators, out var result, null, null, false)) return; var resJArray = (Array)result; @@ -210,24 +175,24 @@ private void OnGetNextBlockValidatorsCommand() [ConsoleCommand("get accountstate", Category = "Vote Commands")] private void OnGetAccountState(UInt160 address) { - const string notice = "No vote record!"; + const string Notice = "No vote record!"; var arg = new JObject { ["type"] = "Hash160", ["value"] = address.ToString() }; - if (!OnInvokeWithResult(NativeContract.NEO.Hash, "getAccountState", out var result, null, new JArray(arg))) return; + if (!OnInvokeWithResult(NativeContract.NEO.Hash, VoteMethods.GetAccountState, out var result, null, new JArray(arg))) return; Console.WriteLine(); if (result.IsNull) { - ConsoleHelper.Warning(notice); + ConsoleHelper.Warning(Notice); return; } var resJArray = (Array)result; if (resJArray is null) { - ConsoleHelper.Warning(notice); + ConsoleHelper.Warning(Notice); return; } @@ -235,7 +200,7 @@ private void OnGetAccountState(UInt160 address) { if (value.IsNull) { - ConsoleHelper.Warning(notice); + ConsoleHelper.Warning(Notice); return; } } @@ -258,5 +223,28 @@ private void OnGetAccountState(UInt160 address) ConsoleHelper.Error("Error parsing the result"); } } + /// + /// Get account or log a warm + /// + /// + /// account or null + private WalletAccount? GetValidAccountOrWarn(UInt160 account) + { + var acct = CurrentWallet?.GetAccount(account); + if (acct == null) + { + ConsoleHelper.Warning("This address isn't in your wallet!"); + return null; + } + if (acct.Lock || acct.WatchOnly) + { + ConsoleHelper.Warning("Locked or WatchOnly address."); + return null; + } + return acct; + } + + private byte[] BuildNeoScript(string method, params object?[] args) + => NativeContract.NEO.Hash.MakeScript(method, args); } }