-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
18 changed files
with
714 additions
and
149 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
Oops, something went wrong.