Skip to content
This repository was archived by the owner on Oct 15, 2024. It is now read-only.

feat: Go wasm - go 1.18 and generics #4

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion go/build.sh
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
mkdir -p .out
GOOS=js GOARCH=wasm go build -o .out/contract.wasm main.go
GOOS=js GOARCH=wasm go1.18rc1 build -o .out/contract.wasm main.go
2 changes: 1 addition & 1 deletion go/common/imports/smartweave/imports.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
)

func ReadContractState(contractTxId string) js.Value {
promise := importSmartWeave().Call("readContractState", contractTxId).JSValue()
promise := importSmartWeave().Call("readContractState", contractTxId)
result, _ := common.Await(promise)

return result[0]
Expand Down
11 changes: 5 additions & 6 deletions go/wasm/wasm.go → go/common/wasm.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
package wasm
package common

import (
"encoding/json"
"github.com/redstone-finance/redstone-contracts-wasm/go/common_types"
"github.com/redstone-finance/redstone-contracts-wasm/go/impl"
"syscall/js"
)

func Run(contract *impl.PstContract) {
func Run[T any](contract common_types.SwContract[T]) {
// the Go way of defining WASM exports...
// standard "exports" from the wasm module do not work here...
// that's kinda ugly TBH
Expand All @@ -22,7 +21,7 @@ func Run(contract *impl.PstContract) {
<-make(chan bool)
}

func handle(contract *impl.PstContract) js.Func {
func handle[T any](contract common_types.SwContract[T]) js.Func {
// note: each 'exported' function has to be wrapped into
// js.FuncOf(func(this js.Value, args []js.Value) interface{}
// - that's kinda ugly too...
Expand Down Expand Up @@ -80,14 +79,14 @@ func lang() interface{} {
})
}

func currentState(contract *impl.PstContract) interface{} {
func currentState[T any](contract common_types.SwContract[T]) interface{} {
return js.FuncOf(func(this js.Value, args []js.Value) interface{} {
data, _ := json.Marshal(contract.CurrentState())
return string(data)
})
}

func initState(contract *impl.PstContract) interface{} {
func initState[T any](contract common_types.SwContract[T]) interface{} {
return js.FuncOf(func(this js.Value, args []js.Value) interface{} {
contract.InitState(args[0].String())
return nil
Expand Down
6 changes: 5 additions & 1 deletion go/common_types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,9 @@ type Action struct {
type ActionResult = interface{}

//easyjson:skip
type SwContract interface {
type SwContract[S any] interface {
Handle(action Action, actionBytes []byte) (*S, ActionResult, error)
InitState(stateJson string)
UpdateState(newState *S)
CurrentState() S
}
2 changes: 1 addition & 1 deletion go/go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/redstone-finance/redstone-contracts-wasm/go

go 1.17
go 1.18

require (
github.com/josharian/intern v1.0.0 // indirect
Expand Down
7 changes: 5 additions & 2 deletions go/main.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
package main

import (
"github.com/redstone-finance/redstone-contracts-wasm/go/common"
"github.com/redstone-finance/redstone-contracts-wasm/go/common_types"
"github.com/redstone-finance/redstone-contracts-wasm/go/impl"
"github.com/redstone-finance/redstone-contracts-wasm/go/wasm"
"github.com/redstone-finance/redstone-contracts-wasm/go/types"
)

// the current state of the contract that contract developers have to define
var contract = impl.PstContract{}

// handles all the WASM-JS related trickery...
func main() {
wasm.Run(&contract)
var swContract common_types.SwContract[types.PstState] = &contract
common.Run(swContract)
}