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

     1  package nep11_test
     2  
     3  import (
     4  	"context"
     5  	"math/big"
     6  
     7  	"github.com/nspcc-dev/neo-go/pkg/encoding/address"
     8  	"github.com/nspcc-dev/neo-go/pkg/rpcclient"
     9  	"github.com/nspcc-dev/neo-go/pkg/rpcclient/actor"
    10  	"github.com/nspcc-dev/neo-go/pkg/rpcclient/invoker"
    11  	"github.com/nspcc-dev/neo-go/pkg/rpcclient/nep11"
    12  	"github.com/nspcc-dev/neo-go/pkg/util"
    13  	"github.com/nspcc-dev/neo-go/pkg/wallet"
    14  )
    15  
    16  func ExampleNonDivisibleReader() {
    17  	// No error checking done at all, intentionally.
    18  	c, _ := rpcclient.New(context.Background(), "url", rpcclient.Options{})
    19  
    20  	// Safe methods are reachable with just an invoker, no need for an account there.
    21  	inv := invoker.New(c, nil)
    22  
    23  	// NEP-11 contract hash.
    24  	nep11Hash := util.Uint160{9, 8, 7}
    25  
    26  	// Most of the time contracts are non-divisible, create a reader for nep11Hash.
    27  	n11 := nep11.NewNonDivisibleReader(inv, nep11Hash)
    28  
    29  	// Get the metadata. Even though these methods are implemented in neptoken package,
    30  	// they're available for NEP-11 wrappers.
    31  	symbol, _ := n11.Symbol()
    32  	supply, _ := n11.TotalSupply()
    33  	_ = symbol
    34  	_ = supply
    35  
    36  	// Account hash we're interested in.
    37  	accHash, _ := address.StringToUint160("NdypBhqkz2CMMnwxBgvoC9X2XjKF5axgKo")
    38  
    39  	// Get account balance.
    40  	balance, _ := n11.BalanceOf(accHash)
    41  	if balance.Sign() > 0 {
    42  		// There are some tokens there, let's look at them.
    43  		tokIter, _ := n11.TokensOf(accHash)
    44  
    45  		for toks, err := tokIter.Next(10); err == nil && len(toks) > 0; toks, err = tokIter.Next(10) {
    46  			for i := range toks {
    47  				// We know the owner of the token, but let's check internal contract consistency.
    48  				owner, _ := n11.OwnerOf(toks[i])
    49  				if !owner.Equals(accHash) {
    50  					panic("NEP-11 contract is broken!")
    51  				}
    52  			}
    53  		}
    54  	}
    55  }
    56  
    57  func ExampleDivisibleReader() {
    58  	// No error checking done at all, intentionally.
    59  	c, _ := rpcclient.New(context.Background(), "url", rpcclient.Options{})
    60  
    61  	// Safe methods are reachable with just an invoker, no need for an account there.
    62  	inv := invoker.New(c, nil)
    63  
    64  	// NEP-11 contract hash.
    65  	nep11Hash := util.Uint160{9, 8, 7}
    66  
    67  	// Divisible contract are more rare, but we can handle them too.
    68  	n11 := nep11.NewDivisibleReader(inv, nep11Hash)
    69  
    70  	// Get the metadata. Even though these methods are implemented in neptoken package,
    71  	// they're available for NEP-11 wrappers.
    72  	symbol, _ := n11.Symbol()
    73  	supply, _ := n11.TotalSupply()
    74  	_ = symbol
    75  	_ = supply
    76  
    77  	// Account hash we're interested in.
    78  	accHash, _ := address.StringToUint160("NdypBhqkz2CMMnwxBgvoC9X2XjKF5axgKo")
    79  
    80  	// Get account balance.
    81  	balance, _ := n11.BalanceOf(accHash)
    82  	if balance.Sign() > 0 && balance.Cmp(big.NewInt(10)) < 0 {
    83  		// We know we have a low number of tokens, so we can use a simple API to get them.
    84  		toks, _ := n11.TokensOfExpanded(accHash, 10)
    85  
    86  		// We can build a list of all owners of account's tokens.
    87  		var owners = make([]util.Uint160, 0)
    88  		for i := range toks {
    89  			ownIter, _ := n11.OwnerOf(toks[i])
    90  			for ows, err := ownIter.Next(10); err == nil && len(ows) > 0; ows, err = ownIter.Next(10) {
    91  				// Notice that it includes accHash too.
    92  				owners = append(owners, ows...)
    93  			}
    94  		}
    95  		// The list can be sorted/deduplicated if needed.
    96  		_ = owners
    97  	}
    98  }
    99  
   100  func ExampleNonDivisible() {
   101  	// No error checking done at all, intentionally.
   102  	w, _ := wallet.NewWalletFromFile("somewhere")
   103  	defer w.Close()
   104  
   105  	c, _ := rpcclient.New(context.Background(), "url", rpcclient.Options{})
   106  
   107  	// Create a simple CalledByEntry-scoped actor (assuming there is an account
   108  	// inside the wallet).
   109  	a, _ := actor.NewSimple(c, w.Accounts[0])
   110  
   111  	// NEP-11 contract hash.
   112  	nep11Hash := util.Uint160{9, 8, 7}
   113  
   114  	// Create a complete non-divisible contract representation.
   115  	n11 := nep11.NewNonDivisible(a, nep11Hash)
   116  
   117  	tgtAcc, _ := address.StringToUint160("NdypBhqkz2CMMnwxBgvoC9X2XjKF5axgKo")
   118  
   119  	// Let's tranfer all of account's tokens to some other account.
   120  	tokIter, _ := n11.TokensOf(a.Sender())
   121  	for toks, err := tokIter.Next(10); err == nil && len(toks) > 0; toks, err = tokIter.Next(10) {
   122  		for i := range toks {
   123  			// This creates a transaction for every token, but you can
   124  			// create a script that will move multiple tokens in one
   125  			// transaction with Builder from smartcontract package.
   126  			txid, vub, _ := n11.Transfer(tgtAcc, toks[i], nil)
   127  			_ = txid
   128  			_ = vub
   129  		}
   130  	}
   131  }
   132  
   133  func ExampleDivisible() {
   134  	// No error checking done at all, intentionally.
   135  	w, _ := wallet.NewWalletFromFile("somewhere")
   136  	defer w.Close()
   137  
   138  	c, _ := rpcclient.New(context.Background(), "url", rpcclient.Options{})
   139  
   140  	// Create a simple CalledByEntry-scoped actor (assuming there is an account
   141  	// inside the wallet).
   142  	a, _ := actor.NewSimple(c, w.Accounts[0])
   143  
   144  	// NEP-11 contract hash.
   145  	nep11Hash := util.Uint160{9, 8, 7}
   146  
   147  	// Create a complete divisible contract representation.
   148  	n11 := nep11.NewDivisible(a, nep11Hash)
   149  
   150  	tgtAcc, _ := address.StringToUint160("NdypBhqkz2CMMnwxBgvoC9X2XjKF5axgKo")
   151  
   152  	// Let's tranfer all of account's tokens to some other account.
   153  	tokIter, _ := n11.TokensOf(a.Sender())
   154  	for toks, err := tokIter.Next(10); err == nil && len(toks) > 0; toks, err = tokIter.Next(10) {
   155  		for i := range toks {
   156  			// It's a divisible token, so balance data is required in general case.
   157  			balance, _ := n11.BalanceOfD(a.Sender(), toks[i])
   158  
   159  			// This creates a transaction for every token, but you can
   160  			// create a script that will move multiple tokens in one
   161  			// transaction with Builder from smartcontract package.
   162  			txid, vub, _ := n11.TransferD(a.Sender(), tgtAcc, balance, toks[i], nil)
   163  			_ = txid
   164  			_ = vub
   165  		}
   166  	}
   167  }