-
Notifications
You must be signed in to change notification settings - Fork 1k
/
Nep17Token.cs
170 lines (152 loc) · 7.28 KB
/
Nep17Token.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
using Neo.IO;
using Neo.Ledger;
using Neo.Persistence;
using Neo.SmartContract.Manifest;
using Neo.VM.Types;
using System;
using System.Collections.Generic;
using System.Numerics;
using Array = Neo.VM.Types.Array;
namespace Neo.SmartContract.Native.Tokens
{
public abstract class Nep17Token<TState> : NativeContract
where TState : AccountState, new()
{
[ContractMethod(0, CallFlags.None)]
public abstract string Symbol { get; }
[ContractMethod(0, CallFlags.None)]
public abstract byte Decimals { get; }
public BigInteger Factor { get; }
protected const byte Prefix_TotalSupply = 11;
protected const byte Prefix_Account = 20;
protected Nep17Token()
{
this.Factor = BigInteger.Pow(10, Decimals);
Manifest.SupportedStandards = new[] { "NEP-17" };
var events = new List<ContractEventDescriptor>(Manifest.Abi.Events)
{
new ContractEventDescriptor
{
Name = "Transfer",
Parameters = new ContractParameterDefinition[]
{
new ContractParameterDefinition()
{
Name = "from",
Type = ContractParameterType.Hash160
},
new ContractParameterDefinition()
{
Name = "to",
Type = ContractParameterType.Hash160
},
new ContractParameterDefinition()
{
Name = "amount",
Type = ContractParameterType.Integer
}
}
}
};
Manifest.Abi.Events = events.ToArray();
}
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.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);
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.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);
PostTransfer(engine, account, null, amount, StackItem.Null, false);
}
[ContractMethod(0_01000000, CallFlags.ReadStates)]
public virtual BigInteger TotalSupply(StoreView snapshot)
{
StorageItem storage = snapshot.Storages.TryGet(CreateStorageKey(Prefix_TotalSupply));
if (storage is null) return BigInteger.Zero;
return storage;
}
[ContractMethod(0_01000000, CallFlags.ReadStates)]
public virtual BigInteger BalanceOf(StoreView snapshot, UInt160 account)
{
StorageItem storage = snapshot.Storages.TryGet(CreateStorageKey(Prefix_Account).Add(account));
if (storage is null) return BigInteger.Zero;
return storage.GetInteroperable<TState>().Balance;
}
[ContractMethod(0_09000000, CallFlags.WriteStates | CallFlags.AllowCall | CallFlags.AllowNotify)]
protected virtual bool Transfer(ApplicationEngine engine, UInt160 from, UInt160 to, BigInteger amount, StackItem data)
{
if (amount.Sign < 0) throw new ArgumentOutOfRangeException(nameof(amount));
if (!from.Equals(engine.CallingScriptHash) && !engine.CheckWitnessInternal(from))
return false;
StorageKey key_from = CreateStorageKey(Prefix_Account).Add(from);
StorageItem storage_from = engine.Snapshot.Storages.GetAndChange(key_from);
if (amount.IsZero)
{
if (storage_from != null)
{
TState state_from = storage_from.GetInteroperable<TState>();
OnBalanceChanging(engine, from, state_from, amount);
}
}
else
{
if (storage_from is null) return false;
TState state_from = storage_from.GetInteroperable<TState>();
if (state_from.Balance < amount) return false;
if (from.Equals(to))
{
OnBalanceChanging(engine, from, state_from, BigInteger.Zero);
}
else
{
OnBalanceChanging(engine, from, state_from, -amount);
if (state_from.Balance == amount)
engine.Snapshot.Storages.Delete(key_from);
else
state_from.Balance -= amount;
StorageKey key_to = CreateStorageKey(Prefix_Account).Add(to);
StorageItem storage_to = engine.Snapshot.Storages.GetAndChange(key_to, () => new StorageItem(new TState()));
TState state_to = storage_to.GetInteroperable<TState>();
OnBalanceChanging(engine, to, state_to, amount);
state_to.Balance += amount;
}
}
PostTransfer(engine, from, to, amount, data, true);
return true;
}
protected virtual void OnBalanceChanging(ApplicationEngine engine, UInt160 account, TState state, BigInteger amount)
{
}
private void PostTransfer(ApplicationEngine engine, UInt160 from, UInt160 to, BigInteger amount, StackItem data, bool callOnPayment)
{
// Send notification
engine.SendNotification(Hash, "Transfer",
new Array { from?.ToArray() ?? StackItem.Null, to?.ToArray() ?? StackItem.Null, amount });
// Check if it's a wallet or smart contract
if (!callOnPayment || to is null || Management.GetContract(engine.Snapshot, to) is null) return;
// Call onPayment method (NEP-17)
engine.CallFromNativeContract(null, to, "onPayment", from?.ToArray() ?? StackItem.Null, amount, data);
}
}
}