github.com/nspcc-dev/neo-go@v0.105.2-0.20240517133400-6be757af3eba/pkg/rpcclient/neo/doc_test.go (about) 1 package neo_test 2 3 import ( 4 "context" 5 "math/big" 6 "sort" 7 8 "github.com/nspcc-dev/neo-go/pkg/encoding/address" 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/invoker" 12 "github.com/nspcc-dev/neo-go/pkg/rpcclient/neo" 13 "github.com/nspcc-dev/neo-go/pkg/wallet" 14 ) 15 16 func ExampleContractReader() { 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 // Create a reader interface. 24 neoToken := neo.NewReader(inv) 25 26 // Account hash we're interested in. 27 accHash, _ := address.StringToUint160("NdypBhqkz2CMMnwxBgvoC9X2XjKF5axgKo") 28 29 // Get the account balance. 30 balance, _ := neoToken.BalanceOf(accHash) 31 _ = balance 32 33 // Get the extended NEO-specific balance data. 34 bNeo, _ := neoToken.GetAccountState(accHash) 35 36 // Account can have no associated vote. 37 if bNeo.VoteTo == nil { 38 return 39 } 40 // Committee keys. 41 comm, _ := neoToken.GetCommittee() 42 43 // Check if the vote is made for a committee member. 44 var votedForCommitteeMember bool 45 for i := range comm { 46 if bNeo.VoteTo.Equal(comm[i]) { 47 votedForCommitteeMember = true 48 break 49 } 50 } 51 _ = votedForCommitteeMember 52 } 53 54 func ExampleContract() { 55 // No error checking done at all, intentionally. 56 w, _ := wallet.NewWalletFromFile("somewhere") 57 defer w.Close() 58 59 c, _ := rpcclient.New(context.Background(), "url", rpcclient.Options{}) 60 61 // Create a simple CalledByEntry-scoped actor (assuming there is an account 62 // inside the wallet). 63 a, _ := actor.NewSimple(c, w.Accounts[0]) 64 65 // Create a complete contract representation. 66 neoToken := neo.New(a) 67 68 tgtAcc, _ := address.StringToUint160("NdypBhqkz2CMMnwxBgvoC9X2XjKF5axgKo") 69 70 // Send a transaction that transfers one token to another account. 71 txid, vub, _ := neoToken.Transfer(a.Sender(), tgtAcc, big.NewInt(1), nil) 72 _ = txid 73 _ = vub 74 75 // Get a list of candidates (it's limited, but should be sufficient in most cases). 76 cands, _ := neoToken.GetCandidates() 77 78 // Sort by votes. 79 sort.Slice(cands, func(i, j int) bool { return cands[i].Votes < cands[j].Votes }) 80 81 // Get the extended NEO-specific balance data. 82 bNeo, _ := neoToken.GetAccountState(a.Sender()) 83 84 // If not yet voted, or voted for suboptimal candidate (we want the one with the least votes), 85 // send a new voting transaction 86 if bNeo.VoteTo == nil || !bNeo.VoteTo.Equal(&cands[0].PublicKey) { 87 txid, vub, _ = neoToken.Vote(a.Sender(), &cands[0].PublicKey) 88 _ = txid 89 _ = vub 90 } 91 }