Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ Check [Keep a Changelog](http://keepachangelog.com/) for recommendations on how

## Unreleased

### Changed

- C# sample contract now provides scaffolding for a ContractUpdate method

## [2.1.45] - 2021-05-18

### Removed
Expand Down
42 changes: 28 additions & 14 deletions resources/new-contract/csharp/$_CLASSNAME_$.cs.template.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
using System.ComponentModel;
using System.Numerics;

using Neo;
using Neo.SmartContract.Framework;
using Neo.SmartContract.Framework.Native;
using Neo.SmartContract.Framework.Services;

namespace $_CONTRACTNAME_$
Expand All @@ -13,10 +15,13 @@ namespace $_CONTRACTNAME_$
[ManifestExtra("Description", "Describe your contract...")]
public class $_CLASSNAME_$ : SmartContract
{
const string MAP_NAME = "$_CLASSNAME_$";
private static StorageMap ContractStorage => new StorageMap(Storage.CurrentContext, "$_CLASSNAME_$");
private static StorageMap ContractMetadata => new StorageMap(Storage.CurrentContext, "Metadata");

private static Transaction Tx => (Transaction) Runtime.ScriptContainer;

[DisplayName("NumberChanged")]
public static event Action<Neo.UInt160, BigInteger> OnNumberChanged;
public static event Action<UInt160, BigInteger> OnNumberChanged;

public static bool ChangeNumber(BigInteger positiveNumber)
{
Expand All @@ -25,24 +30,33 @@ namespace $_CONTRACTNAME_$
throw new Exception("Only positive numbers are allowed.");
}

var tx = (Transaction) Runtime.ScriptContainer;

var storageMap = new StorageMap(Storage.CurrentContext, MAP_NAME);

storageMap.Put(tx.Sender, positiveNumber);

OnNumberChanged(tx.Sender, positiveNumber);

ContractStorage.Put(Tx.Sender, positiveNumber);
OnNumberChanged(Tx.Sender, positiveNumber);
return true;
}

public static ByteString GetNumber()
[DisplayName("_deploy")]
public static void Deploy(object data, bool update)
{
var tx = (Transaction) Runtime.ScriptContainer;
if (!update)
{
ContractMetadata.Put("Owner", (ByteString) Tx.Sender);
}
}

var storageMap = new StorageMap(Storage.CurrentContext, MAP_NAME);
public static ByteString GetNumber()
{
return ContractStorage.Get(Tx.Sender);
}

return storageMap.Get(tx.Sender);
public static void UpdateContract(ByteString nefFile, string manifest)
{
ByteString owner = ContractMetadata.Get("Owner");
if (!Tx.Sender.Equals(owner))
{
throw new Exception("Only the contract owner can do this");
}
ContractManagement.Update(nefFile, manifest, null);
}
}
}