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

Commit c00eec7

Browse files
committed
feat: Go wasm - go 1.18 and generics
1 parent 831d6f5 commit c00eec7

File tree

6 files changed

+18
-12
lines changed

6 files changed

+18
-12
lines changed

go/build.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
mkdir -p .out
2-
GOOS=js GOARCH=wasm go build -o .out/contract.wasm main.go
2+
GOOS=js GOARCH=wasm go1.18rc1 build -o .out/contract.wasm main.go

go/common/imports/smartweave/imports.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import (
66
)
77

88
func ReadContractState(contractTxId string) js.Value {
9-
promise := importSmartWeave().Call("readContractState", contractTxId).JSValue()
9+
promise := importSmartWeave().Call("readContractState", contractTxId)
1010
result, _ := common.Await(promise)
1111

1212
return result[0]

go/wasm/wasm.go renamed to go/common/wasm.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
1-
package wasm
1+
package common
22

33
import (
44
"encoding/json"
55
"github.com/redstone-finance/redstone-contracts-wasm/go/common_types"
6-
"github.com/redstone-finance/redstone-contracts-wasm/go/impl"
76
"syscall/js"
87
)
98

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

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

83-
func currentState(contract *impl.PstContract) interface{} {
82+
func currentState[T any](contract common_types.SwContract[T]) interface{} {
8483
return js.FuncOf(func(this js.Value, args []js.Value) interface{} {
8584
data, _ := json.Marshal(contract.CurrentState())
8685
return string(data)
8786
})
8887
}
8988

90-
func initState(contract *impl.PstContract) interface{} {
89+
func initState[T any](contract common_types.SwContract[T]) interface{} {
9190
return js.FuncOf(func(this js.Value, args []js.Value) interface{} {
9291
contract.InitState(args[0].String())
9392
return nil

go/common_types/types.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,9 @@ type Action struct {
77
type ActionResult = interface{}
88

99
//easyjson:skip
10-
type SwContract interface {
10+
type SwContract[S any] interface {
11+
Handle(action Action, actionBytes []byte) (*S, ActionResult, error)
12+
InitState(stateJson string)
13+
UpdateState(newState *S)
14+
CurrentState() S
1115
}

go/go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module github.com/redstone-finance/redstone-contracts-wasm/go
22

3-
go 1.17
3+
go 1.18
44

55
require (
66
github.com/josharian/intern v1.0.0 // indirect

go/main.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
11
package main
22

33
import (
4+
"github.com/redstone-finance/redstone-contracts-wasm/go/common"
5+
"github.com/redstone-finance/redstone-contracts-wasm/go/common_types"
46
"github.com/redstone-finance/redstone-contracts-wasm/go/impl"
5-
"github.com/redstone-finance/redstone-contracts-wasm/go/wasm"
7+
"github.com/redstone-finance/redstone-contracts-wasm/go/types"
68
)
79

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

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

0 commit comments

Comments
 (0)