-
Notifications
You must be signed in to change notification settings - Fork 18
/
createTestString.go
183 lines (143 loc) · 4.54 KB
/
createTestString.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
// Copyright 2022 Fluidity Money. All rights reserved. Use of this
// source code is governed by a GPL-style license that can be found in the
// LICENSE.md file.
package test_utils
import (
"context"
"encoding/base64"
"encoding/json"
"fmt"
"os"
"sync"
ethcommon "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/ethclient"
"github.com/fluidity-money/fluidity-app/common/ethereum"
"github.com/fluidity-money/fluidity-app/lib/log"
ethTypes "github.com/fluidity-money/fluidity-app/lib/types/ethereum"
"github.com/fluidity-money/fluidity-app/lib/util"
)
type TestStructure struct {
Transfer struct {
Transaction ethTypes.Hash `json:"transaction"`
Log struct {
Data string `json:"data"`
Address ethTypes.Address `json:"address"`
Topics []ethTypes.Hash `json:"topics"`
} `json:"log"`
Application int `json:"application"`
} `json:"transfer"`
Transaction struct {
To ethTypes.Address `json:"to"`
From ethTypes.Address `json:"from"`
Hash ethTypes.Hash `json:"hash"`
} `json:"transaction"`
ExpectedSender string `json:"expected_sender"`
ExpectedRecipient string `json:"expected_recipient"`
ExpectedFees string `json:"expected_fees"`
TokenDecimals int `json:"token_decimals"`
ContractAddress string `json:"contract_address"`
}
const EnvEthereumRpcUrl = `FLU_ETHEREUM_RPC_URL`
func main() {
args := os.Args
if len(args) < 3 {
log.Fatal(func(k *log.Log) {
k.Format(
"Expected arguments containing LogSignature, Transaction Signature(s) got %d",
len(args),
)
})
}
gethHttpApi := util.GetEnvOrFatal(EnvEthereumRpcUrl)
swapSignature := ethcommon.HexToHash(args[1])
testChan := make(chan TestStructure)
tests := make([]TestStructure, 0)
txJobs := new(sync.WaitGroup)
ethClient, err := ethclient.Dial(gethHttpApi)
if err != nil {
log.Fatal(func(k *log.Log) {
k.Format("could not dial eth rpc url %v!", gethHttpApi)
k.Payload = err
})
}
for txIndex := 2; txIndex < len(args); txIndex++ {
transactionHex := ethcommon.HexToHash(args[txIndex])
txJobs.Add(1)
go func() {
defer txJobs.Done()
err := fetchTransactionValues(testChan, ethClient, transactionHex, swapSignature)
if err != nil {
log.Fatal(func(k *log.Log) {
k.Format("could not fetch all values from tx %v!", transactionHex)
k.Payload = err
})
}
}()
}
// Seperate Goroutine allows channel to be read before closing
go func() {
txJobs.Wait()
close(testChan)
}()
for test := range testChan {
tests = append(tests, test)
}
prettyTests, _ := json.Marshal(tests)
fmt.Println(string(prettyTests))
}
// fetchTransactionValues uses ethClient to find Data, Address, Topics, Sender, Recipient and TxHash
// to fill in TestStructure.
// May return multiple relevant Logs per call
func fetchTransactionValues(testChan chan TestStructure, ethClient *ethclient.Client, transactionHash, swapSignature ethcommon.Hash) error {
txReceipt, err := ethClient.TransactionReceipt(context.Background(), transactionHash)
if err != nil {
return fmt.Errorf(
"Couldn't fetch receipt from %v! %v",
transactionHash.String(),
err,
)
}
var (
blockHash = txReceipt.BlockHash
blockNumber = txReceipt.BlockNumber
txHash = txReceipt.TxHash
logs = txReceipt.Logs
)
transaction, _, err := ethClient.TransactionByHash(context.Background(), transactionHash)
if err != nil {
return fmt.Errorf(
"Couldn't fetch transaction from %v! %v",
transactionHash.String(),
err,
)
}
sender, err := ethClient.TransactionSender(context.Background(), transaction, blockHash, uint(blockNumber.Uint64()))
if err != nil {
return fmt.Errorf(
"Couldn't get TransactionSender from tx %v, blockHash %v, blockNum %v! %v",
transactionHash.String(),
blockHash.String(),
blockNumber,
err,
)
}
for _, log := range logs {
logSignature := log.Topics[0]
if logSignature == swapSignature {
var test TestStructure
test.Transfer.Log.Data = base64.StdEncoding.EncodeToString(log.Data)
test.Transfer.Log.Address = ethereum.ConvertGethAddress(log.Address)
testTopics := make([]ethTypes.Hash, 0)
for _, topic := range log.Topics {
testTopics = append(testTopics, ethereum.ConvertGethHash(topic))
}
test.Transfer.Log.Topics = testTopics
test.Transaction.To = ethereum.ConvertGethAddress(*transaction.To())
test.Transaction.From = ethereum.ConvertGethAddress(sender)
test.Transaction.Hash = ethereum.ConvertGethHash(txHash)
test.Transfer.Transaction = ethereum.ConvertGethHash(txHash)
testChan <- test
}
}
return nil
}