This repository was archived by the owner on Nov 26, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathhelpers.go
168 lines (152 loc) · 4.75 KB
/
helpers.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
161
162
163
164
165
166
167
168
package filters
import (
"bytes"
"context"
"encoding/json"
"fmt"
"log"
"math/big"
"reflect"
"github.com/elastic/go-elasticsearch/v8"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/ethclient"
"github.com/taarushv/helios/contracts/erc20"
)
func GetTokenSymbol(tokenAddress common.Address, client *ethclient.Client) string {
tokenIntance, _ := erc20.NewErc20(tokenAddress, client)
sym, _ := tokenIntance.Symbol(nil)
return sym
}
func GetTokenName(tokenAddress common.Address, client *ethclient.Client) string {
tokenIntance, _ := erc20.NewErc20(tokenAddress, client)
name, _ := tokenIntance.Name(nil)
return name
}
func getTxSenderAddress(tx *types.Transaction, client *ethclient.Client) string {
chainID, err := client.NetworkID(context.Background())
if err != nil {
log.Fatal(err)
}
msg, _ := tx.AsMessage(types.NewEIP155Signer(chainID))
return msg.From().Hex()
}
// Format # of tokens transferred into required float
func FormatERC20Decimals(tokensSent *big.Int, tokenAddress common.Address, client *ethclient.Client) float64 {
// Create a ERC20 instance and connect to geth to get decimals
tokenInstance, _ := erc20.NewErc20(tokenAddress, client)
decimals, _ := tokenInstance.Decimals(nil)
// Construct a denominator based on the decimals
// 18 decimals would result in denominator = 10^18
var base, exponent = big.NewInt(10), big.NewInt(int64(decimals))
denominator := base.Exp(base, exponent, nil)
// Convert to float for precision
tokensSentFloat := new(big.Float).SetInt(tokensSent)
denominatorFloat := new(big.Float).SetInt(denominator)
// Divide and return the final result
final, _ := new(big.Float).Quo(tokensSentFloat, denominatorFloat).Float64()
// TODO Take big.Accuracy into account
return final
}
func FormatEthWeiToEther(etherAmount *big.Int) float64 {
var base, exponent = big.NewInt(10), big.NewInt(18)
denominator := base.Exp(base, exponent, nil)
// Convert to float for precision
tokensSentFloat := new(big.Float).SetInt(etherAmount)
denominatorFloat := new(big.Float).SetInt(denominator)
// Divide and return the final result
final, _ := new(big.Float).Quo(tokensSentFloat, denominatorFloat).Float64()
return final
}
func itemExists(arrayType interface{}, item interface{}) bool {
arr := reflect.ValueOf(arrayType)
if arr.Kind() != reflect.Array {
panic("Invalid data-type")
}
for i := 0; i < arr.Len(); i++ {
if arr.Index(i).Interface() == item {
return true
}
}
return false
}
func GetFnIdentifierBySignature(fnSignature string) string {
var r map[string]interface{}
es, err := elasticsearch.NewDefaultClient()
if err != nil {
log.Fatalf("Error connecting to es client: %s", err)
}
// 3. Search for the indexed documents
//
// Build the request body.
var buf bytes.Buffer
query := map[string]interface{}{
"query": map[string]interface{}{
"match": map[string]interface{}{
"fnSignature": fnSignature,
},
},
}
if err := json.NewEncoder(&buf).Encode(query); err != nil {
log.Fatalf("Error encoding query: %s", err)
}
// Perform the search request.
res, err := es.Search(
es.Search.WithContext(context.Background()),
es.Search.WithIndex("4bytes"),
es.Search.WithBody(&buf),
es.Search.WithTrackTotalHits(true),
es.Search.WithPretty(),
)
if err != nil {
log.Fatalf("Error getting response: %s", err)
}
defer res.Body.Close()
if res.IsError() {
var e map[string]interface{}
if err := json.NewDecoder(res.Body).Decode(&e); err != nil {
log.Fatalf("Error parsing the response body: %s", err)
} else {
// Print the response status and error information.
log.Fatalf("[%s] %s: %s",
res.Status(),
e["error"].(map[string]interface{})["type"],
e["error"].(map[string]interface{})["reason"],
)
}
}
if err := json.NewDecoder(res.Body).Decode(&r); err != nil {
log.Fatalf("Error parsing the response body: %s", err)
}
// Print the ID and document source for each hit.
for _, hit := range r["hits"].(map[string]interface{})["hits"].([]interface{}) {
var value = (hit.(map[string]interface{})["_source"])
var fnIdentifier = (value.(map[string]interface{})["fnIdentifier"])
final := fmt.Sprintf("%v", fnIdentifier)
return final
}
return ""
}
func IsTxMined(txHash string, client *ethclient.Client) bool {
finalTxHash := common.HexToHash(txHash)
_, isPending, err := client.TransactionByHash(context.Background(), finalTxHash)
if err != nil {
log.Fatal(err)
}
return !isPending
}
func HasTxFailed(txHash string, client *ethclient.Client) bool {
if IsTxMined(txHash, client) {
receipt, err := client.TransactionReceipt(context.Background(), common.HexToHash(txHash))
if err != nil {
log.Fatal(err)
}
if receipt.Status == 1 {
return false
} else {
return true
}
} else {
return false
}
}