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

Commit 6192127

Browse files
committed
feat: Go wasm - PST contract with tinygo
1 parent 0ee7a19 commit 6192127

16 files changed

+734
-112
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,4 +116,4 @@ pkg
116116
build
117117

118118
# go build folder
119-
out
119+
out/*.wasm

go/README.md

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,21 @@
11
# Go smartweave example contract
22

33
Note: we're using `tinygo` compiler, as the default compiler produces huuuuge binaries
4-
(hello world example - ~2MB - probably because it adds all the WASI related stuff)
4+
(`hello world` example - ~2MB)
55
![img.png](img.png)
66

7-
## How to use
8-
- [Install tinygo](https://tinygo.org/getting-started/install/)
9-
- Build wasm contract file: `bash build.sh` (it should create `pkg` folder)
10-
- Run wasm contract simulation: `node wasm_exec_tiny.js out/main.wasm`
11-
12-
note:
13-
use wasm_exec.js from tinygo, not the defaut one from Go - https://github.com/tinygo-org/tinygo/issues/2484
7+
## How to use (default Go compiler)
8+
- [Install easyjson](https://github.com/mailru/easyjson#install)
9+
- Run `easyjson -all easyjson/easyjson.go`
10+
- Build wasm contract file: `bash build.sh` (it should create `out` folder)
11+
- Run wasm contract simulation: `node run.js`
1412

15-
note 2:
16-
no support for decoding json in tinygo - https://github.com/tinygo-org/tinygo/issues/2660
13+
## How to use (tinygo compiler)
14+
- [Install tinygo](https://tinygo.org/getting-started/install/)
15+
- [Install easyjson](https://github.com/mailru/easyjson#install)
16+
- Run `easyjson -all easyjson/easyjson.go`
17+
- Build wasm contract file: `bash build-tiny.sh` (it should create `out` folder)
18+
- Run wasm contract simulation: `node run-tiny.js`
1719

18-
async features - https://stackoverflow.com/a/68427221/18299469
20+
Size comparison for PST contract:
21+
![img_1.png](img_1.png)

go/build-tiny.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
tinygo build -o out/contract_tiny.wasm -no-debug -target wasm main.go

go/common/async.go

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

33
import (
4-
"bytes"
5-
"encoding/gob"
6-
"encoding/json"
74
"syscall/js"
85
)
96

@@ -34,15 +31,12 @@ func Await(awaitable js.Value) ([]js.Value, []js.Value) {
3431
}
3532
}
3633

37-
func ConvertInto(action map[string]interface{}, into interface {}) {
38-
jsonStr, _ := json.Marshal(action)
39-
json.Unmarshal(jsonStr, into)
40-
}
41-
42-
func DeepCopy(src, dist interface{}) (err error){
43-
buf := bytes.Buffer{}
34+
func DeepCopy(src, dest interface{}) (err error) {
35+
dest = &src
36+
return nil
37+
/*buf := bytes.Buffer{}
4438
if err = gob.NewEncoder(&buf).Encode(src); err != nil {
4539
return
4640
}
47-
return gob.NewDecoder(&buf).Decode(dist)
41+
return gob.NewDecoder(&buf).Decode(dest)*/
4842
}

go/easyjson/easyjson.go

Lines changed: 38 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,41 @@
11
package easyjson
22

3-
//easyjson:json
4-
type PstStateEs struct {
5-
Ticker string `json:"ticker"`
6-
Name string `json:"name"`
7-
Owner string `json:"owner"`
8-
Evolve string `json:"evolve"`
9-
CanEvolve bool `json:"canEvolve"`
3+
type PstState struct {
4+
Ticker string `json:"ticker"`
5+
Name string `json:"name"`
6+
Owner string `json:"owner"`
7+
Evolve string `json:"evolve"`
8+
CanEvolve bool `json:"canEvolve"`
9+
Balances map[string]uint64 `json:"balances"`
1010
}
11+
12+
type Action struct {
13+
Function string `json:"function"`
14+
}
15+
16+
type TransferAction struct {
17+
Function string `json:"function"`
18+
Target string `json:"target"`
19+
Qty uint64 `json:"qty"`
20+
}
21+
22+
type EvolveAction struct {
23+
Action
24+
Value string `json:"target"`
25+
}
26+
27+
type BalanceAction struct {
28+
Action
29+
Target string `json:"target"`
30+
}
31+
32+
type ForeignCallAction struct {
33+
Action
34+
ContractTxId string `json:"contractTxId"`
35+
}
36+
37+
type BalanceResult struct {
38+
Balance uint64 `json:"balance"`
39+
}
40+
41+
type ActionResult = interface{}

0 commit comments

Comments
 (0)