-
Notifications
You must be signed in to change notification settings - Fork 5
/
utils.go
160 lines (136 loc) · 3.85 KB
/
utils.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package ethNotification
import (
"fmt"
"math"
"math/big"
"strconv"
"strings"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/rpc"
)
func ParseInputTx(input string, decimals int8) *InputData {
if len(input) != 138 {
return nil
}
methodID := input[0:10]
if methodID != MethodIDTransferERC20Token.String() {
return nil
}
toAddress := strings.TrimLeft(input[10:74], "0")
valueRaw := strings.TrimLeft(input[75:], "0")
valBigInt, _ := ConvertHexStringToBigInt(valueRaw)
// valDcms := ConvertInputValueWithDecimal(valBigInt.String(), decimals)
return &InputData{
MethodID: methodID,
ToAddress: toAddress,
Value: valBigInt.String(),
// ValueWithDecimals: valDcms,
}
}
func ConvertHexStringToBigInt(str string) (*big.Int, error) {
if !strings.HasPrefix(str, "0x") {
str = "0x" + str
}
bigInt, err := hexutil.DecodeBig(str)
return bigInt, err
}
func ConvertInputValueWithDecimal(valStr string, decimals int8) string {
trimStr := strings.Trim(valStr, "0")
if trimStr == "" || decimals == 0 {
return valStr
}
if trimStr == "." {
return "0"
}
decimalsInt := int(decimals)
lenVal := len(valStr)
arrStr := strings.Split(valStr, "")
if lenVal < decimalsInt {
numberOfZero := decimalsInt - lenVal - 1
return "0." + strings.Repeat("0", numberOfZero) + strings.TrimRight(valStr, "0")
}
if lenVal > decimalsInt {
index := lenVal - decimalsInt
pre := strings.Join(arrStr[:index], "")
suf := strings.Join(arrStr[index:], "")
if strings.Trim(suf, "0") == "" {
return pre
}
return pre + "." + strings.TrimRight(suf, "0")
}
return "0." + strings.TrimRight(valStr, "0")
}
/**
* @author: thienthongthai
*/
func CoinToNumberInString(value *big.Int, decimal int, number_precision int) string {
valueRat := new(big.Rat).SetInt(value)
powDeicmal := new(big.Rat).SetFloat64(math.Pow(float64(10), float64(decimal)))
valFloat, _ := new(big.Rat).Quo(valueRat, powDeicmal).Float64()
pow := math.Pow(float64(10), float64(number_precision))
intValue, _ := strconv.Atoi(fmt.Sprintf("%.0f", valFloat*pow))
return fmt.Sprintf("%."+fmt.Sprintf("%d", number_precision)+"g", float64(intValue)/pow)
}
func getTransactionHashesFromBlock(b *Block) []string {
result := []string{}
for _, tran := range b.Transactions {
result = append(result, tran.Hash)
}
return result
}
func updateTransactionFromReceipt(ds EngineTokenDataSource, trans []Transaction) []Transaction {
for i, tran := range trans {
if tran.Value != "0x0" {
bigInt, _ := ConvertHexStringToBigInt(tran.Value)
trans[i].Value = bigInt.String()
} else {
trans[i].Value = "0"
}
if tran.Receipt.Status == "0x1" {
trans[i].Status = Success
} else {
trans[i].Status = Failure
}
if isERCTransaction(&trans[i]) {
fillTokenInfo(ds, &trans[i])
}
}
return trans
}
func fillTokenInfo(ds EngineTokenDataSource, tran *Transaction) {
tokens := ds.FindTokens([]string{tran.To})
if len(tokens) > 0 {
token := tokens[0]
inputData := ParseInputTx(tran.Input, token.Decimals)
if inputData == nil {
return
}
tran.To = inputData.ToAddress
tran.Value = inputData.Value
tran.ContractAddress = token.ContractAddress
tran.TokenDecimal = int(token.Decimals)
tran.TokenSymbol = token.Symbol
}
}
func generateTxReceiptBatchElements(b *Block) []rpc.BatchElem {
result := []rpc.BatchElem{}
for i, tran := range b.Transactions {
var arg interface{} = tran.Hash
be := rpc.BatchElem{
Method: "eth_getTransactionReceipt",
Args: []interface{}{arg},
Result: &b.Transactions[i].Receipt,
}
result = append(result, be)
}
return result
}
func isETHTransaction(tran *Transaction) bool {
return tran.Input == "0x"
}
func isERCTransaction(tran *Transaction) bool {
return strings.Contains(tran.Input, MethodIDTransferERC20Token.String())
}
func allowPush(tran *Transaction) bool {
return isETHTransaction(tran) || isERCTransaction(tran)
}