2020-02-18 08:09:07 +00:00
|
|
|
package testdata
|
|
|
|
|
2020-03-05 09:28:46 +00:00
|
|
|
import (
|
2021-03-22 09:21:48 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/interop"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/interop/contract"
|
2021-09-16 14:09:42 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/interop/native/ledger"
|
2021-03-22 09:21:48 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/interop/native/management"
|
2021-09-16 14:09:42 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/interop/native/neo"
|
2020-03-05 09:28:46 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/interop/runtime"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/interop/storage"
|
2021-09-16 14:09:42 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/interop/util"
|
2020-03-05 09:28:46 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
totalSupply = 1000000
|
|
|
|
decimals = 2
|
|
|
|
)
|
2020-02-18 08:09:07 +00:00
|
|
|
|
2021-09-16 14:09:42 +00:00
|
|
|
var owner = util.FromAddress("NbrUYaZgyhSkNoRo9ugRyEMdUZxrhkNaWB")
|
|
|
|
|
2020-07-23 15:13:02 +00:00
|
|
|
func Init() bool {
|
|
|
|
ctx := storage.GetContext()
|
|
|
|
h := runtime.GetExecutingScriptHash()
|
|
|
|
amount := totalSupply
|
|
|
|
storage.Put(ctx, h, amount)
|
2020-11-19 15:01:42 +00:00
|
|
|
runtime.Notify("Transfer", []byte{}, h, amount)
|
2020-07-23 15:13:02 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2021-09-16 14:09:42 +00:00
|
|
|
func OnNEP17Payment(from interop.Hash160, amount int, data interface{}) {
|
|
|
|
curr := runtime.GetExecutingScriptHash()
|
|
|
|
balance := neo.BalanceOf(curr)
|
|
|
|
if ledger.CurrentIndex() >= 100 {
|
|
|
|
ok := neo.Transfer(curr, owner, balance, nil)
|
|
|
|
if !ok {
|
|
|
|
panic("owner transfer failed")
|
|
|
|
}
|
|
|
|
ok = neo.Transfer(curr, owner, 0, nil)
|
|
|
|
if !ok {
|
|
|
|
panic("owner transfer failed")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify always returns true and is aimed to serve the TestNEO_RecursiveGASMint.
|
|
|
|
func Verify() bool {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2021-03-22 09:21:48 +00:00
|
|
|
func Transfer(from, to interop.Hash160, amount int, data interface{}) bool {
|
2020-07-23 15:13:02 +00:00
|
|
|
ctx := storage.GetContext()
|
|
|
|
if len(from) != 20 {
|
|
|
|
runtime.Log("invalid 'from' address")
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if len(to) != 20 {
|
|
|
|
runtime.Log("invalid 'to' address")
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if amount < 0 {
|
|
|
|
runtime.Log("invalid amount")
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
var fromBalance int
|
|
|
|
val := storage.Get(ctx, from)
|
|
|
|
if val != nil {
|
|
|
|
fromBalance = val.(int)
|
|
|
|
}
|
|
|
|
if fromBalance < amount {
|
|
|
|
runtime.Log("insufficient funds")
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
fromBalance -= amount
|
|
|
|
storage.Put(ctx, from, fromBalance)
|
2020-03-05 09:28:46 +00:00
|
|
|
|
2020-07-23 15:13:02 +00:00
|
|
|
var toBalance int
|
|
|
|
val = storage.Get(ctx, to)
|
|
|
|
if val != nil {
|
|
|
|
toBalance = val.(int)
|
|
|
|
}
|
|
|
|
toBalance += amount
|
|
|
|
storage.Put(ctx, to, toBalance)
|
2020-03-05 09:28:46 +00:00
|
|
|
|
2020-11-19 15:01:42 +00:00
|
|
|
runtime.Notify("Transfer", from, to, amount)
|
2021-03-22 09:21:48 +00:00
|
|
|
if management.GetContract(to) != nil {
|
|
|
|
contract.Call(to, "onNEP17Payment", contract.All, from, amount, data)
|
|
|
|
}
|
2020-07-23 15:13:02 +00:00
|
|
|
return true
|
|
|
|
}
|
2020-03-05 09:28:46 +00:00
|
|
|
|
2021-03-22 09:21:48 +00:00
|
|
|
func BalanceOf(account interop.Hash160) int {
|
2020-07-23 15:13:02 +00:00
|
|
|
ctx := storage.GetContext()
|
2021-03-22 09:21:48 +00:00
|
|
|
if len(account) != 20 {
|
|
|
|
panic("invalid address")
|
2020-03-05 09:28:46 +00:00
|
|
|
}
|
2020-07-23 15:13:02 +00:00
|
|
|
var amount int
|
2021-03-22 09:21:48 +00:00
|
|
|
val := storage.Get(ctx, account)
|
2020-07-23 15:13:02 +00:00
|
|
|
if val != nil {
|
|
|
|
amount = val.(int)
|
|
|
|
}
|
|
|
|
return amount
|
|
|
|
}
|
|
|
|
|
|
|
|
func Symbol() string {
|
|
|
|
return "RUB"
|
|
|
|
}
|
|
|
|
|
|
|
|
func Decimals() int {
|
|
|
|
return decimals
|
|
|
|
}
|
|
|
|
|
|
|
|
func TotalSupply() int {
|
|
|
|
return totalSupply
|
|
|
|
}
|
|
|
|
|
|
|
|
func PutValue(key []byte, value []byte) bool {
|
|
|
|
ctx := storage.GetContext()
|
|
|
|
storage.Put(ctx, key, value)
|
|
|
|
return true
|
2020-02-18 08:09:07 +00:00
|
|
|
}
|