Skip to content

Commit 2f26134

Browse files
authored
Add contract Deploy/Update/Destory event (#2150)
1 parent 06d4345 commit 2f26134

File tree

1 file changed

+52
-3
lines changed

1 file changed

+52
-3
lines changed

src/neo/SmartContract/Native/ManagementContract.cs

Lines changed: 52 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,51 @@ public sealed class ManagementContract : NativeContract
2121
private const byte Prefix_NextAvailableId = 15;
2222
private const byte Prefix_Contract = 8;
2323

24+
internal ManagementContract()
25+
{
26+
var events = new List<ContractEventDescriptor>(Manifest.Abi.Events)
27+
{
28+
new ContractEventDescriptor
29+
{
30+
Name = "Deploy",
31+
Parameters = new ContractParameterDefinition[]
32+
{
33+
new ContractParameterDefinition()
34+
{
35+
Name = "Hash",
36+
Type = ContractParameterType.Hash160
37+
}
38+
}
39+
},
40+
new ContractEventDescriptor
41+
{
42+
Name = "Update",
43+
Parameters = new ContractParameterDefinition[]
44+
{
45+
new ContractParameterDefinition()
46+
{
47+
Name = "Hash",
48+
Type = ContractParameterType.Hash160
49+
}
50+
}
51+
},
52+
new ContractEventDescriptor
53+
{
54+
Name = "Destory",
55+
Parameters = new ContractParameterDefinition[]
56+
{
57+
new ContractParameterDefinition()
58+
{
59+
Name = "Hash",
60+
Type = ContractParameterType.Hash160
61+
}
62+
}
63+
}
64+
};
65+
66+
Manifest.Abi.Events = events.ToArray();
67+
}
68+
2469
private int GetNextAvailableId(StoreView snapshot)
2570
{
2671
StorageItem item = snapshot.Storages.GetAndChange(CreateStorageKey(Prefix_NextAvailableId), () => new StorageItem(1));
@@ -77,7 +122,7 @@ public IEnumerable<ContractState> ListContracts(StoreView snapshot)
77122
return snapshot.Storages.Find(listContractsPrefix).Select(kvp => kvp.Value.GetInteroperable<ContractState>());
78123
}
79124

80-
[ContractMethod(0, CallFlags.WriteStates)]
125+
[ContractMethod(0, CallFlags.WriteStates | CallFlags.AllowNotify)]
81126
private ContractState Deploy(ApplicationEngine engine, byte[] nefFile, byte[] manifest)
82127
{
83128
if (!(engine.ScriptContainer is Transaction tx))
@@ -116,10 +161,12 @@ private ContractState Deploy(ApplicationEngine engine, byte[] nefFile, byte[] ma
116161
if (md != null)
117162
engine.CallFromNativeContract(Hash, hash, md.Name, false);
118163

164+
engine.SendNotification(Hash, "Deploy", new VM.Types.Array { contract.Hash.ToArray() });
165+
119166
return contract;
120167
}
121168

122-
[ContractMethod(0, CallFlags.WriteStates)]
169+
[ContractMethod(0, CallFlags.WriteStates | CallFlags.AllowNotify)]
123170
private void Update(ApplicationEngine engine, byte[] nefFile, byte[] manifest)
124171
{
125172
if (nefFile is null && manifest is null) throw new ArgumentException();
@@ -154,9 +201,10 @@ private void Update(ApplicationEngine engine, byte[] nefFile, byte[] manifest)
154201
if (md != null)
155202
engine.CallFromNativeContract(Hash, contract.Hash, md.Name, true);
156203
}
204+
engine.SendNotification(Hash, "Update", new VM.Types.Array { contract.Hash.ToArray() });
157205
}
158206

159-
[ContractMethod(0_01000000, CallFlags.WriteStates)]
207+
[ContractMethod(0_01000000, CallFlags.WriteStates | CallFlags.AllowNotify)]
160208
private void Destroy(ApplicationEngine engine)
161209
{
162210
UInt160 hash = engine.CallingScriptHash;
@@ -166,6 +214,7 @@ private void Destroy(ApplicationEngine engine)
166214
engine.Snapshot.Storages.Delete(ckey);
167215
foreach (var (key, _) in engine.Snapshot.Storages.Find(BitConverter.GetBytes(contract.Id)))
168216
engine.Snapshot.Storages.Delete(key);
217+
engine.SendNotification(Hash, "Destory", new VM.Types.Array { hash.ToArray() });
169218
}
170219
}
171220
}

0 commit comments

Comments
 (0)