neo-go/examples
Anna Shaleva a0d991a500 rpcbinding: support map[any]any conversion for extended types
Unfortunately, without pre-set user extended types configuration for events
and without --guess-eventtypes flag set we are allowed to rely only on manifest
information about types. Manifest can't give us a lot of information, but we
still need to be able to generate RPC binding. Arrays and structs are correctly
handled by the current code, but maps always rely on the fact that map's value
type is set. It's not true in the described case, so make the maps type convertor
handle this situation in a similar way how arrays are handled.

Without this commit the following panic occurs on attempt to generate RPC binding:
```
    --- FAIL: TestAssistedRPCBindings/testdata/notifications (0.01s)
panic: runtime error: invalid memory address or nil pointer dereference [recovered]
	panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x0 pc=0x7f7c0e]

goroutine 190 [running]:
testing.tRunner.func1.2({0x109cb40, 0x1d58760})
	/usr/local/go/src/testing/testing.go:1396 +0x24e
testing.tRunner.func1()
	/usr/local/go/src/testing/testing.go:1399 +0x39f
panic({0x109cb40, 0x1d58760})
	/usr/local/go/src/runtime/panic.go:884 +0x212
github.com/nspcc-dev/neo-go/pkg/smartcontract/rpcbinding.extendedTypeToGo({0x22, {0x0, 0x0}, {0x0, 0x0}, 0x0, 0x0, {0x0, 0x0, 0x0}}, ...)
	/home/anna/Documents/GitProjects/nspcc-dev/neo-go/pkg/smartcontract/rpcbinding/binding.go:515 +0x36e
github.com/nspcc-dev/neo-go/pkg/smartcontract/rpcbinding.scTypeToGo({0xc000206d92?, 0xc000206d80?}, 0x22, 0xc0005d70e0)
	/home/anna/Documents/GitProjects/nspcc-dev/neo-go/pkg/smartcontract/rpcbinding/binding.go:643 +0x138
github.com/nspcc-dev/neo-go/pkg/smartcontract/rpcbinding.scTemplateToRPC({{0xc00049bb07, 0x7}, 0xc0004c89c0, {0x33, 0x22, 0x11, 0x0, 0xff, 0xee, 0xdd, ...}, ...}, ...)
	/home/anna/Documents/GitProjects/nspcc-dev/neo-go/pkg/smartcontract/rpcbinding/binding.go:686 +0xbc4
github.com/nspcc-dev/neo-go/pkg/smartcontract/rpcbinding.Generate({{0xc00049bb07, 0x7}, 0xc0004c89c0, {0x33, 0x22, 0x11, 0x0, 0xff, 0xee, 0xdd, ...}, ...})
	/home/anna/Documents/GitProjects/nspcc-dev/neo-go/pkg/smartcontract/rpcbinding/binding.go:421 +0x387
github.com/nspcc-dev/neo-go/cli/smartcontract.contractGenerateSomething(0xc00043e2c0, 0x137cd00)
	/home/anna/Documents/GitProjects/nspcc-dev/neo-go/cli/smartcontract/generate.go:99 +0x855
github.com/nspcc-dev/neo-go/cli/smartcontract.contractGenerateRPCWrapper(0xc00043e2c0?)
	/home/anna/Documents/GitProjects/nspcc-dev/neo-go/cli/smartcontract/generate.go:60 +0x25
github.com/urfave/cli.HandleAction({0x1048380?, 0x137c660?}, 0x13?)
	/home/anna/go/pkg/mod/github.com/urfave/cli@v1.22.5/app.go:524 +0x50
github.com/urfave/cli.Command.Run({{0x123539d, 0x13}, {0x0, 0x0}, {0x0, 0x0, 0x0}, {0x12577ad, 0x2a}, {0x127ad35, ...}, ...}, ...)
	/home/anna/go/pkg/mod/github.com/urfave/cli@v1.22.5/command.go:173 +0x65b
github.com/urfave/cli.(*App).RunAsSubcommand(0xc0001f4000, 0xc00043e000)
	/home/anna/go/pkg/mod/github.com/urfave/cli@v1.22.5/app.go:405 +0x91b
github.com/urfave/cli.Command.startApp({{0x12281e1, 0x8}, {0x0, 0x0}, {0x0, 0x0, 0x0}, {0x1254d8a, 0x28}, {0x0, ...}, ...}, ...)
	/home/anna/go/pkg/mod/github.com/urfave/cli@v1.22.5/command.go:372 +0x6e7
github.com/urfave/cli.Command.Run({{0x12281e1, 0x8}, {0x0, 0x0}, {0x0, 0x0, 0x0}, {0x1254d8a, 0x28}, {0x0, ...}, ...}, ...)
	/home/anna/go/pkg/mod/github.com/urfave/cli@v1.22.5/command.go:102 +0x825
github.com/urfave/cli.(*App).Run(0xc00024e000, {0xc0004f6420, 0xb, 0xb})
	/home/anna/go/pkg/mod/github.com/urfave/cli@v1.22.5/app.go:277 +0x8a7
github.com/nspcc-dev/neo-go/cli/smartcontract.TestAssistedRPCBindings.func1.1(0x9f8829?)
	/home/anna/Documents/GitProjects/nspcc-dev/neo-go/cli/smartcontract/generate_test.go:395 +0x5fc
testing.tRunner(0xc0006824e0, 0xc0004a3680)
	/usr/local/go/src/testing/testing.go:1446 +0x10b
created by testing.(*T).Run
	/usr/local/go/src/testing/testing.go:1493 +0x35f
```

