2021-04-07 21:07:39 +00:00
|
|
|
/*
|
2021-11-18 13:37:42 +00:00
|
|
|
Package nft contains non-divisible non-fungible NEP-11-compatible token
|
2021-04-07 21:07:39 +00:00
|
|
|
implementation. This token can be minted with GAS transfer to contract address,
|
2022-04-20 18:30:09 +00:00
|
|
|
it will hash some data (including data provided in transfer) and produce a
|
2021-06-11 10:39:48 +00:00
|
|
|
base64-encoded string that is your NFT. Since it's based on hashing and basically
|
2021-04-07 21:07:39 +00:00
|
|
|
you own a hash it's HASHY.
|
|
|
|
*/
|
|
|
|
package nft
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/interop"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/interop/contract"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/interop/iterator"
|
2023-03-18 07:44:12 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/interop/lib/address"
|
2021-04-07 21:07:39 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/interop/native/crypto"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/interop/native/gas"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/interop/native/management"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/interop/native/std"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/interop/runtime"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/interop/storage"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/interop/util"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Prefixes used for contract data storage.
|
|
|
|
const (
|
|
|
|
totalSupplyPrefix = "s"
|
2021-05-05 14:48:40 +00:00
|
|
|
// balancePrefix contains map from addresses to balances.
|
|
|
|
balancePrefix = "b"
|
|
|
|
// accountPrefix contains map from address + token id to tokens
|
|
|
|
accountPrefix = "a"
|
|
|
|
// tokenPrefix contains map from token id to it's owner.
|
|
|
|
tokenPrefix = "t"
|
2021-04-07 21:07:39 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
// contractOwner is a special address that can perform some management
|
|
|
|
// functions on this contract like updating/destroying it and can also
|
|
|
|
// be used for contract address verification.
|
2023-03-18 07:44:12 +00:00
|
|
|
contractOwner = address.ToHash160("NbrUYaZgyhSkNoRo9ugRyEMdUZxrhkNaWB")
|
2021-04-07 21:07:39 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Symbol returns token symbol, it's HASHY.
|
|
|
|
func Symbol() string {
|
|
|
|
return "HASHY"
|
|
|
|
}
|
|
|
|
|
|
|
|
// Decimals returns token decimals, this NFT is non-divisible, so it's 0.
|
|
|
|
func Decimals() int {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
// TotalSupply is a contract method that returns the number of tokens minted.
|
|
|
|
func TotalSupply() int {
|
|
|
|
return totalSupply(storage.GetReadOnlyContext())
|
|
|
|
}
|
|
|
|
|
|
|
|
// totalSupply is an internal implementation of TotalSupply operating with
|
2022-04-20 18:30:09 +00:00
|
|
|
// the given context. The number itself is stored raw in the DB with totalSupplyPrefix
|
2021-04-07 21:07:39 +00:00
|
|
|
// key.
|
|
|
|
func totalSupply(ctx storage.Context) int {
|
|
|
|
var res int
|
|
|
|
|
|
|
|
val := storage.Get(ctx, []byte(totalSupplyPrefix))
|
|
|
|
if val != nil {
|
|
|
|
res = val.(int)
|
|
|
|
}
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// mkAccountPrefix creates DB key-prefix for the account tokens specified
|
2021-05-05 14:48:40 +00:00
|
|
|
// by concatenating accountPrefix and account address.
|
|
|
|
func mkAccountPrefix(holder interop.Hash160) []byte {
|
2021-04-07 21:07:39 +00:00
|
|
|
res := []byte(accountPrefix)
|
|
|
|
return append(res, holder...)
|
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// mkBalanceKey creates DB key for the account specified by concatenating balancePrefix
|
2021-05-05 14:48:40 +00:00
|
|
|
// and account address.
|
|
|
|
func mkBalanceKey(holder interop.Hash160) []byte {
|
|
|
|
res := []byte(balancePrefix)
|
|
|
|
return append(res, holder...)
|
2021-04-07 21:07:39 +00:00
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// mkTokenKey creates DB key for the token specified by concatenating tokenPrefix
|
2021-05-05 14:48:40 +00:00
|
|
|
// and token ID.
|
|
|
|
func mkTokenKey(tokenID []byte) []byte {
|
|
|
|
res := []byte(tokenPrefix)
|
|
|
|
return append(res, tokenID...)
|
2021-04-28 14:47:44 +00:00
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// BalanceOf returns the number of tokens owned by the specified address.
|
2021-04-07 21:07:39 +00:00
|
|
|
func BalanceOf(holder interop.Hash160) int {
|
|
|
|
if len(holder) != 20 {
|
|
|
|
panic("bad owner address")
|
|
|
|
}
|
|
|
|
ctx := storage.GetReadOnlyContext()
|
2021-05-05 14:48:40 +00:00
|
|
|
return getBalanceOf(ctx, mkBalanceKey(holder))
|
2021-04-07 21:07:39 +00:00
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// getBalanceOf returns the balance of an account using database key.
|
2021-05-05 14:48:40 +00:00
|
|
|
func getBalanceOf(ctx storage.Context, balanceKey []byte) int {
|
|
|
|
val := storage.Get(ctx, balanceKey)
|
2021-04-07 21:07:39 +00:00
|
|
|
if val != nil {
|
2021-05-05 14:48:40 +00:00
|
|
|
return val.(int)
|
2021-04-07 21:07:39 +00:00
|
|
|
}
|
2021-05-05 14:48:40 +00:00
|
|
|
return 0
|
2021-04-07 21:07:39 +00:00
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// addToBalance adds an amount to the account balance. Amount can be negative.
|
2021-05-05 14:48:40 +00:00
|
|
|
func addToBalance(ctx storage.Context, holder interop.Hash160, amount int) {
|
|
|
|
key := mkBalanceKey(holder)
|
|
|
|
old := getBalanceOf(ctx, key)
|
|
|
|
old += amount
|
|
|
|
if old > 0 {
|
|
|
|
storage.Put(ctx, key, old)
|
2021-04-07 21:07:39 +00:00
|
|
|
} else {
|
|
|
|
storage.Delete(ctx, key)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// addToken adds a token to the account.
|
2021-05-05 14:48:40 +00:00
|
|
|
func addToken(ctx storage.Context, holder interop.Hash160, token []byte) {
|
|
|
|
key := mkAccountPrefix(holder)
|
|
|
|
storage.Put(ctx, append(key, token...), token)
|
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// removeToken removes the token from the account.
|
2021-05-05 14:48:40 +00:00
|
|
|
func removeToken(ctx storage.Context, holder interop.Hash160, token []byte) {
|
|
|
|
key := mkAccountPrefix(holder)
|
|
|
|
storage.Delete(ctx, append(key, token...))
|
2021-04-28 14:47:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Tokens returns an iterator that contains all of the tokens minted by the contract.
|
|
|
|
func Tokens() iterator.Iterator {
|
|
|
|
ctx := storage.GetReadOnlyContext()
|
2021-05-05 14:48:40 +00:00
|
|
|
key := []byte(tokenPrefix)
|
|
|
|
iter := storage.Find(ctx, key, storage.RemovePrefix|storage.KeysOnly)
|
|
|
|
return iter
|
2021-04-28 14:47:44 +00:00
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// TokensOf returns an iterator with all tokens held by the specified address.
|
2021-04-07 21:07:39 +00:00
|
|
|
func TokensOf(holder interop.Hash160) iterator.Iterator {
|
|
|
|
if len(holder) != 20 {
|
|
|
|
panic("bad owner address")
|
|
|
|
}
|
|
|
|
ctx := storage.GetReadOnlyContext()
|
2021-05-05 14:48:40 +00:00
|
|
|
key := mkAccountPrefix(holder)
|
|
|
|
iter := storage.Find(ctx, key, storage.ValuesOnly)
|
|
|
|
return iter
|
2021-04-07 21:07:39 +00:00
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// getOwnerOf returns the current owner of the specified token or panics if token
|
|
|
|
// ID is invalid. The owner is stored as a value of the token key (prefix + token ID).
|
2021-04-07 21:07:39 +00:00
|
|
|
func getOwnerOf(ctx storage.Context, token []byte) interop.Hash160 {
|
|
|
|
key := mkTokenKey(token)
|
|
|
|
val := storage.Get(ctx, key)
|
|
|
|
if val == nil {
|
|
|
|
panic("no token found")
|
|
|
|
}
|
|
|
|
return val.(interop.Hash160)
|
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// setOwnerOf writes the current owner of the specified token into the DB.
|
2021-04-07 21:07:39 +00:00
|
|
|
func setOwnerOf(ctx storage.Context, token []byte, holder interop.Hash160) {
|
|
|
|
key := mkTokenKey(token)
|
|
|
|
storage.Put(ctx, key, holder)
|
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// OwnerOf returns the owner of the specified token.
|
2021-04-07 21:07:39 +00:00
|
|
|
func OwnerOf(token []byte) interop.Hash160 {
|
|
|
|
ctx := storage.GetReadOnlyContext()
|
|
|
|
return getOwnerOf(ctx, token)
|
|
|
|
}
|
|
|
|
|
2021-05-05 10:22:26 +00:00
|
|
|
// Transfer token from its owner to another user, notice that it only has three
|
|
|
|
// parameters because token owner can be deduced from token ID itself.
|
2023-04-03 10:34:24 +00:00
|
|
|
func Transfer(to interop.Hash160, token []byte, data any) bool {
|
2021-04-07 21:07:39 +00:00
|
|
|
if len(to) != 20 {
|
|
|
|
panic("invalid 'to' address")
|
|
|
|
}
|
|
|
|
ctx := storage.GetContext()
|
|
|
|
owner := getOwnerOf(ctx, token)
|
|
|
|
|
|
|
|
// Note that although calling script hash is not checked explicitly in
|
|
|
|
// this contract it is in fact checked for in `CheckWitness` itself.
|
|
|
|
if !runtime.CheckWitness(owner) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2022-07-11 14:29:25 +00:00
|
|
|
if !owner.Equals(to) {
|
2021-05-05 14:48:40 +00:00
|
|
|
addToBalance(ctx, owner, -1)
|
|
|
|
removeToken(ctx, owner, token)
|
|
|
|
|
|
|
|
addToBalance(ctx, to, 1)
|
|
|
|
addToken(ctx, to, token)
|
2021-04-07 21:07:39 +00:00
|
|
|
setOwnerOf(ctx, token, to)
|
|
|
|
}
|
2021-05-05 10:22:26 +00:00
|
|
|
postTransfer(owner, to, token, data)
|
2021-04-07 21:07:39 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// postTransfer emits Transfer event and calls onNEP11Payment if needed.
|
2023-04-03 10:34:24 +00:00
|
|
|
func postTransfer(from interop.Hash160, to interop.Hash160, token []byte, data any) {
|
2021-04-07 21:07:39 +00:00
|
|
|
runtime.Notify("Transfer", from, to, 1, token)
|
|
|
|
if management.GetContract(to) != nil {
|
2021-05-05 10:22:26 +00:00
|
|
|
contract.Call(to, "onNEP11Payment", contract.All, from, 1, token, data)
|
2021-04-07 21:07:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// OnNEP17Payment mints tokens if at least 10 GAS is provided. You don't call
|
|
|
|
// this method directly, instead it's called by GAS contract when you transfer
|
|
|
|
// GAS from your address to the address of this NFT contract.
|
2023-04-03 10:34:24 +00:00
|
|
|
func OnNEP17Payment(from interop.Hash160, amount int, data any) {
|
2022-03-23 08:58:07 +00:00
|
|
|
defer func() {
|
|
|
|
if r := recover(); r != nil {
|
|
|
|
runtime.Log(r.(string))
|
|
|
|
util.Abort()
|
|
|
|
}
|
|
|
|
}()
|
2022-07-11 14:29:25 +00:00
|
|
|
callingHash := runtime.GetCallingScriptHash()
|
|
|
|
if !callingHash.Equals(gas.Hash) {
|
2021-04-07 21:07:39 +00:00
|
|
|
panic("only GAS is accepted")
|
|
|
|
}
|
|
|
|
if amount < 10_00000000 {
|
|
|
|
panic("minting HASHY costs at least 10 GAS")
|
|
|
|
}
|
|
|
|
var tokIn = []byte{}
|
|
|
|
var ctx = storage.GetContext()
|
|
|
|
|
|
|
|
total := totalSupply(ctx)
|
|
|
|
tokIn = append(tokIn, []byte(std.Itoa(total, 10))...)
|
|
|
|
tokIn = append(tokIn, []byte(std.Itoa(amount, 10))...)
|
|
|
|
tokIn = append(tokIn, from...)
|
|
|
|
tx := runtime.GetScriptContainer()
|
|
|
|
tokIn = append(tokIn, tx.Hash...)
|
|
|
|
if data != nil {
|
|
|
|
tokIn = append(tokIn, std.Serialize(data)...)
|
|
|
|
}
|
|
|
|
|
2021-05-05 14:48:40 +00:00
|
|
|
tokenHash := crypto.Ripemd160(tokIn)
|
2021-06-11 10:39:48 +00:00
|
|
|
token := std.Base64Encode(tokenHash)
|
2021-04-07 21:07:39 +00:00
|
|
|
|
2021-05-05 14:48:40 +00:00
|
|
|
addToken(ctx, from, []byte(token))
|
2021-04-07 21:07:39 +00:00
|
|
|
setOwnerOf(ctx, []byte(token), from)
|
2021-05-05 14:48:40 +00:00
|
|
|
addToBalance(ctx, from, 1)
|
2021-04-07 21:07:39 +00:00
|
|
|
|
|
|
|
total++
|
|
|
|
storage.Put(ctx, []byte(totalSupplyPrefix), total)
|
|
|
|
|
2021-05-05 10:22:26 +00:00
|
|
|
postTransfer(nil, from, []byte(token), nil) // no `data` during minting
|
2021-04-07 21:07:39 +00:00
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// Verify allows an owner to manage a contract's address, including earned GAS
|
|
|
|
// transfer from the contract's address to somewhere else. It just checks for the transaction
|
|
|
|
// to also be signed by the contract owner, so contract's witness should be empty.
|
2021-04-07 21:07:39 +00:00
|
|
|
func Verify() bool {
|
|
|
|
return runtime.CheckWitness(contractOwner)
|
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// Destroy destroys the contract, only its owner can do that.
|
2021-04-07 21:07:39 +00:00
|
|
|
func Destroy() {
|
|
|
|
if !Verify() {
|
|
|
|
panic("only owner can destroy")
|
|
|
|
}
|
|
|
|
management.Destroy()
|
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// Update updates the contract, only its owner can do that.
|
2021-04-07 21:07:39 +00:00
|
|
|
func Update(nef, manifest []byte) {
|
|
|
|
if !Verify() {
|
|
|
|
panic("only owner can update")
|
|
|
|
}
|
|
|
|
management.Update(nef, manifest)
|
|
|
|
}
|
2021-04-26 14:09:37 +00:00
|
|
|
|
|
|
|
// Properties returns properties of the given NFT.
|
|
|
|
func Properties(id []byte) map[string]string {
|
|
|
|
ctx := storage.GetReadOnlyContext()
|
2021-05-05 14:48:40 +00:00
|
|
|
owner := storage.Get(ctx, mkTokenKey(id)).(interop.Hash160)
|
|
|
|
if owner == nil {
|
2021-04-26 14:09:37 +00:00
|
|
|
panic("unknown token")
|
|
|
|
}
|
|
|
|
result := map[string]string{
|
2022-02-04 16:18:35 +00:00
|
|
|
"name": "HASHY " + std.Base64Encode(id), // Not a hex for contract simplicity.
|
2021-04-26 14:09:37 +00:00
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|