[#23] alphabet: Add OnPayment method

Without OnPayment method alphabet contracts won't be able
to receive NEO and produce GAS.

Signed-off-by: Alex Vanin <alexey@nspcc.ru>
This commit is contained in:
Alex Vanin 2020-12-10 13:41:16 +03:00 committed by Alex Vanin
parent 37d8905d55
commit bd45e50480
8 changed files with 86 additions and 14 deletions

View file

@ -1,6 +1,7 @@
package alphabetcontract package alphabetcontract
import ( import (
"github.com/nspcc-dev/neo-go/pkg/interop"
"github.com/nspcc-dev/neo-go/pkg/interop/binary" "github.com/nspcc-dev/neo-go/pkg/interop/binary"
"github.com/nspcc-dev/neo-go/pkg/interop/contract" "github.com/nspcc-dev/neo-go/pkg/interop/contract"
"github.com/nspcc-dev/neo-go/pkg/interop/crypto" "github.com/nspcc-dev/neo-go/pkg/interop/crypto"
@ -51,6 +52,14 @@ func init() {
ctx = storage.GetContext() ctx = storage.GetContext()
} }
// OnPayment is a callback for NEP-17 compatible native GAS and NEO contracts.
func OnPayment(from interop.Hash160, amount int, data interface{}) {
caller := runtime.GetCallingScriptHash()
if !bytesEqual(caller, []byte(gasHash)) && !bytesEqual(caller, []byte(neoHash)) {
panic("onPayment: alphabet contract accepts GAS and NEO only")
}
}
func Init(addrNetmap []byte) { func Init(addrNetmap []byte) {
if storage.Get(ctx, netmapContractKey) != nil { if storage.Get(ctx, netmapContractKey) != nil {
panic("contract already deployed") panic("contract already deployed")

View file

@ -1,6 +1,7 @@
package alphabetcontract package alphabetcontract
import ( import (
"github.com/nspcc-dev/neo-go/pkg/interop"
"github.com/nspcc-dev/neo-go/pkg/interop/binary" "github.com/nspcc-dev/neo-go/pkg/interop/binary"
"github.com/nspcc-dev/neo-go/pkg/interop/contract" "github.com/nspcc-dev/neo-go/pkg/interop/contract"
"github.com/nspcc-dev/neo-go/pkg/interop/crypto" "github.com/nspcc-dev/neo-go/pkg/interop/crypto"
@ -33,7 +34,7 @@ const (
netmapContractKey = "netmapScriptHash" netmapContractKey = "netmapScriptHash"
threshold = totalAlphabetContracts * 2 / 3 + 1 threshold = totalAlphabetContracts*2/3 + 1
voteKey = "ballots" voteKey = "ballots"
totalAlphabetContracts = 7 totalAlphabetContracts = 7
@ -51,6 +52,14 @@ func init() {
ctx = storage.GetContext() ctx = storage.GetContext()
} }
// OnPayment is a callback for NEP-17 compatible native GAS and NEO contracts.
func OnPayment(from interop.Hash160, amount int, data interface{}) {
caller := runtime.GetCallingScriptHash()
if !bytesEqual(caller, []byte(gasHash)) && !bytesEqual(caller, []byte(neoHash)) {
panic("onPayment: alphabet contract accepts GAS and NEO only")
}
}
func Init(addrNetmap []byte) { func Init(addrNetmap []byte) {
if storage.Get(ctx, netmapContractKey) != nil { if storage.Get(ctx, netmapContractKey) != nil {
panic("contract already deployed") panic("contract already deployed")
@ -264,7 +273,7 @@ func bytesEqual(a []byte, b []byte) bool {
func voteID(epoch interface{}, args [][]byte) []byte { func voteID(epoch interface{}, args [][]byte) []byte {
var ( var (
result []byte result []byte
epochBytes = epoch.([]byte) epochBytes = epoch.([]byte)
) )

View file

@ -1,6 +1,7 @@
package alphabetcontract package alphabetcontract
import ( import (
"github.com/nspcc-dev/neo-go/pkg/interop"
"github.com/nspcc-dev/neo-go/pkg/interop/binary" "github.com/nspcc-dev/neo-go/pkg/interop/binary"
"github.com/nspcc-dev/neo-go/pkg/interop/contract" "github.com/nspcc-dev/neo-go/pkg/interop/contract"
"github.com/nspcc-dev/neo-go/pkg/interop/crypto" "github.com/nspcc-dev/neo-go/pkg/interop/crypto"
@ -33,7 +34,7 @@ const (
netmapContractKey = "netmapScriptHash" netmapContractKey = "netmapScriptHash"
threshold = totalAlphabetContracts * 2 / 3 + 1 threshold = totalAlphabetContracts*2/3 + 1
voteKey = "ballots" voteKey = "ballots"
totalAlphabetContracts = 7 totalAlphabetContracts = 7
@ -51,6 +52,14 @@ func init() {
ctx = storage.GetContext() ctx = storage.GetContext()
} }
// OnPayment is a callback for NEP-17 compatible native GAS and NEO contracts.
func OnPayment(from interop.Hash160, amount int, data interface{}) {
caller := runtime.GetCallingScriptHash()
if !bytesEqual(caller, []byte(gasHash)) && !bytesEqual(caller, []byte(neoHash)) {
panic("onPayment: alphabet contract accepts GAS and NEO only")
}
}
func Init(addrNetmap []byte) { func Init(addrNetmap []byte) {
if storage.Get(ctx, netmapContractKey) != nil { if storage.Get(ctx, netmapContractKey) != nil {
panic("contract already deployed") panic("contract already deployed")
@ -264,7 +273,7 @@ func bytesEqual(a []byte, b []byte) bool {
func voteID(epoch interface{}, args [][]byte) []byte { func voteID(epoch interface{}, args [][]byte) []byte {
var ( var (
result []byte result []byte
epochBytes = epoch.([]byte) epochBytes = epoch.([]byte)
) )

View file

@ -1,6 +1,7 @@
package alphabetcontract package alphabetcontract
import ( import (
"github.com/nspcc-dev/neo-go/pkg/interop"
"github.com/nspcc-dev/neo-go/pkg/interop/binary" "github.com/nspcc-dev/neo-go/pkg/interop/binary"
"github.com/nspcc-dev/neo-go/pkg/interop/contract" "github.com/nspcc-dev/neo-go/pkg/interop/contract"
"github.com/nspcc-dev/neo-go/pkg/interop/crypto" "github.com/nspcc-dev/neo-go/pkg/interop/crypto"
@ -33,7 +34,7 @@ const (
netmapContractKey = "netmapScriptHash" netmapContractKey = "netmapScriptHash"
threshold = totalAlphabetContracts * 2 / 3 + 1 threshold = totalAlphabetContracts*2/3 + 1
voteKey = "ballots" voteKey = "ballots"
totalAlphabetContracts = 7 totalAlphabetContracts = 7
@ -51,6 +52,14 @@ func init() {
ctx = storage.GetContext() ctx = storage.GetContext()
} }
// OnPayment is a callback for NEP-17 compatible native GAS and NEO contracts.
func OnPayment(from interop.Hash160, amount int, data interface{}) {
caller := runtime.GetCallingScriptHash()
if !bytesEqual(caller, []byte(gasHash)) && !bytesEqual(caller, []byte(neoHash)) {
panic("onPayment: alphabet contract accepts GAS and NEO only")
}
}
func Init(addrNetmap []byte) { func Init(addrNetmap []byte) {
if storage.Get(ctx, netmapContractKey) != nil { if storage.Get(ctx, netmapContractKey) != nil {
panic("contract already deployed") panic("contract already deployed")
@ -264,7 +273,7 @@ func bytesEqual(a []byte, b []byte) bool {
func voteID(epoch interface{}, args [][]byte) []byte { func voteID(epoch interface{}, args [][]byte) []byte {
var ( var (
result []byte result []byte
epochBytes = epoch.([]byte) epochBytes = epoch.([]byte)
) )

View file

@ -1,6 +1,7 @@
package alphabetcontract package alphabetcontract
import ( import (
"github.com/nspcc-dev/neo-go/pkg/interop"
"github.com/nspcc-dev/neo-go/pkg/interop/binary" "github.com/nspcc-dev/neo-go/pkg/interop/binary"
"github.com/nspcc-dev/neo-go/pkg/interop/contract" "github.com/nspcc-dev/neo-go/pkg/interop/contract"
"github.com/nspcc-dev/neo-go/pkg/interop/crypto" "github.com/nspcc-dev/neo-go/pkg/interop/crypto"
@ -33,7 +34,7 @@ const (
netmapContractKey = "netmapScriptHash" netmapContractKey = "netmapScriptHash"
threshold = totalAlphabetContracts * 2 / 3 + 1 threshold = totalAlphabetContracts*2/3 + 1
voteKey = "ballots" voteKey = "ballots"
totalAlphabetContracts = 7 totalAlphabetContracts = 7
@ -51,6 +52,14 @@ func init() {
ctx = storage.GetContext() ctx = storage.GetContext()
} }
// OnPayment is a callback for NEP-17 compatible native GAS and NEO contracts.
func OnPayment(from interop.Hash160, amount int, data interface{}) {
caller := runtime.GetCallingScriptHash()
if !bytesEqual(caller, []byte(gasHash)) && !bytesEqual(caller, []byte(neoHash)) {
panic("onPayment: alphabet contract accepts GAS and NEO only")
}
}
func Init(addrNetmap []byte) { func Init(addrNetmap []byte) {
if storage.Get(ctx, netmapContractKey) != nil { if storage.Get(ctx, netmapContractKey) != nil {
panic("contract already deployed") panic("contract already deployed")
@ -264,7 +273,7 @@ func bytesEqual(a []byte, b []byte) bool {
func voteID(epoch interface{}, args [][]byte) []byte { func voteID(epoch interface{}, args [][]byte) []byte {
var ( var (
result []byte result []byte
epochBytes = epoch.([]byte) epochBytes = epoch.([]byte)
) )

View file

@ -1,6 +1,7 @@
package alphabetcontract package alphabetcontract
import ( import (
"github.com/nspcc-dev/neo-go/pkg/interop"
"github.com/nspcc-dev/neo-go/pkg/interop/binary" "github.com/nspcc-dev/neo-go/pkg/interop/binary"
"github.com/nspcc-dev/neo-go/pkg/interop/contract" "github.com/nspcc-dev/neo-go/pkg/interop/contract"
"github.com/nspcc-dev/neo-go/pkg/interop/crypto" "github.com/nspcc-dev/neo-go/pkg/interop/crypto"
@ -33,7 +34,7 @@ const (
netmapContractKey = "netmapScriptHash" netmapContractKey = "netmapScriptHash"
threshold = totalAlphabetContracts * 2 / 3 + 1 threshold = totalAlphabetContracts*2/3 + 1
voteKey = "ballots" voteKey = "ballots"
totalAlphabetContracts = 7 totalAlphabetContracts = 7
@ -51,6 +52,14 @@ func init() {
ctx = storage.GetContext() ctx = storage.GetContext()
} }
// OnPayment is a callback for NEP-17 compatible native GAS and NEO contracts.
func OnPayment(from interop.Hash160, amount int, data interface{}) {
caller := runtime.GetCallingScriptHash()
if !bytesEqual(caller, []byte(gasHash)) && !bytesEqual(caller, []byte(neoHash)) {
panic("onPayment: alphabet contract accepts GAS and NEO only")
}
}
func Init(addrNetmap []byte) { func Init(addrNetmap []byte) {
if storage.Get(ctx, netmapContractKey) != nil { if storage.Get(ctx, netmapContractKey) != nil {
panic("contract already deployed") panic("contract already deployed")
@ -264,7 +273,7 @@ func bytesEqual(a []byte, b []byte) bool {
func voteID(epoch interface{}, args [][]byte) []byte { func voteID(epoch interface{}, args [][]byte) []byte {
var ( var (
result []byte result []byte
epochBytes = epoch.([]byte) epochBytes = epoch.([]byte)
) )

View file

@ -1,6 +1,7 @@
package alphabetcontract package alphabetcontract
import ( import (
"github.com/nspcc-dev/neo-go/pkg/interop"
"github.com/nspcc-dev/neo-go/pkg/interop/binary" "github.com/nspcc-dev/neo-go/pkg/interop/binary"
"github.com/nspcc-dev/neo-go/pkg/interop/contract" "github.com/nspcc-dev/neo-go/pkg/interop/contract"
"github.com/nspcc-dev/neo-go/pkg/interop/crypto" "github.com/nspcc-dev/neo-go/pkg/interop/crypto"
@ -33,7 +34,7 @@ const (
netmapContractKey = "netmapScriptHash" netmapContractKey = "netmapScriptHash"
threshold = totalAlphabetContracts * 2 / 3 + 1 threshold = totalAlphabetContracts*2/3 + 1
voteKey = "ballots" voteKey = "ballots"
totalAlphabetContracts = 7 totalAlphabetContracts = 7
@ -51,6 +52,14 @@ func init() {
ctx = storage.GetContext() ctx = storage.GetContext()
} }
// OnPayment is a callback for NEP-17 compatible native GAS and NEO contracts.
func OnPayment(from interop.Hash160, amount int, data interface{}) {
caller := runtime.GetCallingScriptHash()
if !bytesEqual(caller, []byte(gasHash)) && !bytesEqual(caller, []byte(neoHash)) {
panic("onPayment: alphabet contract accepts GAS and NEO only")
}
}
func Init(addrNetmap []byte) { func Init(addrNetmap []byte) {
if storage.Get(ctx, netmapContractKey) != nil { if storage.Get(ctx, netmapContractKey) != nil {
panic("contract already deployed") panic("contract already deployed")
@ -264,7 +273,7 @@ func bytesEqual(a []byte, b []byte) bool {
func voteID(epoch interface{}, args [][]byte) []byte { func voteID(epoch interface{}, args [][]byte) []byte {
var ( var (
result []byte result []byte
epochBytes = epoch.([]byte) epochBytes = epoch.([]byte)
) )

View file

@ -1,6 +1,7 @@
package alphabetcontract package alphabetcontract
import ( import (
"github.com/nspcc-dev/neo-go/pkg/interop"
"github.com/nspcc-dev/neo-go/pkg/interop/binary" "github.com/nspcc-dev/neo-go/pkg/interop/binary"
"github.com/nspcc-dev/neo-go/pkg/interop/contract" "github.com/nspcc-dev/neo-go/pkg/interop/contract"
"github.com/nspcc-dev/neo-go/pkg/interop/crypto" "github.com/nspcc-dev/neo-go/pkg/interop/crypto"
@ -33,7 +34,7 @@ const (
netmapContractKey = "netmapScriptHash" netmapContractKey = "netmapScriptHash"
threshold = totalAlphabetContracts * 2 / 3 + 1 threshold = totalAlphabetContracts*2/3 + 1
voteKey = "ballots" voteKey = "ballots"
totalAlphabetContracts = 7 totalAlphabetContracts = 7
@ -51,6 +52,14 @@ func init() {
ctx = storage.GetContext() ctx = storage.GetContext()
} }
// OnPayment is a callback for NEP-17 compatible native GAS and NEO contracts.
func OnPayment(from interop.Hash160, amount int, data interface{}) {
caller := runtime.GetCallingScriptHash()
if !bytesEqual(caller, []byte(gasHash)) && !bytesEqual(caller, []byte(neoHash)) {
panic("onPayment: alphabet contract accepts GAS and NEO only")
}
}
func Init(addrNetmap []byte) { func Init(addrNetmap []byte) {
if storage.Get(ctx, netmapContractKey) != nil { if storage.Get(ctx, netmapContractKey) != nil {
panic("contract already deployed") panic("contract already deployed")
@ -264,7 +273,7 @@ func bytesEqual(a []byte, b []byte) bool {
func voteID(epoch interface{}, args [][]byte) []byte { func voteID(epoch interface{}, args [][]byte) []byte {
var ( var (
result []byte result []byte
epochBytes = epoch.([]byte) epochBytes = epoch.([]byte)
) )