Signed-off-by: Anna Shaleva <shaleva.ann@nspcc.ru>
2023-05-31 15:53:43 +03:00
..
engine *: update interop deps after #2892 2023-04-20 16:11:57 +03:00
events rpcbinding: support map[any]any conversion for extended types 2023-05-31 15:53:43 +03:00
iterator *: update interop deps after #2892 2023-04-20 16:11:57 +03:00
nft-d *: update interop deps after #2892 2023-04-20 16:11:57 +03:00
nft-nd *: update interop deps after #2892 2023-04-20 16:11:57 +03:00
nft-nd-nns *: update interop deps after #2892 2023-04-20 16:11:57 +03:00
oracle *: update interop deps after #2892 2023-04-20 16:11:57 +03:00
runtime examples: fix owner's verification of Runtime example contract 2023-05-19 13:26:56 +03:00
storage *: update interop deps after #2892 2023-04-20 16:11:57 +03:00
timer *: update interop deps after #2892 2023-04-20 16:11:57 +03:00
token *: update interop deps after #2892 2023-04-20 16:11:57 +03:00
my_wallet.json wallet: fix wallet version to conform to NEP-6 2022-08-07 22:41:40 +03:00
README.md *: fix Neo and NeoGo misuses 2022-12-07 17:29:09 +03:00

NeoGo smart contract examples

examples directory contains smart contract examples written in Go. These examples are aimed to demonstrate the basic usage of Go programming language to write Neo smart contracts as far as to provide a brief introduction to the Neo-specific interop package application.

Examples structure

Each example represents a separate folder with the smart contract content inside. The content is presented by the smart contract code written in Go and the smart contract configuration file with .yml extension.

Some contracts have a contract owner, which is needed for witness checking and represented by the owner string constant inside the smart contract code. The owner account address is taken from the my_wallet.json. The wallet is located under the examples directory, has a single account inside with the my_account label which can be decrypted with the qwerty password. You can use my_wallet.json to deploy example contracts.

See the table below for the detailed examples description.

Example Description
engine This contract demonstrates how to use runtime interop package which implements an API for System.Runtime.* Neo system calls. Please, refer to the runtime package documentation for details.
events The contract shows how execution notifications with the different arguments types can be sent with the help of runtime.Notify function of the runtime interop package. Please, refer to the runtime.Notify function documentation for details.
iterator This example describes a way to work with Neo iterators. Please, refer to the iterator package documentation for details.
nft-d NEP-11 divisible NFT. See NEP-11 token standard specification for details.
nft-nd NEP-11 non-divisible NFT. See NEP-11 token standard specification for details.
nft-nd-nns Neo Name Service contract which is NEP-11 non-divisible NFT. The contract implements methods for Neo domain name system managing such as domains registration/transferring, records addition and names resolving. The package also contains tests implemented with neotest.
oracle Oracle demo contract exposing two methods that you can use to process URLs. It uses oracle native contract, see interop package documentation also.
runtime This contract demonstrates how to use special _initialize and _deploy methods. See the compiler documentation for methods details. It also shows the pattern for checking owner witness inside the contract with the help of runtime.CheckWitness interop function.
storage The contract implements API for basic operations with a contract storage. It shows hos to use storage interop package. See the storage package documentation.
timer The idea of the contract is to count tick method invocations and destroy itself after the third invocation. It shows how to use contract.Call interop function to call, update (migrate) and destroy the contract. Please, refer to the contract.Call function documentation
token This contract implements NEP-17 token standard (like NEO and GAS tokens) with all required methods and operations. See the NEP-17 token standard specification for details.

Compile

Please, refer to the neo-go smart contract compiler documentation to compile example smart contracts.

Deploy

You can set up neo-go private network to deploy the example contracts. Please, refer to the consensus documentation to start your own privnet with neo-go nodes.

To deploy smart contracts, refer to the Deploying section of the compiler documentation.

Where to start

Feel free to explore neo-go smart contract development workshop to get the basic concepts of how to develop, compile, debug and deploy Neo smart contracts written in go.