github.com/mavryk-network/mvgo@v1.19.9/internal/generate/contract.go.tmpl (about)

     1  {{- define "entryParamsList" -}}
     2      {{- range .Params}}{{camel .Name}} {{type .Type}},{{end -}}
     3  {{- end -}}
     4  
     5  {{- define "originalSignature" -}}
     6      {{- $nArgs := len .Params -}}
     7  	{{.Name}}({{range $i, $param := .Params}}{{$param.Name}} {{$param.OriginalType}}{{if lt $i (sub $nArgs 1)}}, {{end}}{{end}})
     8  {{- end -}}
     9  
    10  {{- /*gotype: github.com/mavryk-network/mvgo/internal/generate.Data*/ -}}
    11  
    12  // Code generated by tzgen - DO NOT EDIT.
    13  // This file is a binding generated from {{.Contract.Name}} smart contract{{if ne .Address ""}} at address {{.Address}}{{end}}.
    14  // Any manual changes will be lost.
    15  
    16  package {{.Package}}
    17  
    18  import (
    19  	"context"
    20  	"encoding/json"
    21  	"math/big"
    22  	"time"
    23  
    24  	"github.com/mavryk-network/mvgo/contract"
    25  	"github.com/mavryk-network/mvgo/contract/bind"
    26  	"github.com/mavryk-network/mvgo/micheline"
    27  	"github.com/mavryk-network/mvgo/rpc"
    28  	"github.com/mavryk-network/mvgo/mavryk"
    29  	"github.com/pkg/errors"
    30  )
    31  
    32  {{$contract := pascal .Contract.Name}}
    33  {{$r := receiver $contract}}
    34  {{$rc := printf "(%s *%s)" $r $contract}}
    35  {{$rs := printf "(%s *%sSession)" $r $contract}}
    36  {{$rb := printf "(%sBuilder)" $contract}}
    37  
    38  {{with .Contract -}}
    39  
    40  // {{$contract}} is a generated binding to a Tezos smart contract.
    41  type {{$contract}} struct {
    42  	bind.Contract
    43  	builder {{$contract}}Builder
    44  	rpc bind.RPC
    45  	script *micheline.Script
    46  }
    47  
    48  // {{$contract}}Session is a generated binding to a Tezos smart contract, that will
    49  // use Opts for every call.
    50  type {{$contract}}Session struct {
    51  	*{{$contract}}
    52  	Opts *rpc.CallOptions
    53  }
    54  
    55  // {{$contract}}Builder is a generated struct that builds micheline.Parameters from
    56  // go types.
    57  type {{$contract}}Builder struct{}
    58  
    59  // New{{$contract}} creates a new {{$contract}} handle, bound to the provided address
    60  // with the given rpc.
    61  //
    62  // Returns an error if the contract was not found at the given address.
    63  func New{{$contract}}(ctx context.Context, address mavryk.Address, client *rpc.Client) (*{{$contract}}, error) {
    64  	script, err := client.GetContractScript(ctx, address)
    65  	if err != nil {
    66  		return nil, err
    67  	}
    68  
    69  	return &{{$contract}}{
    70  		Contract: contract.NewContract(address, client),
    71  		rpc: client,
    72  		script: script,
    73  	}, nil
    74  }
    75  
    76  // Session returns a new {{$contract}}Session with the configured rpc.CallOptions.
    77  func {{$rc}} Session(opts *rpc.CallOptions) *{{$contract}}Session {
    78  	return &{{$contract}}Session{ {{- $contract}}: {{$r}}, Opts: opts}
    79  }
    80  
    81  // Builder returns the builder struct for this contract.
    82  func {{$rc}} Builder() {{$contract}}Builder {
    83  	return {{$r}}.builder
    84  }
    85  
    86  // Storage queries the current storage of the contract.
    87  func {{$rc}} Storage(ctx context.Context) ({{type .Storage}}, error) {
    88  	return {{$r}}.StorageAt(ctx, rpc.Head)
    89  }
    90  
    91  // StorageAt queries the contract's storage at the given block.
    92  func {{$rc}} StorageAt(ctx context.Context, block rpc.BlockID) ({{type .Storage}}, error) {
    93  	var storage {{type .Storage}}
    94  	prim, err := {{$r}}.rpc.GetContractStorage(ctx, {{$r}}.Contract.Address(), block)
    95  	if err != nil {
    96  		return storage, errors.Wrap(err, "failed to get storage")
    97  	}
    98  	{{if eq .Storage.MichelineType "struct" -}}
    99  		prim = micheline.NewSeq(prim.UnfoldPairRecursive({{$r}}.script.StorageType())...)
   100  	{{- end}}
   101  	err = bind.UnmarshalPrim(prim, &storage)
   102  	if err != nil {
   103  		return storage, errors.Wrap(err, "failed to unmarshal storage")
   104  	}
   105  	{{- if eq .Storage.MichelineType "struct" -}}
   106  		{{- range .Storage.Fields -}}
   107  			{{- if eq .Type.MichelineType "big_map"}}
   108  				storage.{{pascal .Name}}.SetRPC({{$r}}.rpc)
   109  			{{- end -}}
   110  		{{- end -}}
   111  	{{- end}}
   112  	return storage, nil
   113  }
   114  
   115  // StorageFrom decodes micheline prim to contract storage
   116  func {{$rc}} StorageFrom(prim micheline.Prim) ({{type .Storage}}, error) {
   117  	var storage {{type .Storage}}
   118  	{{if eq .Storage.MichelineType "struct" -}}
   119  		prim = micheline.NewSeq(prim.UnfoldPairRecursive({{$r}}.script.StorageType())...)
   120  	{{- end}}
   121  	err := bind.UnmarshalPrim(prim, &storage)
   122  	if err != nil {
   123  		return storage, errors.Wrap(err, "failed to unmarshal storage")
   124  	}
   125  	{{- if eq .Storage.MichelineType "struct" -}}
   126  		{{- range .Storage.Fields -}}
   127  			{{- if eq .Type.MichelineType "big_map"}}
   128  				storage.{{pascal .Name}}.SetRPC({{$r}}.rpc)
   129  			{{- end -}}
   130  		{{- end -}}
   131  	{{- end}}
   132  	return storage, nil
   133  }
   134  
   135  func {{$contract}}StorageFrom(prim micheline.Prim) (*UsdtTetherStorage, error) {
   136      t := {{$contract}}{}
   137      err := json.Unmarshal([]byte({{$contract}}Micheline), &t.script)
   138      if err != nil {
   139          return nil, errors.Wrap(err, "failed to unmarshal contract's script")
   140      }
   141      return t.StorageFrom(prim)
   142  }
   143  
   144  // Deploy{{$contract}} deploys a {{$contract}} contract by using client and opts, and {{$contract}}Micheline.
   145  //
   146  // Returns the receipt and a handle to the {{$contract}} deployed contract.
   147  func Deploy{{$contract}}(ctx context.Context, opts *rpc.CallOptions, client *rpc.Client, storage {{type .Storage}}) (*rpc.Receipt, *{{$contract}}, error) {
   148  	var script *micheline.Script
   149  	err := json.Unmarshal([]byte({{$contract}}Micheline), &script)
   150  	if err != nil {
   151  		return nil, nil, errors.Wrap(err, "failed to unmarshal contract's script")
   152  	}
   153  
   154  	prim, err := bind.MarshalPrim(storage, false)
   155  	if err != nil {
   156  		return nil, nil, errors.Wrap(err, "failed to marshal storage")
   157  	}
   158  	script.Storage = prim
   159  
   160  	c := contract.NewEmptyContract(client).WithScript(script)
   161  	receipt, err := c.Deploy(ctx, opts)
   162  	if err != nil {
   163  		return nil, nil, errors.Wrap(err, "failed to deploy contract")
   164  	}
   165  	return receipt, &{{$contract}}{Contract: c, rpc: client}, nil
   166  }
   167  
   168  // region Entrypoints
   169  
   170  {{range .Entrypoints}}
   171  
   172  // {{pascal .Name}} makes a call to the `{{.Name}}` contract entry.
   173  //
   174  // {{template "originalSignature" .}}
   175  func {{$rc}} {{pascal .Name}}(ctx context.Context, opts *rpc.CallOptions, {{template "entryParamsList" .}}) (*rpc.Receipt, error) {
   176  	params, err := {{$r}}.builder.{{pascal .Name}}({{range .Params}}{{camel .Name}},{{end}})
   177  	if err != nil {
   178  		return nil, err
   179  	}
   180  	return {{$r}}.Contract.Call(ctx, &contract.TxArgs{Params: params}, opts)
   181  }
   182  
   183  // {{pascal .Name}} makes a call to the `{{.Name}}` contract entry.
   184  //
   185  // {{template "originalSignature" .}}
   186  func {{$rs}} {{pascal .Name}}(ctx context.Context, {{template "entryParamsList" .}}) (*rpc.Receipt, error) {
   187  	return {{$r}}.{{$contract}}.{{pascal .Name}}(ctx, {{$r}}.Opts, {{range .Params}}{{camel .Name}},{{end}})
   188  }
   189  
   190  // {{pascal .Name}} builds `{{.Name}}` contract entry's parameters.
   191  //
   192  // {{template "originalSignature" .}}
   193  func {{$rb}} {{pascal .Name}}({{template "entryParamsList" .}}) (micheline.Parameters, error) {
   194  	prim, err := bind.MarshalParams(false, {{range .Params}}{{camel .Name}},{{end}})
   195  	if err != nil {
   196  		return micheline.Parameters{}, errors.Wrap(err, "failed to marshal params")
   197  	}
   198  	return micheline.Parameters{Entrypoint: {{print $contract (pascal .Name)}}Entry, Value: prim}, nil
   199  }
   200  
   201  {{end}}
   202  
   203  // endregion
   204  
   205  {{if gt (len .Getters) 0}}
   206  
   207  // region Getters
   208  
   209  {{range .Getters}}
   210  
   211  // {{pascal .Name}} gets the result of the `{{.Name}}` TZIP-4 view.
   212  //
   213  // {{template "originalSignature" .}}
   214  func {{$rc}} {{pascal .Name}}(ctx context.Context, {{template "entryParamsList" .}}) ({{type .ReturnType}}, error) {
   215  	var res {{type .ReturnType}}
   216  	params, err := {{$r}}.builder.{{pascal .Name}}({{range .Params}}{{camel .Name}},{{end}})
   217  	prim, err := {{$r}}.Contract.RunView(ctx, params.Entrypoint, params.Value)
   218  	if err != nil {
   219  		return res, err
   220  	}
   221  	if err = bind.UnmarshalPrim(prim, &res); err != nil {
   222  		return res, errors.Wrap(err, "failed to unmarshal res")
   223  	}
   224  	return res, nil
   225  }
   226  
   227  // {{pascal .Name}} builds `{{.Name}}` TZIP-4 view's parameters.
   228  //
   229  // {{template "originalSignature" .}}
   230  func {{$rb}} {{pascal .Name}}({{template "entryParamsList" .}}) (micheline.Parameters, error) {
   231  	prim, err := bind.MarshalParams(false, {{range .Params}}{{camel .Name}},{{end}})
   232  	if err != nil {
   233  		return micheline.Parameters{}, errors.Wrap(err, "failed to marshal params")
   234  	}
   235  	return micheline.Parameters{Entrypoint: {{print $contract (pascal .Name)}}Entry, Value: prim}, nil
   236  }
   237  
   238  {{end}}
   239  
   240  // endregion
   241  
   242  {{- end}}
   243  
   244  {{- end}}
   245  
   246  {{range .Structs}}
   247  
   248  {{$rs := printf "(%s *%s)" $r (pascal .Name)}}
   249  {{$n := len .Fields}}
   250  
   251  // {{pascal .Name}} is a generated struct used to interact with {{$contract}} smart contract.
   252  type {{pascal .Name}} struct {
   253  	{{range .Fields}}
   254  		{{pascal .Name}} {{type .Type}}
   255  	{{- end}}
   256  }
   257  
   258  {{$path := printf "%sPaths" (pascal .Name)}}
   259  var {{$path}} = {{printf "%#v" .Path }}
   260  
   261  // MarshalPrim marshals {{pascal .Name}} into a Prim.
   262  func {{$rs}} MarshalPrim(optimized bool) (micheline.Prim, error) {
   263  	return bind.MarshalParamsPath(optimized, {{$path}}, {{range .Fields}}{{$r}}.{{pascal .Name}},{{end}})
   264  }
   265  
   266  // UnmarshalPrim unmarshals a Prim into a {{pascal .Name}}.
   267  func {{$rs}} UnmarshalPrim(prim micheline.Prim) error {
   268  	{{if eq $n 1 -}}
   269  		return bind.UnmarshalPrim(prim, &{{index .Fields 0}})
   270  	{{- else if .Flat -}}
   271  		return bind.UnmarshalPrimPaths(prim, map[string]any{
   272  			{{- range $i, $field := .Fields -}}
   273  				"{{$i}}": &{{$r}}.{{pascal $field.Name}},
   274  			{{- end -}}
   275  		})
   276  	{{- else -}}
   277  		return bind.UnmarshalPrimPaths(prim, map[string]any{
   278  		{{- range $i, $field := .Fields -}}
   279  			"{{pathFromIdx $i $n}}": &{{$r}}.{{pascal $field.Name}},
   280  		{{- end -}}
   281  		})
   282  	{{- end}}
   283  }
   284  
   285  {{end}}
   286  
   287  // {{$contract}} entry names
   288  const (
   289  	{{- range .Contract.Entrypoints}}
   290  		{{print $contract (pascal .Name)}}Entry = "{{.Name}}"
   291  	{{- end -}}
   292  	{{- range .Contract.Getters}}
   293  		{{print $contract (pascal .Name)}}Entry = "{{.Name}}"
   294  	{{- end -}}
   295  )
   296  
   297  const {{$contract}}Micheline = `{{.Contract.Micheline}}`
   298  
   299  var (
   300  	_ = big.NewInt
   301  	_ = micheline.NewPrim
   302  	_ = bind.MarshalParams
   303  	_ = time.Now
   304  )