Skip to content

Commit

Permalink
Update and fix Nep17 template (#891)
Browse files Browse the repository at this point in the history
- Use _deploy to set the owner
- Fix compilation errors
  • Loading branch information
shargon authored Feb 11, 2024
1 parent 71582f7 commit 8a3dfec
Showing 1 changed file with 20 additions and 22 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using Neo;
using Neo.SmartContract;
using Neo.SmartContract.Framework;
using Neo.SmartContract.Framework.Attributes;
using Neo.SmartContract.Framework.Native;
Expand All @@ -25,19 +24,10 @@ public class Contract1 : Nep17Token

private const byte Prefix_Owner = 0xff;

// TODO: Replace it with your own address.
[InitialValue("<Your Address Here>", Neo.SmartContract.ContractParameterType.Hash160)]
private static readonly UInt160 InitialOwner = default;

[Safe]
public static UInt160 GetOwner()
{
var currentOwner = Storage.Get(new[] { Prefix_Owner });

if (currentOwner == null)
return InitialOwner;

return (UInt160)currentOwner;
return (UInt160)Storage.Get(new[] { Prefix_Owner });
}

private static bool IsOwner() =>
Expand All @@ -52,24 +42,22 @@ public static void SetOwner(UInt160 newOwner)
{
if (IsOwner() == false)
throw new InvalidOperationException("No Authorization!");
if (newOwner != null && newOwner.IsValid)
{
Storage.Put(new[] { Prefix_Owner }, newOwner);
OnSetOwner(newOwner);
}

ExecutionEngine.Assert(newOwner.IsValid && !newOwner.IsZero, "owner must be valid");

Storage.Put(new[] { Prefix_Owner }, newOwner);
OnSetOwner(newOwner);
}

#endregion

#region NEP17

// NOTE: Valid Range 0-31
[Safe]
public override byte Decimals() => 8;

// TODO: Replace "EXAMPLE" with a short name all UPPERCASE 3-8 characters
[Safe]
public override string Symbol() => "EXAMPLE";
public override string Symbol { [Safe] get => "EXAMPLE"; }

// NOTE: Valid Range 0-31
public override byte Decimals { [Safe] get => 8; }

public static new void Burn(UInt160 account, BigInteger amount)
{
Expand Down Expand Up @@ -141,6 +129,16 @@ public static void _deploy(object data, bool update)
return;
}

// Init method, you must deploy the contract with the owner as an argument, or it will take the sender
if (data is null) data = Runtime.Transaction.Sender;

UInt160 initialOwner = (UInt160)data;

ExecutionEngine.Assert(initialOwner.IsValid && !initialOwner.IsZero, "owner must exists");

Storage.Put(new[] { Prefix_Owner }, initialOwner);
OnSetOwner(initialOwner);

// This will be executed during deploy
Storage.Put(Storage.CurrentContext, "Hello", "World");
}
Expand Down

0 comments on commit 8a3dfec

Please sign in to comment.