docs: make compiler documentation more up to date
Add some notes on GOROOT setup and output files, properly describe YAML/manifest interactions and remove severely outdated examples (we have better ones in `examples` anyway).
This commit is contained in:
parent
79e11c93a1
commit
a217d62f8a
1 changed files with 28 additions and 94 deletions
122
docs/compiler.md
122
docs/compiler.md
|
@ -42,6 +42,15 @@ It should return no value and accept single bool argument which will be true on
|
|||
|
||||
## Quick start
|
||||
|
||||
### Go setup
|
||||
|
||||
The compiler uses Go parser internally and depends on regular Go compiler
|
||||
presence, so make sure you have it installed and set up. On some distributions
|
||||
this requires you to set proper `GOROOT` environment variable, like
|
||||
```
|
||||
export GOROOT=/usr/lib64/go/1.14
|
||||
```
|
||||
|
||||
### Compiling
|
||||
|
||||
```
|
||||
|
@ -55,7 +64,8 @@ By default the filename will be the name of your .go file with the .nef extensio
|
|||
```
|
||||
|
||||
If you contract is split across multiple files, you must provide a path
|
||||
to the directory where package files are contained instead of a single Go file:
|
||||
to the directory where package files are contained instead of a single Go file
|
||||
(`out.nef` will be used as the default output file in this case):
|
||||
```
|
||||
./bin/neo-go contract compile -i ./path/to/contract
|
||||
```
|
||||
|
@ -133,7 +143,7 @@ Toolkit](https://github.com/neo-project/neo-blockchain-toolkit/). To do that
|
|||
you need to generate debug information using `--debug` option, like this:
|
||||
|
||||
```
|
||||
$ ./bin/neo-go contract compile -i contract.go -o contract.nef --debug contract.debug.json
|
||||
$ ./bin/neo-go contract compile -i contract.go -c contract.yml -m contract.manifest.json -o contract.nef --debug contract.debug.json
|
||||
```
|
||||
|
||||
This file can then be used by debugger and set up to work just like for any
|
||||
|
@ -141,10 +151,18 @@ other supported language.
|
|||
|
||||
### Deploying
|
||||
|
||||
Deploying a contract to blockchain with neo-go requires a configuration file
|
||||
with contract's metadata in YAML format, like the following:
|
||||
|
||||
Deploying a contract to blockchain with neo-go requires both NEF and JSON
|
||||
manifest generated by the compiler from configuration file provided in YAML
|
||||
format. To create contract manifest pass YAML file with `-c` parameter and
|
||||
specify manifest output file with `-m`:
|
||||
```
|
||||
./bin/neo-go contract compile -i mycontract.go -c myconfig.yml -m mycontract.manifest.json
|
||||
```
|
||||
|
||||
Example YAML file contents:
|
||||
```
|
||||
name: Contract
|
||||
safemethods: []
|
||||
supportedstandards: []
|
||||
events:
|
||||
- name: info
|
||||
|
@ -153,10 +171,10 @@ events:
|
|||
type: ByteString
|
||||
```
|
||||
|
||||
It's passed to the `deploy` command via `-c` option:
|
||||
Then the manifest can be passed to the `deploy` command via `-m` option:
|
||||
|
||||
```
|
||||
$ ./bin/neo-go contract deploy -i contract.nef -c contract.yml -r http://localhost:20331 -w wallet.json -g 0.001
|
||||
$ ./bin/neo-go contract deploy -i contract.nef -m contract.manifest.json -r http://localhost:20331 -w wallet.json
|
||||
```
|
||||
|
||||
Deployment works via an RPC server, an address of which is passed via `-r`
|
||||
|
@ -201,93 +219,9 @@ $ ./bin/neo-go contract invokefunction -r http://localhost:20331 -w my_wallet.js
|
|||
|
||||
## Smart contract examples
|
||||
|
||||
Some examples are provided in the [examples directory](../examples).
|
||||
|
||||
### Check if the invoker of the contract is the owning address
|
||||
|
||||
```Golang
|
||||
package mycontract
|
||||
|
||||
import (
|
||||
"github.com/nspcc-dev/neo-go/pkg/interop/runtime"
|
||||
"github.com/nspcc-dev/neo-go/pkg/interop/util"
|
||||
)
|
||||
|
||||
var owner = util.FromAddress("AJX1jGfj3qPBbpAKjY527nPbnrnvSx9nCg")
|
||||
|
||||
func Main() bool {
|
||||
isOwner := runtime.CheckWitness(owner)
|
||||
|
||||
if isOwner {
|
||||
runtime.Log("invoker is the owner")
|
||||
return true
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
```
|
||||
|
||||
### Simple token
|
||||
|
||||
```Golang
|
||||
package mytoken
|
||||
|
||||
import (
|
||||
"github.com/nspcc-dev/neo-go/pkg/interop/runtime"
|
||||
"github.com/nspcc-dev/neo-go/pkg/interop/storage"
|
||||
)
|
||||
|
||||
var owner = util.FromAddress("AJX1jGfj3qPBbpAKjY527nPbnrnvSx9nCg")
|
||||
|
||||
type Token struct {
|
||||
Name string
|
||||
Symbol string
|
||||
TotalSupply int
|
||||
Owner []byte
|
||||
}
|
||||
|
||||
func (t Token) AddToCirculation(amount int) bool {
|
||||
ctx := storage.Context()
|
||||
var inCirc int
|
||||
val := storage.Get(ctx, "in_circ")
|
||||
if val != nil {
|
||||
inCirc = val.(int)
|
||||
}
|
||||
inCirc += amount
|
||||
storage.Put(ctx, "in_circ", inCirc)
|
||||
return true
|
||||
}
|
||||
|
||||
func newToken() Token {
|
||||
return Token{
|
||||
Name: "your awesome NEO token",
|
||||
Symbol: "YANT",
|
||||
TotalSupply: 1000,
|
||||
Owner: owner,
|
||||
}
|
||||
}
|
||||
|
||||
func Main(operation string, args []interface{}) bool {
|
||||
token := newToken()
|
||||
trigger := runtime.GetTrigger()
|
||||
|
||||
if trigger == runtime.Verification {
|
||||
isOwner := runtime.CheckWitness(token.Owner)
|
||||
if isOwner {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
if trigger == runtime.Application {
|
||||
if operation == "mintTokens" {
|
||||
token.AddToCirculation(100)
|
||||
}
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
```
|
||||
Some examples are provided in the [examples directory](../examples). For more
|
||||
sophisticated real-world contracts written in Go check out [NeoFS
|
||||
contracts](https://github.com/nspcc-dev/neofs-contract/).
|
||||
|
||||
## How to report compiler bugs
|
||||
1. Make a proper testcase (example testcases can be found in the tests folder)
|
||||
|
|
Loading…
Reference in a new issue