This repository was archived by the owner on Jul 5, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
100 lines (87 loc) · 2.31 KB
/
main.go
File metadata and controls
100 lines (87 loc) · 2.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package main
import (
"context"
"fmt"
"log"
"math/big"
"os"
"reflect"
"unsafe"
"github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/ethclient"
compiler "aolda/compiler"
"github.com/joho/godotenv"
)
func findZeroFromByte(b []byte) (bool, int) {
for i := 0; i < len(b); i++ {
if b[i] == 0 {
return true, i
}
}
return false, 0
}
func BytesToString(b []byte) string {
isExist, index := findZeroFromByte(b)
var bb []byte
if isExist {
bb = b[:index]
} else {
bb = b
}
bh := (*reflect.SliceHeader)(unsafe.Pointer(&bb))
sh := reflect.StringHeader{bh.Data, bh.Len}
return *(*string)(unsafe.Pointer(&sh))
}
func ByteToInt(b []byte) int {
intFromByte := new(big.Int)
intFromByte.SetBytes(b)
return int(intFromByte.Uint64())
}
func main() {
err := godotenv.Load(".env")
apiKey := os.Getenv("INFURA_API_KEY")
client, err := ethclient.Dial("wss://goerli.infura.io/ws/v3/" + apiKey)
if err != nil {
log.Fatal(err)
}
contractAddress := common.HexToAddress("0xb3D008f2b892b9476cDEb75F701ee8Fb1E23AFc0")
query := ethereum.FilterQuery{
Addresses: []common.Address{contractAddress},
}
logs := make(chan types.Log)
sub, err := client.SubscribeFilterLogs(context.Background(), query, logs)
if err != nil {
log.Fatal(err)
}
for {
select {
case err := <-sub.Err():
log.Fatal(err)
case vLog := <-logs:
if vLog.Topics[0] == common.HexToHash("0x8d397347d14d1ad0861879661d8b750cec64835b214eee49d80b6bf6bb5950de") {
fmt.Println("Listen 'callAolda'")
}
var data []([]byte)
for i := 0; i < len(vLog.Data); i = i + 32 {
data = append(data, vLog.Data[i:i+32])
}
fileNamePointer := ByteToInt(data[0]) / 32
functionNamePointer := ByteToInt(data[1]) / 32
argsPointer := ByteToInt(data[2]) / 32
argsNum := ByteToInt(data[argsPointer])
fileName := BytesToString(data[fileNamePointer+1])
functionName := BytesToString(data[functionNamePointer+1])
var args []string
for i := argsPointer + 1; i < argsPointer+1+argsNum; i++ {
ptr := ByteToInt(data[i]) / 32
arg := BytesToString(data[argsPointer+ptr+2])
args = append(args, arg)
}
res := compiler.ExecuteJS("script.js", fileName, functionName, args)
fmt.Println(res)
//SetValue(functionName, args, res)
}
}
}