From abd332f7646ef84a9cbaffbdae1012aa584cc1d0 Mon Sep 17 00:00:00 2001
From: Anna Shaleva <shaleva.ann@nspcc.ru>
Date: Fri, 16 Sep 2022 11:43:48 +0300
Subject: [PATCH] nns: add Renew event

Port https://github.com/neo-project/non-native-contracts/pull/31.
---
 examples/nft-nd-nns/nns.go      |  2 ++
 examples/nft-nd-nns/nns.yml     |  8 ++++++++
 examples/nft-nd-nns/nns_test.go | 25 +++++++++++++++++++++++--
 3 files changed, 33 insertions(+), 2 deletions(-)

diff --git a/examples/nft-nd-nns/nns.go b/examples/nft-nd-nns/nns.go
index 8aa2d8de7..aeff8dcdf 100644
--- a/examples/nft-nd-nns/nns.go
+++ b/examples/nft-nd-nns/nns.go
@@ -441,11 +441,13 @@ func Renew(name string, years int64) int {
 
 	ctx := storage.GetContext()
 	ns := getNameState(ctx, []byte(name))
+	oldExpiration := ns.Expiration
 	ns.Expiration += int(millisecondsInYear * years)
 	if ns.Expiration > int(runtime.GetTime())+millisecondsInTenYears {
 		panic("10 years of expiration period at max is allowed")
 	}
 	putNameState(ctx, ns)
+	runtime.Notify("Renew", name, oldExpiration, ns.Expiration)
 	return ns.Expiration
 }
 
diff --git a/examples/nft-nd-nns/nns.yml b/examples/nft-nd-nns/nns.yml
index a99234437..4d09e6d54 100644
--- a/examples/nft-nd-nns/nns.yml
+++ b/examples/nft-nd-nns/nns.yml
@@ -23,6 +23,14 @@ events:
         type: Hash160
       - name: newAdmin
         type: Hash160
+  - name: Renew
+    parameters:
+      - name: name
+        type: String
+      - name: oldExpiration
+        type: Integer
+      - name: newExpiration
+        type: Integer
 permissions:
   - hash: fffdc93764dbaddd97c48f252a53ea4643faa3fd
     methods: ["update"]
diff --git a/examples/nft-nd-nns/nns_test.go b/examples/nft-nd-nns/nns_test.go
index ac79c1310..b294885ba 100644
--- a/examples/nft-nd-nns/nns_test.go
+++ b/examples/nft-nd-nns/nns_test.go
@@ -210,8 +210,18 @@ func TestRegisterAndRenew(t *testing.T) {
 	})
 
 	// Renew
+	oldExpiration := expectedExpiration
 	expectedExpiration += millisecondsInYear
-	c.Invoke(t, expectedExpiration, "renew", "neo-go.com")
+	h := c.Invoke(t, expectedExpiration, "renew", "neo-go.com")
+	c.CheckTxNotificationEvent(t, h, 0, state.NotificationEvent{
+		ScriptHash: c.Hash,
+		Name:       "Renew",
+		Item: stackitem.NewArray([]stackitem.Item{
+			stackitem.NewByteArray([]byte("neo-go.com")),
+			stackitem.Make(oldExpiration),
+			stackitem.Make(expectedExpiration),
+		}),
+	})
 
 	props.Add(stackitem.Make("expiration"), stackitem.Make(expectedExpiration))
 	c.Invoke(t, props, "properties", "neo-go.com")
@@ -222,8 +232,19 @@ func TestRegisterAndRenew(t *testing.T) {
 	c.InvokeFail(t, "10 years of expiration period at max is allowed", "renew", "neo-go.com", 10)
 
 	// Non-default renewal period.
+	oldExpiration = expectedExpiration
 	mult := 2
-	c.Invoke(t, expectedExpiration+uint64(mult*millisecondsInYear), "renew", "neo-go.com", mult)
+	expectedExpiration += uint64(mult * millisecondsInYear)
+	h = c.Invoke(t, expectedExpiration, "renew", "neo-go.com", mult)
+	c.CheckTxNotificationEvent(t, h, 0, state.NotificationEvent{
+		ScriptHash: c.Hash,
+		Name:       "Renew",
+		Item: stackitem.NewArray([]stackitem.Item{
+			stackitem.NewByteArray([]byte("neo-go.com")),
+			stackitem.Make(oldExpiration),
+			stackitem.Make(expectedExpiration),
+		}),
+	})
 }
 
 func TestSetAddGetRecord(t *testing.T) {