2022-07-25 17:04:43 +00:00
|
|
|
package smartcontract_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/hex"
|
|
|
|
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/rpcclient"
|
2022-08-07 19:21:03 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/rpcclient/actor"
|
2022-09-06 13:37:21 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/rpcclient/gas"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/rpcclient/neo"
|
2022-07-25 17:04:43 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/smartcontract"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/util"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/wallet"
|
|
|
|
)
|
|
|
|
|
|
|
|
func ExampleBuilder() {
|
|
|
|
// No error checking done at all, intentionally.
|
2022-09-06 13:37:21 +00:00
|
|
|
w, _ := wallet.NewWalletFromFile("somewhere")
|
|
|
|
defer w.Close()
|
|
|
|
|
2022-07-25 17:04:43 +00:00
|
|
|
c, _ := rpcclient.New(context.Background(), "url", rpcclient.Options{})
|
2022-09-06 13:37:21 +00:00
|
|
|
|
|
|
|
// Assuming there is one Account inside.
|
|
|
|
a, _ := actor.NewSimple(c, w.Accounts[0])
|
2022-07-25 17:04:43 +00:00
|
|
|
|
|
|
|
pKey, _ := hex.DecodeString("03d9e8b16bd9b22d3345d6d4cde31be1c3e1d161532e3d0ccecb95ece2eb58336e") // Public key.
|
|
|
|
|
|
|
|
b := smartcontract.NewBuilder()
|
2022-09-06 13:37:21 +00:00
|
|
|
// Transfer + vote in a single script with each action leaving return value on the stack.
|
|
|
|
b.InvokeMethod(neo.Hash, "transfer", a.Sender(), util.Uint160{0xff}, 1, nil)
|
|
|
|
b.InvokeMethod(neo.Hash, "vote", pKey)
|
2022-07-25 17:04:43 +00:00
|
|
|
script, _ := b.Script()
|
|
|
|
|
2022-09-06 13:37:21 +00:00
|
|
|
// Actor has an Invoker inside, so we can perform test invocation using the script.
|
|
|
|
res, _ := a.Run(script)
|
|
|
|
if res.State != "HALT" || len(res.Stack) != 2 {
|
2023-03-29 03:19:23 +00:00
|
|
|
panic("failed") // The script failed completely or didn't return proper number of return values.
|
2022-07-25 17:04:43 +00:00
|
|
|
}
|
|
|
|
|
2022-09-06 13:37:21 +00:00
|
|
|
transferResult, _ := res.Stack[0].TryBool()
|
|
|
|
voteResult, _ := res.Stack[1].TryBool()
|
2022-07-25 17:04:43 +00:00
|
|
|
|
2022-09-06 13:37:21 +00:00
|
|
|
if !transferResult {
|
2023-03-29 03:19:23 +00:00
|
|
|
panic("transfer failed")
|
2022-09-06 13:37:21 +00:00
|
|
|
}
|
|
|
|
if !voteResult {
|
2023-03-29 03:19:23 +00:00
|
|
|
panic("vote failed")
|
2022-09-06 13:37:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
b.Reset() // Copy the old script above if you need it!
|
2022-07-25 17:04:43 +00:00
|
|
|
|
2022-09-06 13:37:21 +00:00
|
|
|
// Multiple transfers of different tokens in a single script. If any of
|
|
|
|
// them fails whole script fails.
|
|
|
|
b.InvokeWithAssert(neo.Hash, "transfer", a.Sender(), util.Uint160{0x70}, 1, nil)
|
|
|
|
b.InvokeWithAssert(gas.Hash, "transfer", a.Sender(), util.Uint160{0x71}, 100000, []byte("data"))
|
|
|
|
b.InvokeWithAssert(neo.Hash, "transfer", a.Sender(), util.Uint160{0x72}, 1, nil)
|
2022-07-25 17:04:43 +00:00
|
|
|
script, _ = b.Script()
|
|
|
|
|
2022-09-06 13:37:21 +00:00
|
|
|
// Now send a transaction with this script via an RPC node.
|
2022-08-07 19:21:03 +00:00
|
|
|
txid, vub, _ := a.SendRun(script)
|
2022-07-25 17:04:43 +00:00
|
|
|
_ = txid
|
2022-08-07 19:21:03 +00:00
|
|
|
_ = vub
|
2022-07-25 17:04:43 +00:00
|
|
|
}
|