github.com/nspcc-dev/neo-go@v0.105.2-0.20240517133400-6be757af3eba/internal/basicchain/testdata/test_contract.go (about)

     1  package testdata
     2  
     3  import (
     4  	"github.com/nspcc-dev/neo-go/pkg/interop"
     5  	"github.com/nspcc-dev/neo-go/pkg/interop/contract"
     6  	"github.com/nspcc-dev/neo-go/pkg/interop/lib/address"
     7  	"github.com/nspcc-dev/neo-go/pkg/interop/native/ledger"
     8  	"github.com/nspcc-dev/neo-go/pkg/interop/native/management"
     9  	"github.com/nspcc-dev/neo-go/pkg/interop/native/neo"
    10  	"github.com/nspcc-dev/neo-go/pkg/interop/runtime"
    11  	"github.com/nspcc-dev/neo-go/pkg/interop/storage"
    12  )
    13  
    14  const (
    15  	totalSupply = 1000000
    16  	decimals    = 2
    17  )
    18  
    19  var owner = address.ToHash160("NbrUYaZgyhSkNoRo9ugRyEMdUZxrhkNaWB")
    20  
    21  func Init() bool {
    22  	ctx := storage.GetContext()
    23  	h := runtime.GetExecutingScriptHash()
    24  	amount := totalSupply
    25  	storage.Put(ctx, h, amount)
    26  	runtime.Notify("Transfer", interop.Hash160(nil), // should use `nil` (not `[]byte{}`) due to notifications manifest compliance check.
    27  		h, amount)
    28  	return true
    29  }
    30  
    31  func OnNEP17Payment(from interop.Hash160, amount int, data any) {
    32  	curr := runtime.GetExecutingScriptHash()
    33  	balance := neo.BalanceOf(curr)
    34  	if ledger.CurrentIndex() >= 100 {
    35  		ok := neo.Transfer(curr, owner, balance, nil)
    36  		if !ok {
    37  			panic("owner transfer failed")
    38  		}
    39  		ok = neo.Transfer(curr, owner, 0, nil)
    40  		if !ok {
    41  			panic("owner transfer failed")
    42  		}
    43  	}
    44  }
    45  
    46  // Verify always returns true and is aimed to serve the TestNEO_RecursiveGASMint.
    47  func Verify() bool {
    48  	return true
    49  }
    50  
    51  func Transfer(from, to interop.Hash160, amount int, data any) bool {
    52  	ctx := storage.GetContext()
    53  	if len(from) != 20 {
    54  		runtime.Log("invalid 'from' address")
    55  		return false
    56  	}
    57  	if len(to) != 20 {
    58  		runtime.Log("invalid 'to' address")
    59  		return false
    60  	}
    61  	if amount < 0 {
    62  		runtime.Log("invalid amount")
    63  		return false
    64  	}
    65  
    66  	var fromBalance int
    67  	val := storage.Get(ctx, from)
    68  	if val != nil {
    69  		fromBalance = val.(int)
    70  	}
    71  	if fromBalance < amount {
    72  		runtime.Log("insufficient funds")
    73  		return false
    74  	}
    75  	fromBalance -= amount
    76  	storage.Put(ctx, from, fromBalance)
    77  
    78  	var toBalance int
    79  	val = storage.Get(ctx, to)
    80  	if val != nil {
    81  		toBalance = val.(int)
    82  	}
    83  	toBalance += amount
    84  	storage.Put(ctx, to, toBalance)
    85  
    86  	runtime.Notify("Transfer", from, to, amount)
    87  	if management.GetContract(to) != nil {
    88  		contract.Call(to, "onNEP17Payment", contract.All, from, amount, data)
    89  	}
    90  	return true
    91  }
    92  
    93  func BalanceOf(account interop.Hash160) int {
    94  	ctx := storage.GetContext()
    95  	if len(account) != 20 {
    96  		panic("invalid address")
    97  	}
    98  	var amount int
    99  	val := storage.Get(ctx, account)
   100  	if val != nil {
   101  		amount = val.(int)
   102  	}
   103  	return amount
   104  }
   105  
   106  func Symbol() string {
   107  	return "RUB"
   108  }
   109  
   110  func Decimals() int {
   111  	return decimals
   112  }
   113  
   114  func TotalSupply() int {
   115  	return totalSupply
   116  }
   117  
   118  func PutValue(key []byte, value []byte) bool {
   119  	ctx := storage.GetContext()
   120  	storage.Put(ctx, key, value)
   121  	return true
   122  }