/
common.go
133 lines (120 loc) · 3.37 KB
/
common.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
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/*
* This file is part of The AnnChain.
*
* The AnnChain is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* The AnnChain is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with The www.annchain.io. If not, see <http://www.gnu.org/licenses/>.
*/
package main
import (
"errors"
"fmt"
"strings"
"github.com/dappledger/AnnChain/eth/accounts/abi"
"github.com/dappledger/AnnChain/eth/common"
"github.com/dappledger/AnnChain/eth/rlp"
"github.com/dappledger/AnnChain/angine/types"
ac "github.com/dappledger/AnnChain/module/lib/go-common"
cl "github.com/dappledger/AnnChain/module/lib/go-rpc/client"
)
func panicErr(err error) {
if err != nil {
panic(err)
}
}
func getNonce(client *cl.ClientJSONRPC, address string) (uint64, error) {
tmResult := new(types.RPCResult)
addrHex := ac.SanitizeHex(address)
addr := common.Hex2Bytes(addrHex)
query := append([]byte{1}, addr...)
if client == nil {
client = cl.NewClientJSONRPC(logger, rpcTarget)
}
_, err := client.Call("query", []interface{}{defaultChainID, query}, tmResult)
if err != nil {
return 0, err
}
res := (*tmResult).(*types.ResultQuery)
if res.Result.IsErr() {
fmt.Println(res.Result.Code, res.Result.Log)
return 0, errors.New(res.Result.Error())
}
nonce := new(uint64)
err = rlp.DecodeBytes(res.Result.Data, nonce)
panicErr(err)
return *nonce, nil
}
func unpackResult(method string, abiDef abi.ABI, output string) (interface{}, error) {
m, ok := abiDef.Methods[method]
if !ok {
return nil, errors.New("No such method")
}
if len(m.Outputs) == 0 {
return nil, errors.New("method " + m.Name + " doesn't have any returns")
}
if len(m.Outputs) == 1 {
var result interface{}
parsedData := common.ParseData(output)
if err := abiDef.Unpack(&result, method, parsedData); err != nil {
fmt.Println("error:", err)
return nil, err
}
if strings.Index(m.Outputs[0].Type.String(), "bytes") == 0 {
b := result.([]byte)
idx := 0
for i := 0; i < len(b); i++ {
if b[i] == 0 {
idx = i
} else {
break
}
}
b = b[idx+1:]
return fmt.Sprintf("%s", b), nil
}
return result, nil
}
d := common.ParseData(output)
var result []interface{}
if err := abiDef.Unpack(&result, method, d); err != nil {
fmt.Println("fail to unpack outpus:", err)
return nil, err
}
retVal := map[string]interface{}{}
for i, output := range m.Outputs {
if strings.Index(output.Type.String(), "bytes") == 0 {
b := result[i].([]byte)
idx := 0
for i := 0; i < len(b); i++ {
if b[i] == 0 {
idx = i
} else {
break
}
}
b = b[idx+1:]
retVal[output.Name] = fmt.Sprintf("%s", b)
} else {
retVal[output.Name] = result[i]
}
}
return retVal, nil
}
func assertContractExist(client *cl.ClientJSONRPC) {
if client == nil {
client = cl.NewClientJSONRPC(logger, rpcTarget)
}
exist := existContract(client, defaultPrivKey, defaultContractAddr, defaultBytecode)
if !exist {
panic("contract not exists")
}
}