Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update and fix Nep17 template #891

Merged
merged 1 commit into from
Feb 11, 2024
Merged
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
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