github.com/nspcc-dev/neo-go@v0.105.2-0.20240517133400-6be757af3eba/pkg/rpcclient/notary/doc_test.go (about)

     1  package notary_test
     2  
     3  import (
     4  	"context"
     5  	"math/big"
     6  	"time"
     7  
     8  	"github.com/nspcc-dev/neo-go/pkg/core/transaction"
     9  	"github.com/nspcc-dev/neo-go/pkg/rpcclient"
    10  	"github.com/nspcc-dev/neo-go/pkg/rpcclient/actor"
    11  	"github.com/nspcc-dev/neo-go/pkg/rpcclient/gas"
    12  	"github.com/nspcc-dev/neo-go/pkg/rpcclient/notary"
    13  	"github.com/nspcc-dev/neo-go/pkg/rpcclient/policy"
    14  	"github.com/nspcc-dev/neo-go/pkg/vm/vmstate"
    15  	"github.com/nspcc-dev/neo-go/pkg/wallet"
    16  )
    17  
    18  func ExampleActor() {
    19  	// No error checking done at all, intentionally.
    20  	w, _ := wallet.NewWalletFromFile("somewhere")
    21  	defer w.Close()
    22  	// We assume there are two accounts in the wallet --- one is a simple signature
    23  	// account and another one is committee account. The first one will send notary
    24  	// requests, while committee signatures need to be collected.
    25  
    26  	// Create an RPC client.
    27  	c, _ := rpcclient.New(context.Background(), "url", rpcclient.Options{})
    28  
    29  	// An actor for the first account.
    30  	single, _ := actor.NewSimple(c, w.Accounts[0])
    31  
    32  	// Transfer some GAS to the Notary contract to be able to send notary requests
    33  	// from the first account.
    34  	gasSingle := gas.New(single)
    35  	txid, vub, _ := gasSingle.Transfer(single.Sender(), notary.Hash, big.NewInt(10_0000_0000), &notary.OnNEP17PaymentData{Till: 10000000})
    36  
    37  	var depositOK bool
    38  	// Wait for transaction to be persisted, either it gets in and we get
    39  	// an application log with some result or it expires.
    40  	for height, err := c.GetBlockCount(); err == nil && height <= vub; height, err = c.GetBlockCount() {
    41  		appLog, err := c.GetApplicationLog(txid, nil)
    42  		// We can't separate "application log missing" from other errors at the moment, see #2248.
    43  		if err != nil {
    44  			time.Sleep(5 * time.Second)
    45  			continue
    46  		}
    47  		if len(appLog.Executions) == 1 && appLog.Executions[0].VMState == vmstate.Halt {
    48  			depositOK = true
    49  		} else {
    50  			break
    51  		}
    52  	}
    53  	if !depositOK {
    54  		panic("deposit failed")
    55  	}
    56  
    57  	var opts = new(notary.ActorOptions)
    58  	// Add high priority attribute, we gonna be making committee-signed transactions anyway.
    59  	opts.MainAttributes = []transaction.Attribute{{Type: transaction.HighPriority}}
    60  
    61  	// Create an Actor with the simple account used for paying fees and committee
    62  	// signature to be collected.
    63  	multi, _ := notary.NewTunedActor(c, []actor.SignerAccount{{
    64  		// Sender, regular account with None scope.
    65  		Signer: transaction.Signer{
    66  			Account: w.Accounts[0].ScriptHash(),
    67  			Scopes:  transaction.None,
    68  		},
    69  		Account: w.Accounts[0],
    70  	}, {
    71  		// Commmitee.
    72  		Signer: transaction.Signer{
    73  			Account: w.Accounts[1].ScriptHash(),
    74  			Scopes:  transaction.CalledByEntry,
    75  		},
    76  		Account: w.Accounts[1],
    77  	}}, opts)
    78  
    79  	// Use the Policy contract to perform something requiring committee signature.
    80  	policyContract := policy.New(multi)
    81  
    82  	// Wrap a transaction to set storage price into a notary request. Fallback will
    83  	// be create automatically and all appropriate attributes will be added to both
    84  	// transactions.
    85  	mainTx, fbTx, vub, _ := multi.Notarize(policyContract.SetStoragePriceTransaction(10))
    86  	_ = mainTx
    87  	_ = fbTx
    88  	_ = vub
    89  }