github.com/nspcc-dev/neo-go@v0.105.2-0.20240517133400-6be757af3eba/cli/smartcontract/testdata/nameservice/nns.go (about) 1 // Code generated by neo-go contract generate-rpcwrapper --manifest <file.json> --out <file.go> [--hash <hash>] [--config <config>]; DO NOT EDIT. 2 3 // Package nameservice contains RPC wrappers for NameService contract. 4 package nameservice 5 6 import ( 7 "errors" 8 "fmt" 9 "github.com/google/uuid" 10 "github.com/nspcc-dev/neo-go/pkg/core/transaction" 11 "github.com/nspcc-dev/neo-go/pkg/neorpc/result" 12 "github.com/nspcc-dev/neo-go/pkg/rpcclient/nep11" 13 "github.com/nspcc-dev/neo-go/pkg/rpcclient/unwrap" 14 "github.com/nspcc-dev/neo-go/pkg/smartcontract" 15 "github.com/nspcc-dev/neo-go/pkg/util" 16 "github.com/nspcc-dev/neo-go/pkg/vm/stackitem" 17 "math/big" 18 "unicode/utf8" 19 ) 20 21 // Hash contains contract hash. 22 var Hash = util.Uint160{0xde, 0x46, 0x5f, 0x5d, 0x50, 0x57, 0xcf, 0x33, 0x28, 0x47, 0x94, 0xc5, 0xcf, 0xc2, 0xc, 0x69, 0x37, 0x1c, 0xac, 0x50} 23 24 // SetAdminEvent represents "SetAdmin" event emitted by the contract. 25 type SetAdminEvent struct { 26 Name string 27 OldAdmin util.Uint160 28 NewAdmin util.Uint160 29 } 30 31 // RenewEvent represents "Renew" event emitted by the contract. 32 type RenewEvent struct { 33 Name string 34 OldExpiration *big.Int 35 NewExpiration *big.Int 36 } 37 38 // Invoker is used by ContractReader to call various safe methods. 39 type Invoker interface { 40 nep11.Invoker 41 } 42 43 // Actor is used by Contract to call state-changing methods. 44 type Actor interface { 45 Invoker 46 47 nep11.Actor 48 49 MakeCall(contract util.Uint160, method string, params ...any) (*transaction.Transaction, error) 50 MakeRun(script []byte) (*transaction.Transaction, error) 51 MakeUnsignedCall(contract util.Uint160, method string, attrs []transaction.Attribute, params ...any) (*transaction.Transaction, error) 52 MakeUnsignedRun(script []byte, attrs []transaction.Attribute) (*transaction.Transaction, error) 53 SendCall(contract util.Uint160, method string, params ...any) (util.Uint256, uint32, error) 54 SendRun(script []byte) (util.Uint256, uint32, error) 55 } 56 57 // ContractReader implements safe contract methods. 58 type ContractReader struct { 59 nep11.NonDivisibleReader 60 invoker Invoker 61 hash util.Uint160 62 } 63 64 // Contract implements all contract methods. 65 type Contract struct { 66 ContractReader 67 nep11.BaseWriter 68 actor Actor 69 hash util.Uint160 70 } 71 72 // NewReader creates an instance of ContractReader using Hash and the given Invoker. 73 func NewReader(invoker Invoker) *ContractReader { 74 var hash = Hash 75 return &ContractReader{*nep11.NewNonDivisibleReader(invoker, hash), invoker, hash} 76 } 77 78 // New creates an instance of Contract using Hash and the given Actor. 79 func New(actor Actor) *Contract { 80 var hash = Hash 81 var nep11ndt = nep11.NewNonDivisible(actor, hash) 82 return &Contract{ContractReader{nep11ndt.NonDivisibleReader, actor, hash}, nep11ndt.BaseWriter, actor, hash} 83 } 84 85 // Roots invokes `roots` method of contract. 86 func (c *ContractReader) Roots() (uuid.UUID, result.Iterator, error) { 87 return unwrap.SessionIterator(c.invoker.Call(c.hash, "roots")) 88 } 89 90 // RootsExpanded is similar to Roots (uses the same contract 91 // method), but can be useful if the server used doesn't support sessions and 92 // doesn't expand iterators. It creates a script that will get the specified 93 // number of result items from the iterator right in the VM and return them to 94 // you. It's only limited by VM stack and GAS available for RPC invocations. 95 func (c *ContractReader) RootsExpanded(_numOfIteratorItems int) ([]stackitem.Item, error) { 96 return unwrap.Array(c.invoker.CallAndExpandIterator(c.hash, "roots", _numOfIteratorItems)) 97 } 98 99 // GetPrice invokes `getPrice` method of contract. 100 func (c *ContractReader) GetPrice(length *big.Int) (*big.Int, error) { 101 return unwrap.BigInt(c.invoker.Call(c.hash, "getPrice", length)) 102 } 103 104 // IsAvailable invokes `isAvailable` method of contract. 105 func (c *ContractReader) IsAvailable(name string) (bool, error) { 106 return unwrap.Bool(c.invoker.Call(c.hash, "isAvailable", name)) 107 } 108 109 // GetRecord invokes `getRecord` method of contract. 110 func (c *ContractReader) GetRecord(name string, typev *big.Int) (string, error) { 111 return unwrap.UTF8String(c.invoker.Call(c.hash, "getRecord", name, typev)) 112 } 113 114 // GetAllRecords invokes `getAllRecords` method of contract. 115 func (c *ContractReader) GetAllRecords(name string) (uuid.UUID, result.Iterator, error) { 116 return unwrap.SessionIterator(c.invoker.Call(c.hash, "getAllRecords", name)) 117 } 118 119 // GetAllRecordsExpanded is similar to GetAllRecords (uses the same contract 120 // method), but can be useful if the server used doesn't support sessions and 121 // doesn't expand iterators. It creates a script that will get the specified 122 // number of result items from the iterator right in the VM and return them to 123 // you. It's only limited by VM stack and GAS available for RPC invocations. 124 func (c *ContractReader) GetAllRecordsExpanded(name string, _numOfIteratorItems int) ([]stackitem.Item, error) { 125 return unwrap.Array(c.invoker.CallAndExpandIterator(c.hash, "getAllRecords", _numOfIteratorItems, name)) 126 } 127 128 // Resolve invokes `resolve` method of contract. 129 func (c *ContractReader) Resolve(name string, typev *big.Int) (string, error) { 130 return unwrap.UTF8String(c.invoker.Call(c.hash, "resolve", name, typev)) 131 } 132 133 // Update creates a transaction invoking `update` method of the contract. 134 // This transaction is signed and immediately sent to the network. 135 // The values returned are its hash, ValidUntilBlock value and error if any. 136 func (c *Contract) Update(nef []byte, manifest string) (util.Uint256, uint32, error) { 137 return c.actor.SendCall(c.hash, "update", nef, manifest) 138 } 139 140 // UpdateTransaction creates a transaction invoking `update` method of the contract. 141 // This transaction is signed, but not sent to the network, instead it's 142 // returned to the caller. 143 func (c *Contract) UpdateTransaction(nef []byte, manifest string) (*transaction.Transaction, error) { 144 return c.actor.MakeCall(c.hash, "update", nef, manifest) 145 } 146 147 // UpdateUnsigned creates a transaction invoking `update` method of the contract. 148 // This transaction is not signed, it's simply returned to the caller. 149 // Any fields of it that do not affect fees can be changed (ValidUntilBlock, 150 // Nonce), fee values (NetworkFee, SystemFee) can be increased as well. 151 func (c *Contract) UpdateUnsigned(nef []byte, manifest string) (*transaction.Transaction, error) { 152 return c.actor.MakeUnsignedCall(c.hash, "update", nil, nef, manifest) 153 } 154 155 // AddRoot creates a transaction invoking `addRoot` method of the contract. 156 // This transaction is signed and immediately sent to the network. 157 // The values returned are its hash, ValidUntilBlock value and error if any. 158 func (c *Contract) AddRoot(root string) (util.Uint256, uint32, error) { 159 return c.actor.SendCall(c.hash, "addRoot", root) 160 } 161 162 // AddRootTransaction creates a transaction invoking `addRoot` method of the contract. 163 // This transaction is signed, but not sent to the network, instead it's 164 // returned to the caller. 165 func (c *Contract) AddRootTransaction(root string) (*transaction.Transaction, error) { 166 return c.actor.MakeCall(c.hash, "addRoot", root) 167 } 168 169 // AddRootUnsigned creates a transaction invoking `addRoot` method of the contract. 170 // This transaction is not signed, it's simply returned to the caller. 171 // Any fields of it that do not affect fees can be changed (ValidUntilBlock, 172 // Nonce), fee values (NetworkFee, SystemFee) can be increased as well. 173 func (c *Contract) AddRootUnsigned(root string) (*transaction.Transaction, error) { 174 return c.actor.MakeUnsignedCall(c.hash, "addRoot", nil, root) 175 } 176 177 // SetPrice creates a transaction invoking `setPrice` method of the contract. 178 // This transaction is signed and immediately sent to the network. 179 // The values returned are its hash, ValidUntilBlock value and error if any. 180 func (c *Contract) SetPrice(priceList []any) (util.Uint256, uint32, error) { 181 return c.actor.SendCall(c.hash, "setPrice", priceList) 182 } 183 184 // SetPriceTransaction creates a transaction invoking `setPrice` method of the contract. 185 // This transaction is signed, but not sent to the network, instead it's 186 // returned to the caller. 187 func (c *Contract) SetPriceTransaction(priceList []any) (*transaction.Transaction, error) { 188 return c.actor.MakeCall(c.hash, "setPrice", priceList) 189 } 190 191 // SetPriceUnsigned creates a transaction invoking `setPrice` method of the contract. 192 // This transaction is not signed, it's simply returned to the caller. 193 // Any fields of it that do not affect fees can be changed (ValidUntilBlock, 194 // Nonce), fee values (NetworkFee, SystemFee) can be increased as well. 195 func (c *Contract) SetPriceUnsigned(priceList []any) (*transaction.Transaction, error) { 196 return c.actor.MakeUnsignedCall(c.hash, "setPrice", nil, priceList) 197 } 198 199 func (c *Contract) scriptForRegister(name string, owner util.Uint160) ([]byte, error) { 200 return smartcontract.CreateCallWithAssertScript(c.hash, "register", name, owner) 201 } 202 203 // Register creates a transaction invoking `register` method of the contract. 204 // This transaction is signed and immediately sent to the network. 205 // The values returned are its hash, ValidUntilBlock value and error if any. 206 func (c *Contract) Register(name string, owner util.Uint160) (util.Uint256, uint32, error) { 207 script, err := c.scriptForRegister(name, owner) 208 if err != nil { 209 return util.Uint256{}, 0, err 210 } 211 return c.actor.SendRun(script) 212 } 213 214 // RegisterTransaction creates a transaction invoking `register` method of the contract. 215 // This transaction is signed, but not sent to the network, instead it's 216 // returned to the caller. 217 func (c *Contract) RegisterTransaction(name string, owner util.Uint160) (*transaction.Transaction, error) { 218 script, err := c.scriptForRegister(name, owner) 219 if err != nil { 220 return nil, err 221 } 222 return c.actor.MakeRun(script) 223 } 224 225 // RegisterUnsigned creates a transaction invoking `register` method of the contract. 226 // This transaction is not signed, it's simply returned to the caller. 227 // Any fields of it that do not affect fees can be changed (ValidUntilBlock, 228 // Nonce), fee values (NetworkFee, SystemFee) can be increased as well. 229 func (c *Contract) RegisterUnsigned(name string, owner util.Uint160) (*transaction.Transaction, error) { 230 script, err := c.scriptForRegister(name, owner) 231 if err != nil { 232 return nil, err 233 } 234 return c.actor.MakeUnsignedRun(script, nil) 235 } 236 237 // Renew creates a transaction invoking `renew` method of the contract. 238 // This transaction is signed and immediately sent to the network. 239 // The values returned are its hash, ValidUntilBlock value and error if any. 240 func (c *Contract) Renew(name string) (util.Uint256, uint32, error) { 241 return c.actor.SendCall(c.hash, "renew", name) 242 } 243 244 // RenewTransaction creates a transaction invoking `renew` method of the contract. 245 // This transaction is signed, but not sent to the network, instead it's 246 // returned to the caller. 247 func (c *Contract) RenewTransaction(name string) (*transaction.Transaction, error) { 248 return c.actor.MakeCall(c.hash, "renew", name) 249 } 250 251 // RenewUnsigned creates a transaction invoking `renew` method of the contract. 252 // This transaction is not signed, it's simply returned to the caller. 253 // Any fields of it that do not affect fees can be changed (ValidUntilBlock, 254 // Nonce), fee values (NetworkFee, SystemFee) can be increased as well. 255 func (c *Contract) RenewUnsigned(name string) (*transaction.Transaction, error) { 256 return c.actor.MakeUnsignedCall(c.hash, "renew", nil, name) 257 } 258 259 // Renew2 creates a transaction invoking `renew` method of the contract. 260 // This transaction is signed and immediately sent to the network. 261 // The values returned are its hash, ValidUntilBlock value and error if any. 262 func (c *Contract) Renew2(name string, years *big.Int) (util.Uint256, uint32, error) { 263 return c.actor.SendCall(c.hash, "renew", name, years) 264 } 265 266 // Renew2Transaction creates a transaction invoking `renew` method of the contract. 267 // This transaction is signed, but not sent to the network, instead it's 268 // returned to the caller. 269 func (c *Contract) Renew2Transaction(name string, years *big.Int) (*transaction.Transaction, error) { 270 return c.actor.MakeCall(c.hash, "renew", name, years) 271 } 272 273 // Renew2Unsigned creates a transaction invoking `renew` method of the contract. 274 // This transaction is not signed, it's simply returned to the caller. 275 // Any fields of it that do not affect fees can be changed (ValidUntilBlock, 276 // Nonce), fee values (NetworkFee, SystemFee) can be increased as well. 277 func (c *Contract) Renew2Unsigned(name string, years *big.Int) (*transaction.Transaction, error) { 278 return c.actor.MakeUnsignedCall(c.hash, "renew", nil, name, years) 279 } 280 281 // SetAdmin creates a transaction invoking `setAdmin` method of the contract. 282 // This transaction is signed and immediately sent to the network. 283 // The values returned are its hash, ValidUntilBlock value and error if any. 284 func (c *Contract) SetAdmin(name string, admin util.Uint160) (util.Uint256, uint32, error) { 285 return c.actor.SendCall(c.hash, "setAdmin", name, admin) 286 } 287 288 // SetAdminTransaction creates a transaction invoking `setAdmin` method of the contract. 289 // This transaction is signed, but not sent to the network, instead it's 290 // returned to the caller. 291 func (c *Contract) SetAdminTransaction(name string, admin util.Uint160) (*transaction.Transaction, error) { 292 return c.actor.MakeCall(c.hash, "setAdmin", name, admin) 293 } 294 295 // SetAdminUnsigned creates a transaction invoking `setAdmin` method of the contract. 296 // This transaction is not signed, it's simply returned to the caller. 297 // Any fields of it that do not affect fees can be changed (ValidUntilBlock, 298 // Nonce), fee values (NetworkFee, SystemFee) can be increased as well. 299 func (c *Contract) SetAdminUnsigned(name string, admin util.Uint160) (*transaction.Transaction, error) { 300 return c.actor.MakeUnsignedCall(c.hash, "setAdmin", nil, name, admin) 301 } 302 303 // SetRecord creates a transaction invoking `setRecord` method of the contract. 304 // This transaction is signed and immediately sent to the network. 305 // The values returned are its hash, ValidUntilBlock value and error if any. 306 func (c *Contract) SetRecord(name string, typev *big.Int, data string) (util.Uint256, uint32, error) { 307 return c.actor.SendCall(c.hash, "setRecord", name, typev, data) 308 } 309 310 // SetRecordTransaction creates a transaction invoking `setRecord` method of the contract. 311 // This transaction is signed, but not sent to the network, instead it's 312 // returned to the caller. 313 func (c *Contract) SetRecordTransaction(name string, typev *big.Int, data string) (*transaction.Transaction, error) { 314 return c.actor.MakeCall(c.hash, "setRecord", name, typev, data) 315 } 316 317 // SetRecordUnsigned creates a transaction invoking `setRecord` method of the contract. 318 // This transaction is not signed, it's simply returned to the caller. 319 // Any fields of it that do not affect fees can be changed (ValidUntilBlock, 320 // Nonce), fee values (NetworkFee, SystemFee) can be increased as well. 321 func (c *Contract) SetRecordUnsigned(name string, typev *big.Int, data string) (*transaction.Transaction, error) { 322 return c.actor.MakeUnsignedCall(c.hash, "setRecord", nil, name, typev, data) 323 } 324 325 // DeleteRecord creates a transaction invoking `deleteRecord` method of the contract. 326 // This transaction is signed and immediately sent to the network. 327 // The values returned are its hash, ValidUntilBlock value and error if any. 328 func (c *Contract) DeleteRecord(name string, typev *big.Int) (util.Uint256, uint32, error) { 329 return c.actor.SendCall(c.hash, "deleteRecord", name, typev) 330 } 331 332 // DeleteRecordTransaction creates a transaction invoking `deleteRecord` method of the contract. 333 // This transaction is signed, but not sent to the network, instead it's 334 // returned to the caller. 335 func (c *Contract) DeleteRecordTransaction(name string, typev *big.Int) (*transaction.Transaction, error) { 336 return c.actor.MakeCall(c.hash, "deleteRecord", name, typev) 337 } 338 339 // DeleteRecordUnsigned creates a transaction invoking `deleteRecord` method of the contract. 340 // This transaction is not signed, it's simply returned to the caller. 341 // Any fields of it that do not affect fees can be changed (ValidUntilBlock, 342 // Nonce), fee values (NetworkFee, SystemFee) can be increased as well. 343 func (c *Contract) DeleteRecordUnsigned(name string, typev *big.Int) (*transaction.Transaction, error) { 344 return c.actor.MakeUnsignedCall(c.hash, "deleteRecord", nil, name, typev) 345 } 346 347 // SetAdminEventsFromApplicationLog retrieves a set of all emitted events 348 // with "SetAdmin" name from the provided [result.ApplicationLog]. 349 func SetAdminEventsFromApplicationLog(log *result.ApplicationLog) ([]*SetAdminEvent, error) { 350 if log == nil { 351 return nil, errors.New("nil application log") 352 } 353 354 var res []*SetAdminEvent 355 for i, ex := range log.Executions { 356 for j, e := range ex.Events { 357 if e.Name != "SetAdmin" { 358 continue 359 } 360 event := new(SetAdminEvent) 361 err := event.FromStackItem(e.Item) 362 if err != nil { 363 return nil, fmt.Errorf("failed to deserialize SetAdminEvent from stackitem (execution #%d, event #%d): %w", i, j, err) 364 } 365 res = append(res, event) 366 } 367 } 368 369 return res, nil 370 } 371 372 // FromStackItem converts provided [stackitem.Array] to SetAdminEvent or 373 // returns an error if it's not possible to do to so. 374 func (e *SetAdminEvent) FromStackItem(item *stackitem.Array) error { 375 if item == nil { 376 return errors.New("nil item") 377 } 378 arr, ok := item.Value().([]stackitem.Item) 379 if !ok { 380 return errors.New("not an array") 381 } 382 if len(arr) != 3 { 383 return errors.New("wrong number of structure elements") 384 } 385 386 var ( 387 index = -1 388 err error 389 ) 390 index++ 391 e.Name, err = func(item stackitem.Item) (string, error) { 392 b, err := item.TryBytes() 393 if err != nil { 394 return "", err 395 } 396 if !utf8.Valid(b) { 397 return "", errors.New("not a UTF-8 string") 398 } 399 return string(b), nil 400 }(arr[index]) 401 if err != nil { 402 return fmt.Errorf("field Name: %w", err) 403 } 404 405 index++ 406 e.OldAdmin, err = func(item stackitem.Item) (util.Uint160, error) { 407 b, err := item.TryBytes() 408 if err != nil { 409 return util.Uint160{}, err 410 } 411 u, err := util.Uint160DecodeBytesBE(b) 412 if err != nil { 413 return util.Uint160{}, err 414 } 415 return u, nil 416 }(arr[index]) 417 if err != nil { 418 return fmt.Errorf("field OldAdmin: %w", err) 419 } 420 421 index++ 422 e.NewAdmin, err = func(item stackitem.Item) (util.Uint160, error) { 423 b, err := item.TryBytes() 424 if err != nil { 425 return util.Uint160{}, err 426 } 427 u, err := util.Uint160DecodeBytesBE(b) 428 if err != nil { 429 return util.Uint160{}, err 430 } 431 return u, nil 432 }(arr[index]) 433 if err != nil { 434 return fmt.Errorf("field NewAdmin: %w", err) 435 } 436 437 return nil 438 } 439 440 // RenewEventsFromApplicationLog retrieves a set of all emitted events 441 // with "Renew" name from the provided [result.ApplicationLog]. 442 func RenewEventsFromApplicationLog(log *result.ApplicationLog) ([]*RenewEvent, error) { 443 if log == nil { 444 return nil, errors.New("nil application log") 445 } 446 447 var res []*RenewEvent 448 for i, ex := range log.Executions { 449 for j, e := range ex.Events { 450 if e.Name != "Renew" { 451 continue 452 } 453 event := new(RenewEvent) 454 err := event.FromStackItem(e.Item) 455 if err != nil { 456 return nil, fmt.Errorf("failed to deserialize RenewEvent from stackitem (execution #%d, event #%d): %w", i, j, err) 457 } 458 res = append(res, event) 459 } 460 } 461 462 return res, nil 463 } 464 465 // FromStackItem converts provided [stackitem.Array] to RenewEvent or 466 // returns an error if it's not possible to do to so. 467 func (e *RenewEvent) FromStackItem(item *stackitem.Array) error { 468 if item == nil { 469 return errors.New("nil item") 470 } 471 arr, ok := item.Value().([]stackitem.Item) 472 if !ok { 473 return errors.New("not an array") 474 } 475 if len(arr) != 3 { 476 return errors.New("wrong number of structure elements") 477 } 478 479 var ( 480 index = -1 481 err error 482 ) 483 index++ 484 e.Name, err = func(item stackitem.Item) (string, error) { 485 b, err := item.TryBytes() 486 if err != nil { 487 return "", err 488 } 489 if !utf8.Valid(b) { 490 return "", errors.New("not a UTF-8 string") 491 } 492 return string(b), nil 493 }(arr[index]) 494 if err != nil { 495 return fmt.Errorf("field Name: %w", err) 496 } 497 498 index++ 499 e.OldExpiration, err = arr[index].TryInteger() 500 if err != nil { 501 return fmt.Errorf("field OldExpiration: %w", err) 502 } 503 504 index++ 505 e.NewExpiration, err = arr[index].TryInteger() 506 if err != nil { 507 return fmt.Errorf("field NewExpiration: %w", err) 508 } 509 510 return nil 511 }