private bool Vote(ApplicationEngine engine, UInt160 account, ECPoint voteTo) { if (!engine.CheckWitnessInternal(account)) { return(false); } NeoAccountState state_account = engine.Snapshot.Storages.GetAndChange(CreateStorageKey(Prefix_Account).Add(account))?.GetInteroperable <NeoAccountState>(); if (state_account is null) { return(false); } CandidateState validator_new = null; if (voteTo != null) { validator_new = engine.Snapshot.Storages.GetAndChange(CreateStorageKey(Prefix_Candidate).Add(voteTo))?.GetInteroperable <CandidateState>(); if (validator_new is null) { return(false); } if (!validator_new.Registered) { return(false); } } if (state_account.VoteTo is null ^ voteTo is null) { StorageItem item = engine.Snapshot.Storages.GetAndChange(CreateStorageKey(Prefix_VotersCount)); if (state_account.VoteTo is null) { item.Add(state_account.Balance); } else { item.Add(-state_account.Balance); } } if (state_account.VoteTo != null) { StorageKey key = CreateStorageKey(Prefix_Candidate).Add(state_account.VoteTo); StorageItem storage_validator = engine.Snapshot.Storages.GetAndChange(key); CandidateState state_validator = storage_validator.GetInteroperable <CandidateState>(); state_validator.Votes -= state_account.Balance; if (!state_validator.Registered && state_validator.Votes.IsZero) { engine.Snapshot.Storages.Delete(key); } } state_account.VoteTo = voteTo; if (validator_new != null) { validator_new.Votes += state_account.Balance; } return(true); }
private async ContractTask <bool> Vote(ApplicationEngine engine, UInt160 account, ECPoint voteTo) { if (!engine.CheckWitnessInternal(account)) { return(false); } VauthAccountState state_account = engine.Snapshot.GetAndChange(CreateStorageKey(Prefix_Account).Add(account))?.GetInteroperable <VauthAccountState>(); if (state_account is null) { return(false); } CandidateState validator_new = null; if (voteTo != null) { validator_new = engine.Snapshot.GetAndChange(CreateStorageKey(Prefix_Candidate).Add(voteTo))?.GetInteroperable <CandidateState>(); if (validator_new is null) { return(false); } if (!validator_new.Registered) { return(false); } } if (state_account.VoteTo is null ^ voteTo is null) { StorageItem item = engine.Snapshot.GetAndChange(CreateStorageKey(Prefix_VotersCount)); if (state_account.VoteTo is null) { item.Add(state_account.Balance); } else { item.Add(-state_account.Balance); } } await DistributeValt(engine, account, state_account); if (state_account.VoteTo != null) { StorageKey key = CreateStorageKey(Prefix_Candidate).Add(state_account.VoteTo); StorageItem storage_validator = engine.Snapshot.GetAndChange(key); CandidateState state_validator = storage_validator.GetInteroperable <CandidateState>(); state_validator.Votes -= state_account.Balance; CheckCandidate(engine.Snapshot, state_account.VoteTo, state_validator); } state_account.VoteTo = voteTo; if (validator_new != null) { validator_new.Votes += state_account.Balance; } return(true); }
internal async ContractTask Burn(ApplicationEngine engine, UInt160 account, BigInteger amount) { if (amount.Sign < 0) { throw new ArgumentOutOfRangeException(nameof(amount)); } if (amount.IsZero) { return; } StorageKey key = CreateStorageKey(Prefix_Account).Add(account); StorageItem storage = engine.Snapshot.GetAndChange(key); TState state = storage.GetInteroperable <TState>(); if (state.Balance < amount) { throw new InvalidOperationException(); } await OnBalanceChanging(engine, account, state, -amount); if (state.Balance == amount) { engine.Snapshot.Delete(key); } else { state.Balance -= amount; } storage = engine.Snapshot.GetAndChange(CreateStorageKey(Prefix_TotalSupply)); storage.Add(-amount); await PostTransfer(engine, account, null, amount, StackItem.Null, false); }
internal protected virtual void Burn(ApplicationEngine engine, UInt160 account, BigInteger amount) { if (amount.Sign < 0) { throw new ArgumentOutOfRangeException(nameof(amount)); } if (amount.IsZero) { return; } StorageKey key = CreateStorageKey(Prefix_Account).Add(account); StorageItem storage = engine.Snapshot.Storages.GetAndChange(key); TState state = storage.GetInteroperable <TState>(); if (state.Balance < amount) { throw new InvalidOperationException(); } OnBalanceChanging(engine, account, state, -amount); if (state.Balance == amount) { engine.Snapshot.Storages.Delete(key); } else { state.Balance -= amount; } storage = engine.Snapshot.Storages.GetAndChange(CreateStorageKey(Prefix_TotalSupply)); storage.Add(-amount); engine.SendNotification(Hash, "Transfer", new Array { account.ToArray(), StackItem.Null, amount }); }
private int GetNextAvailableId(DataCache snapshot) { StorageItem item = snapshot.GetAndChange(CreateStorageKey(Prefix_NextAvailableId)); int value = (int)(BigInteger)item; item.Add(1); return value; }
private int GetNextAvailableId(StoreView snapshot) { StorageItem item = snapshot.Storages.GetAndChange(CreateStorageKey(Prefix_NextAvailableId), () => new StorageItem(1)); int value = (int)(BigInteger)item; item.Add(1); return(value); }
internal protected virtual void Mint(ApplicationEngine engine, UInt160 account, BigInteger amount, bool callOnPayment) { if (amount.Sign < 0) throw new ArgumentOutOfRangeException(nameof(amount)); if (amount.IsZero) return; StorageItem storage = engine.Snapshot.GetAndChange(CreateStorageKey(Prefix_Account).Add(account), () => new StorageItem(new TState())); TState state = storage.GetInteroperable<TState>(); OnBalanceChanging(engine, account, state, amount); state.Balance += amount; storage = engine.Snapshot.GetAndChange(CreateStorageKey(Prefix_TotalSupply), () => new StorageItem(BigInteger.Zero)); storage.Add(amount); PostTransfer(engine, null, account, amount, StackItem.Null, callOnPayment); }
internal protected virtual void Burn(ApplicationEngine engine, UInt160 account, BigInteger amount) { if (amount.Sign < 0) throw new ArgumentOutOfRangeException(nameof(amount)); if (amount.IsZero) return; StorageKey key = CreateStorageKey(Prefix_Account).Add(account); StorageItem storage = engine.Snapshot.GetAndChange(key); TState state = storage.GetInteroperable<TState>(); if (state.Balance < amount) throw new InvalidOperationException(); OnBalanceChanging(engine, account, state, -amount); if (state.Balance == amount) engine.Snapshot.Delete(key); else state.Balance -= amount; storage = engine.Snapshot.GetAndChange(CreateStorageKey(Prefix_TotalSupply)); storage.Add(-amount); PostTransfer(engine, account, null, amount, StackItem.Null, false); }
internal protected virtual void Mint(ApplicationEngine engine, UInt160 account, BigInteger amount) { if (amount.Sign < 0) { throw new ArgumentOutOfRangeException(nameof(amount)); } if (amount.IsZero) { return; } StorageItem storage = engine.Snapshot.Storages.GetAndChange(CreateStorageKey(Prefix_Account).Add(account), () => new StorageItem(new TState())); TState state = storage.GetInteroperable <TState>(); OnBalanceChanging(engine, account, state, amount); state.Balance += amount; storage = engine.Snapshot.Storages.GetAndChange(CreateStorageKey(Prefix_TotalSupply), () => new StorageItem(BigInteger.Zero)); storage.Add(amount); engine.SendNotification(Hash, "Transfer", new Array { StackItem.Null, account.ToArray(), amount }); }