-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
189 lines (173 loc) · 5.31 KB
/
main.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
184
185
186
187
188
189
package main
import (
"bufio"
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"log"
"math/big"
"net/http"
"os"
"strconv"
"strings"
"time"
"github.com/diadata-org/diadata/internal/pkg/blockchain-scrapers/blockchains/ethereum/diaScifiOracleService"
"github.com/diadata-org/diadata/pkg/dia"
models "github.com/diadata-org/diadata/pkg/model"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/ethclient"
)
func main() {
/*
* Read in Oracle address
*/
var deployedContract = flag.String("deployedContract", "", "Address of the deployed oracle contract")
var secretsFile = flag.String("secretsFile", "/run/secrets/oracle_keys", "File with wallet secrets")
var blockchainNode = flag.String("blockchainNode", "http://159.69.120.42:8545/", "Node address for blockchain connection")
var frequencySeconds = flag.Int("frequencySeconds", 86400, "Number of seconds to sleep between full oracle runs")
var chainId = flag.Int64("chainId", 1, "Chain-ID of the network to connect to")
flag.Parse()
/*
* Read secrets for unlocking the ETH account
*/
var lines []string
file, err := os.Open(*secretsFile) // Read in key information
if err != nil {
log.Fatal(err)
}
defer file.Close()
scanner := bufio.NewScanner(file)
for scanner.Scan() {
lines = append(lines, scanner.Text())
}
if err := scanner.Err(); err != nil {
log.Fatal(err)
}
if len(lines) != 2 {
log.Fatal("Secrets file should have exactly two lines")
}
key := lines[0]
key_password := lines[1]
/*
* Setup connection to contract, deploy if necessary
*/
conn, err := ethclient.Dial(*blockchainNode)
if err != nil {
log.Fatalf("Failed to connect to the Ethereum client: %v", err)
}
auth, err := bind.NewTransactorWithChainID(strings.NewReader(key), key_password, big.NewInt(*chainId))
if err != nil {
log.Fatalf("Failed to create authorized transactor: %v", err)
}
var contract *diaScifiOracleService.DIAScifiOracle
err = deployOrBindContract(*deployedContract, conn, auth, &contract)
if err != nil {
log.Fatalf("Failed to Deploy or Bind contract: %v", err)
}
indexName := "SCIFI"
periodicOracleUpdateHelper(indexName, auth, contract)
/*
* Update Oracle periodically with top coins
*/
ticker := time.NewTicker(time.Duration(*frequencySeconds) * time.Second)
go func() {
for {
select {
case <-ticker.C:
periodicOracleUpdateHelper(indexName, auth, contract)
}
}
}()
select {}
}
func periodicOracleUpdateHelper(indexName string, auth *bind.TransactOpts, contract *diaScifiOracleService.DIAScifiOracle) error {
rawIndex, err := getIndexValueFromDia(indexName)
if err != nil {
log.Fatalf("Failed to retrieve crypto index data from DIA: %v", err)
return err
}
err = updateIndexValue(rawIndex, auth, contract)
if err != nil {
log.Fatalf("Failed to update Scifi index Oracle: %v", err)
return err
}
return nil
}
func updateIndexValue(iv *models.CryptoIndex, auth *bind.TransactOpts, contract *diaScifiOracleService.DIAScifiOracle) error {
symbol := iv.Name
value := iv.Value
timestamp := iv.CalculationTime.Unix()
err := updateOracle(contract, auth, symbol, int64(value * 10000), timestamp)
if err != nil {
log.Fatalf("Failed to update Oracle: %v", err)
return err
}
return nil
}
func getIndexValueFromDia(symbol string) (*models.CryptoIndex, error) {
response, err := http.Get(dia.BaseUrl + "/v1/index/" + symbol + "?starttime=" + strconv.FormatInt(time.Now().Add(-200 * time.Second).Unix(), 10) + "&endtime=" + strconv.FormatInt(time.Now().Unix(), 10))
if err != nil {
return nil, err
}
defer response.Body.Close()
if 200 != response.StatusCode {
return nil, fmt.Errorf("Error on dia api with return code %d", response.StatusCode)
}
contents, err := ioutil.ReadAll(response.Body)
if err != nil {
return nil, err
}
var indices []models.CryptoIndex
err = json.Unmarshal([]byte(contents), &indices)
if err != nil {
return nil, err
}
return &indices[0], nil
}
func deployOrBindContract(deployedContract string, conn *ethclient.Client, auth *bind.TransactOpts, contract **diaScifiOracleService.DIAScifiOracle) error {
var err error
if deployedContract != "" {
*contract, err = diaScifiOracleService.NewDIAScifiOracle(common.HexToAddress(deployedContract), conn)
if err != nil {
return err
}
} else {
// deploy contract
var addr common.Address
var tx *types.Transaction
addr, tx, *contract, err = diaScifiOracleService.DeployDIAScifiOracle(auth, conn)
if err != nil {
log.Fatalf("could not deploy contract: %v", err)
return err
}
log.Printf("Contract pending deploy: 0x%x\n", addr)
log.Printf("Transaction waiting to be mined: 0x%x\n\n", tx.Hash())
time.Sleep(180000 * time.Millisecond)
}
return nil
}
func updateOracle(
contract *diaScifiOracleService.DIAScifiOracle,
auth *bind.TransactOpts,
key string,
value int64,
timestamp int64) error {
// Write values to smart contract
tx, err := contract.SetValue(&bind.TransactOpts{
From: auth.From,
Signer: auth.Signer,
GasLimit: 800725,
// Nonce: big.NewInt(time.Now().Unix()),
}, key, big.NewInt(value), big.NewInt(timestamp))
if err != nil {
return err
}
fmt.Println(tx.GasPrice())
log.Printf("key: %s\n", key)
log.Printf("Tx To: %s\n", tx.To().String())
log.Printf("Tx Hash: 0x%x\n", tx.Hash())
return nil
}