-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathabi.go
executable file
·70 lines (60 loc) · 1.55 KB
/
abi.go
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
package msgpack
import (
"fmt"
"io/ioutil"
"net/http"
"strings"
"encoding/json"
"github.com/bitly/go-simplejson"
)
//ParseAbi parse abiraw to struct for contracts
func ParseAbi(abiRaw []byte) (*ABI, error) {
abis := &ABIStructs{}
err := json.Unmarshal(abiRaw, abis)
if err != nil {
return &ABI{}, err
}
abi := &ABI{}
abi.Structs = make([]ABIStruct, len(abis.Structs))
for i := range abi.Structs {
abi.Structs[i].Fields = New()
}
err = json.Unmarshal(abiRaw, abi)
if err != nil {
return &ABI{}, err
}
return abi, nil
}
//GetAbibyContractName function
func GetAbibyContractName(contractname string) (ABI, error) {
var abistring string
NodeIp := "127.0.0.1"
addr := "http://" + NodeIp + ":8080/rpc"
params := `service=bottos&method=Chain.GetAbi&request={
"contract":"%s"}`
s := fmt.Sprintf(params, contractname)
respBody, err := http.Post(addr, "application/x-www-form-urlencoded",
strings.NewReader(s))
if err != nil {
fmt.Println(err)
return ABI{}, err
}
defer respBody.Body.Close()
body, err := ioutil.ReadAll(respBody.Body)
if err != nil {
fmt.Println(err)
return ABI{}, err
}
jss, _ := simplejson.NewJson([]byte(body))
abistring = jss.Get("result").MustString()
if len(abistring) <= 0 {
fmt.Println(err)
return ABI{}, err
}
Abi, err := ParseAbi([]byte(abistring))
if err != nil {
fmt.Println("Parse abistring", abistring, " to abi failed!")
return ABI{}, err
}
return *Abi, nil
}