smartcontract: modernize Builder example

And make it a bit more useful.
This commit is contained in:
Roman Khimov 2022-09-06 16:37:21 +03:00
parent aca8ce0d28
commit 69176168c3

View file

@ -4,9 +4,10 @@ import (
"context" "context"
"encoding/hex" "encoding/hex"
"github.com/nspcc-dev/neo-go/pkg/core/transaction"
"github.com/nspcc-dev/neo-go/pkg/rpcclient" "github.com/nspcc-dev/neo-go/pkg/rpcclient"
"github.com/nspcc-dev/neo-go/pkg/rpcclient/actor" "github.com/nspcc-dev/neo-go/pkg/rpcclient/actor"
"github.com/nspcc-dev/neo-go/pkg/rpcclient/gas"
"github.com/nspcc-dev/neo-go/pkg/rpcclient/neo"
"github.com/nspcc-dev/neo-go/pkg/smartcontract" "github.com/nspcc-dev/neo-go/pkg/smartcontract"
"github.com/nspcc-dev/neo-go/pkg/util" "github.com/nspcc-dev/neo-go/pkg/util"
"github.com/nspcc-dev/neo-go/pkg/wallet" "github.com/nspcc-dev/neo-go/pkg/wallet"
@ -14,37 +15,48 @@ import (
func ExampleBuilder() { func ExampleBuilder() {
// No error checking done at all, intentionally. // No error checking done at all, intentionally.
w, _ := wallet.NewWalletFromFile("somewhere")
defer w.Close()
c, _ := rpcclient.New(context.Background(), "url", rpcclient.Options{}) c, _ := rpcclient.New(context.Background(), "url", rpcclient.Options{})
neoHash, _ := c.GetNativeContractHash("NeoToken")
// Assuming there is one Account inside.
a, _ := actor.NewSimple(c, w.Accounts[0])
pKey, _ := hex.DecodeString("03d9e8b16bd9b22d3345d6d4cde31be1c3e1d161532e3d0ccecb95ece2eb58336e") // Public key. pKey, _ := hex.DecodeString("03d9e8b16bd9b22d3345d6d4cde31be1c3e1d161532e3d0ccecb95ece2eb58336e") // Public key.
b := smartcontract.NewBuilder() b := smartcontract.NewBuilder()
// Single NEO "vote" call with a check // Transfer + vote in a single script with each action leaving return value on the stack.
b.InvokeWithAssert(neoHash, "vote", pKey) b.InvokeMethod(neo.Hash, "transfer", a.Sender(), util.Uint160{0xff}, 1, nil)
b.InvokeMethod(neo.Hash, "vote", pKey)
script, _ := b.Script() script, _ := b.Script()
// The script can then be used to create transaction or to invoke via RPC. // Actor has an Invoker inside, so we can perform test invocation using the script.
res, _ := c.InvokeScript(script, []transaction.Signer{{Account: util.Uint160{0x01, 0x02, 0x03}, Scopes: transaction.CalledByEntry}}) res, _ := a.Run(script)
if res.State != "HALT" { if res.State != "HALT" || len(res.Stack) != 2 {
// The script failed // The script failed completely or didn't return proper number of return values.
}
transferResult, _ := res.Stack[0].TryBool()
voteResult, _ := res.Stack[1].TryBool()
if !transferResult {
// Transfer failed.
}
if !voteResult {
// Vote failed.
} }
b.Reset() // Copy the old script above if you need it! b.Reset() // Copy the old script above if you need it!
w, _ := wallet.NewWalletFromFile("somewhere") // Multiple transfers of different tokens in a single script. If any of
defer w.Close() // them fails whole script fails.
// Assuming there is one Account inside b.InvokeWithAssert(neo.Hash, "transfer", a.Sender(), util.Uint160{0x70}, 1, nil)
a, _ := actor.NewSimple(c, w.Accounts[0]) b.InvokeWithAssert(gas.Hash, "transfer", a.Sender(), util.Uint160{0x71}, 100000, []byte("data"))
from := w.Accounts[0].Contract.ScriptHash() // Assuming Contract is present. b.InvokeWithAssert(neo.Hash, "transfer", a.Sender(), util.Uint160{0x72}, 1, nil)
// Multiple transfers in a single script. If any of them fail whole script fails.
b.InvokeWithAssert(neoHash, "transfer", from, util.Uint160{0x70}, 1, nil)
b.InvokeWithAssert(neoHash, "transfer", from, util.Uint160{0x71}, 10, []byte("data"))
b.InvokeWithAssert(neoHash, "transfer", from, util.Uint160{0x72}, 1, nil)
script, _ = b.Script() script, _ = b.Script()
// The script can then be used to create transaction or to invoke via RPC. // Now send a transaction with this script via an RPC node.
txid, vub, _ := a.SendRun(script) txid, vub, _ := a.SendRun(script)
_ = txid _ = txid
_ = vub _ = vub