2020-02-18 08:09:07 +00:00
|
|
|
package testdata
|
|
|
|
|
2020-03-05 09:28:46 +00:00
|
|
|
import (
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/interop/engine"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/interop/runtime"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/interop/storage"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
totalSupply = 1000000
|
|
|
|
decimals = 2
|
|
|
|
)
|
2020-02-18 08:09:07 +00:00
|
|
|
|
|
|
|
func Main(operation string, args []interface{}) interface{} {
|
2020-04-05 15:14:54 +00:00
|
|
|
runtime.Notify("contract call", operation, args)
|
2020-03-05 09:28:46 +00:00
|
|
|
switch operation {
|
|
|
|
case "Put":
|
|
|
|
ctx := storage.GetContext()
|
|
|
|
storage.Put(ctx, args[0].([]byte), args[1].([]byte))
|
|
|
|
return true
|
|
|
|
case "totalSupply":
|
|
|
|
return totalSupply
|
|
|
|
case "decimals":
|
|
|
|
return decimals
|
|
|
|
case "name":
|
|
|
|
return "Rubl"
|
|
|
|
case "symbol":
|
|
|
|
return "RUB"
|
|
|
|
case "balanceOf":
|
|
|
|
ctx := storage.GetContext()
|
|
|
|
addr := args[0].([]byte)
|
|
|
|
if len(addr) != 20 {
|
|
|
|
runtime.Log("invalid address")
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
amount := storage.Get(ctx, addr).(int)
|
2020-04-05 15:14:54 +00:00
|
|
|
runtime.Notify("balanceOf", addr, amount)
|
2020-03-05 09:28:46 +00:00
|
|
|
return amount
|
|
|
|
case "transfer":
|
|
|
|
ctx := storage.GetContext()
|
|
|
|
from := args[0].([]byte)
|
|
|
|
if len(from) != 20 {
|
|
|
|
runtime.Log("invalid 'from' address")
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
to := args[1].([]byte)
|
|
|
|
if len(to) != 20 {
|
|
|
|
runtime.Log("invalid 'to' address")
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
amount := args[2].(int)
|
|
|
|
if amount < 0 {
|
|
|
|
runtime.Log("invalid amount")
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
fromBalance := storage.Get(ctx, from).(int)
|
|
|
|
if fromBalance < amount {
|
|
|
|
runtime.Log("insufficient funds")
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
fromBalance -= amount
|
|
|
|
storage.Put(ctx, from, fromBalance)
|
|
|
|
|
|
|
|
toBalance := storage.Get(ctx, to).(int)
|
|
|
|
toBalance += amount
|
|
|
|
storage.Put(ctx, to, toBalance)
|
|
|
|
|
2020-04-05 15:14:54 +00:00
|
|
|
runtime.Notify("transfer", from, to, amount)
|
2020-03-05 09:28:46 +00:00
|
|
|
|
|
|
|
return true
|
|
|
|
case "init":
|
|
|
|
ctx := storage.GetContext()
|
|
|
|
h := engine.GetExecutingScriptHash()
|
|
|
|
amount := totalSupply
|
|
|
|
storage.Put(ctx, h, amount)
|
2020-04-05 15:14:54 +00:00
|
|
|
runtime.Notify("transfer", []byte{}, h, amount)
|
2020-03-05 09:28:46 +00:00
|
|
|
return true
|
|
|
|
default:
|
|
|
|
panic("invalid operation")
|
|
|
|
}
|
2020-02-18 08:09:07 +00:00
|
|
|
}
|