Skip to content

Commit

Permalink
update abi & clean code (#15)
Browse files Browse the repository at this point in the history
  • Loading branch information
Liuhaai authored Feb 1, 2023
1 parent 91ee695 commit 1d5d7f2
Show file tree
Hide file tree
Showing 18 changed files with 714 additions and 149 deletions.
61 changes: 57 additions & 4 deletions blockchain/blockchain.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,61 @@
package blockchain

import "github.com/machinefi/w3bstream-wasm-golang-sdk/common"
import (
"errors"
"fmt"
"math/big"
"strings"
"unsafe"

func SendTx(key string) int32 {
addr, size := common.StringToPointer(key)
return common.WS_send_tx(addr, size)
"github.com/machinefi/w3bstream-wasm-golang-sdk/common"
)

func SendTx(chainID uint32, to string, value *big.Int, data string) (string, error) {
str := fmt.Sprintf(fmt.Sprintf(
`{
"to": "%s",
"value": "%s",
"data": "%s"
}`,
to,
value.String(),
strings.TrimPrefix(data, "0x"),
))
addr, size := common.StringToPointer(str)

vaddr := uintptr(unsafe.Pointer(new(uint32)))
vsize := uintptr(unsafe.Pointer(new(uint32)))
if ret := common.WS_send_tx(chainID, addr, size, uint32(vaddr), uint32(vsize)); ret != 0 {
return "", errors.New("sent tx failed")
}

m := common.Allocations.GetByAddr(*(*uint32)(unsafe.Pointer(vaddr)))
if m == nil {
return "", errors.New("sent tx failed")
}
return string(m.Data), nil
}

func CallContract(chainID uint32, to string, data string) ([]byte, error) {
str := fmt.Sprintf(fmt.Sprintf(
`{
"to": "%s",
"data": "%s"
}`,
to,
strings.TrimPrefix(data, "0x"),
))
addr, size := common.StringToPointer(str)

vaddr := uintptr(unsafe.Pointer(new(uint32)))
vsize := uintptr(unsafe.Pointer(new(uint32)))
if ret := common.WS_call_contract(chainID, addr, size, uint32(vaddr), uint32(vsize)); ret != 0 {
return nil, errors.New("sent tx failed")
}

m := common.Allocations.GetByAddr(*(*uint32)(unsafe.Pointer(vaddr)))
if m == nil {
return nil, errors.New("sent tx failed")
}
return m.Data, nil
}
6 changes: 5 additions & 1 deletion common/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,11 @@ func WS_get_sql_db(ptr, size uint32, vaddr, vsize uint32) int32

//go:wasm-module env
//export ws_send_tx
func WS_send_tx(kaddr, ksize uint32) (v int32)
func WS_send_tx(chainID uint32, kaddr, ksize uint32, vaddr, vsize uint32) (v int32)

//go:wasm-module env
//export ws_call_contract
func WS_call_contract(chainID uint32, kaddr, ksize uint32, vaddr, vsize uint32) (v int32)

//go:wasm-module env
//export ws_get_env
Expand Down
27 changes: 11 additions & 16 deletions database/kv.go
Original file line number Diff line number Diff line change
@@ -1,42 +1,37 @@
package database

import (
"bytes"
"encoding/binary"
"errors"
"unsafe"

"github.com/machinefi/w3bstream-wasm-golang-sdk/common"
)

func GetDB(key string) int32 {
func Get(key string) ([]byte, error) {
addr, size := common.StringToPointer(key)

rAddr := uintptr(unsafe.Pointer(new(uint32)))
rSize := uintptr(unsafe.Pointer(new(uint32)))

if ret := common.WS_get_db(addr, size, uint32(rAddr), uint32(rSize)); ret != 0 {
return 0
return nil, errors.New("fail to get the data from db")
}

vaddr := *(*uint32)(unsafe.Pointer(rAddr))
m := common.Allocations.GetByAddr(vaddr)
if m == nil {
return 0
return nil, errors.New("fail to get the data from db")
}

var ret int32
buf := bytes.NewBuffer(m.Data)
binary.Read(buf, binary.LittleEndian, &ret)
return ret
return m.Data, nil
}

func SetDB(key string, v int32) {
func Set(key string, value []byte) error {
addr, size := common.StringToPointer(key)
vaddr, vsize := common.BytesToPointer(value)

buf := new(bytes.Buffer)
binary.Write(buf, binary.LittleEndian, v)
vaddr, vsize := common.BytesToPointer(buf.Bytes())

// TODO: error handle
_ = common.WS_set_db(addr, size, vaddr, vsize)
if ret := common.WS_set_db(addr, size, vaddr, vsize); ret != 0 {
return errors.New("fail to set the data into db")
}
return nil
}
Loading

0 comments on commit 1d5d7f2

Please sign in to comment.