Skip to main content
Version: v1.3

Add Function Descriptors

You can use the Schema Tool to access, and initiate smart contract functions seamlessly using function descriptors. These descriptors allow you to access optional Params and Results maps through strict compile-time checked interfaces.

Function Descriptors Overview

Function descriptors are structures that:

  • Offer access to the optional Params and Results maps.
  • Allow synchronous or asynchronous initiation of functions through call() or post() requests.

The Schema Tool in Action

The Schema Tool performs the following tasks:

1. Generate Specific Descriptors:

  • For each function (func) and view.
  • The outcome: Structs granting access to Params or Results maps, wherever specified.

2. Create the ScFuncs Interface:

  • Facilitate the creation and initialization of each function descriptor.
  • Incorporate a member function for every func or view to craft their respective function descriptor properly.
package dividend

import "github.com/iotaledger/wasp/packages/wasmvm/wasmlib/go/wasmlib"

type DivideCall struct {
Func *wasmlib.ScFunc
}

type InitCall struct {
Func *wasmlib.ScInitFunc
Params MutableInitParams
}

type MemberCall struct {
Func *wasmlib.ScFunc
Params MutableMemberParams
}

type SetOwnerCall struct {
Func *wasmlib.ScFunc
Params MutableSetOwnerParams
}

type GetFactorCall struct {
Func *wasmlib.ScView
Params MutableGetFactorParams
Results ImmutableGetFactorResults
}

type GetOwnerCall struct {
Func *wasmlib.ScView
Results ImmutableGetOwnerResults
}

type Funcs struct{}

var ScFuncs Funcs

// divide tokens over members
func (sc Funcs) Divide(ctx wasmlib.ScFuncCallContext) *DivideCall {
return &DivideCall{Func: wasmlib.NewScFunc(ctx, HScName, HFuncDivide)}
}

func (sc Funcs) Init(ctx wasmlib.ScFuncCallContext) *InitCall {
f := &InitCall{Func: wasmlib.NewScInitFunc(ctx, HScName, HFuncInit)}
f.Params.proxy = wasmlib.NewCallParamsProxy(&f.Func.ScView)
return f
}

func (sc Funcs) Member(ctx wasmlib.ScFuncCallContext) *MemberCall {
f := &MemberCall{Func: wasmlib.NewScFunc(ctx, HScName, HFuncMember)}
f.Params.proxy = wasmlib.NewCallParamsProxy(&f.Func.ScView)
return f
}

func (sc Funcs) SetOwner(ctx wasmlib.ScFuncCallContext) *SetOwnerCall {
f := &SetOwnerCall{Func: wasmlib.NewScFunc(ctx, HScName, HFuncSetOwner)}
f.Params.proxy = wasmlib.NewCallParamsProxy(&f.Func.ScView)
return f
}

func (sc Funcs) GetFactor(ctx wasmlib.ScViewCallContext) *GetFactorCall {
f := &GetFactorCall{Func: wasmlib.NewScView(ctx, HScName, HViewGetFactor)}
f.Params.proxy = wasmlib.NewCallParamsProxy(f.Func)
wasmlib.NewCallResultsProxy(f.Func, &f.Results.proxy)
return f
}

func (sc Funcs) GetOwner(ctx wasmlib.ScViewCallContext) *GetOwnerCall {
f := &GetOwnerCall{Func: wasmlib.NewScView(ctx, HScName, HViewGetOwner)}
wasmlib.NewCallResultsProxy(f.Func, &f.Results.proxy)
return f
}

dividend Example - Generated Code

In the dividend example in contract.xx, specific structs for Funcs and Views are created. Here's how they operate:

Access

  • Via the func member within each struct.
  • The func member type: Either ScFunc or ScView, contingent on whether it’s a function or a view.

Utilization

  • The func member is used to initiate function calls in various ways.