2022-10-26 20:27:24 +00:00
|
|
|
// Package nextoken contains RPC wrappers for NEX Token contract.
|
|
|
|
package nextoken
|
|
|
|
|
|
|
|
import (
|
2023-05-24 08:52:14 +00:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
2022-10-27 13:20:35 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/transaction"
|
2022-10-26 20:27:24 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
|
2023-05-24 08:52:14 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/neorpc/result"
|
2022-10-26 20:27:24 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/rpcclient/nep17"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/rpcclient/unwrap"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/util"
|
2023-05-24 08:52:14 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/vm/stackitem"
|
2022-10-26 20:27:24 +00:00
|
|
|
"math/big"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Hash contains contract hash.
|
|
|
|
var Hash = util.Uint160{0xa8, 0x1a, 0xa1, 0xf0, 0x4b, 0xf, 0xdc, 0x4a, 0xa2, 0xce, 0xd5, 0xbf, 0xc6, 0x22, 0xcf, 0xe8, 0x9, 0x7f, 0xa6, 0xa2}
|
|
|
|
|
2023-05-24 08:52:14 +00:00
|
|
|
// OnMintEvent represents "OnMint" event emitted by the contract.
|
|
|
|
type OnMintEvent struct {
|
|
|
|
From util.Uint160
|
|
|
|
To util.Uint160
|
|
|
|
Amount *big.Int
|
|
|
|
SwapId *big.Int
|
|
|
|
}
|
|
|
|
|
2022-10-26 20:27:24 +00:00
|
|
|
// Invoker is used by ContractReader to call various safe methods.
|
|
|
|
type Invoker interface {
|
|
|
|
nep17.Invoker
|
|
|
|
}
|
|
|
|
|
2022-10-27 13:20:35 +00:00
|
|
|
// Actor is used by Contract to call state-changing methods.
|
|
|
|
type Actor interface {
|
|
|
|
Invoker
|
2022-11-08 13:36:38 +00:00
|
|
|
|
2022-10-27 13:20:35 +00:00
|
|
|
nep17.Actor
|
2022-11-08 13:36:38 +00:00
|
|
|
|
2023-04-03 10:34:24 +00:00
|
|
|
MakeCall(contract util.Uint160, method string, params ...any) (*transaction.Transaction, error)
|
2022-10-27 13:20:35 +00:00
|
|
|
MakeRun(script []byte) (*transaction.Transaction, error)
|
2023-04-03 10:34:24 +00:00
|
|
|
MakeUnsignedCall(contract util.Uint160, method string, attrs []transaction.Attribute, params ...any) (*transaction.Transaction, error)
|
2022-10-27 13:20:35 +00:00
|
|
|
MakeUnsignedRun(script []byte, attrs []transaction.Attribute) (*transaction.Transaction, error)
|
2023-04-03 10:34:24 +00:00
|
|
|
SendCall(contract util.Uint160, method string, params ...any) (util.Uint256, uint32, error)
|
2022-10-27 13:20:35 +00:00
|
|
|
SendRun(script []byte) (util.Uint256, uint32, error)
|
|
|
|
}
|
|
|
|
|
2022-10-26 20:27:24 +00:00
|
|
|
// ContractReader implements safe contract methods.
|
|
|
|
type ContractReader struct {
|
|
|
|
nep17.TokenReader
|
|
|
|
invoker Invoker
|
2023-05-11 10:25:45 +00:00
|
|
|
hash util.Uint160
|
2022-10-26 20:27:24 +00:00
|
|
|
}
|
|
|
|
|
2022-10-27 13:20:35 +00:00
|
|
|
// Contract implements all contract methods.
|
|
|
|
type Contract struct {
|
|
|
|
ContractReader
|
|
|
|
nep17.TokenWriter
|
|
|
|
actor Actor
|
2023-05-11 10:25:45 +00:00
|
|
|
hash util.Uint160
|
2022-10-27 13:20:35 +00:00
|
|
|
}
|
|
|
|
|
2022-10-26 20:27:24 +00:00
|
|
|
// NewReader creates an instance of ContractReader using Hash and the given Invoker.
|
|
|
|
func NewReader(invoker Invoker) *ContractReader {
|
2023-05-11 10:25:45 +00:00
|
|
|
var hash = Hash
|
|
|
|
return &ContractReader{*nep17.NewReader(invoker, hash), invoker, hash}
|
2022-10-26 20:27:24 +00:00
|
|
|
}
|
|
|
|
|
2022-10-27 13:20:35 +00:00
|
|
|
// New creates an instance of Contract using Hash and the given Actor.
|
|
|
|
func New(actor Actor) *Contract {
|
2023-05-11 10:25:45 +00:00
|
|
|
var hash = Hash
|
|
|
|
var nep17t = nep17.New(actor, hash)
|
|
|
|
return &Contract{ContractReader{nep17t.TokenReader, actor, hash}, nep17t.TokenWriter, actor, hash}
|
2022-10-27 13:20:35 +00:00
|
|
|
}
|
|
|
|
|
2022-10-26 20:27:24 +00:00
|
|
|
// Cap invokes `cap` method of contract.
|
|
|
|
func (c *ContractReader) Cap() (*big.Int, error) {
|
2023-05-11 10:25:45 +00:00
|
|
|
return unwrap.BigInt(c.invoker.Call(c.hash, "cap"))
|
2022-10-26 20:27:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetMinter invokes `getMinter` method of contract.
|
|
|
|
func (c *ContractReader) GetMinter() (*keys.PublicKey, error) {
|
2023-05-11 10:25:45 +00:00
|
|
|
return unwrap.PublicKey(c.invoker.Call(c.hash, "getMinter"))
|
2022-10-26 20:27:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetOwner invokes `getOwner` method of contract.
|
|
|
|
func (c *ContractReader) GetOwner() (util.Uint160, error) {
|
2023-05-11 10:25:45 +00:00
|
|
|
return unwrap.Uint160(c.invoker.Call(c.hash, "getOwner"))
|
2022-10-26 20:27:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// TotalMinted invokes `totalMinted` method of contract.
|
|
|
|
func (c *ContractReader) TotalMinted() (*big.Int, error) {
|
2023-05-11 10:25:45 +00:00
|
|
|
return unwrap.BigInt(c.invoker.Call(c.hash, "totalMinted"))
|
2022-10-26 20:27:24 +00:00
|
|
|
}
|
2022-10-27 13:20:35 +00:00
|
|
|
|
|
|
|
// ChangeMinter creates a transaction invoking `changeMinter` method of the contract.
|
|
|
|
// This transaction is signed and immediately sent to the network.
|
|
|
|
// The values returned are its hash, ValidUntilBlock value and error if any.
|
|
|
|
func (c *Contract) ChangeMinter(newMinter *keys.PublicKey) (util.Uint256, uint32, error) {
|
2023-05-11 10:25:45 +00:00
|
|
|
return c.actor.SendCall(c.hash, "changeMinter", newMinter)
|
2022-10-27 13:20:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ChangeMinterTransaction creates a transaction invoking `changeMinter` method of the contract.
|
|
|
|
// This transaction is signed, but not sent to the network, instead it's
|
|
|
|
// returned to the caller.
|
|
|
|
func (c *Contract) ChangeMinterTransaction(newMinter *keys.PublicKey) (*transaction.Transaction, error) {
|
2023-05-11 10:25:45 +00:00
|
|
|
return c.actor.MakeCall(c.hash, "changeMinter", newMinter)
|
2022-10-27 13:20:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ChangeMinterUnsigned creates a transaction invoking `changeMinter` method of the contract.
|
|
|
|
// This transaction is not signed, it's simply returned to the caller.
|
|
|
|
// Any fields of it that do not affect fees can be changed (ValidUntilBlock,
|
|
|
|
// Nonce), fee values (NetworkFee, SystemFee) can be increased as well.
|
|
|
|
func (c *Contract) ChangeMinterUnsigned(newMinter *keys.PublicKey) (*transaction.Transaction, error) {
|
2023-05-11 10:25:45 +00:00
|
|
|
return c.actor.MakeUnsignedCall(c.hash, "changeMinter", nil, newMinter)
|
2022-10-27 13:20:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ChangeOwner creates a transaction invoking `changeOwner` method of the contract.
|
|
|
|
// This transaction is signed and immediately sent to the network.
|
|
|
|
// The values returned are its hash, ValidUntilBlock value and error if any.
|
|
|
|
func (c *Contract) ChangeOwner(newOwner util.Uint160) (util.Uint256, uint32, error) {
|
2023-05-11 10:25:45 +00:00
|
|
|
return c.actor.SendCall(c.hash, "changeOwner", newOwner)
|
2022-10-27 13:20:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ChangeOwnerTransaction creates a transaction invoking `changeOwner` method of the contract.
|
|
|
|
// This transaction is signed, but not sent to the network, instead it's
|
|
|
|
// returned to the caller.
|
|
|
|
func (c *Contract) ChangeOwnerTransaction(newOwner util.Uint160) (*transaction.Transaction, error) {
|
2023-05-11 10:25:45 +00:00
|
|
|
return c.actor.MakeCall(c.hash, "changeOwner", newOwner)
|
2022-10-27 13:20:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ChangeOwnerUnsigned creates a transaction invoking `changeOwner` method of the contract.
|
|
|
|
// This transaction is not signed, it's simply returned to the caller.
|
|
|
|
// Any fields of it that do not affect fees can be changed (ValidUntilBlock,
|
|
|
|
// Nonce), fee values (NetworkFee, SystemFee) can be increased as well.
|
|
|
|
func (c *Contract) ChangeOwnerUnsigned(newOwner util.Uint160) (*transaction.Transaction, error) {
|
2023-05-11 10:25:45 +00:00
|
|
|
return c.actor.MakeUnsignedCall(c.hash, "changeOwner", nil, newOwner)
|
2022-10-27 13:20:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Destroy creates a transaction invoking `destroy` method of the contract.
|
|
|
|
// This transaction is signed and immediately sent to the network.
|
|
|
|
// The values returned are its hash, ValidUntilBlock value and error if any.
|
|
|
|
func (c *Contract) Destroy() (util.Uint256, uint32, error) {
|
2023-05-11 10:25:45 +00:00
|
|
|
return c.actor.SendCall(c.hash, "destroy")
|
2022-10-27 13:20:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// DestroyTransaction creates a transaction invoking `destroy` method of the contract.
|
|
|
|
// This transaction is signed, but not sent to the network, instead it's
|
|
|
|
// returned to the caller.
|
|
|
|
func (c *Contract) DestroyTransaction() (*transaction.Transaction, error) {
|
2023-05-11 10:25:45 +00:00
|
|
|
return c.actor.MakeCall(c.hash, "destroy")
|
2022-10-27 13:20:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// DestroyUnsigned creates a transaction invoking `destroy` method of the contract.
|
|
|
|
// This transaction is not signed, it's simply returned to the caller.
|
|
|
|
// Any fields of it that do not affect fees can be changed (ValidUntilBlock,
|
|
|
|
// Nonce), fee values (NetworkFee, SystemFee) can be increased as well.
|
|
|
|
func (c *Contract) DestroyUnsigned() (*transaction.Transaction, error) {
|
2023-05-11 10:25:45 +00:00
|
|
|
return c.actor.MakeUnsignedCall(c.hash, "destroy", nil)
|
2022-10-27 13:20:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// MaxSupply creates a transaction invoking `maxSupply` method of the contract.
|
|
|
|
// This transaction is signed and immediately sent to the network.
|
|
|
|
// The values returned are its hash, ValidUntilBlock value and error if any.
|
|
|
|
func (c *Contract) MaxSupply() (util.Uint256, uint32, error) {
|
2023-05-11 10:25:45 +00:00
|
|
|
return c.actor.SendCall(c.hash, "maxSupply")
|
2022-10-27 13:20:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// MaxSupplyTransaction creates a transaction invoking `maxSupply` method of the contract.
|
|
|
|
// This transaction is signed, but not sent to the network, instead it's
|
|
|
|
// returned to the caller.
|
|
|
|
func (c *Contract) MaxSupplyTransaction() (*transaction.Transaction, error) {
|
2023-05-11 10:25:45 +00:00
|
|
|
return c.actor.MakeCall(c.hash, "maxSupply")
|
2022-10-27 13:20:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// MaxSupplyUnsigned creates a transaction invoking `maxSupply` method of the contract.
|
|
|
|
// This transaction is not signed, it's simply returned to the caller.
|
|
|
|
// Any fields of it that do not affect fees can be changed (ValidUntilBlock,
|
|
|
|
// Nonce), fee values (NetworkFee, SystemFee) can be increased as well.
|
|
|
|
func (c *Contract) MaxSupplyUnsigned() (*transaction.Transaction, error) {
|
2023-05-11 10:25:45 +00:00
|
|
|
return c.actor.MakeUnsignedCall(c.hash, "maxSupply", nil)
|
2022-10-27 13:20:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Mint creates a transaction invoking `mint` method of the contract.
|
|
|
|
// This transaction is signed and immediately sent to the network.
|
|
|
|
// The values returned are its hash, ValidUntilBlock value and error if any.
|
2023-04-03 10:34:24 +00:00
|
|
|
func (c *Contract) Mint(from util.Uint160, to util.Uint160, amount *big.Int, swapId *big.Int, signature []byte, data any) (util.Uint256, uint32, error) {
|
2023-05-11 10:25:45 +00:00
|
|
|
return c.actor.SendCall(c.hash, "mint", from, to, amount, swapId, signature, data)
|
2022-10-27 13:20:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// MintTransaction creates a transaction invoking `mint` method of the contract.
|
|
|
|
// This transaction is signed, but not sent to the network, instead it's
|
|
|
|
// returned to the caller.
|
2023-04-03 10:34:24 +00:00
|
|
|
func (c *Contract) MintTransaction(from util.Uint160, to util.Uint160, amount *big.Int, swapId *big.Int, signature []byte, data any) (*transaction.Transaction, error) {
|
2023-05-11 10:25:45 +00:00
|
|
|
return c.actor.MakeCall(c.hash, "mint", from, to, amount, swapId, signature, data)
|
2022-10-27 13:20:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// MintUnsigned creates a transaction invoking `mint` method of the contract.
|
|
|
|
// This transaction is not signed, it's simply returned to the caller.
|
|
|
|
// Any fields of it that do not affect fees can be changed (ValidUntilBlock,
|
|
|
|
// Nonce), fee values (NetworkFee, SystemFee) can be increased as well.
|
2023-04-03 10:34:24 +00:00
|
|
|
func (c *Contract) MintUnsigned(from util.Uint160, to util.Uint160, amount *big.Int, swapId *big.Int, signature []byte, data any) (*transaction.Transaction, error) {
|
2023-05-11 10:25:45 +00:00
|
|
|
return c.actor.MakeUnsignedCall(c.hash, "mint", nil, from, to, amount, swapId, signature, data)
|
2022-10-27 13:20:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Update creates a transaction invoking `update` method of the contract.
|
|
|
|
// This transaction is signed and immediately sent to the network.
|
|
|
|
// The values returned are its hash, ValidUntilBlock value and error if any.
|
|
|
|
func (c *Contract) Update(nef []byte, manifest []byte) (util.Uint256, uint32, error) {
|
2023-05-11 10:25:45 +00:00
|
|
|
return c.actor.SendCall(c.hash, "update", nef, manifest)
|
2022-10-27 13:20:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// UpdateTransaction creates a transaction invoking `update` method of the contract.
|
|
|
|
// This transaction is signed, but not sent to the network, instead it's
|
|
|
|
// returned to the caller.
|
|
|
|
func (c *Contract) UpdateTransaction(nef []byte, manifest []byte) (*transaction.Transaction, error) {
|
2023-05-11 10:25:45 +00:00
|
|
|
return c.actor.MakeCall(c.hash, "update", nef, manifest)
|
2022-10-27 13:20:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// UpdateUnsigned creates a transaction invoking `update` method of the contract.
|
|
|
|
// This transaction is not signed, it's simply returned to the caller.
|
|
|
|
// Any fields of it that do not affect fees can be changed (ValidUntilBlock,
|
|
|
|
// Nonce), fee values (NetworkFee, SystemFee) can be increased as well.
|
|
|
|
func (c *Contract) UpdateUnsigned(nef []byte, manifest []byte) (*transaction.Transaction, error) {
|
2023-05-11 10:25:45 +00:00
|
|
|
return c.actor.MakeUnsignedCall(c.hash, "update", nil, nef, manifest)
|
2022-10-27 13:20:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// UpdateCap creates a transaction invoking `updateCap` method of the contract.
|
|
|
|
// This transaction is signed and immediately sent to the network.
|
|
|
|
// The values returned are its hash, ValidUntilBlock value and error if any.
|
|
|
|
func (c *Contract) UpdateCap(newCap *big.Int) (util.Uint256, uint32, error) {
|
2023-05-11 10:25:45 +00:00
|
|
|
return c.actor.SendCall(c.hash, "updateCap", newCap)
|
2022-10-27 13:20:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// UpdateCapTransaction creates a transaction invoking `updateCap` method of the contract.
|
|
|
|
// This transaction is signed, but not sent to the network, instead it's
|
|
|
|
// returned to the caller.
|
|
|
|
func (c *Contract) UpdateCapTransaction(newCap *big.Int) (*transaction.Transaction, error) {
|
2023-05-11 10:25:45 +00:00
|
|
|
return c.actor.MakeCall(c.hash, "updateCap", newCap)
|
2022-10-27 13:20:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// UpdateCapUnsigned creates a transaction invoking `updateCap` method of the contract.
|
|
|
|
// This transaction is not signed, it's simply returned to the caller.
|
|
|
|
// Any fields of it that do not affect fees can be changed (ValidUntilBlock,
|
|
|
|
// Nonce), fee values (NetworkFee, SystemFee) can be increased as well.
|
|
|
|
func (c *Contract) UpdateCapUnsigned(newCap *big.Int) (*transaction.Transaction, error) {
|
2023-05-11 10:25:45 +00:00
|
|
|
return c.actor.MakeUnsignedCall(c.hash, "updateCap", nil, newCap)
|
2022-10-27 13:20:35 +00:00
|
|
|
}
|
2023-05-24 08:52:14 +00:00
|
|
|
|
|
|
|
// OnMintEventsFromApplicationLog retrieves a set of all emitted events
|
2023-05-31 12:47:10 +00:00
|
|
|
// with "OnMint" name from the provided [result.ApplicationLog].
|
2023-05-24 08:52:14 +00:00
|
|
|
func OnMintEventsFromApplicationLog(log *result.ApplicationLog) ([]*OnMintEvent, error) {
|
|
|
|
if log == nil {
|
|
|
|
return nil, errors.New("nil application log")
|
|
|
|
}
|
|
|
|
|
|
|
|
var res []*OnMintEvent
|
|
|
|
for i, ex := range log.Executions {
|
|
|
|
for j, e := range ex.Events {
|
|
|
|
if e.Name != "OnMint" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
event := new(OnMintEvent)
|
|
|
|
err := event.FromStackItem(e.Item)
|
|
|
|
if err != nil {
|
2023-05-30 08:59:16 +00:00
|
|
|
return nil, fmt.Errorf("failed to deserialize OnMintEvent from stackitem (execution #%d, event #%d): %w", i, j, err)
|
2023-05-24 08:52:14 +00:00
|
|
|
}
|
|
|
|
res = append(res, event)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
|
2023-05-31 12:47:10 +00:00
|
|
|
// FromStackItem converts provided [stackitem.Array] to OnMintEvent or
|
2023-05-30 14:34:51 +00:00
|
|
|
// returns an error if it's not possible to do to so.
|
2023-05-24 08:52:14 +00:00
|
|
|
func (e *OnMintEvent) FromStackItem(item *stackitem.Array) error {
|
|
|
|
if item == nil {
|
|
|
|
return errors.New("nil item")
|
|
|
|
}
|
|
|
|
arr, ok := item.Value().([]stackitem.Item)
|
|
|
|
if !ok {
|
|
|
|
return errors.New("not an array")
|
|
|
|
}
|
|
|
|
if len(arr) != 4 {
|
|
|
|
return errors.New("wrong number of structure elements")
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
index = -1
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
index++
|
|
|
|
e.From, err = func (item stackitem.Item) (util.Uint160, error) {
|
|
|
|
b, err := item.TryBytes()
|
|
|
|
if err != nil {
|
|
|
|
return util.Uint160{}, err
|
|
|
|
}
|
|
|
|
u, err := util.Uint160DecodeBytesBE(b)
|
|
|
|
if err != nil {
|
|
|
|
return util.Uint160{}, err
|
|
|
|
}
|
|
|
|
return u, nil
|
|
|
|
} (arr[index])
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("field From: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
index++
|
|
|
|
e.To, err = func (item stackitem.Item) (util.Uint160, error) {
|
|
|
|
b, err := item.TryBytes()
|
|
|
|
if err != nil {
|
|
|
|
return util.Uint160{}, err
|
|
|
|
}
|
|
|
|
u, err := util.Uint160DecodeBytesBE(b)
|
|
|
|
if err != nil {
|
|
|
|
return util.Uint160{}, err
|
|
|
|
}
|
|
|
|
return u, nil
|
|
|
|
} (arr[index])
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("field To: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
index++
|
|
|
|
e.Amount, err = arr[index].TryInteger()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("field Amount: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
index++
|
|
|
|
e.SwapId, err = arr[index].TryInteger()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("field SwapId: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|