[#47] Use native GAS and NEO contract wrappers

Signed-off-by: Alex Vanin <alexey@nspcc.ru>
This commit is contained in:
Alex Vanin 2021-02-09 14:55:58 +03:00 committed by Alex Vanin
parent 76c63b7d5c
commit 79e42fc31b
2 changed files with 15 additions and 33 deletions

View file

@ -5,18 +5,14 @@ import (
"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"
"github.com/nspcc-dev/neo-go/pkg/interop/native/gas"
"github.com/nspcc-dev/neo-go/pkg/interop/native/neo"
"github.com/nspcc-dev/neo-go/pkg/interop/runtime" "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/storage"
"github.com/nspcc-dev/neofs-contract/common" "github.com/nspcc-dev/neofs-contract/common"
) )
const ( const (
// native gas token script hash
gasHash = "\x28\xb3\xad\xab\x72\x69\xf9\xc2\x18\x1d\xb3\xcb\x74\x1e\xbf\x55\x19\x30\xe2\x70"
// native neo token script hash
neoHash = "\x83\xab\x06\x79\xad\x55\xc0\x50\xa1\x3a\xd4\x3f\x59\x36\xea\x73\xf5\xeb\x1e\xf6"
netmapKey = "netmapScriptHash" netmapKey = "netmapScriptHash"
indexKey = "index" indexKey = "index"
totalKey = "threshold" totalKey = "threshold"
@ -33,7 +29,7 @@ func init() {
// OnNEP17Payment is a callback for NEP-17 compatible native GAS and NEO contracts. // OnNEP17Payment is a callback for NEP-17 compatible native GAS and NEO contracts.
func OnNEP17Payment(from interop.Hash160, amount int, data interface{}) { func OnNEP17Payment(from interop.Hash160, amount int, data interface{}) {
caller := runtime.GetCallingScriptHash() caller := runtime.GetCallingScriptHash()
if !common.BytesEqual(caller, []byte(gasHash)) && !common.BytesEqual(caller, []byte(neoHash)) { if !common.BytesEqual(caller, []byte(gas.Hash)) && !common.BytesEqual(caller, []byte(neo.Hash)) {
panic("onNEP17Payment: alphabet contract accepts GAS and NEO only") panic("onNEP17Payment: alphabet contract accepts GAS and NEO only")
} }
} }
@ -60,18 +56,11 @@ func Init(addrNetmap []byte, name string, index, total int) {
} }
func Gas() int { func Gas() int {
contractHash := runtime.GetExecutingScriptHash() return gas.BalanceOf(runtime.GetExecutingScriptHash())
return balance(gasHash, contractHash)
} }
func Neo() int { func Neo() int {
contractHash := runtime.GetExecutingScriptHash() return neo.BalanceOf(runtime.GetExecutingScriptHash())
return balance(neoHash, contractHash)
}
func balance(hash string, addr []byte) int {
balance := contract.Call([]byte(hash), "balanceOf", contract.ReadOnly, addr)
return balance.(int)
} }
func irList() []common.IRNode { func irList() []common.IRNode {
@ -123,12 +112,11 @@ func Emit() bool {
} }
contractHash := runtime.GetExecutingScriptHash() contractHash := runtime.GetExecutingScriptHash()
neo := balance(neoHash, contractHash)
_ = contract.Call([]byte(neoHash), "transfer", contract.All, contractHash, contractHash, neo, nil) _ = neo.Transfer(contractHash, contractHash, neo.BalanceOf(contractHash), nil)
gas := balance(gasHash, contractHash) gasBalance := gas.BalanceOf(contractHash)
gasPerNode := gas * 7 / 8 / len(innerRingKeys) gasPerNode := gasBalance * 7 / 8 / len(innerRingKeys)
if gasPerNode == 0 { if gasPerNode == 0 {
runtime.Log("no gas to emit") runtime.Log("no gas to emit")
@ -139,7 +127,7 @@ func Emit() bool {
node := innerRingKeys[i] node := innerRingKeys[i]
address := contract.CreateStandardAccount(node.PublicKey) address := contract.CreateStandardAccount(node.PublicKey)
_ = contract.Call([]byte(gasHash), "transfer", contract.All, contractHash, address, gasPerNode, nil) _ = gas.Transfer(contractHash, address, gasPerNode, nil)
} }
runtime.Log("utility token has been emitted to inner ring nodes") runtime.Log("utility token has been emitted to inner ring nodes")
@ -171,7 +159,7 @@ func Vote(epoch int, candidates [][]byte) {
candidate := candidates[index%len(candidates)] candidate := candidates[index%len(candidates)]
address := runtime.GetExecutingScriptHash() address := runtime.GetExecutingScriptHash()
ok := contract.Call([]byte(neoHash), "vote", contract.All, address, candidate).(bool) ok := neo.Vote(address, candidate)
if ok { if ok {
runtime.Log(name + ": successfully voted for validator") runtime.Log(name + ": successfully voted for validator")
removeVotes(ctx, id) removeVotes(ctx, id)

View file

@ -37,6 +37,7 @@ import (
"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"
"github.com/nspcc-dev/neo-go/pkg/interop/iterator" "github.com/nspcc-dev/neo-go/pkg/interop/iterator"
"github.com/nspcc-dev/neo-go/pkg/interop/native/gas"
"github.com/nspcc-dev/neo-go/pkg/interop/runtime" "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/storage"
"github.com/nspcc-dev/neofs-contract/common" "github.com/nspcc-dev/neofs-contract/common"
@ -54,9 +55,6 @@ type (
) )
const ( const (
// native gas token script hash
tokenHash = "\x28\xb3\xad\xab\x72\x69\xf9\xc2\x18\x1d\xb3\xcb\x74\x1e\xbf\x55\x19\x30\xe2\x70"
defaultCandidateFee = 100 * 1_0000_0000 // 100 Fixed8 Gas defaultCandidateFee = 100 * 1_0000_0000 // 100 Fixed8 Gas
candidateFeeConfigKey = "InnerRingCandidateFee" candidateFeeConfigKey = "InnerRingCandidateFee"
@ -177,9 +175,7 @@ func InnerRingCandidateAdd(key []byte) bool {
to := runtime.GetExecutingScriptHash() to := runtime.GetExecutingScriptHash()
fee := getConfig(ctx, candidateFeeConfigKey).(int) fee := getConfig(ctx, candidateFeeConfigKey).(int)
transferred := contract.Call([]byte(tokenHash), transferred := gas.Transfer(from, to, fee, []byte(ignoreDepositNotification))
"transfer", contract.All, from, to, fee,
[]byte(ignoreDepositNotification)).(bool)
if !transferred { if !transferred {
panic("irCandidateAdd: failed to transfer funds, aborting") panic("irCandidateAdd: failed to transfer funds, aborting")
} }
@ -198,7 +194,7 @@ func OnNEP17Payment(from interop.Hash160, amount int, data interface{}) {
} }
caller := runtime.GetCallingScriptHash() caller := runtime.GetCallingScriptHash()
if !common.BytesEqual(caller, []byte(tokenHash)) { if !common.BytesEqual(caller, []byte(gas.Hash)) {
panic("onNEP17Payment: only GAS can be accepted for deposit") panic("onNEP17Payment: only GAS can be accepted for deposit")
} }
@ -233,8 +229,7 @@ func Deposit(from interop.Hash160, amount int, rcv interop.Hash160) bool {
to := runtime.GetExecutingScriptHash() to := runtime.GetExecutingScriptHash()
transferred := contract.Call([]byte(tokenHash), "transfer", transferred := gas.Transfer(from, to, amount, rcv)
contract.All, from, to, amount, rcv).(bool)
if !transferred { if !transferred {
panic("deposit: failed to transfer funds, aborting") panic("deposit: failed to transfer funds, aborting")
} }
@ -293,8 +288,7 @@ func Cheque(id, user []byte, amount int, lockAcc []byte) bool {
from := runtime.GetExecutingScriptHash() from := runtime.GetExecutingScriptHash()
transferred := contract.Call([]byte(tokenHash), transferred := gas.Transfer(from, user, amount, nil)
"transfer", contract.All, from, user, amount, nil).(bool)
if !transferred { if !transferred {
panic("cheque: failed to transfer funds, aborting") panic("cheque: failed to transfer funds, aborting")
} }