examples: use interop equality helpers where possible

This commit is contained in:
Anna Shaleva 2022-07-11 17:29:25 +03:00
parent c0186f8224
commit 079f68a8c3
5 changed files with 11 additions and 10 deletions

View file

@ -203,7 +203,7 @@ func Transfer(to interop.Hash160, token []byte, data interface{}) bool {
key := mkBalanceKey(owner, token)
amount := getBalanceOf(ctx, key)
if string(owner) != string(to) {
if !owner.Equals(to) {
addToBalance(ctx, owner, token, -amount)
removeOwner(ctx, token, owner)
@ -293,7 +293,7 @@ func TransferDivisible(from, to interop.Hash160, amount int, token []byte, data
return false
}
if string(from) != string(to) {
if !from.Equals(to) {
updBalance := addToBalance(ctx, from, token, -amount)
if updBalance == 0 {
removeOwner(ctx, token, from)
@ -357,7 +357,8 @@ func OnNEP17Payment(from interop.Hash160, amount int, data interface{}) {
util.Abort()
}
}()
if string(runtime.GetCallingScriptHash()) != gas.Hash {
callingHash := runtime.GetCallingScriptHash()
if !callingHash.Equals(gas.Hash) {
panic("only GAS is accepted")
}
if amount < 10_00000000 {

View file

@ -18,7 +18,6 @@ import (
"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.
@ -164,7 +163,7 @@ func Transfer(to interop.Hash160, tokenID []byte, data interface{}) bool {
if !runtime.CheckWitness(from) {
return false
}
if !util.Equals(from, to) {
if !from.Equals(to) {
// update token info
ns.Owner = to
ns.Admin = nil

View file

@ -186,7 +186,7 @@ func Transfer(to interop.Hash160, token []byte, data interface{}) bool {
return false
}
if string(owner) != string(to) {
if !owner.Equals(to) {
addToBalance(ctx, owner, -1)
removeToken(ctx, owner, token)
@ -216,7 +216,8 @@ func OnNEP17Payment(from interop.Hash160, amount int, data interface{}) {
util.Abort()
}
}()
if string(runtime.GetCallingScriptHash()) != gas.Hash {
callingHash := runtime.GetCallingScriptHash()
if !callingHash.Equals(gas.Hash) {
panic("only GAS is accepted")
}
if amount < 10_00000000 {

View file

@ -26,7 +26,8 @@ func FilteredRequest(url string, filter []byte) {
func OracleCallback(url string, data interface{}, code int, res []byte) {
// This function shouldn't be called directly, we only expect oracle native
// contract to be calling it.
if string(runtime.GetCallingScriptHash()) != oracle.Hash {
callingHash := runtime.GetCallingScriptHash()
if !callingHash.Equals(oracle.Hash) {
panic("not called from oracle contract")
}
if code != oracle.Success {

View file

@ -6,7 +6,6 @@ import (
"github.com/nspcc-dev/neo-go/pkg/interop/native/management"
"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"
)
// Token holds all token info
@ -105,7 +104,7 @@ func IsUsableAddress(addr []byte) bool {
// Check if a smart contract is calling scripthash
callingScriptHash := runtime.GetCallingScriptHash()
if util.Equals(callingScriptHash, addr) {
if callingScriptHash.Equals(addr) {
return true
}
}